2
2
import shutil
3
3
4
4
from .Config import Config
5
- from ..model import TrackJson , LocalModule , AttrDict , MagiskUpdateJson , OnlineModule
5
+ from ..model import LocalModule , AttrDict , MagiskUpdateJson , OnlineModule
6
6
from ..modifier import Result
7
- from ..utils import Log , HttpUtils , GitUtils
8
7
from ..track import LocalTracks
8
+ from ..utils import Log , HttpUtils , GitUtils
9
9
10
10
11
11
class Pull :
@@ -36,6 +36,14 @@ def _copy_file(old, new, delete_old=True):
36
36
def _safe_download (url , out ):
37
37
return HttpUtils .download (url , out )
38
38
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
+
39
47
def _get_file_url (self , module_id , file ):
40
48
module_folder = self .modules_folder .joinpath (module_id )
41
49
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):
49
57
if not isinstance (changelog , str ) or changelog == "" :
50
58
return None
51
59
52
- unsupported = False
53
- changelog_file = None
54
60
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
64
67
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
76
79
77
80
return changelog_file
78
81
79
82
def _from_zip_common (self , module_id , zip_file , changelog_file , * , delete_tmp = True ):
80
83
module_folder = self .modules_folder .joinpath (module_id )
81
84
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
+
82
91
zip_file_size = os .path .getsize (zip_file ) / (1024 ** 2 )
83
92
if zip_file_size > self ._max_size :
84
93
module_folder .joinpath (LocalTracks .TAG_DISABLE ).touch ()
@@ -95,11 +104,9 @@ def get_online_module():
95
104
96
105
result = get_online_module ()
97
106
if result .is_failure :
98
- if delete_tmp :
99
- os .remove (zip_file )
100
-
101
107
msg = Log .get_msg (result .error )
102
108
self ._log .e (f"_from_zip_common: [{ module_id } ] -> { msg } " )
109
+ remove_file ()
103
110
return None
104
111
else :
105
112
online_module : OnlineModule = result .value
@@ -110,8 +117,7 @@ def get_online_module():
110
117
if not target_zip_file .exists () and len (target_files ) == 0 :
111
118
self ._copy_file (zip_file , target_zip_file , delete_tmp )
112
119
else :
113
- if delete_tmp :
114
- os .remove (zip_file )
120
+ remove_file ()
115
121
return None
116
122
117
123
target_changelog_file = module_folder .joinpath (online_module .changelog_filename )
0 commit comments