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

Commit

Permalink
Release v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev authored Jun 16, 2023
2 parents 071f296 + 0ca0d1f commit 55fac0a
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 60 deletions.
2 changes: 1 addition & 1 deletion sync/cli/Main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
import os
import sys
from pathlib import Path
from datetime import datetime
from pathlib import Path

from .Parameters import Parameters
from .SafeArgs import SafeArgs
Expand Down
2 changes: 1 addition & 1 deletion sync/cli/Parameters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from argparse import ArgumentParser as ArgumentParserBase
# noinspection PyUnresolvedReferences,PyProtectedMember
from argparse import (
Action,
Expand All @@ -7,6 +6,7 @@
_HelpAction,
_StoreAction
)
from argparse import ArgumentParser as ArgumentParserBase
from pathlib import Path
from typing import Sequence, Optional

Expand Down
64 changes: 35 additions & 29 deletions sync/core/Pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import shutil

from .Config import Config
from ..model import TrackJson, LocalModule, AttrDict, MagiskUpdateJson, OnlineModule
from ..model import LocalModule, AttrDict, MagiskUpdateJson, OnlineModule
from ..modifier import Result
from ..utils import Log, HttpUtils, GitUtils
from ..track import LocalTracks
from ..utils import Log, HttpUtils, GitUtils


class Pull:
Expand Down Expand Up @@ -36,6 +36,14 @@ def _copy_file(old, new, delete_old=True):
def _safe_download(url, out):
return HttpUtils.download(url, out)

