├── .gitignore ├── sync ├── track │ ├── BaseTracks.py │ ├── __init__.py │ ├── BaseTracks.pyi │ ├── LocalTracks.pyi │ ├── GithubTracks.pyi │ ├── LocalTracks.py │ └── GithubTracks.py ├── cli │ ├── __init__.py │ ├── Main.py │ └── Parameters.py ├── error │ ├── ConfigError.py │ ├── MagiskModuleError.py │ ├── __init__.py │ └── Result.py ├── model │ ├── AttrDict.pyi │ ├── JsonIO.pyi │ ├── AttrDict.py │ ├── LocalModule.pyi │ ├── MagiskUpdateJson.pyi │ ├── __init__.py │ ├── JsonIO.py │ ├── ConfigJson.pyi │ ├── UpdateJson.pyi │ ├── UpdateJson.py │ ├── TrackJson.pyi │ ├── ConfigJson.py │ ├── MagiskUpdateJson.py │ ├── ModulesJson.pyi │ ├── LocalModule.py │ ├── ModulesJson.py │ └── TrackJson.py ├── __version__.py ├── __init__.py ├── core │ ├── __init__.py │ ├── Migrate.pyi │ ├── Config.pyi │ ├── Sync.pyi │ ├── Check.pyi │ ├── Index.pyi │ ├── Pull.pyi │ ├── Config.py │ ├── Migrate.py │ ├── Index.py │ ├── Sync.py │ ├── Check.py │ └── Pull.py └── utils │ ├── __init__.py │ ├── StrUtils.py │ ├── GitUtils.py │ ├── HttpUtils.py │ ├── GitHubGraphQLAPI.py │ └── Log.py ├── requirements.txt ├── cli.py ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | .idea/ 3 | 4 | .DS_Store -------------------------------------------------------------------------------- /sync/track/BaseTracks.py: -------------------------------------------------------------------------------- 1 | class BaseTracks: 2 | pass 3 | -------------------------------------------------------------------------------- /sync/cli/__init__.py: -------------------------------------------------------------------------------- 1 | from .Main import Main 2 | 3 | __all__ = ["Main"] 4 | -------------------------------------------------------------------------------- /sync/error/ConfigError.py: -------------------------------------------------------------------------------- 1 | class ConfigError(IOError): 2 | """A Config error occurred.""" 3 | -------------------------------------------------------------------------------- /sync/error/MagiskModuleError.py: -------------------------------------------------------------------------------- 1 | class MagiskModuleError(IOError): 2 | """A Magisk Module error occurred.""" 3 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pygithub>=1.59.0 2 | python-dateutil>=2.8.2 3 | requests>=2.31.0 4 | tabulate>=0.9.0 5 | gitpython>=3.1.37 -------------------------------------------------------------------------------- /sync/model/AttrDict.pyi: -------------------------------------------------------------------------------- 1 | class AttrDict(dict): 2 | def __setattr__(self, key, value): ... 3 | def __getattr__(self, item): ... 4 | def __hash__(self) -> int: ... 5 | def copy(self, **kwargs) -> AttrDict: ... 6 | -------------------------------------------------------------------------------- /sync/__version__.py: -------------------------------------------------------------------------------- 1 | def get_version() -> str: 2 | return "2.3.3" 3 | 4 | 5 | def get_version_code() -> int: 6 | return 233 7 | 8 | 9 | __all__ = [ 10 | "get_version", 11 | "get_version_code" 12 | ] 13 | -------------------------------------------------------------------------------- /sync/error/__init__.py: -------------------------------------------------------------------------------- 1 | from .ConfigError import ConfigError 2 | from .MagiskModuleError import MagiskModuleError 3 | from .Result import Result 4 | 5 | __all__ = [ 6 | "ConfigError", 7 | "MagiskModuleError", 8 | "Result" 9 | ] 10 | -------------------------------------------------------------------------------- /sync/__init__.py: -------------------------------------------------------------------------------- 1 | from .core import * 2 | from .track import * 3 | from .utils import Log 4 | 5 | Log.set_file_prefix("sync") 6 | 7 | __all__ = [ 8 | "Check", 9 | "Config", 10 | "Index", 11 | "Pull", 12 | "Sync", 13 | "LocalTracks", 14 | "GithubTracks" 15 | ] 16 | -------------------------------------------------------------------------------- /sync/model/JsonIO.pyi: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from typing import Dict 3 | 4 | 5 | class JsonIO: 6 | def write(self: Dict, file: Path): ... 7 | @classmethod 8 | def filter(cls, text: str) -> str: ... 9 | @classmethod 10 | def load(cls, file: Path) -> Dict: ... 11 | -------------------------------------------------------------------------------- /sync/core/__init__.py: -------------------------------------------------------------------------------- 1 | from .Check import Check 2 | from .Config import Config 3 | from .Index import Index 4 | from .Migrate import Migrate 5 | from .Pull import Pull 6 | from .Sync import Sync 7 | 8 | __all__ = [ 9 | "Check", 10 | "Config", 11 | "Index", 12 | "Migrate", 13 | "Pull", 14 | "Sync" 15 | ] 16 | -------------------------------------------------------------------------------- /sync/utils/__init__.py: -------------------------------------------------------------------------------- 1 | from .GitHubGraphQLAPI import GitHubGraphQLAPI 2 | from .GitUtils import GitUtils 3 | from .HttpUtils import HttpUtils 4 | from .Log import Log 5 | from .StrUtils import StrUtils 6 | 7 | __all__ = [ 8 | "GitHubGraphQLAPI", 9 | "GitUtils", 10 | "HttpUtils", 11 | "Log", 12 | "StrUtils" 13 | ] 14 | -------------------------------------------------------------------------------- /sync/track/__init__.py: -------------------------------------------------------------------------------- 1 | from .BaseTracks import BaseTracks 2 | from .LocalTracks import LocalTracks 3 | 4 | try: 5 | from .GithubTracks import GithubTracks 6 | except ImportError: 7 | from .BaseTracks import BaseTracks as GithubTracks 8 | 9 | 10 | __all__ = [ 11 | "BaseTracks", 12 | "GithubTracks", 13 | "LocalTracks" 14 | ] 15 | -------------------------------------------------------------------------------- /sync/track/BaseTracks.pyi: -------------------------------------------------------------------------------- 1 | from typing import Optional, List 2 | 3 | from ..model import TrackJson 4 | 5 | 6 | class BaseTracks: 7 | def get_track(self, *args, **kwargs) -> Optional[TrackJson]: ... 8 | def get_tracks(self, *args, **kwargs) -> List[TrackJson]: ... 9 | @property 10 | def size(self) -> int: ... 11 | @property 12 | def tracks(self) -> List[TrackJson]: ... 13 | -------------------------------------------------------------------------------- /sync/model/AttrDict.py: -------------------------------------------------------------------------------- 1 | class AttrDict(dict): 2 | def __setattr__(self, key, value): 3 | self.__setitem__(key, value) 4 | 5 | def __getattr__(self, item): 6 | return self.get(item) 7 | 8 | def __hash__(self): 9 | return hash(frozenset(self.__dict__.items())) 10 | 11 | def copy(self, **kwargs): 12 | new = super().copy() 13 | new.update(**kwargs) 14 | 15 | return AttrDict(new) 16 | -------------------------------------------------------------------------------- /sync/model/LocalModule.pyi: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from typing import Dict, Type 3 | 4 | from .AttrDict import AttrDict 5 | 6 | 7 | class LocalModule(AttrDict): 8 | id: str 9 | name: str 10 | version: str 11 | versionCode: int 12 | author: str 13 | description: str 14 | 15 | @classmethod 16 | def load(cls, file: Path) -> LocalModule: ... 17 | @classmethod 18 | def expected_fields(cls, __type: bool = ...) -> Dict[str, Type]: ... 19 | -------------------------------------------------------------------------------- /sync/core/Migrate.pyi: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from typing import Dict, Any 3 | 4 | T = Dict[str, Any] 5 | 6 | class Migrate: 7 | _root_folder: Path 8 | 9 | def __init__(self, root_folder: Path): ... 10 | @staticmethod 11 | def _config_0_1(old_config: T) -> T: ... 12 | @staticmethod 13 | def _config_1_2(old_config: T) -> T: ... 14 | @staticmethod 15 | def _config_new(old_config: T) -> T: ... 16 | def config(self): ... 17 | def track(self): ... -------------------------------------------------------------------------------- /sync/model/MagiskUpdateJson.pyi: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from typing import Union 3 | 4 | from .AttrDict import AttrDict 5 | 6 | 7 | class MagiskUpdateJson(AttrDict): 8 | version: str 9 | versionCode: int 10 | zipUrl: str 11 | changelog: str 12 | 13 | @property 14 | def version_display(self) -> str: ... 15 | @property 16 | def zipfile_name(self) -> str: ... 17 | @classmethod 18 | def load(cls, path: Union[str, Path]) -> MagiskUpdateJson: ... 19 | -------------------------------------------------------------------------------- /sync/model/__init__.py: -------------------------------------------------------------------------------- 1 | from .AttrDict import AttrDict 2 | from .ConfigJson import ConfigJson 3 | from .JsonIO import JsonIO 4 | from .LocalModule import LocalModule 5 | from .MagiskUpdateJson import MagiskUpdateJson 6 | from .ModulesJson import ModulesJson, OnlineModule 7 | from .TrackJson import TrackJson, TrackType 8 | from .UpdateJson import UpdateJson, VersionItem 9 | 10 | __all__ = [ 11 | "AttrDict", 12 | "ConfigJson", 13 | "JsonIO", 14 | "LocalModule", 15 | "MagiskUpdateJson", 16 | "ModulesJson", 17 | "OnlineModule", 18 | "TrackJson", 19 | "TrackType", 20 | "UpdateJson", 21 | "VersionItem", 22 | ] 23 | -------------------------------------------------------------------------------- /cli.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | import os 4 | import sys 5 | from pathlib import Path 6 | from signal import SIGINT, signal 7 | 8 | from sync.cli import Main 9 | 10 | 11 | # noinspection PyUnresolvedReferences,PyProtectedMember 12 | def signal_handler(*args): 13 | os._exit(1) 14 | 15 | 16 | if __name__ == "__main__": 17 | signal(SIGINT, signal_handler) 18 | cwd_folder = Path(__name__).resolve().parent 19 | 20 | try: 21 | github_token = os.environ["GITHUB_TOKEN"] 22 | except KeyError: 23 | github_token = None 24 | 25 | Main.set_default_args(root_folder=cwd_folder, github_token=github_token) 26 | sys.exit(Main.exec()) 27 | -------------------------------------------------------------------------------- /sync/model/JsonIO.py: -------------------------------------------------------------------------------- 1 | import json 2 | import re 3 | 4 | 5 | class JsonIO: 6 | def write(self, file): 7 | assert isinstance(self, dict) 8 | 9 | file.parent.mkdir(parents=True, exist_ok=True) 10 | 11 | with open(file, "w") as f: 12 | json.dump(self, f, indent=2) 13 | 14 | @classmethod 15 | def filter(cls, text): 16 | return re.sub(r",(?=\s*?[}\]])", "", text) 17 | 18 | @classmethod 19 | def load(cls, file): 20 | with open(file, encoding="utf-8", mode="r") as f: 21 | text = cls.filter(f.read()) 22 | obj = json.loads(text) 23 | 24 | assert isinstance(obj, dict) 25 | 26 | return obj 27 | -------------------------------------------------------------------------------- /sync/core/Config.pyi: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | from ..model import ConfigJson 4 | from ..utils import Log 5 | 6 | 7 | class Config(ConfigJson): 8 | _log: Log 9 | _root_folder: Path 10 | 11 | def __init__(self, root_folder: Path): ... 12 | def _check_values(self): ... 13 | @property 14 | def json_folder(self) -> Path: ... 15 | @property 16 | def modules_folder(self) -> Path: ... 17 | @property 18 | def local_folder(self) -> Path: ... 19 | @classmethod 20 | def get_json_folder(cls, root_folder: Path) -> Path: ... 21 | @classmethod 22 | def get_modules_folder(cls, root_folder: Path) -> Path: ... 23 | @classmethod 24 | def get_local_folder(cls, root_folder: Path) -> Path: ... 25 | -------------------------------------------------------------------------------- /sync/model/ConfigJson.pyi: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from typing import ( 3 | Dict, 4 | Any, 5 | Optional, 6 | Self, 7 | Union, 8 | Type 9 | ) 10 | 11 | from .AttrDict import AttrDict 12 | from .JsonIO import JsonIO 13 | 14 | T = Dict[str, Any] 15 | 16 | class ConfigJson(AttrDict, JsonIO): 17 | name: str 18 | base_url: str 19 | max_num: int 20 | enable_log: bool 21 | log_dir: Optional[Path] 22 | 23 | def write(self: Union[Self, Dict], file: Path): ... 24 | @classmethod 25 | def load(cls, file: Path) -> ConfigJson: ... 26 | @classmethod 27 | def default(cls) -> ConfigJson: ... 28 | @classmethod 29 | def filename(cls) -> str: ... 30 | @classmethod 31 | def expected_fields(cls, __type: bool = ...) -> Dict[str, Union[str, Union[str, Type]]]: ... 32 | -------------------------------------------------------------------------------- /sync/model/UpdateJson.pyi: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from typing import List 3 | 4 | from .AttrDict import AttrDict 5 | from .JsonIO import JsonIO 6 | 7 | 8 | class VersionItem(AttrDict): 9 | timestamp: float 10 | version: str 11 | versionCode: int 12 | zipUrl: str 13 | changelog: str 14 | 15 | @property 16 | def id(self) -> str: ... 17 | @property 18 | def version_display(self) -> str: ... 19 | @property 20 | def changelog_filename(self) -> str: ... 21 | @property 22 | def zipfile_name(self) -> str: ... 23 | 24 | 25 | class UpdateJson(AttrDict, JsonIO): 26 | id: str 27 | timestamp: float 28 | versions: List[VersionItem] 29 | 30 | @classmethod 31 | def load(cls, file: Path) -> UpdateJson: ... 32 | @classmethod 33 | def filename(cls) -> str: ... 34 | -------------------------------------------------------------------------------- /sync/model/UpdateJson.py: -------------------------------------------------------------------------------- 1 | from .AttrDict import AttrDict 2 | from .JsonIO import JsonIO 3 | from ..utils import StrUtils 4 | 5 | 6 | class VersionItem(AttrDict): 7 | @property 8 | def id(self): 9 | return self.zipUrl.split("/")[-2] 10 | 11 | @property 12 | def version_display(self): 13 | return StrUtils.get_version_display(self.version, self.versionCode) 14 | 15 | @property 16 | def changelog_filename(self): 17 | return self.changelog.split("/")[-1] 18 | 19 | @property 20 | def zipfile_name(self): 21 | return self.zipUrl.split("/")[-1] 22 | 23 | 24 | class UpdateJson(AttrDict, JsonIO): 25 | @classmethod 26 | def load(cls, file): 27 | obj = JsonIO.load(file) 28 | obj["versions"] = [VersionItem(_obj) for _obj in obj["versions"]] 29 | obj["versions"].sort(key=lambda v: v.versionCode) 30 | return UpdateJson(obj) 31 | 32 | @classmethod 33 | def filename(cls): 34 | return "update.json" 35 | -------------------------------------------------------------------------------- /sync/error/Result.py: -------------------------------------------------------------------------------- 1 | from typing import Optional, Any, Callable 2 | 3 | 4 | class Result: 5 | def __init__(self, value: Optional[Any] = None, error: Optional[BaseException] = None): 6 | self.value = value 7 | self.error = error 8 | 9 | @property 10 | def is_success(self): 11 | return self.error is None 12 | 13 | @property 14 | def is_failure(self): 15 | return not self.is_success 16 | 17 | def get_or_default(self, default: Any) -> Any: 18 | return self.value or default 19 | 20 | @classmethod 21 | def catching(cls): 22 | def decorator(func: Callable[..., Any]) -> Callable[..., Result]: 23 | def wrapper(*args, **kwargs): 24 | try: 25 | value = func(*args, **kwargs) 26 | return Result(value=value) 27 | except BaseException as err: 28 | return Result(error=err) 29 | 30 | return wrapper 31 | return decorator 32 | -------------------------------------------------------------------------------- /sync/model/TrackJson.pyi: -------------------------------------------------------------------------------- 1 | from enum import Enum 2 | from pathlib import Path 3 | from typing import Dict, Type 4 | 5 | from .AttrDict import AttrDict 6 | from .JsonIO import JsonIO 7 | 8 | 9 | class TrackJson(AttrDict, JsonIO): 10 | id: str 11 | enable: bool 12 | update_to: str 13 | changelog: str 14 | license: str 15 | homepage: str 16 | source: str 17 | support: str 18 | donate: str 19 | max_num: int 20 | 21 | # without manually 22 | added: float 23 | last_update: float 24 | versions: int 25 | 26 | @property 27 | def type(self) -> TrackType: ... 28 | def json(self) -> AttrDict: ... 29 | def write(self, file: Path): ... 30 | @classmethod 31 | def load(cls, file: Path) -> TrackJson: ... 32 | @classmethod 33 | def filename(cls) -> str: ... 34 | @classmethod 35 | def expected_fields(cls, __type: bool = ...) -> Dict[str, Type]: ... 36 | 37 | 38 | class TrackType(Enum): 39 | UNKNOWN: TrackType 40 | ONLINE_JSON: TrackType 41 | ONLINE_ZIP: TrackType 42 | GIT: TrackType 43 | LOCAL_JSON: TrackType 44 | LOCAL_ZIP: TrackType 45 | -------------------------------------------------------------------------------- /sync/track/LocalTracks.pyi: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from typing import Optional, List 3 | 4 | from .BaseTracks import BaseTracks 5 | from ..error import Result 6 | from ..model import TrackJson, ConfigJson 7 | from ..utils import Log 8 | 9 | 10 | class LocalTracks(BaseTracks): 11 | _log: Log 12 | _modules_folder: Path 13 | _tracks: List[TrackJson] 14 | 15 | def __init__(self, modules_folder: Path, config: ConfigJson): ... 16 | @Result.catching() 17 | def _get_from_file(self, file: Path) -> Result: ... 18 | def get_track(self, module_id: str) -> Optional[TrackJson]: ... 19 | def get_tracks(self, module_ids: Optional[List[str]] = ...) -> List[TrackJson]: ... 20 | def get_tracks_table(self) -> str: ... 21 | @property 22 | def size(self) -> int: ... 23 | @property 24 | def tracks(self) -> List[TrackJson]: ... 25 | @classmethod 26 | def add_track(cls, track: TrackJson, modules_folder: Path, cover: bool = ...): ... 27 | @classmethod 28 | def del_track(cls, module_id: str, modules_folder: Path): ... 29 | @classmethod 30 | def update_track(cls, track: TrackJson, modules_folder: Path): ... 31 | -------------------------------------------------------------------------------- /sync/model/ConfigJson.py: -------------------------------------------------------------------------------- 1 | from .AttrDict import AttrDict 2 | from .JsonIO import JsonIO 3 | 4 | 5 | class ConfigJson(AttrDict, JsonIO): 6 | name: str 7 | base_url: str 8 | max_num: int 9 | enable_log: bool 10 | log_dir: str 11 | 12 | def write(self, file): 13 | new = AttrDict() 14 | for key in self.expected_fields().keys(): 15 | value = self.get(key) 16 | if value is not None: 17 | new[key] = value 18 | 19 | JsonIO.write(new, file) 20 | 21 | @classmethod 22 | def load(cls, file): 23 | obj = JsonIO.load(file) 24 | return ConfigJson(obj) 25 | 26 | @classmethod 27 | def default(cls): 28 | return ConfigJson( 29 | name="Unknown", 30 | base_url="", 31 | max_num=3, 32 | enable_log=True, 33 | log_dir=None 34 | ) 35 | 36 | @classmethod 37 | def filename(cls): 38 | return "config.json" 39 | 40 | @classmethod 41 | def expected_fields(cls, __type=True): 42 | if __type: 43 | return cls.__annotations__ 44 | 45 | return {k: v.__name__ for k, v in cls.__annotations__.items()} 46 | -------------------------------------------------------------------------------- /sync/model/MagiskUpdateJson.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | from .AttrDict import AttrDict 4 | from .JsonIO import JsonIO 5 | from ..utils import HttpUtils, StrUtils 6 | 7 | 8 | class MagiskUpdateJson(AttrDict): 9 | @property 10 | def version_display(self): 11 | return StrUtils.get_version_display(self.version, self.versionCode) 12 | 13 | @property 14 | def zipfile_name(self): 15 | return StrUtils.get_filename(self.version_display, "zip") 16 | 17 | @classmethod 18 | def load(cls, path): 19 | if isinstance(path, str): 20 | obj = HttpUtils.load_json(path) 21 | elif isinstance(path, Path): 22 | obj = JsonIO.load(path) 23 | else: 24 | raise ValueError(f"unsupported type {type(path).__name__}") 25 | 26 | try: 27 | obj["versionCode"] = int(obj["versionCode"]) 28 | except ValueError: 29 | msg = f"wrong type of versionCode, expected int but got {type(obj['versionCode']).__name__}" 30 | raise ValueError(msg) 31 | except TypeError: 32 | raise ValueError("versionCode does not exist in module.prop") 33 | 34 | return MagiskUpdateJson(obj) 35 | -------------------------------------------------------------------------------- /sync/model/ModulesJson.pyi: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from typing import List, Dict, Any 3 | 4 | from .AttrDict import AttrDict 5 | from .JsonIO import JsonIO 6 | from .UpdateJson import VersionItem 7 | 8 | 9 | class OnlineModule(AttrDict): 10 | id: str 11 | license: str 12 | version: str 13 | versionCode: int 14 | name: str 15 | author: str 16 | description: str 17 | 18 | # for ModulesJson 19 | track: AttrDict 20 | versions: List[VersionItem] 21 | 22 | @property 23 | def version_display(self) -> str: ... 24 | @property 25 | def changelog_filename(self) -> str: ... 26 | @property 27 | def zipfile_name(self) -> str: ... 28 | def to_VersionItem(self, timestamp: float) -> VersionItem: ... 29 | @classmethod 30 | def from_dict(cls, obj: Dict[str, Any]) -> OnlineModule: ... 31 | 32 | 33 | class ModulesJson(AttrDict, JsonIO): 34 | name: str 35 | metadata: AttrDict 36 | modules: List[OnlineModule] 37 | 38 | @property 39 | def size(self) -> int: ... 40 | def get_timestamp(self) -> float: ... 41 | @classmethod 42 | def load(cls, file: Path) -> ModulesJson: ... 43 | @classmethod 44 | def filename(cls) -> str: ... 45 | -------------------------------------------------------------------------------- /sync/core/Sync.pyi: -------------------------------------------------------------------------------- 1 | from datetime import date 2 | from pathlib import Path 3 | from typing import Optional, List, Tuple 4 | 5 | from .Pull import Pull 6 | from ..model import ( 7 | ConfigJson, 8 | TrackJson, 9 | OnlineModule, 10 | VersionItem 11 | ) 12 | from ..track import BaseTracks 13 | from ..utils import Log 14 | 15 | 16 | class Sync: 17 | _log: Log 18 | _root_folder: Path 19 | _pull: Pull 20 | 21 | _json_folder: Path 22 | _modules_folder: Path 23 | _config: ConfigJson 24 | _tracks: BaseTracks 25 | _updated_diff: List[Tuple[Optional[VersionItem], OnlineModule]] 26 | 27 | def __init__(self, root_folder: Path, config: ConfigJson, tracks: Optional[BaseTracks] = ...): ... 28 | def _update_jsons(self, track: TrackJson, force: bool) -> Optional[OnlineModule]: ... 29 | @staticmethod 30 | def _check_tracks(obj: BaseTracks, cls: type) -> bool: ... 31 | def create_github_tracks(self, api_token: str, after_date: Optional[date] = ...) -> BaseTracks: ... 32 | def create_local_tracks(self) -> BaseTracks: ... 33 | def update( 34 | self, 35 | module_ids: Optional[List[str]] = ..., 36 | force: bool = ..., 37 | single: bool = ..., 38 | **kwargs 39 | ): ... 40 | def get_versions_diff(self) -> Optional[str]: ... 41 | -------------------------------------------------------------------------------- /sync/core/Check.pyi: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from typing import Optional, List 3 | 4 | from ..model import ( 5 | ConfigJson, 6 | TrackJson, 7 | OnlineModule, 8 | UpdateJson, 9 | VersionItem 10 | ) 11 | from ..track import LocalTracks 12 | from ..utils import Log 13 | 14 | 15 | class Check: 16 | _log: Log 17 | 18 | _local_folder: Path 19 | _modules_folder: Path 20 | _config: ConfigJson 21 | _tracks: LocalTracks 22 | 23 | def __init__(self, root_folder: Path, config: ConfigJson): ... 24 | def _get_file_url(self, module_id: str, file: Path) -> str: ... 25 | def _get_tracks(self, module_ids: Optional[List[str]], new: bool) -> List[TrackJson]: ... 26 | def _check_folder(self, track: TrackJson, target_id: str) -> bool: ... 27 | def _get_new_version_item(self, track: TrackJson, item: VersionItem) -> Optional[VersionItem]: ... 28 | def _check_update_json(self, track: TrackJson, update_json: UpdateJson, check_id: bool)-> bool: ... 29 | def get_online_module(self, module_id: str, zip_file: Path) -> Optional[OnlineModule]: ... 30 | def url(self, module_ids: Optional[List[str]] = ..., new: bool = ...): ... 31 | def ids(self, module_ids: Optional[List[str]] = ..., new: bool = ...): ... 32 | def old(self, module_ids: Optional[List[str]] = ..., new: bool = ...): ... 33 | -------------------------------------------------------------------------------- /sync/utils/StrUtils.py: -------------------------------------------------------------------------------- 1 | import re 2 | from urllib.parse import urlparse 3 | 4 | 5 | class StrUtils: 6 | @classmethod 7 | def is_with(cls, text: str, start: str, end: str) -> bool: 8 | return text.startswith(start) and text.endswith(end) 9 | 10 | @classmethod 11 | def is_html(cls, text: str) -> bool: 12 | pattern = r'|||' 13 | return re.search(pattern, text, re.IGNORECASE) is not None 14 | 15 | @classmethod 16 | def is_blob_url(cls, url: str) -> bool: 17 | pattern = r"https://github\.com/[^/]+/[^/]+/blob/.+" 18 | match = re.match(pattern, url) 19 | if match: 20 | return True 21 | else: 22 | return False 23 | 24 | @classmethod 25 | def is_url(cls, text: str) -> bool: 26 | parse_result = urlparse(text) 27 | return bool(parse_result.scheme) 28 | 29 | @classmethod 30 | def get_version_display(cls, version: str, version_code: int): 31 | included = re.search(fr"\(.*?{version_code}.*?\)", version) is not None 32 | 33 | if included: 34 | return version 35 | else: 36 | return f"{version} ({version_code})" 37 | 38 | @classmethod 39 | def get_filename(cls, base: str, suffix: str): 40 | filename = base.replace(" ", "_") 41 | filename = re.sub(r"[^a-zA-Z0-9\-._]", "", filename) 42 | return f"{filename}.{suffix}" 43 | -------------------------------------------------------------------------------- /sync/core/Index.pyi: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from typing import Optional, List 3 | 4 | from ..model import ( 5 | ConfigJson, 6 | TrackJson, 7 | OnlineModule, 8 | ModulesJson, 9 | UpdateJson 10 | ) 11 | from ..track import LocalTracks 12 | from ..utils import Log 13 | 14 | 15 | class Index: 16 | _log: Log 17 | _root_folder: Path 18 | 19 | _json_folder: Path 20 | _modules_folder: Path 21 | _config: ConfigJson 22 | _tracks: LocalTracks 23 | 24 | modules_json: ModulesJson 25 | 26 | versions: List[int] 27 | latest_version: int 28 | 29 | def __init__(self, root_folder: Path, config: ConfigJson): ... 30 | def __call__(self, version: int, to_file: bool) -> ModulesJson: ... 31 | def _add_modules_json_0( 32 | self, 33 | track: TrackJson, 34 | update_json: UpdateJson, 35 | online_module: OnlineModule 36 | ): ... 37 | def _add_modules_json_1( 38 | self, 39 | track: TrackJson, 40 | update_json: UpdateJson, 41 | online_module: OnlineModule 42 | ): ... 43 | def _add_modules_json( 44 | self, 45 | track: TrackJson, 46 | update_json: UpdateJson, 47 | online_module: OnlineModule, 48 | version: int 49 | ): ... 50 | def get_online_module(self, module_id: str, zip_file: Path) -> Optional[OnlineModule]: ... 51 | def push_by_git(self, branch: str): ... 52 | def get_versions_table(self) -> str: ... -------------------------------------------------------------------------------- /sync/utils/GitUtils.py: -------------------------------------------------------------------------------- 1 | import functools 2 | import os 3 | import shutil 4 | from pathlib import Path 5 | from typing import Optional 6 | 7 | from git import Repo, InvalidGitRepositoryError, GitCommandError 8 | 9 | 10 | class GitUtils: 11 | @classmethod 12 | @functools.lru_cache() 13 | def current_branch(cls, repo_dir: Path) -> Optional[str]: 14 | try: 15 | return Repo(repo_dir).active_branch.name 16 | except InvalidGitRepositoryError: 17 | return None 18 | 19 | @classmethod 20 | def clone_and_zip(cls, url: str, out: Path) -> float: 21 | repo_dir = out.with_suffix("") 22 | if repo_dir.exists(): 23 | shutil.rmtree(repo_dir) 24 | 25 | try: 26 | repo = Repo.clone_from(url, repo_dir) 27 | last_committed = float(repo.commit().committed_date) 28 | except GitCommandError: 29 | shutil.rmtree(repo_dir, ignore_errors=True) 30 | raise GitCommandError(f"clone failed: {url}") 31 | 32 | for path in repo_dir.iterdir(): 33 | if path.name.startswith(".git"): 34 | if path.is_dir(): 35 | shutil.rmtree(path, ignore_errors=True) 36 | if path.is_file(): 37 | path.unlink(missing_ok=True) 38 | 39 | continue 40 | 41 | os.utime(path, (last_committed, last_committed)) 42 | 43 | try: 44 | shutil.make_archive(repo_dir.as_posix(), format="zip", root_dir=repo_dir) 45 | shutil.rmtree(repo_dir) 46 | except FileNotFoundError: 47 | raise FileNotFoundError(f"archive failed: {repo_dir.as_posix()}") 48 | 49 | return last_committed 50 | -------------------------------------------------------------------------------- /sync/core/Pull.pyi: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from typing import Optional, Tuple 3 | 4 | from ..error import Result 5 | from ..model import ( 6 | TrackJson, 7 | ConfigJson, 8 | OnlineModule 9 | ) 10 | from ..utils import Log 11 | 12 | 13 | class Pull: 14 | _log: Log 15 | 16 | _local_folder: Path 17 | _modules_folder: Path 18 | _config: ConfigJson 19 | 20 | _max_size: float 21 | 22 | def __init__(self, root_folder: Path, config: ConfigJson): ... 23 | @staticmethod 24 | def _copy_file(old: Path, new: Path, delete_old: bool): ... 25 | @staticmethod 26 | @Result.catching() 27 | def _download(url: str, out: Path) -> Result: ... 28 | def _check_changelog(self, module_id: str, file: Path) -> bool: ... 29 | def _check_version_code(self, module_id: str, version_code: int) -> bool: ... 30 | def _get_file_url(self, module_id: str, file: Path) -> str: ... 31 | def _get_changelog_common(self, module_id: str, changelog: Optional[str]) -> Optional[Path]: ... 32 | def _from_zip_common( 33 | self, 34 | module_id: str, 35 | zip_file: Path, 36 | changelog_file: Optional[Path], 37 | *, 38 | delete_tmp: bool 39 | ) -> Optional[OnlineModule]: ... 40 | def from_json(self, track: TrackJson, *, local: bool) -> Tuple[Optional[OnlineModule], float]: ... 41 | def from_url(self, track: TrackJson) -> Tuple[Optional[OnlineModule], float]: ... 42 | def from_git(self, track: TrackJson) -> Tuple[Optional[OnlineModule], float]: ... 43 | def from_zip(self, track: TrackJson) -> Tuple[Optional[OnlineModule], float]: ... 44 | def from_track(self, track: TrackJson) -> Tuple[Optional[OnlineModule], float]: ... 45 | @classmethod 46 | def set_max_size(cls, value: float): ... 47 | -------------------------------------------------------------------------------- /sync/utils/HttpUtils.py: -------------------------------------------------------------------------------- 1 | import json 2 | import re 3 | from datetime import datetime 4 | from pathlib import Path 5 | from typing import Union 6 | 7 | import requests 8 | from dateutil.parser import parse 9 | from requests import HTTPError 10 | 11 | from .StrUtils import StrUtils 12 | 13 | 14 | class HttpUtils: 15 | @classmethod 16 | def _filter_json(cls, text: str) -> str: 17 | return re.sub(r",(?=\s*?[}\]])", "", text) 18 | 19 | @classmethod 20 | def load_json(cls, url: str) -> Union[list, dict]: 21 | response = requests.get(url, stream=True) 22 | if not response.ok: 23 | if StrUtils.is_html(response.text): 24 | msg = "404 not found" 25 | else: 26 | msg = response.text 27 | raise HTTPError(msg) 28 | 29 | text = cls._filter_json(response.text) 30 | obj = json.loads(text) 31 | 32 | return obj 33 | 34 | @classmethod 35 | def download(cls, url: str, out: Path) -> float: 36 | out.parent.mkdir(parents=True, exist_ok=True) 37 | 38 | response = requests.get(url, stream=True) 39 | if response.status_code == 200: 40 | block_size = 1024 41 | with open(out, 'wb') as file: 42 | for data in response.iter_content(block_size): 43 | file.write(data) 44 | 45 | if "Last-Modified" in response.headers: 46 | last_modified = response.headers["Last-Modified"] 47 | return parse(last_modified).timestamp() 48 | else: 49 | return datetime.now().timestamp() 50 | else: 51 | out.unlink(missing_ok=True) 52 | 53 | if StrUtils.is_html(response.text): 54 | msg = "404 not found" 55 | else: 56 | msg = response.text 57 | raise HTTPError(msg) 58 | -------------------------------------------------------------------------------- /sync/model/LocalModule.py: -------------------------------------------------------------------------------- 1 | from zipfile import ZipFile 2 | 3 | from .AttrDict import AttrDict 4 | from ..error import MagiskModuleError 5 | 6 | 7 | class LocalModule(AttrDict): 8 | id: str 9 | name: str 10 | version: str 11 | versionCode: int 12 | author: str 13 | description: str 14 | 15 | @classmethod 16 | def load(cls, file): 17 | zipfile = ZipFile(file, "r") 18 | fields = cls.expected_fields() 19 | 20 | try: 21 | if ( 22 | "#MAGISK" not in zipfile 23 | .read("META-INF/com/google/android/updater-script") 24 | .decode("utf-8") 25 | ): 26 | raise 27 | 28 | zipfile.read("META-INF/com/google/android/update-binary") 29 | except BaseException: 30 | msg = f"{file.name} is not a magisk module" 31 | raise MagiskModuleError(msg) 32 | 33 | try: 34 | props = zipfile.read("module.prop") 35 | except BaseException as err: 36 | raise MagiskModuleError(err.args) 37 | 38 | obj = AttrDict() 39 | for item in props.decode("utf-8").splitlines(): 40 | prop = item.split("=", maxsplit=1) 41 | if len(prop) != 2: 42 | continue 43 | 44 | key, value = prop 45 | if key == "" or key.startswith("#") or key not in fields: 46 | continue 47 | 48 | _type = fields[key] 49 | obj[key] = _type(value) 50 | 51 | local_module = LocalModule() 52 | for key in fields.keys(): 53 | local_module[key] = obj.get(key) 54 | 55 | return local_module 56 | 57 | @classmethod 58 | def expected_fields(cls, __type=True): 59 | if __type: 60 | return cls.__annotations__ 61 | 62 | return {k: v.__name__ for k, v in cls.__annotations__.items()} 63 | -------------------------------------------------------------------------------- /sync/track/GithubTracks.pyi: -------------------------------------------------------------------------------- 1 | from datetime import date 2 | from pathlib import Path 3 | from typing import Optional, List 4 | 5 | from github import Github 6 | from github.Repository import Repository 7 | 8 | from .BaseTracks import BaseTracks 9 | from ..error import Result 10 | from ..model import TrackJson, ConfigJson 11 | from ..utils import Log, GitHubGraphQLAPI 12 | 13 | 14 | class GithubTracks(BaseTracks): 15 | _log: Log 16 | _modules_folder: Path 17 | _api_token: str 18 | _after_date: date 19 | _github: Github 20 | _graphql_api: GitHubGraphQLAPI 21 | _tracks: List[TrackJson] 22 | 23 | def __init__( 24 | self, 25 | modules_folder: Path, 26 | config: ConfigJson, 27 | *, 28 | api_token: str, 29 | after_date: Optional[date] = ... 30 | ): ... 31 | @Result.catching() 32 | def _get_from_repo_common(self, repo: Repository, use_ssh: bool) -> Result: ... 33 | def _get_from_repo(self, repo: Repository, cover: bool, use_ssh: bool) -> Optional[TrackJson]: ... 34 | def get_track( 35 | self, 36 | user_name: str, 37 | repo_name: str, 38 | *, 39 | cover: bool = ..., 40 | use_ssh: bool = ... 41 | ) -> Optional[TrackJson]: ... 42 | def get_tracks( 43 | self, 44 | user_name: str, 45 | repo_names: Optional[List[str]] = ..., 46 | *, 47 | single: bool = ..., 48 | cover: bool = ..., 49 | use_ssh: bool = ... 50 | ) -> List[TrackJson]: ... 51 | def clear_tracks(self): ... 52 | @property 53 | def size(self) -> int: ... 54 | @property 55 | def tracks(self) -> List[TrackJson]: ... 56 | @classmethod 57 | def get_license(cls, repo: Repository) -> str: ... 58 | @classmethod 59 | def get_changelog(cls, repo: Repository) -> str: ... 60 | @classmethod 61 | def is_module_repo(cls, repo: Repository) -> bool: ... 62 | -------------------------------------------------------------------------------- /sync/model/ModulesJson.py: -------------------------------------------------------------------------------- 1 | from .AttrDict import AttrDict 2 | from .JsonIO import JsonIO 3 | from .UpdateJson import VersionItem 4 | from ..utils import StrUtils 5 | 6 | 7 | class OnlineModule(AttrDict): 8 | @property 9 | def version_display(self): 10 | return StrUtils.get_version_display(self.version, self.versionCode) 11 | 12 | @property 13 | def changelog_filename(self): 14 | return StrUtils.get_filename(self.version_display, "md") 15 | 16 | @property 17 | def zipfile_name(self): 18 | return StrUtils.get_filename(self.version_display, "zip") 19 | 20 | def to_VersionItem(self, timestamp): 21 | return VersionItem( 22 | timestamp=timestamp, 23 | version=self.version, 24 | versionCode=self.versionCode, 25 | zipUrl=self.latest.zipUrl, 26 | changelog=self.latest.changelog 27 | ) 28 | 29 | @classmethod 30 | def from_dict(cls, obj): 31 | versions = obj.get("versions") 32 | if versions is not None: 33 | obj["versions"] = [VersionItem(_obj) for _obj in versions] 34 | 35 | track = obj.get("track") 36 | if track is not None: 37 | obj["track"] = AttrDict(track) 38 | 39 | return OnlineModule(obj) 40 | 41 | 42 | class ModulesJson(AttrDict, JsonIO): 43 | @property 44 | def size(self): 45 | return len(self.modules) 46 | 47 | def get_timestamp(self): 48 | value0 = self.get("timestamp") 49 | 50 | value1 = None 51 | metadata = self.get("metadata") 52 | if metadata is not None: 53 | value1 = metadata.get("timestamp") 54 | 55 | return value0 or value1 or 0.0 56 | 57 | @classmethod 58 | def load(cls, file): 59 | obj = JsonIO.load(file) 60 | obj["modules"] = [OnlineModule.from_dict(_obj) for _obj in obj["modules"]] 61 | return ModulesJson(obj) 62 | 63 | @classmethod 64 | def filename(cls): 65 | return "modules.json" 66 | -------------------------------------------------------------------------------- /sync/core/Config.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | from ..error import ConfigError 4 | from ..model import ConfigJson, JsonIO 5 | from ..utils import Log, StrUtils 6 | 7 | 8 | class Config(ConfigJson): 9 | def __init__(self, root_folder): 10 | self._log = Log("Config", enable_log=True) 11 | self._root_folder = root_folder 12 | 13 | json_file = self.json_folder.joinpath(ConfigJson.filename()) 14 | if not json_file.exists(): 15 | raise FileNotFoundError(json_file.as_posix()) 16 | 17 | obj = JsonIO.load(json_file) 18 | super().__init__(obj) 19 | 20 | self._check_values() 21 | 22 | self._log = Log("Config", enable_log=self.enable_log, log_dir=self.log_dir) 23 | for key in ConfigJson.expected_fields().keys(): 24 | self._log.d(f"{key} = {self.get(key)}") 25 | 26 | def _check_values(self): 27 | default = self.default() 28 | 29 | name = self.get("name", default.name) 30 | if name == default.name: 31 | self._log.w("_check_values: 'name' is undefined") 32 | 33 | base_url = self.get("base_url", default.base_url) 34 | if base_url == default.base_url: 35 | raise ConfigError("'base_url' is undefined") 36 | elif not StrUtils.is_with(base_url, "https", "/"): 37 | raise ConfigError("'base_url' must start with 'https' and end with '/'") 38 | 39 | max_num = self.get("max_num", default.max_num) 40 | enable_log = self.get("enable_log", default.enable_log) 41 | 42 | log_dir = self.get("log_dir", default.log_dir) 43 | if log_dir != default.log_dir: 44 | log_dir = Path(log_dir) 45 | 46 | if not log_dir.is_absolute(): 47 | log_dir = self._root_folder.joinpath(log_dir) 48 | 49 | self.update( 50 | name=name, 51 | base_url=base_url, 52 | max_num=max_num, 53 | enable_log=enable_log, 54 | log_dir=log_dir 55 | ) 56 | 57 | @property 58 | def json_folder(self): 59 | return self.get_json_folder(self._root_folder) 60 | 61 | @property 62 | def modules_folder(self): 63 | return self.get_modules_folder(self._root_folder) 64 | 65 | @property 66 | def local_folder(self): 67 | return self.get_local_folder(self._root_folder) 68 | 69 | @classmethod 70 | def get_json_folder(cls, root_folder): 71 | return root_folder.joinpath("json") 72 | 73 | @classmethod 74 | def get_modules_folder(cls, root_folder): 75 | return root_folder.joinpath("modules") 76 | 77 | @classmethod 78 | def get_local_folder(cls, root_folder): 79 | return root_folder.joinpath("local") 80 | -------------------------------------------------------------------------------- /sync/utils/GitHubGraphQLAPI.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | from typing import Optional, List 3 | 4 | import requests 5 | from dateutil.parser import parse 6 | 7 | 8 | class GitHubGraphQLAPI: 9 | def __init__(self, api_token: str): 10 | self._api_token = api_token 11 | 12 | def _graphql_query(self, query: str) -> Optional[dict]: 13 | query = {"query": query} 14 | 15 | response = requests.post( 16 | url="https://api.github.com/graphql", 17 | headers={ 18 | "Authorization": f"bearer {self._api_token}", 19 | "Content-Type": "application/json", 20 | }, 21 | json=query 22 | ) 23 | 24 | if response.ok: 25 | return response.json() 26 | else: 27 | return None 28 | 29 | def _query_repository(self, owner: str, name: str, query: str) -> Optional[dict]: 30 | params = "owner: \"{}\", name: \"{}\"".format(owner, name) 31 | _query = "query { repository(%s) { %s } }" % (params, query) 32 | result = self._graphql_query(_query) 33 | 34 | try: 35 | data = result.get("data") 36 | repository = data.get("repository") 37 | return repository 38 | except AttributeError: 39 | return None 40 | 41 | def get_sponsor_url(self, owner: str, name: str) -> List[str]: 42 | repository = self._query_repository( 43 | owner=owner, 44 | name=name, 45 | query="fundingLinks { platform url }" 46 | ) 47 | if repository is None: 48 | return list() 49 | 50 | links = list() 51 | funding_links = repository["fundingLinks"] 52 | 53 | for item in funding_links: 54 | if item["platform"] == "GITHUB": 55 | name = item["url"].split("/")[-1] 56 | links.append(f"https://github.com/sponsors/{name}") 57 | else: 58 | links.append(item["url"]) 59 | 60 | return links 61 | 62 | def get_homepage_url(self, owner: str, name: str) -> Optional[str]: 63 | repository = self._query_repository( 64 | owner=owner, 65 | name=name, 66 | query="homepageUrl" 67 | ) 68 | if repository is None: 69 | return None 70 | 71 | homepage_url = repository["homepageUrl"] 72 | if homepage_url != "": 73 | return homepage_url 74 | else: 75 | return None 76 | 77 | def get_pushed_at(self, owner: str, name: str) -> Optional[datetime]: 78 | repository = self._query_repository( 79 | owner=owner, 80 | name=name, 81 | query="pushedAt" 82 | ) 83 | if repository is None: 84 | return None 85 | 86 | try: 87 | return parse(repository["pushedAt"]) 88 | except TypeError: 89 | return None 90 | -------------------------------------------------------------------------------- /sync/core/Migrate.py: -------------------------------------------------------------------------------- 1 | import shutil 2 | 3 | from .Config import Config 4 | from ..model import JsonIO, ConfigJson, TrackJson 5 | 6 | 7 | class Migrate: 8 | def __init__(self, root_folder): 9 | self._root_folder = root_folder 10 | 11 | @staticmethod 12 | def _config_0_1(old_config): 13 | return { 14 | "NAME": old_config.get("repo_name"), 15 | "BASE_URL": old_config.get("repo_url"), 16 | "MAX_NUM": old_config.get("max_num"), 17 | "ENABLE_LOG": old_config.get("show_log"), 18 | "LOG_DIR": old_config.get("log_dir") 19 | } 20 | 21 | @staticmethod 22 | def _config_1_2(old_config): 23 | return { 24 | "name": old_config.get("NAME"), 25 | "base_url": old_config.get("BASE_URL"), 26 | "max_num": old_config.get("MAX_NUM"), 27 | "enable_log": old_config.get("ENABLE_LOG"), 28 | "log_dir": old_config.get("LOG_DIR") 29 | } 30 | 31 | def config(self): 32 | config_folder = self._root_folder.joinpath("config") 33 | json_folder = self._root_folder.joinpath("json") 34 | 35 | config_json_0 = config_folder.joinpath("config.json") 36 | config_json_1 = json_folder.joinpath("config.json") 37 | config_json_new = json_folder.joinpath("config.json") 38 | 39 | config = ConfigJson(version=-1) 40 | 41 | # v0 42 | if config_json_0.exists(): 43 | config.update(JsonIO.load(config_json_0)) 44 | config.version = 0 45 | shutil.rmtree(config_folder) 46 | 47 | # v1 48 | if config_json_1.exists(): 49 | config.update(JsonIO.load(config_json_1)) 50 | 51 | if config.get("NAME") is not None: 52 | config.version = 1 53 | 54 | # v0 -> v1 55 | if config.version == 0: 56 | config.update(self._config_0_1(config)) 57 | config.version = 1 58 | 59 | # v1 -> v2 60 | if config.version == 1: 61 | config.update(self._config_1_2(config)) 62 | config.version = 2 63 | 64 | # write to file 65 | if config.version != -1: 66 | config.write(config_json_new) 67 | 68 | def track(self): 69 | modules_folder = Config.get_modules_folder(self._root_folder) 70 | 71 | for module_folder in modules_folder.glob("*/"): 72 | json_file = module_folder.joinpath(TrackJson.filename()) 73 | if not json_file.exists(): 74 | continue 75 | 76 | track = TrackJson.load(json_file) 77 | 78 | # remove .disable 79 | tag_disable = module_folder.joinpath(".disable") 80 | if tag_disable.exists(): 81 | track.enable = False 82 | tag_disable.unlink() 83 | elif track.enable is None: 84 | track.enable = True 85 | 86 | # write to file (remove null values) 87 | track.write(json_file) 88 | -------------------------------------------------------------------------------- /sync/model/TrackJson.py: -------------------------------------------------------------------------------- 1 | from enum import Enum 2 | 3 | from .AttrDict import AttrDict 4 | from .JsonIO import JsonIO 5 | 6 | 7 | class TrackJson(AttrDict, JsonIO): 8 | id: str 9 | enable: bool 10 | update_to: str 11 | changelog: str 12 | license: str 13 | homepage: str 14 | source: str 15 | support: str 16 | donate: str 17 | max_num: int 18 | 19 | # noinspection PyAttributeOutsideInit 20 | @property 21 | def type(self): 22 | if self._type is not None: 23 | return self._type 24 | 25 | if self.update_to.startswith("http"): 26 | if self.update_to.endswith(".json"): 27 | self._type = TrackType.ONLINE_JSON 28 | elif self.update_to.endswith(".zip"): 29 | self._type = TrackType.ONLINE_ZIP 30 | elif self.update_to.endswith(".git"): 31 | self._type = TrackType.GIT 32 | 33 | elif self.update_to.startswith("git@"): 34 | if self.update_to.endswith(".git"): 35 | self._type = TrackType.GIT 36 | 37 | else: 38 | if self.update_to.endswith(".json"): 39 | self._type = TrackType.LOCAL_JSON 40 | elif self.update_to.endswith(".zip"): 41 | self._type = TrackType.LOCAL_ZIP 42 | 43 | if self._type is None: 44 | self._type = TrackType.UNKNOWN 45 | 46 | return self._type 47 | 48 | def json(self): 49 | return AttrDict( 50 | type=self.type.name, 51 | added=self.added, 52 | license=self.license or "", 53 | homepage=self.homepage or "", 54 | source=self.source or "", 55 | support=self.support or "", 56 | donate=self.donate or "" 57 | ) 58 | 59 | def write(self, file): 60 | new = AttrDict() 61 | keys = list(self.expected_fields().keys()) 62 | 63 | # fields without manually 64 | keys.extend(["added", "last_update", "versions"]) 65 | 66 | for key in keys: 67 | value = self.get(key, "") 68 | if value is None: 69 | continue 70 | 71 | if isinstance(value, str): 72 | if value == "" or value.isspace(): 73 | continue 74 | 75 | new[key] = value 76 | 77 | JsonIO.write(new, file) 78 | 79 | @classmethod 80 | def load(cls, file): 81 | obj = JsonIO.load(file) 82 | return TrackJson(obj) 83 | 84 | @classmethod 85 | def filename(cls): 86 | return "track.json" 87 | 88 | @classmethod 89 | def expected_fields(cls, __type=True): 90 | if __type: 91 | return cls.__annotations__ 92 | 93 | return {k: v.__name__ for k, v in cls.__annotations__.items()} 94 | 95 | 96 | class TrackType(Enum): 97 | UNKNOWN = 0 98 | ONLINE_JSON = 1 99 | ONLINE_ZIP = 2 100 | GIT = 3 101 | LOCAL_JSON = 4 102 | LOCAL_ZIP = 5 103 | -------------------------------------------------------------------------------- /sync/track/LocalTracks.py: -------------------------------------------------------------------------------- 1 | import shutil 2 | from datetime import datetime 3 | from typing import List 4 | 5 | from tabulate import tabulate 6 | 7 | from .BaseTracks import BaseTracks 8 | from ..error import Result 9 | from ..model import TrackJson 10 | from ..utils import Log 11 | 12 | 13 | class LocalTracks(BaseTracks): 14 | def __init__(self, modules_folder, config): 15 | self._log = Log("LocalTracks", enable_log=config.enable_log, log_dir=config.log_dir) 16 | self._modules_folder = modules_folder 17 | 18 | self._tracks: List[TrackJson] = list() 19 | 20 | @Result.catching() 21 | def _get_from_file(self, file): 22 | return TrackJson.load(file) 23 | 24 | def get_track(self, module_id): 25 | module_folder = self._modules_folder.joinpath(module_id) 26 | json_file = module_folder.joinpath(TrackJson.filename()) 27 | 28 | result = self._get_from_file(json_file) 29 | if result.is_failure: 30 | msg = Log.get_msg(result.error) 31 | self._log.e(f"get_track: [{module_id}] -> {msg}") 32 | 33 | return None 34 | else: 35 | return result.value 36 | 37 | def get_tracks(self, module_ids=None): 38 | self._tracks.clear() 39 | self._log.i(f"get_tracks: modules_folder = {self._modules_folder}") 40 | 41 | if module_ids is None: 42 | module_ids = [_dir.name for _dir in sorted(self._modules_folder.glob("[!.]*/"))] 43 | 44 | for module_id in module_ids: 45 | track_json = self.get_track(module_id) 46 | if track_json is not None: 47 | self._tracks.append(track_json) 48 | 49 | self._log.i(f"get_tracks: size = {self.size}") 50 | return self._tracks 51 | 52 | def get_tracks_table(self): 53 | if self.size == 0: 54 | self.get_tracks() 55 | 56 | headers = ["id", "add time", "last update", "versions"] 57 | table = [] 58 | 59 | for track in self.tracks: 60 | if track.versions is None: 61 | last_update = "-" 62 | versions = 0 63 | else: 64 | last_update = datetime.fromtimestamp(track.last_update).date() 65 | versions = track.versions 66 | 67 | table.append([ 68 | track.id, 69 | datetime.fromtimestamp(track.added).date(), 70 | last_update, 71 | versions 72 | ]) 73 | 74 | markdown_text = tabulate(table, headers, tablefmt="github") 75 | return markdown_text 76 | 77 | @property 78 | def size(self): 79 | return self._tracks.__len__() 80 | 81 | @property 82 | def tracks(self): 83 | return self._tracks 84 | 85 | @classmethod 86 | def add_track(cls, track, modules_folder, cover=True): 87 | module_folder = modules_folder.joinpath(track.id) 88 | json_file = module_folder.joinpath(TrackJson.filename()) 89 | 90 | if not json_file.exists(): 91 | track.added = datetime.now().timestamp() 92 | track.enable = True 93 | track.write(json_file) 94 | elif cover: 95 | old = TrackJson.load(json_file) 96 | old.added = old.added or json_file.stat().st_mtime 97 | old.enable = old.enable or True 98 | 99 | old.update(track) 100 | old.write(json_file) 101 | 102 | @classmethod 103 | def del_track(cls, module_id, modules_folder): 104 | module_folder = modules_folder.joinpath(module_id) 105 | shutil.rmtree(module_folder, ignore_errors=True) 106 | 107 | @classmethod 108 | def update_track(cls, track, modules_folder): 109 | module_folder = modules_folder.joinpath(track.id) 110 | json_file = module_folder.joinpath(TrackJson.filename()) 111 | 112 | if not json_file.exists(): 113 | return 114 | 115 | old = TrackJson.load(json_file) 116 | old.update(track) 117 | old.write(json_file) 118 | -------------------------------------------------------------------------------- /sync/utils/Log.py: -------------------------------------------------------------------------------- 1 | import functools 2 | import logging 3 | import sys 4 | from datetime import date 5 | from pathlib import Path 6 | from typing import Optional, Dict, Union 7 | 8 | 9 | class Log: 10 | _logger_initialized: dict = {} 11 | _file_prefix: Optional[str] = None 12 | _enable_stdout: bool = True 13 | _log_level: int = logging.DEBUG 14 | 15 | def __init__(self, tag: str, *, enable_log: bool = True, log_dir: Optional[Path] = None): 16 | if log_dir is not None: 17 | if self._file_prefix is None: 18 | prefix = tag.lower() 19 | else: 20 | prefix = self._file_prefix 21 | 22 | log_file = f"{prefix}_{date.today()}.log" 23 | log_file = log_dir.joinpath(log_file) 24 | self.clear(log_dir, prefix) 25 | else: 26 | log_file = None 27 | 28 | self._enable_log = enable_log 29 | self._logging = self.get_logger( 30 | name=tag, 31 | log_file=log_file, 32 | log_level=self._log_level 33 | ) 34 | 35 | def log(self, level: int, msg: str): 36 | if self._enable_log: 37 | self._logging.log(level=level, msg=msg) 38 | 39 | def d(self, msg: str): 40 | self.log(level=logging.DEBUG, msg=msg) 41 | 42 | def i(self, msg: str): 43 | self.log(level=logging.INFO, msg=msg) 44 | 45 | def w(self, msg: str): 46 | self.log(level=logging.WARN, msg=msg) 47 | 48 | def e(self, msg: str): 49 | self.log(level=logging.ERROR, msg=msg) 50 | 51 | @classmethod 52 | def levels(cls) -> Dict[str, int]: 53 | return { 54 | "ERROR": logging.ERROR, 55 | "WARN": logging.WARNING, 56 | "WARNING": logging.WARNING, 57 | "INFO": logging.INFO, 58 | "DEBUG": logging.DEBUG, 59 | } 60 | 61 | @classmethod 62 | def set_file_prefix(cls, name: str): 63 | cls._file_prefix = name 64 | 65 | @classmethod 66 | def set_enable_stdout(cls, value: bool): 67 | cls._enable_stdout = value 68 | 69 | @classmethod 70 | def set_log_level(cls, level: Union[int, str]): 71 | levels = cls.levels() 72 | 73 | if isinstance(level, str): 74 | level = levels.get(level, logging.DEBUG) 75 | elif isinstance(level, int) and level not in levels.values(): 76 | level = logging.DEBUG 77 | 78 | cls._log_level = level 79 | 80 | @classmethod 81 | def clear(cls, log_dir: Path, prefix: str, max_num: int = 3): 82 | log_files = sorted(log_dir.glob(f"{prefix}*"), reverse=True) 83 | if len(log_files) >= max_num + 1: 84 | for log_file in log_files[max_num:]: 85 | log_file.unlink() 86 | 87 | @classmethod 88 | def get_msg(cls, err: BaseException) -> str: 89 | msg = "{} " * len(err.args) 90 | msg = msg.format(*err.args).rstrip() 91 | return f"{err.__class__.__name__}({msg})" 92 | 93 | @classmethod 94 | @functools.lru_cache() 95 | def get_logger(cls, name: str = "root", log_file: Optional[Path] = None, log_level: int = logging.DEBUG): 96 | logger = logging.getLogger(name) 97 | if name in cls._logger_initialized: 98 | return logger 99 | for logger_name in cls._logger_initialized: 100 | if name.startswith(logger_name): 101 | return logger 102 | 103 | formatter = logging.Formatter( 104 | "[%(asctime)s] %(name)s %(levelname)s: %(message)s", 105 | datefmt="%Y/%m/%d %H:%M:%S") 106 | 107 | if cls._enable_stdout: 108 | stdout_handler = logging.StreamHandler(stream=sys.stdout) 109 | stdout_handler.setFormatter(formatter) 110 | stdout_handler.addFilter(lambda log: log.levelno < logging.ERROR) 111 | logger.addHandler(stdout_handler) 112 | 113 | stderr_handler = logging.StreamHandler(stream=sys.stderr) 114 | stderr_handler.setFormatter(formatter) 115 | stderr_handler.addFilter(lambda log: log.levelno >= logging.ERROR) 116 | logger.addHandler(stderr_handler) 117 | 118 | if log_file is not None: 119 | log_file.parent.mkdir(parents=True, exist_ok=True) 120 | 121 | file_handler = logging.FileHandler(log_file, "a") 122 | file_handler.setFormatter(formatter) 123 | logger.addHandler(file_handler) 124 | 125 | logger.setLevel(log_level) 126 | cls._logger_initialized[name] = True 127 | return logger 128 | -------------------------------------------------------------------------------- /sync/core/Index.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | 3 | from git import Repo 4 | from tabulate import tabulate 5 | 6 | from .Config import Config 7 | from ..error import Result 8 | from ..model import ( 9 | AttrDict, 10 | ModulesJson, 11 | UpdateJson, 12 | LocalModule, 13 | OnlineModule 14 | ) 15 | from ..track import LocalTracks 16 | from ..utils import Log 17 | 18 | 19 | class Index: 20 | versions = [0, 1] 21 | latest_version = versions[-1] 22 | 23 | def __init__(self, root_folder, config): 24 | self._log = Log("Index", enable_log=config.enable_log, log_dir=config.log_dir) 25 | self._root_folder = root_folder 26 | 27 | self._modules_folder = Config.get_modules_folder(root_folder) 28 | self._tracks = LocalTracks(self._modules_folder, config) 29 | self._config = config 30 | 31 | self._json_folder = Config.get_json_folder(root_folder) 32 | 33 | # noinspection PyTypeChecker 34 | self.modules_json = None 35 | 36 | def _add_modules_json_0(self, track, update_json, online_module): 37 | if self.modules_json is None: 38 | self.modules_json = ModulesJson( 39 | name=self._config.name, 40 | timestamp=datetime.now().timestamp(), 41 | modules=list() 42 | ) 43 | 44 | latest_item = update_json.versions[-1] 45 | 46 | online_module.license = track.license or "" 47 | online_module.states = AttrDict( 48 | zipUrl=latest_item.zipUrl, 49 | changelog=latest_item.changelog 50 | ) 51 | 52 | self.modules_json.modules.append(online_module) 53 | 54 | def _add_modules_json_1(self, track, update_json, online_module): 55 | if self.modules_json is None: 56 | self.modules_json = ModulesJson( 57 | name=self._config.name, 58 | metadata=AttrDict( 59 | version=1, 60 | timestamp=datetime.now().timestamp() 61 | ), 62 | modules=list() 63 | ) 64 | 65 | online_module.track = track.json() 66 | online_module.versions = update_json.versions 67 | 68 | self.modules_json.modules.append(online_module) 69 | 70 | def _add_modules_json(self, track, update_json, online_module, version): 71 | if version not in self.versions: 72 | raise RuntimeError(f"unsupported version: {version}") 73 | 74 | func = getattr(self, f"_add_modules_json_{version}") 75 | func( 76 | track=track, 77 | update_json=update_json, 78 | online_module=online_module, 79 | ) 80 | 81 | def get_online_module(self, module_id, zip_file): 82 | @Result.catching() 83 | def get_online_module(): 84 | local_module = LocalModule.load(zip_file) 85 | return OnlineModule.from_dict(local_module) 86 | 87 | result = get_online_module() 88 | if result.is_failure: 89 | msg = Log.get_msg(result.error) 90 | self._log.e(f"get_online_module: [{module_id}] -> {msg}") 91 | return None 92 | 93 | else: 94 | return result.value 95 | 96 | def __call__(self, version, to_file): 97 | for track in self._tracks.get_tracks(): 98 | module_folder = self._modules_folder.joinpath(track.id) 99 | update_json_file = module_folder.joinpath(UpdateJson.filename()) 100 | if not update_json_file.exists(): 101 | continue 102 | 103 | update_json = UpdateJson.load(update_json_file) 104 | latest_item = update_json.versions[-1] 105 | 106 | zip_file = module_folder.joinpath(latest_item.zipfile_name) 107 | if not zip_file.exists(): 108 | continue 109 | 110 | online_module = self.get_online_module(track.id, zip_file) 111 | if online_module is None: 112 | continue 113 | 114 | self._add_modules_json( 115 | track=track, 116 | update_json=update_json, 117 | online_module=online_module, 118 | version=version 119 | ) 120 | 121 | self.modules_json.modules.sort(key=lambda v: v.id) 122 | if to_file: 123 | json_file = self._json_folder.joinpath(ModulesJson.filename()) 124 | self.modules_json.write(json_file) 125 | 126 | return self.modules_json 127 | 128 | def push_by_git(self, branch): 129 | json_file = self._json_folder.joinpath(ModulesJson.filename()) 130 | timestamp = ModulesJson.load(json_file).get_timestamp() 131 | msg = f"Update by CLI ({datetime.fromtimestamp(timestamp)})" 132 | 133 | repo = Repo(self._root_folder) 134 | repo.git.add(all=True) 135 | repo.index.commit(msg) 136 | repo.remote().push(branch) 137 | 138 | def get_versions_table(self): 139 | headers = ["id", "name", "latest version"] 140 | table = [] 141 | 142 | for track in self._tracks.get_tracks(): 143 | module_folder = self._modules_folder.joinpath(track.id) 144 | update_json_file = module_folder.joinpath(UpdateJson.filename()) 145 | 146 | if not update_json_file.exists(): 147 | table.append( 148 | [track.id, "-", "-"] 149 | ) 150 | continue 151 | 152 | update_json = UpdateJson.load(update_json_file) 153 | latest = update_json.versions[-1] 154 | zip_file = module_folder.joinpath(latest.zipfile_name) 155 | online_module = self.get_online_module(track.id, zip_file) 156 | 157 | if online_module is not None: 158 | name = online_module.name.replace("|", "-") 159 | table.append( 160 | [online_module.id, name, online_module.version_display] 161 | ) 162 | else: 163 | table.append( 164 | [track.id, "-", "-"] 165 | ) 166 | 167 | markdown_text = tabulate(table, headers, tablefmt="github") 168 | return markdown_text 169 | -------------------------------------------------------------------------------- /sync/core/Sync.py: -------------------------------------------------------------------------------- 1 | import concurrent.futures 2 | from concurrent.futures import ThreadPoolExecutor 3 | 4 | from tabulate import tabulate 5 | 6 | from .Config import Config 7 | from .Pull import Pull 8 | from ..model import ( 9 | UpdateJson, 10 | TrackJson 11 | ) 12 | from ..track import BaseTracks, LocalTracks, GithubTracks 13 | from ..utils import Log 14 | 15 | 16 | class Sync: 17 | def __init__(self, root_folder, config, tracks=None): 18 | self._log = Log("Sync", enable_log=config.enable_log, log_dir=config.log_dir) 19 | self._root_folder = root_folder 20 | self._pull = Pull(root_folder, config) 21 | 22 | self._json_folder = Config.get_json_folder(root_folder) 23 | self._modules_folder = Config.get_modules_folder(root_folder) 24 | self._config = config 25 | 26 | if tracks is None: 27 | self._tracks = BaseTracks() 28 | else: 29 | self._tracks = tracks 30 | 31 | self._updated_diff = list() 32 | 33 | def _update_jsons(self, track, force): 34 | module_folder = self._modules_folder.joinpath(track.id) 35 | 36 | if not track.enable: 37 | self._log.i(f"_update_jsons: [{track.id}] -> update check has been disabled") 38 | return None 39 | 40 | online_module, timestamp = self._pull.from_track(track) 41 | if online_module is None: 42 | return None 43 | 44 | update_json_file = module_folder.joinpath(UpdateJson.filename()) 45 | track_json_file = module_folder.joinpath(TrackJson.filename()) 46 | 47 | if force: 48 | for file in module_folder.glob("*"): 49 | if file.name not in [ 50 | TrackJson.filename(), 51 | online_module.zipfile_name, 52 | online_module.changelog_filename 53 | ]: 54 | file.unlink() 55 | 56 | if update_json_file.exists(): 57 | update_json = UpdateJson.load(update_json_file) 58 | update_json.update(id=track.id) 59 | else: 60 | update_json = UpdateJson( 61 | id=track.id, 62 | timestamp=timestamp, 63 | versions=list() 64 | ) 65 | 66 | version_item = online_module.to_VersionItem(timestamp) 67 | update_json.versions.append(version_item) 68 | 69 | max_num = self._config.max_num 70 | if track.max_num is not None: 71 | max_num = track.max_num 72 | 73 | if len(update_json.versions) > max_num: 74 | old_item = update_json.versions.pop(0) 75 | zipfile = module_folder.joinpath(old_item.zipfile_name) 76 | changelog = module_folder.joinpath(old_item.changelog_filename) 77 | 78 | for path in [zipfile, changelog]: 79 | if not (path.exists() and path.is_file()): 80 | continue 81 | 82 | self._log.d(f"_update_jsons: [{track.id}] -> remove {path.name}") 83 | path.unlink() 84 | 85 | track.last_update = timestamp 86 | track.versions = len(update_json.versions) 87 | 88 | update_json.write(update_json_file) 89 | track.write(track_json_file) 90 | 91 | if len(update_json.versions) >= 2: 92 | self._updated_diff.append( 93 | (update_json.versions[-2], online_module) 94 | ) 95 | else: 96 | self._updated_diff.append( 97 | (None, online_module) 98 | ) 99 | 100 | return online_module 101 | 102 | @staticmethod 103 | def _check_tracks(obj, cls): 104 | if type(obj) is BaseTracks: 105 | raise RuntimeError("tracks interface has not been created") 106 | 107 | return isinstance(obj, cls) 108 | 109 | def create_github_tracks(self, api_token, after_date=None): 110 | self._tracks = GithubTracks( 111 | modules_folder=self._modules_folder, 112 | config=self._config, 113 | api_token=api_token, 114 | after_date=after_date 115 | ) 116 | return self._tracks 117 | 118 | def create_local_tracks(self): 119 | self._tracks = LocalTracks( 120 | modules_folder=self._modules_folder, 121 | config=self._config 122 | ) 123 | return self._tracks 124 | 125 | def update(self, module_ids=None, force=False, single=False, **kwargs): 126 | user_name = kwargs.get("user_name") 127 | if user_name is not None: 128 | if self._check_tracks(self._tracks, GithubTracks): 129 | tracks = self._tracks.get_tracks( 130 | user_name=user_name, 131 | repo_names=module_ids, 132 | single=single, 133 | cover=kwargs.get("cover", False), 134 | use_ssh=kwargs.get("use_ssh", True) 135 | ) 136 | else: 137 | msg = f"unsupported tracks interface type [{type(self._tracks).__name__}]" 138 | raise RuntimeError(msg) 139 | else: 140 | tracks = self._tracks.get_tracks(module_ids) 141 | 142 | with ThreadPoolExecutor(max_workers=1 if single else None) as executor: 143 | futures = [] 144 | for track in tracks: 145 | futures.append( 146 | executor.submit(self._update_jsons, track=track, force=force) 147 | ) 148 | 149 | for future in concurrent.futures.as_completed(futures): 150 | online_module = future.result() 151 | if online_module is not None: 152 | self._log.i(f"update: [{online_module.id}] -> update to {online_module.version_display}") 153 | 154 | def get_versions_diff(self): 155 | headers = ["id", "name", "version"] 156 | table = [] 157 | 158 | if len(self._updated_diff) == 0: 159 | return None 160 | 161 | for last, new in self._updated_diff: 162 | version = new.version_display 163 | if last is not None: 164 | version = f"{last.version_display} -> {version}" 165 | 166 | name = new.name.replace("|", "_") 167 | table.append( 168 | [new.id, name, version] 169 | ) 170 | 171 | markdown_text = tabulate(table, headers, tablefmt="github") 172 | return markdown_text 173 | -------------------------------------------------------------------------------- /sync/track/GithubTracks.py: -------------------------------------------------------------------------------- 1 | import concurrent.futures 2 | import shutil 3 | from concurrent.futures import ThreadPoolExecutor 4 | from datetime import date 5 | 6 | from github import Github, Auth, UnknownObjectException 7 | from github.Repository import Repository 8 | 9 | from .BaseTracks import BaseTracks 10 | from .LocalTracks import LocalTracks 11 | from ..error import MagiskModuleError, Result 12 | from ..model import TrackJson 13 | from ..utils import Log, GitHubGraphQLAPI 14 | 15 | 16 | class GithubTracks(BaseTracks): 17 | def __init__(self, modules_folder, config, *, api_token, after_date=None): 18 | self._log = Log("GithubTracks", enable_log=config.enable_log, log_dir=config.log_dir) 19 | self._modules_folder = modules_folder 20 | 21 | if after_date is None: 22 | after_date = date(2016, 9, 8) 23 | 24 | self._api_token = api_token 25 | self._after_date = after_date 26 | self._github = Github(auth=Auth.Token(api_token)) 27 | self._graphql_api = GitHubGraphQLAPI(api_token=api_token) 28 | self._tracks = list() 29 | 30 | @Result.catching() 31 | def _get_from_repo_common(self, repo: Repository, use_ssh): 32 | if not self.is_module_repo(repo): 33 | raise MagiskModuleError(f"{repo.name} is not a target magisk module repository") 34 | 35 | try: 36 | update_to = repo.get_contents("update.json").download_url 37 | changelog = "" 38 | except UnknownObjectException: 39 | if use_ssh: 40 | update_to = repo.ssh_url 41 | else: 42 | update_to = repo.clone_url 43 | changelog = self.get_changelog(repo) 44 | 45 | if repo.has_issues: 46 | issues = f"{repo.html_url}/issues" 47 | else: 48 | issues = "" 49 | 50 | donate_urls = self._graphql_api.get_sponsor_url( 51 | owner=repo.owner.login, 52 | name=repo.name 53 | ) 54 | if len(donate_urls) == 0: 55 | donate = "" 56 | else: 57 | donate = donate_urls[0] 58 | 59 | homepage = self._graphql_api.get_homepage_url( 60 | owner=repo.owner.login, 61 | name=repo.name 62 | ) 63 | if homepage is None: 64 | homepage = "" 65 | 66 | return TrackJson( 67 | id=repo.name, 68 | update_to=update_to, 69 | license=self.get_license(repo), 70 | changelog=changelog, 71 | homepage=homepage, 72 | source=repo.clone_url, 73 | support=issues, 74 | donate=donate 75 | ) 76 | 77 | def _get_from_repo(self, repo, cover, use_ssh): 78 | self._log.d(f"_get_from_repo: repo_name = {repo.name}") 79 | 80 | pushed_at = self._graphql_api.get_pushed_at( 81 | owner=repo.owner.login, 82 | name=repo.name 83 | ) 84 | if pushed_at is None: 85 | return None 86 | 87 | if pushed_at.date() < self._after_date: 88 | msg = f"pushed at {pushed_at.date()}, too old" 89 | self._log.w(f"_get_from_repo: [{repo.name}] -> {msg}") 90 | return None 91 | 92 | result = self._get_from_repo_common(repo, use_ssh) 93 | if result.is_failure: 94 | msg = Log.get_msg(result.error) 95 | self._log.e(f"_get_from_repo: [{repo.name}] -> {msg}") 96 | return None 97 | else: 98 | track_json: TrackJson = result.value 99 | LocalTracks.add_track(track_json, self._modules_folder, cover) 100 | 101 | return track_json 102 | 103 | def get_track(self, user_name, repo_name, *, cover=False, use_ssh=True): 104 | user = self._github.get_user(user_name) 105 | repo = user.get_repo(repo_name) 106 | 107 | return self._get_from_repo(repo, cover, use_ssh) 108 | 109 | def get_tracks(self, user_name, repo_names=None, *, single=False, cover=False, use_ssh=True): 110 | self._tracks.clear() 111 | self._log.i(f"get_tracks: user_name = {user_name}") 112 | 113 | user = self._github.get_user(user_name) 114 | repos = [] 115 | 116 | if repo_names is not None: 117 | for repo_name in repo_names: 118 | try: 119 | repo = user.get_repo(repo_name) 120 | repos.append(repo) 121 | except UnknownObjectException as err: 122 | msg = Log.get_msg(err) 123 | self._log.e(f"get_tracks: [{repo_name}] -> {msg}") 124 | else: 125 | repos = user.get_repos() 126 | 127 | with ThreadPoolExecutor(max_workers=1 if single else None) as executor: 128 | futures = [] 129 | for repo in repos: 130 | futures.append( 131 | executor.submit(self._get_from_repo, repo=repo, cover=cover, use_ssh=use_ssh) 132 | ) 133 | 134 | for future in concurrent.futures.as_completed(futures): 135 | track_json = future.result() 136 | if track_json is not None: 137 | self._tracks.append(track_json) 138 | 139 | self._log.i(f"get_tracks: size = {self.size}") 140 | return self._tracks 141 | 142 | def clear_tracks(self): 143 | names = [track.id for track in self._tracks] 144 | for module_folder in self._modules_folder.glob("*/"): 145 | if module_folder.name not in names: 146 | self._log.i(f"clear_tracks: [{module_folder.name}] -> removed") 147 | shutil.rmtree(module_folder, ignore_errors=True) 148 | 149 | @property 150 | def size(self): 151 | return self._tracks.__len__() 152 | 153 | @property 154 | def tracks(self): 155 | return self._tracks 156 | 157 | @classmethod 158 | def get_license(cls, repo): 159 | try: 160 | _license = repo.get_license().license.spdx_id 161 | if _license == "NOASSERTION": 162 | _license = "UNKNOWN" 163 | except UnknownObjectException: 164 | _license = "" 165 | 166 | return _license 167 | 168 | @classmethod 169 | def get_changelog(cls, repo): 170 | try: 171 | changelog = repo.get_contents("changelog.md").download_url 172 | except UnknownObjectException: 173 | changelog = "" 174 | 175 | return changelog 176 | 177 | @classmethod 178 | def is_module_repo(cls, repo): 179 | try: 180 | repo.get_contents("module.prop") 181 | repo.get_contents("META-INF/com/google/android/update-binary") 182 | repo.get_contents("META-INF/com/google/android/updater-script") 183 | return True 184 | except UnknownObjectException: 185 | return False 186 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Magisk Modules Repo Util 2 | [![python](https://img.shields.io/badge/3.10+-blue.svg?label=python)](https://github.com/MRepoApp/magisk-modules-repo-util) [![release](https://img.shields.io/github/v/release/MRepoApp/magisk-modules-repo-util?label=release&color=green)](https://github.com/MRepoApp/magisk-modules-repo-util/releases/latest) [![license](https://img.shields.io/github/license/MRepoApp/magisk-modules-repo-util)](LICENSE) 3 | 4 | A util for building modules repository 5 | 6 | ## cli.py 7 | ``` 8 | cli.py --help 9 | usage: cli.py [-h] [-v] [-V] command ... 10 | 11 | Magisk Modules Repo Util 12 | 13 | positional arguments: 14 | command 15 | config Modify config of repository. 16 | track Module tracks utility. 17 | github Generate tracks from GitHub. 18 | sync Sync modules in repository. 19 | index Generate modules.json from local. 20 | check Content check and migrate. 21 | 22 | options: 23 | -h, --help Show this help message and exit. 24 | -v, --version Show util version and exit. 25 | -V, --version-code Show util version code and exit. 26 | ``` 27 | 28 | ## config.json 29 | ```json 30 | { 31 | "name": "str", 32 | "base_url": "str", 33 | "max_num": "int", 34 | "enable_log": "bool", 35 | "log_dir": "str" 36 | } 37 | ``` 38 | | Key | Attribute | Description | 39 | |:-:|:-:|:-:| 40 | | name | required | Name of your module repository | 41 | | base_url | required | Need to end with `/` | 42 | | max_num | optional | Max num of versions for modules, default is `3` | 43 | | enable_log | optional | default is `true` | 44 | | log_dir | optional | default is `null` | 45 | 46 | ## track.json 47 | ```json 48 | { 49 | "id": "str", 50 | "enable": "bool", 51 | "update_to": "str", 52 | "changelog": "str", 53 | "license": "str", 54 | "homepage": "str", 55 | "source": "str", 56 | "support": "str", 57 | "donate": "str", 58 | "max_num": "int" 59 | } 60 | ``` 61 | | Key | Attribute | Description | 62 | |:-:|:-:|:-:| 63 | | id | required | Id of Module (_in `module.prop`_) | 64 | | enable | required | - | 65 | | update_to | required | - | 66 | | changelog | optional | Markdown or Simple Text (**_no HTML_**) | 67 | | license | optional | SPDX ID | 68 | | homepage | optional | Url | 69 | | source | optional | Url | 70 | | support | optional | Url | 71 | | donate | optional | Url | 72 | | max_num | optional | Overload `MAX_NUM` in config.json | 73 | 74 | ### Update from updateJson 75 | > For those modules that provide [updateJson](https://topjohnwu.github.io/Magisk/guides.html#moduleprop). 76 | ```json 77 | { 78 | "id": "zygisk_lsposed", 79 | "update_to": "https://lsposed.github.io/LSPosed/release/zygisk.json", 80 | "license": "GPL-3.0" 81 | } 82 | ``` 83 | 84 | ### Update from local updateJson 85 | > *update_to* requires a relative directory of *local*. 86 | ```json 87 | { 88 | "id": "zygisk_lsposed", 89 | "update_to": "zygisk.json", 90 | "license": "GPL-3.0" 91 | } 92 | ``` 93 | 94 | ### Update from url 95 | > For those have a same url to release new modules. 96 | ```json 97 | { 98 | "id": "zygisk_lsposed", 99 | "update_to": "https://github.com/LSPosed/LSPosed/releases/download/v1.8.6/LSPosed-v1.8.6-6712-zygisk-release.zip", 100 | "license": "GPL-3.0", 101 | "changelog": "https://lsposed.github.io/LSPosed/release/changelog.md" 102 | } 103 | ``` 104 | 105 | ### Update from git 106 | > For those we can get module by packaging all files in the repository, such as [Magisk-Modules-Repo](https://github.com/Magisk-Modules-Repo) and [Magisk-Modules-Alt-Repo](https://github.com/Magisk-Modules-Alt-Repo). 107 | ```json 108 | { 109 | "id": "busybox-ndk", 110 | "update_to": "https://github.com/Magisk-Modules-Repo/busybox-ndk.git" 111 | } 112 | ``` 113 | 114 | ### Update from local zip 115 | > *update_to* and *changelog* requires a relative directory of *local*. 116 | ```json 117 | { 118 | "id": "zygisk_lsposed", 119 | "update_to": "LSPosed-v1.8.6-6712-zygisk-release.zip", 120 | "license": "GPL-3.0", 121 | "changelog": "changelog.md" 122 | } 123 | ``` 124 | 125 | ## Data structure 126 | ### modules.json (v1) 127 | ```json 128 | { 129 | "name": "{name}", 130 | "metadata": { 131 | "version": 1, 132 | "timestamp": 1692439764.10608 133 | }, 134 | "modules": [ 135 | { 136 | "id": "zygisk_lsposed", 137 | "name": "Zygisk - LSPosed", 138 | "version": "v1.8.6 (6712)", 139 | "versionCode": 6712, 140 | "author": "LSPosed Developers", 141 | "description": "Another enhanced implementation of Xposed Framework. Supports Android 8.1 ~ 13. Requires Magisk 24.0+ and Zygisk enabled.", 142 | "track": { 143 | "type": "ONLINE_JSON", 144 | "added": 1679025505.129431, 145 | "license": "GPL-3.0", 146 | "homepage": "https://lsposed.org/", 147 | "source": "https://github.com/LSPosed/LSPosed.git", 148 | "support": "https://github.com/LSPosed/LSPosed/issues", 149 | "donate": "" 150 | }, 151 | "versions": [ 152 | { 153 | "timestamp": 1673882223.0, 154 | "version": "v1.8.6 (6712)", 155 | "versionCode": 6712, 156 | "zipUrl": "{base_url}modules/zygisk_lsposed/v1.8.6_(6712)_6712.zip", 157 | "changelog": "{base_url}modules/zygisk_lsposed/v1.8.6_(6712)_6712.md" 158 | } 159 | ] 160 | } 161 | ] 162 | } 163 | ``` 164 | 165 | ### modules.json (v0) 166 | ```json 167 | { 168 | "name": "{name}", 169 | "timestamp": 1692439602.46997, 170 | "modules": [ 171 | { 172 | "id": "zygisk_lsposed", 173 | "name": "Zygisk - LSPosed", 174 | "version": "v1.8.6 (6712)", 175 | "versionCode": 6712, 176 | "author": "LSPosed Developers", 177 | "description": "Another enhanced implementation of Xposed Framework. Supports Android 8.1 ~ 13. Requires Magisk 24.0+ and Zygisk enabled.", 178 | "license": "GPL-3.0", 179 | "states": { 180 | "zipUrl": "{base_url}modules/zygisk_lsposed/v1.8.6_(6712)_6712.zip", 181 | "changelog": "{base_url}modules/zygisk_lsposed/v1.8.6_(6712)_6712.md" 182 | } 183 | } 184 | ] 185 | } 186 | ``` 187 | 188 | ### update.json (internal) 189 | ```json 190 | { 191 | "id": "zygisk_lsposed", 192 | "timestamp": 1673882223.0, 193 | "versions": [ 194 | { 195 | "timestamp": 1673882223.0, 196 | "version": "v1.8.6 (6712)", 197 | "versionCode": 6712, 198 | "zipUrl": "{base_url}modules/zygisk_lsposed/v1.8.6_(6712)_6712.zip", 199 | "changelog": "{base_url}modules/zygisk_lsposed/v1.8.6_(6712)_6712.md" 200 | } 201 | ] 202 | } 203 | ``` 204 | 205 | ### track.json (internal) 206 | ```json 207 | { 208 | "id": "zygisk_lsposed", 209 | "update_to": "https://lsposed.github.io/LSPosed/release/zygisk.json", 210 | "license": "GPL-3.0", 211 | "homepage": "https://lsposed.org/", 212 | "source": "https://github.com/LSPosed/LSPosed.git", 213 | "support": "https://github.com/LSPosed/LSPosed/issues", 214 | "added": 1679025505.129431, 215 | "last_update": 1673882223.0, 216 | "versions": 1 217 | } 218 | ``` 219 | -------------------------------------------------------------------------------- /sync/core/Check.py: -------------------------------------------------------------------------------- 1 | import shutil 2 | 3 | from .Config import Config 4 | from .Index import Index 5 | from .Pull import Pull 6 | from ..model import TrackJson, UpdateJson, VersionItem 7 | from ..track import LocalTracks 8 | from ..utils import Log 9 | 10 | 11 | class Check: 12 | def __init__(self, root_folder, config): 13 | self._log = Log("Check", enable_log=config.enable_log, log_dir=config.log_dir) 14 | 15 | self._local_folder = Config.get_local_folder(root_folder) 16 | self._modules_folder = Config.get_modules_folder(root_folder) 17 | self._tracks = LocalTracks(self._modules_folder, config) 18 | self._config = config 19 | 20 | def _get_file_url(self, module_id, file): 21 | func = getattr(Pull, "_get_file_url") 22 | return func(self, module_id, file) 23 | 24 | def _get_tracks(self, module_ids, new): 25 | if new or self._tracks.size == 0: 26 | return self._tracks.get_tracks(module_ids) 27 | 28 | return self._tracks.tracks 29 | 30 | def _check_folder(self, track, target_id): 31 | if track.id == target_id: 32 | return True 33 | 34 | msg = f"id is not same as in module.prop ({target_id})" 35 | self._log.d(f"_check_folder: [{track.id}] -> {msg}") 36 | 37 | old_module_folder = self._modules_folder.joinpath(track.id) 38 | new_module_folder = self._modules_folder.joinpath(target_id) 39 | 40 | if new_module_folder.exists(): 41 | new_module_folder = self._local_folder.joinpath(track.id) 42 | msg = f"{target_id} already exists, move the old to {new_module_folder.as_posix()}" 43 | self._log.w(f"_check_folder: [{track.id}] -> {msg}") 44 | shutil.move(old_module_folder, new_module_folder) 45 | 46 | return True 47 | 48 | old_module_folder.rename(new_module_folder) 49 | track.update(id=target_id) 50 | 51 | return False 52 | 53 | def _get_new_version_item(self, track, item): 54 | module_folder = self._modules_folder.joinpath(track.id) 55 | 56 | zipfile_name = item.zipfile_name 57 | zipfile = module_folder.joinpath(zipfile_name) 58 | if not zipfile.exists(): 59 | msg = f"{zipfile_name} does not exist, it will be removed from {UpdateJson.filename()}" 60 | self._log.w(f"_get_new_version_item: [{track.id}] -> {msg}") 61 | return None 62 | 63 | new_zip_url = self._get_file_url(track.id, zipfile) 64 | 65 | changelog = module_folder.joinpath(item.changelog_filename) 66 | new_changelog_url = "" 67 | if changelog.exists() and changelog.is_file(): 68 | new_changelog_url = self._get_file_url(track.id, changelog) 69 | 70 | return VersionItem( 71 | timestamp=item.timestamp, 72 | version=item.version, 73 | versionCode=item.versionCode, 74 | zipUrl=new_zip_url, 75 | changelog=new_changelog_url 76 | ) 77 | 78 | def _check_update_json(self, track, update_json, check_id): 79 | new_update_json = UpdateJson( 80 | id=track.id, 81 | timestamp=update_json.timestamp, 82 | versions=list() 83 | ) 84 | 85 | for item in update_json.versions: 86 | if check_id and item.id == track.id: 87 | continue 88 | elif not check_id and item.zipUrl.startswith(self._config.base_url): 89 | continue 90 | 91 | new_item = self._get_new_version_item(track, item) 92 | if new_item is None: 93 | continue 94 | 95 | new_update_json.versions.append(new_item) 96 | 97 | if len(new_update_json.versions) != 0: 98 | update_json.clear() 99 | update_json.update(new_update_json) 100 | return False 101 | 102 | return True 103 | 104 | def get_online_module(self, module_id, zip_file): 105 | func = getattr(Index, "get_online_module") 106 | return func(self, module_id, zip_file) 107 | 108 | def url(self, module_ids=None, new=False): 109 | for track in self._get_tracks(module_ids, new): 110 | module_folder = self._modules_folder.joinpath(track.id) 111 | update_json_file = module_folder.joinpath(UpdateJson.filename()) 112 | if not update_json_file.exists(): 113 | continue 114 | 115 | update_json = UpdateJson.load(update_json_file) 116 | 117 | if not self._check_update_json(track, update_json, False): 118 | self._log.i(f"url: [{track.id}] -> {UpdateJson.filename()} has been updated") 119 | update_json.write(update_json_file) 120 | 121 | def ids(self, module_ids=None, new=False): 122 | for track in self._get_tracks(module_ids, new): 123 | old_id = track.id 124 | module_folder = self._modules_folder.joinpath(old_id) 125 | 126 | zip_files = sorted( 127 | module_folder.glob("*.zip"), 128 | key=lambda f: f.stat().st_mtime, 129 | reverse=True 130 | ) 131 | 132 | if len(zip_files) == 0: 133 | continue 134 | 135 | latest_zip = zip_files[0] 136 | online_module = self.get_online_module(track.id, latest_zip) 137 | if online_module is None: 138 | continue 139 | 140 | if not self._check_folder(track, online_module.id): 141 | self._log.i(f"ids: [{old_id}] -> track has been migrated to {track.id}") 142 | module_folder = self._modules_folder.joinpath(track.id) 143 | track_json_file = module_folder.joinpath(TrackJson.filename()) 144 | track.write(track_json_file) 145 | 146 | update_json_file = module_folder.joinpath(UpdateJson.filename()) 147 | if not update_json_file.exists(): 148 | continue 149 | 150 | update_json = UpdateJson.load(update_json_file) 151 | if not self._check_update_json(track, update_json, True): 152 | self._log.i(f"ids: [{track.id}] -> {UpdateJson.filename()} has been updated") 153 | update_json.write(update_json_file) 154 | 155 | def old(self, module_ids=None, new=False): 156 | for track in self._get_tracks(module_ids, new): 157 | module_folder = self._modules_folder.joinpath(track.id) 158 | update_json_file = module_folder.joinpath(UpdateJson.filename()) 159 | if not update_json_file.exists(): 160 | continue 161 | 162 | update_json = UpdateJson.load(update_json_file) 163 | 164 | max_num = self._config.max_num 165 | if track.max_num is not None: 166 | max_num = track.max_num 167 | 168 | if len(update_json.versions) <= max_num: 169 | continue 170 | 171 | old_versions = update_json.versions[:-max_num] 172 | for old_item in old_versions: 173 | update_json.versions.remove(old_item) 174 | zipfile = module_folder.joinpath(old_item.zipfile_name) 175 | changelog = module_folder.joinpath(old_item.changelog_filename) 176 | 177 | for path in [zipfile, changelog]: 178 | if not (path.exists() and path.is_file()): 179 | continue 180 | 181 | self._log.d(f"old: [{track.id}] -> remove {path.name}") 182 | path.unlink() 183 | 184 | self._log.i(f"old: [{track.id}] -> {UpdateJson.filename()} has been updated") 185 | update_json.write(update_json_file) 186 | 187 | self._log.i(f"old: [{track.id}] -> {TrackJson.filename()} has been updated") 188 | track_json_file = module_folder.joinpath(TrackJson.filename()) 189 | track.versions = len(update_json.versions) 190 | track.write(track_json_file) 191 | -------------------------------------------------------------------------------- /sync/core/Pull.py: -------------------------------------------------------------------------------- 1 | import shutil 2 | 3 | from .Config import Config 4 | from ..error import Result 5 | from ..model import ( 6 | LocalModule, 7 | AttrDict, 8 | MagiskUpdateJson, 9 | OnlineModule, 10 | TrackType, 11 | UpdateJson 12 | ) 13 | from ..utils import ( 14 | Log, 15 | HttpUtils, 16 | GitUtils, 17 | StrUtils 18 | ) 19 | 20 | 21 | class Pull: 22 | _max_size = 50 23 | 24 | def __init__(self, root_folder, config): 25 | self._log = Log("Pull", enable_log=config.enable_log, log_dir=config.log_dir) 26 | 27 | self._local_folder = Config.get_local_folder(root_folder) 28 | self._modules_folder = Config.get_modules_folder(root_folder) 29 | self._config = config 30 | 31 | @staticmethod 32 | def _copy_file(old, new, delete_old): 33 | shutil.copy(old, new) 34 | if delete_old: 35 | old.unlink() 36 | 37 | @staticmethod 38 | @Result.catching() 39 | def _download(url, out): 40 | return HttpUtils.download(url, out) 41 | 42 | def _check_changelog(self, module_id, file): 43 | text = file.read_text() 44 | if StrUtils.is_html(text): 45 | self._log.w(f"_check_changelog: [{module_id}] -> unsupported type (html text)") 46 | return False 47 | else: 48 | return True 49 | 50 | def _check_version_code(self, module_id, version_code): 51 | module_folder = self._modules_folder.joinpath(module_id) 52 | json_file = module_folder.joinpath(UpdateJson.filename()) 53 | 54 | if not json_file.exists(): 55 | return True 56 | 57 | update_json = UpdateJson.load(json_file) 58 | if len(update_json.versions) != 0 and version_code > update_json.versions[-1].versionCode: 59 | return True 60 | 61 | self._log.i(f"_check_version_code: [{module_id}] -> already the latest version") 62 | return False 63 | 64 | def _get_file_url(self, module_id, file): 65 | module_folder = self._modules_folder.joinpath(module_id) 66 | url = f"{self._config.base_url}{self._modules_folder.name}/{module_id}/{file.name}" 67 | 68 | if not (file.parent == module_folder and file.exists()): 69 | raise FileNotFoundError(f"{file} is not in {module_folder}") 70 | else: 71 | return url 72 | 73 | def _get_changelog_common(self, module_id, changelog): 74 | if changelog is None: 75 | return None 76 | elif isinstance(changelog, str) and changelog == "": 77 | return None 78 | 79 | if StrUtils.is_url(changelog): 80 | if StrUtils.is_blob_url(changelog): 81 | msg = f"'{changelog}' is not unsupported type, please use 'https://raw.githubusercontent.com'" 82 | self._log.w(f"_get_changelog_common: [{module_id}] -> {msg}") 83 | return None 84 | 85 | changelog_file = self._modules_folder.joinpath(module_id, f"{module_id}.md") 86 | result = self._download(changelog, changelog_file) 87 | if result.is_failure: 88 | msg = Log.get_msg(result.error) 89 | self._log.e(f"_get_changelog_common: [{module_id}] -> {msg}") 90 | changelog_file = None 91 | 92 | else: 93 | changelog_file = self._modules_folder.joinpath(module_id, f"{module_id}.md") 94 | changelog_file.write_text(changelog) 95 | 96 | if changelog_file is not None: 97 | if not self._check_changelog(module_id, changelog_file): 98 | changelog_file.unlink() 99 | changelog_file = None 100 | 101 | return changelog_file 102 | 103 | def _from_zip_common(self, module_id, zip_file, changelog_file, *, delete_tmp): 104 | module_folder = self._modules_folder.joinpath(module_id) 105 | 106 | def remove_file(): 107 | if delete_tmp: 108 | zip_file.unlink() 109 | if delete_tmp and changelog_file is not None: 110 | changelog_file.unlink() 111 | 112 | zip_file_size = zip_file.stat().st_size / (1024 ** 2) 113 | if zip_file_size > self._max_size: 114 | new_module_folder = self._local_folder.joinpath(module_id) 115 | msg = f"zip file is oversize ({self._max_size} MB), move this module to {new_module_folder}" 116 | self._log.w(f"_from_zip_common: [{module_id}] -> {msg}") 117 | shutil.rmtree(new_module_folder, ignore_errors=True) 118 | shutil.move(module_folder, new_module_folder) 119 | 120 | return None 121 | 122 | @Result.catching() 123 | def get_online_module(): 124 | local_module = LocalModule.load(zip_file) 125 | return OnlineModule.from_dict(local_module) 126 | 127 | result = get_online_module() 128 | if result.is_failure: 129 | msg = Log.get_msg(result.error) 130 | self._log.e(f"_from_zip_common: [{module_id}] -> {msg}") 131 | remove_file() 132 | return None 133 | else: 134 | online_module: OnlineModule = result.value 135 | 136 | target_zip_file = module_folder.joinpath(online_module.zipfile_name) 137 | if self._check_version_code(module_id, online_module.versionCode): 138 | self._copy_file(zip_file, target_zip_file, delete_tmp) 139 | else: 140 | remove_file() 141 | return None 142 | 143 | target_changelog_file = module_folder.joinpath(online_module.changelog_filename) 144 | changelog_url = "" 145 | if changelog_file is not None: 146 | self._copy_file(changelog_file, target_changelog_file, delete_tmp) 147 | changelog_url = self._get_file_url(module_id, target_changelog_file) 148 | 149 | # For OnlineModule.to_VersionItem 150 | online_module.latest = AttrDict( 151 | zipUrl=self._get_file_url(module_id, target_zip_file), 152 | changelog=changelog_url 153 | ) 154 | 155 | return online_module 156 | 157 | def from_json(self, track, *, local): 158 | if local: 159 | update_to = self._local_folder.joinpath(track.update_to) 160 | else: 161 | update_to = track.update_to 162 | 163 | @Result.catching() 164 | def load_json(): 165 | return MagiskUpdateJson.load(update_to) 166 | 167 | result = load_json() 168 | if result.is_failure: 169 | msg = Log.get_msg(result.error) 170 | self._log.e(f"from_json: [{track.id}] -> {msg}") 171 | return None, 0.0 172 | else: 173 | update_json: MagiskUpdateJson = result.value 174 | 175 | if not self._check_version_code(track.id, update_json.versionCode): 176 | return None, 0.0 177 | 178 | zip_file = self._modules_folder.joinpath(track.id, f"{track.id}.zip") 179 | 180 | result = self._download(update_json.zipUrl, zip_file) 181 | if result.is_failure: 182 | msg = Log.get_msg(result.error) 183 | self._log.e(f"from_json: [{track.id}] -> {msg}") 184 | return None, 0.0 185 | else: 186 | last_modified = result.value 187 | 188 | changelog = self._get_changelog_common(track.id, update_json.changelog) 189 | online_module = self._from_zip_common(track.id, zip_file, changelog, delete_tmp=True) 190 | return online_module, last_modified 191 | 192 | def from_url(self, track): 193 | zip_file = self._modules_folder.joinpath(track.id, f"{track.id}.zip") 194 | 195 | result = self._download(track.update_to, zip_file) 196 | if result.is_failure: 197 | msg = Log.get_msg(result.error) 198 | self._log.e(f"from_url: [{track.id}] -> {msg}") 199 | return None, 0.0 200 | else: 201 | last_modified = result.value 202 | 203 | changelog = self._get_changelog_common(track.id, track.changelog) 204 | online_module = self._from_zip_common(track.id, zip_file, changelog, delete_tmp=True) 205 | return online_module, last_modified 206 | 207 | def from_git(self, track): 208 | zip_file = self._modules_folder.joinpath(track.id, f"{track.id}.zip") 209 | 210 | @Result.catching() 211 | def git_clone(): 212 | return GitUtils.clone_and_zip(track.update_to, zip_file) 213 | 214 | result = git_clone() 215 | if result.is_failure: 216 | msg = Log.get_msg(result.error) 217 | self._log.e(f"from_git: [{track.id}] -> {msg}") 218 | return None, 0.0 219 | else: 220 | last_committed = result.value 221 | 222 | changelog = self._get_changelog_common(track.id, track.changelog) 223 | online_module = self._from_zip_common(track.id, zip_file, changelog, delete_tmp=True) 224 | return online_module, last_committed 225 | 226 | def from_zip(self, track): 227 | zip_file = self._local_folder.joinpath(track.update_to) 228 | changelog = self._local_folder.joinpath(track.changelog) 229 | last_modified = zip_file.stat().st_mtime 230 | 231 | if not zip_file.exists(): 232 | msg = f"{track.update_to} is not in {self._local_folder}" 233 | self._log.i(f"from_zip: [{track.id}] -> {msg}") 234 | return None, 0.0 235 | 236 | if not changelog.exists(): 237 | changelog = None 238 | 239 | online_module = self._from_zip_common(track.id, zip_file, changelog, delete_tmp=False) 240 | return online_module, last_modified 241 | 242 | def from_track(self, track): 243 | self._log.d(f"from_track: [{track.id}] -> type: {track.type.name}") 244 | 245 | if track.type == TrackType.ONLINE_JSON: 246 | return self.from_json(track, local=False) 247 | elif track.type == TrackType.ONLINE_ZIP: 248 | return self.from_url(track) 249 | elif track.type == TrackType.GIT: 250 | return self.from_git(track) 251 | elif track.type == TrackType.LOCAL_JSON: 252 | return self.from_json(track, local=True) 253 | elif track.type == TrackType.LOCAL_ZIP: 254 | return self.from_zip(track) 255 | 256 | self._log.e(f"from_track: [{track.id}] -> unsupported type [{track.update_to}]") 257 | return None, 0.0 258 | 259 | @classmethod 260 | def set_max_size(cls, value): 261 | cls._max_size = value 262 | -------------------------------------------------------------------------------- /sync/cli/Main.py: -------------------------------------------------------------------------------- 1 | import json 2 | import logging 3 | import os 4 | import sys 5 | from argparse import Namespace 6 | from pathlib import Path 7 | from typing import Sequence, Type, Tuple 8 | 9 | from dateutil.parser import parse 10 | 11 | from .Parameters import Parameters 12 | from ..core import ( 13 | Check, 14 | Config, 15 | Index, 16 | Migrate, 17 | Pull, 18 | Sync 19 | ) 20 | from ..model import TrackJson, JsonIO, ConfigJson, AttrDict 21 | from ..track import LocalTracks, GithubTracks 22 | from ..utils import Log 23 | 24 | 25 | class SafeArgs(Namespace): 26 | def __init__(self, args: Namespace): 27 | super().__init__(**args.__dict__) 28 | 29 | def __getattr__(self, item): 30 | if item not in self.__dict__: 31 | return None 32 | 33 | return self.__dict__[item] 34 | 35 | 36 | class Main: 37 | _args: SafeArgs 38 | CODE_FAILURE = 1 39 | CODE_SUCCESS = 0 40 | 41 | @classmethod 42 | def set_default_args(cls, **kwargs): 43 | root_folder = kwargs.get("root_folder", os.getcwd()) 44 | root_folder = Path(root_folder).resolve() 45 | Parameters.set_root_folder(root_folder) 46 | 47 | github_token = kwargs.get("github_token") 48 | Parameters.set_github_token(github_token) 49 | 50 | @classmethod 51 | def exec(cls) -> int: 52 | parser = Parameters.generate_parser() 53 | cls._args = SafeArgs(parser.parse_args()) 54 | 55 | code = cls._check_args() 56 | if code == cls.CODE_FAILURE: 57 | if cls._args.cmd is None: 58 | parser.print_help() 59 | else: 60 | Parameters.print_cmd_help(cls._args.cmd) 61 | 62 | return code 63 | 64 | @classmethod 65 | def _check_args(cls) -> int: 66 | if cls._args.cmd is None: 67 | return cls.CODE_FAILURE 68 | elif cls._args.cmd == Parameters.CONFIG: 69 | return cls.config() 70 | elif cls._args.cmd == Parameters.TRACK: 71 | return cls.track() 72 | elif cls._args.cmd == Parameters.GITHUB: 73 | return cls.github() 74 | elif cls._args.cmd == Parameters.SYNC: 75 | return cls.sync() 76 | elif cls._args.cmd == Parameters.INDEX: 77 | return cls.index() 78 | elif cls._args.cmd == Parameters.CHECK: 79 | return cls.check() 80 | 81 | @classmethod 82 | def config(cls) -> int: 83 | root_folder = Path(cls._args.root_folder).resolve() 84 | json_folder = Config.get_json_folder(root_folder) 85 | json_file = json_folder.joinpath(Config.filename()) 86 | 87 | if cls._args.migrate: 88 | migrate = Migrate(root_folder) 89 | migrate.config() 90 | return cls.CODE_SUCCESS 91 | 92 | if cls._args.config_values is not None: 93 | _dict, _error = json_parse(cls._args.config_values, ConfigJson) 94 | if len(_error) != 0: 95 | error = json.dumps(obj=_error, indent=2) 96 | print_error(error) 97 | 98 | else: 99 | if json_file.exists(): 100 | config = ConfigJson.load(json_file) 101 | else: 102 | config = ConfigJson() 103 | 104 | config.update(_dict) 105 | config.write(json_file) 106 | 107 | elif cls._args.stdin: 108 | config_dict = json.load(fp=sys.stdin) 109 | ConfigJson.write(config_dict, json_file) 110 | 111 | elif cls._args.json and json_file.exists(): 112 | config_dict = JsonIO.load(json_file) 113 | print_json(config_dict) 114 | 115 | elif cls._args.keys: 116 | fields = ConfigJson.expected_fields(False) 117 | print_json(fields) 118 | 119 | else: 120 | return cls.CODE_FAILURE 121 | 122 | return cls.CODE_SUCCESS 123 | 124 | @classmethod 125 | def track(cls) -> int: 126 | root_folder = Path(cls._args.root_folder).resolve() 127 | modules_folder = Config.get_modules_folder(root_folder) 128 | Log.set_enable_stdout(False) 129 | 130 | if cls._args.migrate: 131 | migrate = Migrate(root_folder) 132 | migrate.track() 133 | return cls.CODE_SUCCESS 134 | 135 | if cls._args.list: 136 | config = Config(root_folder) 137 | tracks = LocalTracks(modules_folder=modules_folder, config=config) 138 | markdown_text = tracks.get_tracks_table() 139 | print(markdown_text) 140 | 141 | elif cls._args.track_values is not None: 142 | _dict, _error = json_parse(cls._args.track_values, TrackJson) 143 | if len(_error) != 0: 144 | error = json.dumps(obj=_error, indent=2) 145 | print_error(error) 146 | 147 | else: 148 | track = TrackJson(_dict) 149 | LocalTracks.add_track( 150 | track=track, 151 | modules_folder=modules_folder, 152 | cover=True 153 | ) 154 | 155 | elif cls._args.remove_module_ids is not None: 156 | for module_id in cls._args.remove_module_ids: 157 | LocalTracks.del_track( 158 | module_id=module_id, 159 | modules_folder=modules_folder 160 | ) 161 | 162 | elif cls._args.stdin: 163 | track = TrackJson(json.load(fp=sys.stdin)) 164 | LocalTracks.add_track( 165 | track=track, 166 | modules_folder=modules_folder, 167 | cover=True 168 | ) 169 | 170 | elif cls._args.keys: 171 | keys = TrackJson.expected_fields(False) 172 | print_json(keys) 173 | 174 | elif cls._args.modify_module_id is not None: 175 | module_folder = modules_folder.joinpath(cls._args.modify_module_id) 176 | json_file = module_folder.joinpath(TrackJson.filename()) 177 | 178 | if not json_file.exists(): 179 | print_error(f"There is no track for this id ({cls._args.modify_module_id})") 180 | return cls.CODE_SUCCESS 181 | 182 | if cls._args.update_track_values is not None: 183 | _dict, _error = json_parse(cls._args.update_track_values, TrackJson) 184 | if len(_error) != 0: 185 | error = json.dumps(obj=_error, indent=2) 186 | print_error(error) 187 | 188 | else: 189 | track = TrackJson(_dict) 190 | track.update(id=cls._args.modify_module_id) 191 | LocalTracks.update_track( 192 | track=track, 193 | modules_folder=modules_folder 194 | ) 195 | 196 | elif cls._args.remove_key_list is not None and json_file.exists(): 197 | track = TrackJson.load(json_file) 198 | for key in cls._args.remove_key_list: 199 | track.pop(key, None) 200 | track.write(json_file) 201 | 202 | elif cls._args.json and json_file.exists(): 203 | track = TrackJson.load(json_file) 204 | print_json(track) 205 | 206 | else: 207 | return cls.CODE_FAILURE 208 | 209 | else: 210 | return cls.CODE_FAILURE 211 | 212 | return cls.CODE_SUCCESS 213 | 214 | @classmethod 215 | def github(cls) -> int: 216 | root_folder = Path(cls._args.root_folder).resolve() 217 | modules_folder = Config.get_modules_folder(root_folder) 218 | Log.set_enable_stdout(not cls._args.quiet) 219 | Pull.set_max_size(cls._args.max_size) 220 | 221 | config = Config(root_folder) 222 | 223 | tracks = GithubTracks( 224 | modules_folder=modules_folder, 225 | config=config, 226 | api_token=cls._args.token, 227 | after_date=parse(cls._args.after_date).date() 228 | ) 229 | 230 | tracks.get_tracks( 231 | user_name=cls._args.user_name, 232 | repo_names=cls._args.repo_names, 233 | single=cls._args.single, 234 | cover=cls._args.cover, 235 | use_ssh=cls._args.ssh 236 | ) 237 | 238 | if cls._args.clear: 239 | tracks.clear_tracks() 240 | 241 | return cls.CODE_SUCCESS 242 | 243 | @classmethod 244 | def sync(cls) -> int: 245 | root_folder = Path(cls._args.root_folder).resolve() 246 | Log.set_enable_stdout(not cls._args.quiet) 247 | Pull.set_max_size(cls._args.max_size) 248 | 249 | config = Config(root_folder) 250 | 251 | sync = Sync(root_folder=root_folder, config=config) 252 | sync.create_local_tracks() 253 | sync.update( 254 | module_ids=cls._args.module_ids, 255 | force=cls._args.force, 256 | single=cls._args.single 257 | ) 258 | 259 | if cls._args.diff_file: 260 | markdown_text = sync.get_versions_diff() 261 | if markdown_text is not None: 262 | if isinstance(cls._args.diff_file, str): 263 | diff_file = Path(cls._args.diff_file) 264 | diff_file.write_text(markdown_text) 265 | 266 | else: 267 | print(markdown_text) 268 | 269 | if cls._args.push: 270 | index = Index(root_folder=root_folder, config=config) 271 | index(version=cls._args.index_version, to_file=True) 272 | index.push_by_git(cls._args.git_branch) 273 | 274 | return cls.CODE_SUCCESS 275 | 276 | @classmethod 277 | def index(cls) -> int: 278 | root_folder = Path(cls._args.root_folder).resolve() 279 | Log.set_enable_stdout(False) 280 | 281 | config = Config(root_folder) 282 | 283 | index = Index(root_folder=root_folder, config=config) 284 | 285 | if cls._args.list: 286 | markdown_text = index.get_versions_table() 287 | print(markdown_text) 288 | 289 | else: 290 | index(version=cls._args.index_version, to_file=not cls._args.json) 291 | 292 | if cls._args.json: 293 | print_json(index.modules_json) 294 | 295 | elif cls._args.push: 296 | index.push_by_git(cls._args.git_branch) 297 | 298 | return cls.CODE_SUCCESS 299 | 300 | @classmethod 301 | def check(cls) -> int: 302 | root_folder = Path(cls._args.root_folder).resolve() 303 | Log.set_log_level(logging.INFO) 304 | 305 | if not ( 306 | cls._args.check_id 307 | or cls._args.check_url 308 | or cls._args.remove_old 309 | ): 310 | return cls.CODE_FAILURE 311 | 312 | config = Config(root_folder) 313 | check = Check(root_folder=root_folder, config=config) 314 | 315 | if cls._args.check_id: 316 | check.ids(module_ids=cls._args.module_ids) 317 | 318 | if cls._args.check_url: 319 | check.url(module_ids=cls._args.module_ids) 320 | 321 | if cls._args.remove_old: 322 | check.old(module_ids=cls._args.module_ids) 323 | 324 | return cls.CODE_SUCCESS 325 | 326 | 327 | def print_error(msg): 328 | print(f"Error: {msg}") 329 | 330 | 331 | def print_json(obj: dict): 332 | string = json.dumps(obj, indent=2) 333 | print(string) 334 | 335 | 336 | def json_parse(texts: Sequence[str], __cls: Type) -> Tuple[AttrDict, AttrDict]: 337 | _error = AttrDict() 338 | _dict = AttrDict() 339 | 340 | _member = AttrDict() 341 | for p in __cls.__mro__: 342 | if hasattr(p, "__annotations__"): 343 | _member.update(p.__annotations__) 344 | 345 | for text in texts: 346 | values = text.split("=", maxsplit=1) 347 | if len(values) != 2: 348 | continue 349 | 350 | key, value = values[0], values[1] 351 | 352 | _type = _member.get(key) 353 | if _type is None: 354 | continue 355 | 356 | try: 357 | if _type is bool: 358 | _dict[key] = value.lower() == "true" 359 | else: 360 | _dict[key] = _type(value) 361 | except BaseException as err: 362 | _error[key] = str(err) 363 | 364 | return _dict, _error 365 | -------------------------------------------------------------------------------- /sync/cli/Parameters.py: -------------------------------------------------------------------------------- 1 | # noinspection PyUnresolvedReferences,PyProtectedMember 2 | from argparse import ( 3 | ArgumentParser as ArgumentParserBase, 4 | RawDescriptionHelpFormatter, 5 | Action, 6 | _HelpAction 7 | ) 8 | from pathlib import Path 9 | from typing import Optional 10 | 11 | from ..__version__ import get_version, get_version_code 12 | from ..core import Index 13 | from ..model import UpdateJson, ModulesJson 14 | from ..utils import GitUtils 15 | 16 | 17 | class ArgumentParser(ArgumentParserBase): 18 | def __init__(self, *args, **kwargs): 19 | if not kwargs.get("formatter_class"): 20 | kwargs["formatter_class"] = RawDescriptionHelpFormatter 21 | if "add_help" not in kwargs: 22 | add_custom_help = True 23 | kwargs["add_help"] = False 24 | else: 25 | add_custom_help = False 26 | super().__init__(*args, **kwargs) 27 | 28 | if add_custom_help: 29 | self.add_argument( 30 | "-h", 31 | "--help", 32 | action=_HelpAction, 33 | help="Show this help message and exit.", 34 | ) 35 | 36 | 37 | class BoolOrStrAction(Action): 38 | def __call__(self, parser, namespace, values, option_string=None): 39 | if values is None or values == "" or values.isspace(): 40 | values = True 41 | 42 | setattr(namespace, self.dest, values) 43 | 44 | 45 | class Parameters: 46 | _root_folder: Path 47 | _github_token: Optional[str] 48 | _choices: dict 49 | 50 | CONFIG = "config" 51 | TRACK = "track" 52 | GITHUB = "github" 53 | SYNC = "sync" 54 | INDEX = "index" 55 | CHECK = "check" 56 | 57 | @classmethod 58 | def set_root_folder(cls, root: Path): 59 | cls._root_folder = root 60 | 61 | @classmethod 62 | def set_github_token(cls, token: Optional[str]): 63 | cls._github_token = token 64 | 65 | @classmethod 66 | def print_cmd_help(cls, cmd: Optional[str]): 67 | cls._choices[cmd].print_help() 68 | 69 | @classmethod 70 | def generate_parser(cls): 71 | p = ArgumentParser( 72 | description="Magisk Modules Repo Util" 73 | ) 74 | p.add_argument( 75 | "-v", 76 | "--version", 77 | action="version", 78 | version=get_version(), 79 | help="Show util version and exit." 80 | ) 81 | p.add_argument( 82 | "-V", 83 | "--version-code", 84 | action="version", 85 | version=str(get_version_code()), 86 | help="Show util version code and exit." 87 | ) 88 | 89 | sub_parsers = p.add_subparsers( 90 | dest="cmd", 91 | metavar="command" 92 | ) 93 | 94 | cls.configure_parser_config(sub_parsers) 95 | cls.configure_parser_track(sub_parsers) 96 | cls.configure_parser_github(sub_parsers) 97 | cls.configure_parser_sync(sub_parsers) 98 | cls.configure_parser_index(sub_parsers) 99 | cls.configure_parser_check(sub_parsers) 100 | 101 | cls._choices = sub_parsers.choices 102 | 103 | return p 104 | 105 | @classmethod 106 | def configure_parser_config(cls, sub_parsers): 107 | p = sub_parsers.add_parser( 108 | cls.CONFIG, 109 | help="Modify config of repository." 110 | ) 111 | p.add_argument( 112 | "-w", 113 | "--write", 114 | dest="config_values", 115 | metavar="KEY=VALUE", 116 | nargs="+", 117 | default=None, 118 | help="Write values to config." 119 | ) 120 | p.add_argument( 121 | "--stdin", 122 | action="store_true", 123 | help="Write config piped through stdin." 124 | ) 125 | p.add_argument( 126 | "--json", 127 | action="store_true", 128 | help="Show config of repository." 129 | ) 130 | p.add_argument( 131 | "--keys", 132 | action="store_true", 133 | help="Show fields available in config." 134 | ) 135 | p.add_argument( 136 | "--migrate", 137 | action="store_true", 138 | help=f"Migrate config structure and content." 139 | ) 140 | 141 | cls.add_parser_env(p) 142 | 143 | @classmethod 144 | def configure_parser_track(cls, sub_parsers): 145 | p = sub_parsers.add_parser( 146 | cls.TRACK, 147 | help="Module tracks utility." 148 | ) 149 | p.add_argument( 150 | "-l", 151 | "--list", 152 | action="store_true", 153 | help="List tracks in repository." 154 | ) 155 | p.add_argument( 156 | "-a", 157 | "--add", 158 | dest="track_values", 159 | metavar="KEY=VALUE", 160 | nargs="+", 161 | default=None, 162 | help="Add a track to repository." 163 | ) 164 | p.add_argument( 165 | "-r", 166 | "--remove", 167 | dest="remove_module_ids", 168 | metavar="MODULE_ID", 169 | nargs="+", 170 | default=None, 171 | help="Remove tracks from repository." 172 | ) 173 | p.add_argument( 174 | "--stdin", 175 | action="store_true", 176 | help="Add track piped through stdin." 177 | ) 178 | p.add_argument( 179 | "--keys", 180 | action="store_true", 181 | help="Show fields available in track." 182 | ) 183 | p.add_argument( 184 | "--migrate", 185 | action="store_true", 186 | help=f"Migrate tracks structure and content." 187 | ) 188 | 189 | modify = p.add_argument_group("modify") 190 | modify.add_argument( 191 | "-i", 192 | dest="modify_module_id", 193 | metavar="MODULE_ID", 194 | type=str, 195 | default=None, 196 | help="Id of the module to modify." 197 | ) 198 | modify.add_argument( 199 | "-u", 200 | "--update", 201 | dest="update_track_values", 202 | metavar="KEY=VALUE", 203 | nargs="+", 204 | default=None, 205 | help="Update values to the track." 206 | ) 207 | modify.add_argument( 208 | "-d", 209 | "--remove-key", 210 | dest="remove_key_list", 211 | metavar="KEY", 212 | nargs="+", 213 | default=None, 214 | help="Remove keys (and all its values) in the track." 215 | ) 216 | modify.add_argument( 217 | "--json", 218 | action="store_true", 219 | help="Show the track of module." 220 | ) 221 | 222 | cls.add_parser_env(p) 223 | 224 | @classmethod 225 | def configure_parser_github(cls, sub_parsers): 226 | p = sub_parsers.add_parser( 227 | cls.GITHUB, 228 | help="Generate tracks from GitHub." 229 | ) 230 | p.add_argument( 231 | "-u", 232 | dest="user_name", 233 | metavar="USERNAME", 234 | type=str, 235 | required=True, 236 | help="User name or organization name on GitHub." 237 | ) 238 | p.add_argument( 239 | "-r", 240 | dest="repo_names", 241 | metavar="REPO_NAME", 242 | nargs="+", 243 | default=None, 244 | help="Names of repository, default is all." 245 | ) 246 | p.add_argument( 247 | "-d", 248 | "--date", 249 | dest="after_date", 250 | metavar="DATE", 251 | type=str, 252 | default="2016-09-08", 253 | help="Filter out outdated repositories by latest push date, default: {0}.".format("%(default)s") 254 | ) 255 | p.add_argument( 256 | "-S", 257 | "--ssh", 258 | action="store_true", 259 | help="Use SSH instead of HTTPS for git clone (deploy SSH key by yourself)." 260 | ) 261 | p.add_argument( 262 | "-C", 263 | "--cover", 264 | action="store_true", 265 | help="Overwrite fields of tracks (exclude 'added')." 266 | ) 267 | p.add_argument( 268 | "--clear", 269 | action="store_true", 270 | help="Remove tracks, exclude those in the current session." 271 | ) 272 | 273 | env = cls.add_parser_env(p, add_quiet=True) 274 | env.add_argument( 275 | "--single", 276 | action="store_true", 277 | help="Run in single-threaded mode." 278 | ) 279 | env.add_argument( 280 | "--token", 281 | metavar="GITHUB_TOKEN", 282 | type=str, 283 | default=cls._github_token, 284 | help="GitHub REST API Token for PyGitHub, same as 'export GITHUB_TOKEN=...'." 285 | ) 286 | 287 | @classmethod 288 | def configure_parser_sync(cls, sub_parsers): 289 | p = sub_parsers.add_parser( 290 | cls.SYNC, 291 | help="Sync modules in repository." 292 | ) 293 | p.add_argument( 294 | "-i", 295 | dest="module_ids", 296 | metavar="MODULE_ID", 297 | nargs="+", 298 | default=None, 299 | help="Ids of modules to update, default is all." 300 | ) 301 | p.add_argument( 302 | "-v", 303 | dest="index_version", 304 | metavar="VERSION", 305 | type=int, 306 | default=Index.latest_version, 307 | help="Version of the index file ({0}), default: {1}.".format(ModulesJson.filename(), "%(default)s") 308 | ) 309 | p.add_argument( 310 | "--diff", 311 | dest="diff_file", 312 | metavar="FILE", 313 | action=BoolOrStrAction, 314 | nargs="?", 315 | help="List versions diff of modules." 316 | ) 317 | p.add_argument( 318 | "--force", 319 | action="store_true", 320 | help="Remove all old versions of modules." 321 | ) 322 | cls.add_parser_git(p) 323 | env = cls.add_parser_env(p, add_quiet=True) 324 | env.add_argument( 325 | "--single", 326 | action="store_true", 327 | help="Run in single-threaded mode." 328 | ) 329 | 330 | @classmethod 331 | def configure_parser_index(cls, sub_parsers): 332 | p = sub_parsers.add_parser( 333 | cls.INDEX, 334 | help="Generate modules.json from local." 335 | ) 336 | p.add_argument( 337 | "-v", 338 | dest="index_version", 339 | metavar="VERSION", 340 | type=int, 341 | default=Index.latest_version, 342 | help="Version of the index file ({0}), default: {1}.".format(ModulesJson.filename(), "%(default)s") 343 | ) 344 | p.add_argument( 345 | "--json", 346 | action="store_true", 347 | help="Show modules.json." 348 | ) 349 | p.add_argument( 350 | "--list", 351 | action="store_true", 352 | help="List versions of modules." 353 | ) 354 | 355 | cls.add_parser_git(p, add_set_size=False) 356 | cls.add_parser_env(p) 357 | 358 | @classmethod 359 | def configure_parser_check(cls, sub_parsers): 360 | p = sub_parsers.add_parser( 361 | cls.CHECK, 362 | help="Content check and migrate." 363 | ) 364 | p.add_argument( 365 | "-i", 366 | dest="module_ids", 367 | metavar="MODULE_ID", 368 | nargs="+", 369 | default=None, 370 | help="Ids of modules to check, default is all." 371 | ) 372 | p.add_argument( 373 | "-I", 374 | "--id", 375 | dest="check_id", 376 | action="store_true", 377 | help="Check id of the module in all json." 378 | ) 379 | p.add_argument( 380 | "-U", 381 | "--url", 382 | dest="check_url", 383 | action="store_true", 384 | help=f"Check urls of files in {UpdateJson.filename()}." 385 | ) 386 | p.add_argument( 387 | "-O", 388 | "--old", 389 | dest="remove_old", 390 | action="store_true", 391 | help=f"Remove old versions by max_num." 392 | ) 393 | 394 | cls.add_parser_env(p) 395 | 396 | @classmethod 397 | def add_parser_env(cls, p, add_quiet=False): 398 | env = p.add_argument_group("env") 399 | env.add_argument( 400 | "-p", 401 | "--prefix", 402 | dest="root_folder", 403 | metavar="ROOT_FOLDER", 404 | type=str, 405 | default=cls._root_folder.as_posix(), 406 | help="Full path to repository location, current: {0}.".format("%(default)s") 407 | ) 408 | 409 | if add_quiet: 410 | env.add_argument( 411 | "-q", 412 | "--quiet", 413 | action="store_true", 414 | help="Show only error logs (piped through stderr)." 415 | ) 416 | 417 | return env 418 | 419 | @classmethod 420 | def add_parser_git(cls, p, add_set_size=True): 421 | git = p.add_argument_group("git") 422 | git.add_argument( 423 | "--push", 424 | action="store_true", 425 | help="Push to git repository." 426 | ) 427 | git.add_argument( 428 | "--branch", 429 | dest="git_branch", 430 | metavar="GIT_BRANCH", 431 | type=str, 432 | default=GitUtils.current_branch(cls._root_folder), 433 | help="Define branch to push, current: {0}.".format("%(default)s") 434 | ) 435 | 436 | if add_set_size: 437 | git.add_argument( 438 | "--size", 439 | dest="max_size", 440 | metavar="MAX_SIZE", 441 | type=float, 442 | default=50.0, 443 | help="Filter out oversize zip files, default: {0} MB.".format("%(default)s") 444 | ) 445 | 446 | return git 447 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------