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

Commit 55fac0a

Browse files
authored
Release v1.0.0
2 parents 071f296 + 0ca0d1f commit 55fac0a

File tree

11 files changed

+56
-60
lines changed

11 files changed

+56
-60
lines changed

sync/cli/Main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import json
22
import os
33
import sys
4-
from pathlib import Path
54
from datetime import datetime
5+
from pathlib import Path
66

77
from .Parameters import Parameters
88
from .SafeArgs import SafeArgs

sync/cli/Parameters.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from argparse import ArgumentParser as ArgumentParserBase
21
# noinspection PyUnresolvedReferences,PyProtectedMember
32
from argparse import (
43
Action,
@@ -7,6 +6,7 @@
76
_HelpAction,
87
_StoreAction
98
)
9+
from argparse import ArgumentParser as ArgumentParserBase
1010
from pathlib import Path
1111
from typing import Sequence, Optional
1212

sync/core/Pull.py

+35-29
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import shutil
33

44
from .Config import Config
5-
from ..model import TrackJson, LocalModule, AttrDict, MagiskUpdateJson, OnlineModule
5+
from ..model import LocalModule, AttrDict, MagiskUpdateJson, OnlineModule
66
from ..modifier import Result
7-
from ..utils import Log, HttpUtils, GitUtils
87
from ..track import LocalTracks
8+
from ..utils import Log, HttpUtils, GitUtils
99

1010

1111
class Pull:
@@ -36,6 +36,14 @@ def _copy_file(old, new, delete_old=True):
3636
def _safe_download(url, out):
3737
return HttpUtils.download(url, out)
3838

39+
def _check_changelog(self, module_id, file):
40+
text = file.read_text()
41+
if HttpUtils.is_html(text):
42+
self._log.w(f"_check_changelog: [{module_id}] -> unsupported changelog type [the content is html text]")
43+
return False
44+
else:
45+
return True
46+
3947
def _get_file_url(self, module_id, file):
4048
module_folder = self.modules_folder.joinpath(module_id)
4149
url = f"{self._config.repo_url}{self.modules_folder.name}/{module_id}/{file.name}"
@@ -49,36 +57,37 @@ def _get_changelog_common(self, module_id, changelog):
4957
if not isinstance(changelog, str) or changelog == "":
5058
return None
5159

52-
unsupported = False
53-
changelog_file = None
5460
if changelog.startswith("http"):
55-
if changelog.endswith("md"):
56-
changelog_file = self.modules_folder.joinpath(module_id, f"{module_id}.md")
57-
result = self._safe_download(changelog, changelog_file)
58-
if result.is_failure:
59-
msg = Log.get_msg(result.error)
60-
self._log.e(f"_get_changelog_common: [{module_id}] -> {msg}")
61-
changelog_file = None
62-
else:
63-
unsupported = True
61+
changelog_file = self.modules_folder.joinpath(module_id, f"{module_id}.md")
62+
result = self._safe_download(changelog, changelog_file)
63+
if result.is_failure:
64+
msg = Log.get_msg(result.error)
65+
self._log.e(f"_get_changelog_common: [{module_id}] -> {msg}")
66+
changelog_file = None
6467
else:
65-
if changelog.endswith("md") or changelog.endswith("log"):
66-
changelog_file = self._local_folder.joinpath(changelog)
67-
if not changelog_file.exists():
68-
msg = f"_get_changelog_common: [{module_id}] -> {changelog} is not in {self._local_folder}"
69-
self._log.i(msg)
70-
changelog_file = None
71-
else:
72-
unsupported = True
73-
74-
if unsupported:
75-
self._log.w(f"_get_changelog_common: [{module_id}] -> unsupported changelog type [{changelog}]")
68+
changelog_file = self._local_folder.joinpath(changelog)
69+
if not changelog_file.exists():
70+
msg = f"_get_changelog_common: [{module_id}] -> {changelog} is not in {self._local_folder}"
71+
self._log.i(msg)
72+
changelog_file = None
73+
74+
if changelog_file is not None:
75+
is_target_type = self._check_changelog(module_id, changelog_file)
76+
if not is_target_type:
77+
os.remove(changelog_file)
78+
changelog_file = None
7679

7780
return changelog_file
7881

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

85+
def remove_file():
86+
if delete_tmp:
87+
os.remove(zip_file)
88+
if delete_tmp and changelog_file is not None:
89+
os.remove(changelog_file)
90+
8291
zip_file_size = os.path.getsize(zip_file) / (1024 ** 2)
8392
if zip_file_size > self._max_size:
8493
module_folder.joinpath(LocalTracks.TAG_DISABLE).touch()
@@ -95,11 +104,9 @@ def get_online_module():
95104

96105
result = get_online_module()
97106
if result.is_failure:
98-
if delete_tmp:
99-
os.remove(zip_file)
100-
101107
msg = Log.get_msg(result.error)
102108
self._log.e(f"_from_zip_common: [{module_id}] -> {msg}")
109+
remove_file()
103110
return None
104111
else:
105112
online_module: OnlineModule = result.value
@@ -110,8 +117,7 @@ def get_online_module():
110117
if not target_zip_file.exists() and len(target_files) == 0:
111118
self._copy_file(zip_file, target_zip_file, delete_tmp)
112119
else:
113-
if delete_tmp:
114-
os.remove(zip_file)
120+
remove_file()
115121
return None
116122

117123
target_changelog_file = module_folder.joinpath(online_module.changelog_filename)

sync/core/Pull.pyi

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from pathlib import Path
22
from typing import Optional, Tuple
33

4-
from ..modifier import Result
54
from ..model import TrackJson, ConfigJson, OnlineModule
5+
from ..modifier import Result
66
from ..utils import Log
77

88

