From 89cc3a8dae262031fea9f53eb89751563b8c2798 Mon Sep 17 00:00:00 2001 From: Lee CQ Date: Fri, 8 Dec 2023 17:16:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20checker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 17 ++++ alist_sync/__main__.py | 18 +++-- alist_sync/base_sync.py | 8 +- alist_sync/models.py | 7 +- tests/resource/SyncDirs-m.json | 143 +++++++++++++++++++++++++++++++++ tests/test_check.py | 7 +- 6 files changed, 187 insertions(+), 13 deletions(-) create mode 100644 tests/resource/SyncDirs-m.json diff --git a/.vscode/launch.json b/.vscode/launch.json index 5f68860..bbd8625 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,7 @@ // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { "name": "CLI - main copy", "type": "python", @@ -32,6 +33,22 @@ "console": "integratedTerminal", "justMyCode": true, + }, + { + "name": "debug - check", + "type": "python", + "request": "launch", + "program": "alist_sync/__main__.py", + "console": "integratedTerminal", + "justMyCode": true, + "args": [ + "check", + "-u", "admin", + "-p", "123456", + "-t", "/local", + "-t", "/local_dst", + "-t", "/local_dst2" + ] } ] } \ No newline at end of file diff --git a/alist_sync/__main__.py b/alist_sync/__main__.py index 0ca0bb7..a73e5d3 100644 --- a/alist_sync/__main__.py +++ b/alist_sync/__main__.py @@ -40,8 +40,8 @@ help="Verify SSL Certificates" ) -_stores_config: str = Option( - ..., '-c', '--store-path', +_stores_config: str | None = Option( + None, '-c', '--store-path', help="一个包含存储配置的JSON文件,可以是AList的备份文件" ) @@ -63,13 +63,16 @@ def check( password=password, token=token, verify=verify, - storage_config=storage_config, + storage_config=Path(storage_config) if storage_config else None, ) echo( - f"Will Be Check {target} " - "on {alist_info.base_url} [{alist_info.username}]" + f"Will Be Check '{target} " + f"on {alist_info.base_url} [{alist_info.username}] \n" + f"将会从 {alist_info.storage_config} 存储库获取存储库信息。" ) - return SyncBase(alist_info=alist_info, sync_dirs=target).sync_job.checker.model_dump_table() + c = SyncBase(alist_info=alist_info, sync_dirs=target) + c.run() + c.sync_job.checker.model_dump_table() @app.command(name="copy") @@ -94,7 +97,8 @@ def copy( ) echo( f"Will Be Copy '{source}' -> {target} " - "on {alist_info.base_url} [{alist_info.username}]" + f"on {alist_info.base_url} [{alist_info.username}]" + f"将会从 {alist_info.storage_config} 存储库获取存储库信息。" ) return CopyToTarget(alist_info, source_path=source, targets_path=target).run() diff --git a/alist_sync/base_sync.py b/alist_sync/base_sync.py index 101829a..641a65d 100644 --- a/alist_sync/base_sync.py +++ b/alist_sync/base_sync.py @@ -18,7 +18,9 @@ def __init__(self, sync_dirs: list[str | os.PathLike] ): self.client = AlistClient( - timeout=30, **alist_info.model_dump(exclude={'storage_config'}) + timeout=30, **alist_info.model_dump( + exclude={'storage_config'} + ) ) self.sync_dirs = sync_dirs @@ -103,7 +105,9 @@ async def async_run(self): if not self.sync_job.sync_dirs.values(): await self.scans() self.save_to_cache() - self.sync_job.checker = Checker.checker(*self.sync_job.sync_dirs.values()) + self.sync_job.checker = Checker.checker( + *self.sync_job.sync_dirs.values() + ) else: logger.info(f"一件从缓存中找到 %d 个 SyncDir", len(self.sync_job.sync_dirs)) diff --git a/alist_sync/models.py b/alist_sync/models.py index dff9c9b..c647d5a 100644 --- a/alist_sync/models.py +++ b/alist_sync/models.py @@ -1,4 +1,5 @@ import datetime +import json from pathlib import PurePosixPath, Path from typing import Optional, Literal @@ -23,7 +24,7 @@ class AlistServer(BaseModel): has_opt: Optional[bool] = False max_connect: int = 30 # 最大同时连接数 - storage_config: Path = [] + storage_config: Optional[Path] = None # httpx 的参数 verify: Optional[bool] = True @@ -39,7 +40,7 @@ def is_storage(_st): return True return False - if not self.storage_config: + if not self.storage_config or self.storage_config == Path(): return [] if not self.storage_config.exists(): raise FileNotFoundError(f"找不到文件:{self.storage_config}") @@ -198,6 +199,6 @@ def model_dump_table(self): from pathlib import Path checker = Checker.checker(*[SyncDir(**s) for s in json.load( - Path(__file__).parent.parent.joinpath('tests/resource/SyncDirs.json').open()) + Path(__file__).parent.parent.joinpath('tests/resource/SyncDirs-m.json').open()) ]) checker.model_dump_table() diff --git a/tests/resource/SyncDirs-m.json b/tests/resource/SyncDirs-m.json new file mode 100644 index 0000000..97589b3 --- /dev/null +++ b/tests/resource/SyncDirs-m.json @@ -0,0 +1,143 @@ +[ + { + "base_path": "/local", + "items": [ + { + "parent": "/local", + "name": "check.py", + "size": 540, + "is_dir": false, + "hashinfo": "null", + "hash_info": null, + "modified": "2023-12-02T06:10:17Z", + "created": "2023-12-02T06:10:17Z", + "sign": "", + "thumb": "https://japane/.Dh75k", + "type": 4, + "full_name": "/onedrive/Music/check.py" + }, + { + "parent": "/local/a", + "name": "a.txt", + "size": 540, + "is_dir": false, + "hashinfo": "null", + "hash_info": null, + "modified": "2023-12-02T06:10:17Z", + "created": "2023-12-02T06:10:17Z", + "sign": "", + "thumb": "https://japane/.Dh75k", + "type": 4, + "full_name": "/onedrive/Music/check.py" + }, + { + "parent": "/local/b", + "name": "b.txt", + "size": 540, + "is_dir": false, + "hashinfo": "null", + "hash_info": null, + "modified": "2023-12-02T06:10:17Z", + "created": "2023-12-02T06:10:17Z", + "sign": "", + "thumb": "https://japane/.Dh75k", + "type": 4, + "full_name": "/onedrive/Music/check.py" + } + ] + }, + { + "base_path": "/local_dst", + "items": [ + { + "parent": "/local_dst", + "name": "check.py", + "size": 540, + "is_dir": false, + "hashinfo": "null", + "hash_info": null, + "modified": "2023-12-02T06:10:17Z", + "created": "2023-12-02T06:10:17Z", + "sign": "", + "thumb": "https://japane/.Dh75k", + "type": 4, + "full_name": "/onedrive/Music/check.py" + }, + { + "parent": "/local_dst/a", + "name": "a.txt", + "size": 540, + "is_dir": false, + "hashinfo": "null", + "hash_info": null, + "modified": "2023-12-02T06:10:17Z", + "created": "2023-12-02T06:10:17Z", + "sign": "", + "thumb": "https://japane/.Dh75k", + "type": 4, + "full_name": "/onedrive/Music/check.py" + }, + { + "parent": "/local_dst/c", + "name": "c.txt", + "size": 540, + "is_dir": false, + "hashinfo": "null", + "hash_info": null, + "modified": "2023-12-02T06:10:17Z", + "created": "2023-12-02T06:10:17Z", + "sign": "", + "thumb": "https://japane/.Dh75k", + "type": 4, + "full_name": "/onedrive/Music/check.py" + } + ] + }, + { + "base_path": "/local_dst2", + "items": [ + { + "parent": "/local_dst2", + "name": "check.py", + "size": 540, + "is_dir": false, + "hashinfo": "null", + "hash_info": null, + "modified": "2023-12-02T06:10:17Z", + "created": "2023-12-02T06:10:17Z", + "sign": "", + "thumb": "https://japane/.Dh75k", + "type": 4, + "full_name": "/onedrive/Music/check.py" + }, + { + "parent": "/local_dst2/d", + "name": "d.txt", + "size": 540, + "is_dir": false, + "hashinfo": "null", + "hash_info": null, + "modified": "2023-12-02T06:10:17Z", + "created": "2023-12-02T06:10:17Z", + "sign": "", + "thumb": "https://japane/.Dh75k", + "type": 4, + "full_name": "/onedrive/Music/check.py" + }, + { + "parent": "/local_dst2/c", + "name": "c.txt", + "size": 540, + "is_dir": false, + "hashinfo": "null", + "hash_info": null, + "modified": "2023-12-02T06:10:17Z", + "created": "2023-12-02T06:10:17Z", + "sign": "", + "thumb": "https://japane/.Dh75k", + "type": 4, + "full_name": "/onedrive/Music/check.py" + } + ] + } +] \ No newline at end of file diff --git a/tests/test_check.py b/tests/test_check.py index 04618be..1c3563c 100644 --- a/tests/test_check.py +++ b/tests/test_check.py @@ -20,7 +20,12 @@ @pytest.mark.parametrize( "scanned_dirs", - [[SyncDir(**s) for s in json.load(SUP_DIR.joinpath('SyncDirs.json').open())]] + [ + [SyncDir(**s) + for s in json.load(SUP_DIR.joinpath('SyncDirs.json').open())], + [SyncDir(**s) + for s in json.load(SUP_DIR.joinpath('SyncDirs-m.json').open())] + ] ) def test_check(scanned_dirs): checker = Checker.checker(*scanned_dirs)