Skip to content
This repository has been archived by the owner on Jun 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #23 from ya0211/2.x
Browse files Browse the repository at this point in the history
- Fix licenses property in modules.json
- Fix type definition of tracks
  • Loading branch information
SanmerDev authored Jun 12, 2023
2 parents 26ad2e5 + 1d23d55 commit 071f296
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 56 deletions.
6 changes: 2 additions & 4 deletions sync/cli/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -245,8 +244,7 @@ def index(cls) -> int:

sync = Sync(
root_folder=root_folder,
config=config,
tracks=None
config=config
)
sync.create_local_tracks()

Expand Down
60 changes: 27 additions & 33 deletions sync/core/Pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -37,48 +36,48 @@ 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

unsupported = False
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:
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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
)

Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down
7 changes: 3 additions & 4 deletions sync/core/Pull.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Pull:
_local_folder: Path

_config: ConfigJson
_track: TrackJson

modules_folder: Path

Expand All @@ -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]: ...
Expand Down
1 change: 1 addition & 0 deletions sync/core/Sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sync/core/Sync.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ...
Expand Down
12 changes: 0 additions & 12 deletions sync/model/TrackJson.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 0 additions & 2 deletions sync/model/TrackJson.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ class TrackJson(AttrDict, JsonIO):
def load(cls, file: Path) -> TrackJson: ...
@classmethod
def filename(cls) -> str: ...
@classmethod
def empty(cls) -> TrackJson: ...

0 comments on commit 071f296

Please sign in to comment.