@@ -22,10 +22,11 @@ class Pull:
2222
@staticmethod
2323
@Result.catching()
2424
def _safe_download(url: str, out: Path) -> Result: ...
25+
def _check_changelog(self, module_id: str, file: Path) -> bool: ...
2526
def _get_file_url(self, module_id: str, file: Path) -> str: ...
2627
def _get_changelog_common(self, module_id: str, changelog: str) -> Optional[Path]: ...
2728
def _from_zip_common(
28-
self, module_id: str, zip_file: Path, changelog: Optional[Path], *, delete_tmp: bool = ...
29+
self, module_id: str, zip_file: Path, changelog_file: Optional[Path], *, delete_tmp: bool = ...
2930
) -> Optional[OnlineModule]: ...
3031
def from_json(self, track: TrackJson, *, local: bool) -> Tuple[Optional[OnlineModule], float]: ...
3132
def from_url(self, track: TrackJson) -> Tuple[Optional[OnlineModule], float]: ...

sync/core/Sync.py

+5-13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ..model import ModulesJson, UpdateJson, TrackJson, LocalModule, AttrDict
88
from ..track import BaseTracks, LocalTracks, GithubTracks
99
from ..utils import Log, GitUtils
10+
from ..__version__ import version, versionCode
1011

1112

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

18-
self._is_updatable = False
1919
self._is_full_update = True
2020
self._json_folder = Config.get_json_folder(root_folder)
2121
self._modules_folder = self._pull.modules_folder
@@ -31,10 +31,6 @@ def __init__(self, root_folder, config, tracks=None):
3131
def __del__(self):
3232
self._log.d("__del__")
3333

34-
def _set_updatable(self):
35-
if not self.is_updatable:
36-
self._is_updatable = True
37-
3834
def _check_ids(self, track, online_module):
3935
if track.id == online_module.id:
4036
return
@@ -163,7 +159,6 @@ def update_by_ids(self, module_ids, force, **kwargs):
163159
if online_module is None:
164160
continue
165161

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

169164
def update_all(self, force, **kwargs):
@@ -176,6 +171,10 @@ def create_modules_json(self, to_file):
176171
modules_json = ModulesJson(
177172
name=self._config.repo_name,
178173
timestamp=timestamp,
174+
metadata=AttrDict(
175+
version=version,
176+
versionCode=versionCode
177+
),
179178
modules=list()
180179
)
181180

@@ -215,9 +214,6 @@ def create_modules_json(self, to_file):
215214
return modules_json
216215

217216
def push_by_git(self, branch):
218-
if not self.is_updatable:
219-
return
220-
221217
json_file = self._json_folder.joinpath(ModulesJson.filename())
222218
if not json_file.exists():
223219
self._log.e(f"push_by_git: {json_file.name} is not in {self._json_folder}")
@@ -233,7 +229,3 @@ def push_by_git(self, branch):
233229
subprocess.run(["git", "add", "."], cwd=self._root_folder.as_posix())
234230
subprocess.run(["git", "commit", "-m", msg], cwd=self._root_folder.as_posix())
235231
subprocess.run(["git", "push", "-u", "origin", branch], cwd=self._root_folder.as_posix())
236-
237-
@property
238-
def is_updatable(self):
239-
return self._is_updatable

sync/core/Sync.pyi

-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ class Sync:
1212
_root_folder: Path
1313
_pull: Pull
1414

15-
_is_updatable: bool
1615
_is_full_update: bool
1716
_json_folder: Path
1817
_modules_folder: Path
1918
_config: ConfigJson
2019
_tracks: BaseTracks
2120

2221
def __init__(self, root_folder: Path, config: ConfigJson, tracks: Optional[BaseTracks] = ...): ...
23-
def _set_updatable(self): ...
2422
def _check_ids(self, track: TrackJson, online_module: OnlineModule): ...
2523
def _update_jsons(self, track: TrackJson, force: bool) -> Optional[OnlineModule]: ...
2624
def _check_tracks(self, obj: BaseTracks, cls: type): ...
@@ -31,5 +29,3 @@ class Sync:
3129
def update_all(self, force: bool, **kwargs): ...
3230
def create_modules_json(self, to_file: bool) -> ModulesJson: ...
3331
def push_by_git(self, branch: str): ...
34-
@property
35-
def is_updatable(self): ...

sync/modifier/Command.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import subprocess
22
from pathlib import Path
3-
from typing import Callable, Any, Optional
3+
from typing import Callable, Optional
4+
45
from .Result import Result
56

67

sync/track/LocalTracks.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def add_track(cls, track, modules_folder, cover=True):
7373
track.write(json_file)
7474
elif cover:
7575
old = TrackJson.load(json_file)
76-
track.added = old.added
77-
track.write(json_file)
76+
old.update(track)
77+
old.write(json_file)
7878

7979
@classmethod
8080
def del_track(cls, module_id, modules_folder):

sync/utils/GitUtils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import subprocess
44
from datetime import datetime
55
from pathlib import Path
6-
from typing import Optional
76
from subprocess import CalledProcessError
7+
from typing import Optional
88

99
from dateutil.parser import parse
1010
from requests import HTTPError

sync/utils/HttpUtils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def load_json(cls, url: str) -> Union[list, AttrDict]:
3131
return obj
3232

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

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

55-
if cls._is_html(response.text):
55+
if cls.is_html(response.text):
5656
msg = "404 not found"
5757
else:
5858
msg = response.text

sync/utils/Log.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import functools
2+
import logging
13
import os
24
import sys
3-
import logging
4-
import functools
5-
from typing import Optional
5+
from datetime import date
66
from glob import glob
77
from pathlib import Path
8-
from datetime import date
8+
from typing import Optional
99

1010
logger_initialized = {}
1111

0 commit comments

Comments
 (0)