From d5a99fc50a76704488abddea7c10d91c3be8c5a0 Mon Sep 17 00:00:00 2001 From: ya0211 Date: Mon, 12 Jun 2023 16:34:54 +0800 Subject: [PATCH 1/4] Fix licenses property in modules.json --- sync/core/Sync.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sync/core/Sync.py b/sync/core/Sync.py index 4ec6f8b..8909f3f 100644 --- a/sync/core/Sync.py +++ b/sync/core/Sync.py @@ -200,6 +200,7 @@ def create_modules_json(self, to_file): continue online_module = LocalModule.from_file(zip_file).to_OnlineModule() + online_module.license = track.license online_module.states = AttrDict( zipUrl=latest_item.zipUrl, changelog=latest_item.changelog From 670a41198af764126f6a5130c27620c11ef42171 Mon Sep 17 00:00:00 2001 From: ya0211 Date: Mon, 12 Jun 2023 17:00:14 +0800 Subject: [PATCH 2/4] No pass track via class property --- sync/core/Pull.py | 60 +++++++++++++++++++++------------------------- sync/core/Pull.pyi | 7 +++--- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/sync/core/Pull.py b/sync/core/Pull.py index 6ab0bea..7227541 100644 --- a/sync/core/Pull.py +++ b/sync/core/Pull.py @@ -16,7 +16,6 @@ def __init__(self, root_folder, config): self._local_folder = root_folder.joinpath("local") self._config = config - self._track = TrackJson.empty() self.modules_folder = Config.get_modules_folder(root_folder) self.modules_folder.mkdir(exist_ok=True) @@ -37,16 +36,16 @@ def _copy_file(old, new, delete_old=True): def _safe_download(url, out): return HttpUtils.download(url, out) - def _get_file_url(self, file): - module_folder = self.modules_folder.joinpath(self._track.id) - url = f"{self._config.repo_url}{self.modules_folder.name}/{self._track.id}/{file.name}" + def _get_file_url(self, module_id, file): + module_folder = self.modules_folder.joinpath(module_id) + url = f"{self._config.repo_url}{self.modules_folder.name}/{module_id}/{file.name}" if not (file.is_relative_to(module_folder) and file.exists()): raise FileNotFoundError(f"{file} is not in {module_folder}") else: return url - def _get_changelog_common(self, changelog): + def _get_changelog_common(self, module_id, changelog): if not isinstance(changelog, str) or changelog == "": return None @@ -54,31 +53,31 @@ def _get_changelog_common(self, changelog): changelog_file = None if changelog.startswith("http"): if changelog.endswith("md"): - changelog_file = self.modules_folder.joinpath(self._track.id, f"{self._track.id}.md") + changelog_file = self.modules_folder.joinpath(module_id, f"{module_id}.md") result = self._safe_download(changelog, changelog_file) if result.is_failure: msg = Log.get_msg(result.error) - self._log.e(f"_get_changelog_common: [{self._track.id}] -> {msg}") + self._log.e(f"_get_changelog_common: [{module_id}] -> {msg}") changelog_file = None else: unsupported = True else: - if changelog.endswith("md"): + if changelog.endswith("md") or changelog.endswith("log"): changelog_file = self._local_folder.joinpath(changelog) if not changelog_file.exists(): - msg = f"_get_changelog_common: [{self._track.id}] -> {changelog} is not in {self._local_folder}" + msg = f"_get_changelog_common: [{module_id}] -> {changelog} is not in {self._local_folder}" self._log.i(msg) changelog_file = None else: unsupported = True if unsupported: - self._log.w(f"_get_changelog_common: [{self._track.id}] -> unsupported changelog type [{changelog}]") + self._log.w(f"_get_changelog_common: [{module_id}] -> unsupported changelog type [{changelog}]") return changelog_file - def _from_zip_common(self, zip_file, changelog_file, *, delete_tmp): - module_folder = self.modules_folder.joinpath(self._track.id) + def _from_zip_common(self, module_id, zip_file, changelog_file, *, delete_tmp=True): + module_folder = self.modules_folder.joinpath(module_id) zip_file_size = os.path.getsize(zip_file) / (1024 ** 2) if zip_file_size > self._max_size: @@ -87,7 +86,7 @@ def _from_zip_common(self, zip_file, changelog_file, *, delete_tmp): os.remove(zip_file) msg = f"file size exceeds limit ({self._max_size} MB), update check disabled" - self._log.w(f"_from_zip_common: [{self._track.id}] -> {msg}") + self._log.w(f"_from_zip_common: [{module_id}] -> {msg}") return None @Result.catching() @@ -100,7 +99,7 @@ def get_online_module(): os.remove(zip_file) msg = Log.get_msg(result.error) - self._log.e(f"_from_zip_common: [{self._track.id}] -> {msg}") + self._log.e(f"_from_zip_common: [{module_id}] -> {msg}") return None else: online_module: OnlineModule = result.value @@ -119,11 +118,10 @@ def get_online_module(): changelog_url = "" if changelog_file is not None: self._copy_file(changelog_file, target_changelog_file, delete_tmp) - changelog_url = self._get_file_url(target_changelog_file) + changelog_url = self._get_file_url(module_id, target_changelog_file) - online_module.license = self._track.license online_module.states = AttrDict( - zipUrl=self._get_file_url(target_zip_file), + zipUrl=self._get_file_url(module_id, target_zip_file), changelog=changelog_url ) @@ -141,7 +139,7 @@ def load(): result = load() if result.is_failure: msg = Log.get_msg(result.error) - self._log.e(f"from_json: [{self._track.id}] -> {msg}") + self._log.e(f"from_json: [{track.id}] -> {msg}") return None, 0.0 else: update_json: MagiskUpdateJson = result.value @@ -157,14 +155,13 @@ def load(): result = self._safe_download(update_json.zipUrl, zip_file) if result.is_failure: msg = Log.get_msg(result.error) - self._log.e(f"from_json: [{self._track.id}] -> {msg}") + self._log.e(f"from_json: [{track.id}] -> {msg}") return None, 0.0 else: last_modified = result.value - self._track = track - changelog = self._get_changelog_common(update_json.changelog) - online_module = self._from_zip_common(zip_file, changelog, delete_tmp=True) + changelog = self._get_changelog_common(track.id, update_json.changelog) + online_module = self._from_zip_common(track.id, zip_file, changelog, delete_tmp=True) return online_module, last_modified def from_url(self, track): @@ -173,14 +170,13 @@ def from_url(self, track): result = self._safe_download(track.update_to, zip_file) if result.is_failure: msg = Log.get_msg(result.error) - self._log.e(f"from_url: [{self._track.id}] -> {msg}") + self._log.e(f"from_url: [{track.id}] -> {msg}") return None, 0.0 else: last_modified = result.value - self._track = track - changelog = self._get_changelog_common(track.changelog) - online_module = self._from_zip_common(zip_file, changelog, delete_tmp=True) + changelog = self._get_changelog_common(track.id, track.changelog) + online_module = self._from_zip_common(track.id, zip_file, changelog, delete_tmp=True) return online_module, last_modified def from_git(self, track): @@ -193,14 +189,13 @@ def git(): result = git() if result.is_failure: msg = Log.get_msg(result.error) - self._log.e(f"from_git: [{self._track.id}] -> {msg}") + self._log.e(f"from_git: [{track.id}] -> {msg}") return None, 0.0 else: last_committed = result.value - self._track = track - changelog = self._get_changelog_common(track.changelog) - online_module = self._from_zip_common(zip_file, changelog, delete_tmp=True) + changelog = self._get_changelog_common(track.id, track.changelog) + online_module = self._from_zip_common(track.id, zip_file, changelog, delete_tmp=True) return online_module, last_committed def from_zip(self, track): @@ -211,9 +206,8 @@ def from_zip(self, track): self._log.i(f"from_zip: [{track.id}] -> {track.update_to} is not in {self._local_folder}") return None, 0.0 - self._track = track - changelog = self._get_changelog_common(track.changelog) - online_module = self._from_zip_common(zip_file, changelog, delete_tmp=False) + changelog = self._get_changelog_common(track.id, track.changelog) + online_module = self._from_zip_common(track.id, zip_file, changelog, delete_tmp=False) return online_module, last_modified def from_track(self, track): diff --git a/sync/core/Pull.pyi b/sync/core/Pull.pyi index de1d098..4674bb7 100644 --- a/sync/core/Pull.pyi +++ b/sync/core/Pull.pyi @@ -13,7 +13,6 @@ class Pull: _local_folder: Path _config: ConfigJson - _track: TrackJson modules_folder: Path @@ -23,10 +22,10 @@ class Pull: @staticmethod @Result.catching() def _safe_download(url: str, out: Path) -> Result: ... - def _get_file_url(self, file: Path) -> str: ... - def _get_changelog_common(self, changelog: str) -> Optional[Path]: ... + def _get_file_url(self, module_id: str, file: Path) -> str: ... + def _get_changelog_common(self, module_id: str, changelog: str) -> Optional[Path]: ... def _from_zip_common( - self, zip_file: Path, changelog: Optional[Path], *, delete_tmp: bool = ... + self, module_id: str, zip_file: Path, changelog: Optional[Path], *, delete_tmp: bool = ... ) -> Optional[OnlineModule]: ... def from_json(self, track: TrackJson, *, local: bool) -> Tuple[Optional[OnlineModule], float]: ... def from_url(self, track: TrackJson) -> Tuple[Optional[OnlineModule], float]: ... From 18cd429dea69e97f20358c3d3adac33be5e14e44 Mon Sep 17 00:00:00 2001 From: ya0211 Date: Mon, 12 Jun 2023 17:02:01 +0800 Subject: [PATCH 3/4] Remove method empty --- sync/model/TrackJson.py | 12 ------------ sync/model/TrackJson.pyi | 2 -- 2 files changed, 14 deletions(-) diff --git a/sync/model/TrackJson.py b/sync/model/TrackJson.py index 51b1dc7..aabe015 100644 --- a/sync/model/TrackJson.py +++ b/sync/model/TrackJson.py @@ -11,15 +11,3 @@ def load(cls, file): @classmethod def filename(cls): return "track.json" - - @classmethod - def empty(cls): - return TrackJson( - id="", - update_to="", - license="", - changelog="", - added=0.0, - last_update=0.0, - versions=0 - ) diff --git a/sync/model/TrackJson.pyi b/sync/model/TrackJson.pyi index 238153e..a201802 100644 --- a/sync/model/TrackJson.pyi +++ b/sync/model/TrackJson.pyi @@ -17,6 +17,4 @@ class TrackJson(AttrDict, JsonIO): def load(cls, file: Path) -> TrackJson: ... @classmethod def filename(cls) -> str: ... - @classmethod - def empty(cls) -> TrackJson: ... From 1d23d5528556c704434830c37ef50cb26c7a2a19 Mon Sep 17 00:00:00 2001 From: ya0211 Date: Mon, 12 Jun 2023 17:06:45 +0800 Subject: [PATCH 4/4] Fix type definition of tracks --- sync/cli/Main.py | 6 ++---- sync/core/Sync.pyi | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/sync/cli/Main.py b/sync/cli/Main.py index 0927a33..290abfc 100644 --- a/sync/cli/Main.py +++ b/sync/cli/Main.py @@ -221,8 +221,7 @@ def sync(cls) -> int: sync = Sync( root_folder=root_folder, - config=config, - tracks=None + config=config ) sync.create_local_tracks() sync.update_by_ids( @@ -245,8 +244,7 @@ def index(cls) -> int: sync = Sync( root_folder=root_folder, - config=config, - tracks=None + config=config ) sync.create_local_tracks() diff --git a/sync/core/Sync.pyi b/sync/core/Sync.pyi index b72f803..447c95e 100644 --- a/sync/core/Sync.pyi +++ b/sync/core/Sync.pyi @@ -19,7 +19,7 @@ class Sync: _config: ConfigJson _tracks: BaseTracks - def __init__(self, root_folder: Path, config: ConfigJson, tracks: Optional[BaseTracks]): ... + def __init__(self, root_folder: Path, config: ConfigJson, tracks: Optional[BaseTracks] = ...): ... def _set_updatable(self): ... def _check_ids(self, track: TrackJson, online_module: OnlineModule): ... def _update_jsons(self, track: TrackJson, force: bool) -> Optional[OnlineModule]: ...