def _check_changelog(self, module_id, file):
text = file.read_text()
if HttpUtils.is_html(text):
self._log.w(f"_check_changelog: [{module_id}] -> unsupported changelog type [the content is html text]")
return False
else:
return True

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}"
Expand All @@ -49,36 +57,37 @@ 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(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: [{module_id}] -> {msg}")
changelog_file = None
else:
unsupported = True
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: [{module_id}] -> {msg}")
changelog_file = None
else:
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: [{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: [{module_id}] -> unsupported changelog type [{changelog}]")
changelog_file = self._local_folder.joinpath(changelog)
if not changelog_file.exists():
msg = f"_get_changelog_common: [{module_id}] -> {changelog} is not in {self._local_folder}"
self._log.i(msg)
changelog_file = None

if changelog_file is not None:
is_target_type = self._check_changelog(module_id, changelog_file)
if not is_target_type:
os.remove(changelog_file)
changelog_file = None

return changelog_file

def _from_zip_common(self, module_id, zip_file, changelog_file, *, delete_tmp=True):
module_folder = self.modules_folder.joinpath(module_id)

def remove_file():
if delete_tmp:
os.remove(zip_file)
if delete_tmp and changelog_file is not None:
os.remove(changelog_file)

zip_file_size = os.path.getsize(zip_file) / (1024 ** 2)
if zip_file_size > self._max_size:
module_folder.joinpath(LocalTracks.TAG_DISABLE).touch()
Expand All @@ -95,11 +104,9 @@ def get_online_module():

result = get_online_module()
if result.is_failure:
if delete_tmp:
os.remove(zip_file)

msg = Log.get_msg(result.error)
self._log.e(f"_from_zip_common: [{module_id}] -> {msg}")
remove_file()
return None
else:
online_module: OnlineModule = result.value
Expand All @@ -110,8 +117,7 @@ def get_online_module():
if not target_zip_file.exists() and len(target_files) == 0:
self._copy_file(zip_file, target_zip_file, delete_tmp)
else:
if delete_tmp:
os.remove(zip_file)
remove_file()
return None

target_changelog_file = module_folder.joinpath(online_module.changelog_filename)
Expand Down
5 changes: 3 additions & 2 deletions sync/core/Pull.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from pathlib import Path
from typing import Optional, Tuple

from ..modifier import Result
from ..model import TrackJson, ConfigJson, OnlineModule
from ..modifier import Result
from ..utils import Log


Expand All @@ -22,10 +22,11 @@ class Pull:
@staticmethod
@Result.catching()
def _safe_download(url: str, out: Path) -> Result: ...
def _check_changelog(self, module_id: str, file: Path) -> bool: ...
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, module_id: str, zip_file: Path, changelog: Optional[Path], *, delete_tmp: bool = ...
self, module_id: str, zip_file: Path, changelog_file: 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
18 changes: 5 additions & 13 deletions sync/core/Sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ..model import ModulesJson, UpdateJson, TrackJson, LocalModule, AttrDict
from ..track import BaseTracks, LocalTracks, GithubTracks
from ..utils import Log, GitUtils
from ..__version__ import version, versionCode


class Sync:
Expand All @@ -15,7 +16,6 @@ def __init__(self, root_folder, config, tracks=None):
self._root_folder = root_folder
self._pull = Pull(root_folder, config)

self._is_updatable = False
self._is_full_update = True
self._json_folder = Config.get_json_folder(root_folder)
self._modules_folder = self._pull.modules_folder
Expand All @@ -31,10 +31,6 @@ def __init__(self, root_folder, config, tracks=None):
def __del__(self):
self._log.d("__del__")

def _set_updatable(self):
if not self.is_updatable:
self._is_updatable = True

def _check_ids(self, track, online_module):
if track.id == online_module.id:
return
Expand Down Expand Up @@ -163,7 +159,6 @@ def update_by_ids(self, module_ids, force, **kwargs):
if online_module is None:
continue

self._set_updatable()
self._log.i(f"update_by_ids: [{track.id}] -> update to {online_module.version_display}")

def update_all(self, force, **kwargs):
Expand All @@ -176,6 +171,10 @@ def create_modules_json(self, to_file):
modules_json = ModulesJson(
name=self._config.repo_name,
timestamp=timestamp,
metadata=AttrDict(
version=version,
versionCode=versionCode
),
modules=list()
)

Expand Down Expand Up @@ -215,9 +214,6 @@ def create_modules_json(self, to_file):
return modules_json

def push_by_git(self, branch):
if not self.is_updatable:
return

json_file = self._json_folder.joinpath(ModulesJson.filename())
if not json_file.exists():
self._log.e(f"push_by_git: {json_file.name} is not in {self._json_folder}")
Expand All @@ -233,7 +229,3 @@ def push_by_git(self, branch):
subprocess.run(["git", "add", "."], cwd=self._root_folder.as_posix())
subprocess.run(["git", "commit", "-m", msg], cwd=self._root_folder.as_posix())
subprocess.run(["git", "push", "-u", "origin", branch], cwd=self._root_folder.as_posix())

@property
def is_updatable(self):
return self._is_updatable
4 changes: 0 additions & 4 deletions sync/core/Sync.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ class Sync:
_root_folder: Path
_pull: Pull

_is_updatable: bool
_is_full_update: bool
_json_folder: Path
_modules_folder: Path
_config: ConfigJson
_tracks: 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]: ...
def _check_tracks(self, obj: BaseTracks, cls: type): ...
Expand All @@ -31,5 +29,3 @@ class Sync:
def update_all(self, force: bool, **kwargs): ...
def create_modules_json(self, to_file: bool) -> ModulesJson: ...
def push_by_git(self, branch: str): ...
@property
def is_updatable(self): ...
3 changes: 2 additions & 1 deletion sync/modifier/Command.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import subprocess
from pathlib import Path
from typing import Callable, Any, Optional
from typing import Callable, Optional

from .Result import Result


Expand Down
4 changes: 2 additions & 2 deletions sync/track/LocalTracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def add_track(cls, track, modules_folder, cover=True):
track.write(json_file)
elif cover:
old = TrackJson.load(json_file)
track.added = old.added
track.write(json_file)
old.update(track)
old.write(json_file)

@classmethod
def del_track(cls, module_id, modules_folder):
Expand Down
2 changes: 1 addition & 1 deletion sync/utils/GitUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import subprocess
from datetime import datetime
from pathlib import Path
from typing import Optional
from subprocess import CalledProcessError
from typing import Optional

from dateutil.parser import parse
from requests import HTTPError
Expand Down
4 changes: 2 additions & 2 deletions sync/utils/HttpUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def load_json(cls, url: str) -> Union[list, AttrDict]:
return obj

@classmethod
def _is_html(cls, text):
def is_html(cls, text):
pattern = r'<html\s*>|<head\s*>|<body\s*>|<!doctype\s*html\s*>'
return re.search(pattern, text, re.IGNORECASE) is not None

Expand All @@ -52,7 +52,7 @@ def download(cls, url: str, out: Path) -> float:
else:
os.remove(out)

if cls._is_html(response.text):
if cls.is_html(response.text):
msg = "404 not found"
else:
msg = response.text
Expand Down
8 changes: 4 additions & 4 deletions sync/utils/Log.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import functools
import logging
import os
import sys
import logging
import functools
from typing import Optional
from datetime import date
from glob import glob
from pathlib import Path
from datetime import date
from typing import Optional

logger_initialized = {}

Expand Down

0 comments on commit 55fac0a

Please sign in to comment.