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

Commit 375682d

Browse files
committed
Add a check for max_num
close #29
1 parent 92ef6e4 commit 375682d

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

Diff for: sync/cli/Main.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,8 @@ def check(cls) -> int:
298298
root_folder = Path(cls._args.root_folder).resolve()
299299
Log.set_log_level(logging.INFO)
300300

301-
if not (cls._args.check_id or cls._args.check_url or cls._args.remove_empty):
302-
return cls.CODE_FAILURE
303-
else:
304-
config = Config(root_folder)
305-
check = Check(root_folder=root_folder, config=config)
301+
config = Config(root_folder)
302+
check = Check(root_folder=root_folder, config=config)
306303

307304
if cls._args.check_id:
308305
check.ids(module_ids=cls._args.module_ids)
@@ -311,9 +308,16 @@ def check(cls) -> int:
311308
check.url(module_ids=cls._args.module_ids)
312309

313310
if cls._args.remove_empty:
314-
check.empty_values(module_ids=cls._args.module_ids)
311+
check.empty(module_ids=cls._args.module_ids)
315312

316-
return cls.CODE_SUCCESS
313+
if cls._args.remove_old:
314+
check.old(module_ids=cls._args.module_ids)
315+
316+
_tracks: LocalTracks = getattr(check, "_tracks")
317+
if _tracks.size == 0:
318+
return cls.CODE_FAILURE
319+
else:
320+
return cls.CODE_SUCCESS
317321

318322

319323
def print_error(msg):

Diff for: sync/cli/Parameters.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -347,23 +347,29 @@ def configure_parser_check(cls, sub_parsers):
347347
help="Ids of modules to check, default is all."
348348
)
349349
p.add_argument(
350-
"-c",
350+
"-I",
351351
"--check-id",
352352
action="store_true",
353353
help="Check id of the module in all json."
354354
)
355355
p.add_argument(
356-
"-u",
356+
"-U",
357357
"--check-url",
358358
action="store_true",
359359
help=f"Check urls of files in {UpdateJson.filename()}."
360360
)
361361
p.add_argument(
362-
"-e",
362+
"-E",
363363
"--remove-empty",
364364
action="store_true",
365365
help=f"Remove empty values in {TrackJson.filename()}."
366366
)
367+
p.add_argument(
368+
"-O",
369+
"--remove-old",
370+
action="store_true",
371+
help=f"Remove old versions based on max_num."
372+
)
367373

368374
cls.add_parser_env(p)
369375

Diff for: sync/core/Check.py

+42-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def _get_file_url(self, module_id, file):
1919
return func(self, module_id, file)
2020

2121
def _get_tracks(self, module_ids, new):
22-
if new or len(self._tracks.tracks) == 0:
22+
if new or self._tracks.size == 0:
2323
return self._tracks.get_tracks(module_ids)
2424

2525
return self._tracks.tracks
@@ -106,7 +106,7 @@ def url(self, module_ids=None, new=False):
106106
update_json = UpdateJson.load(update_json_file)
107107

108108
if not self._check_update_json(track, update_json, False):
109-
self._log.i(f"url: [{track.id}] -> {UpdateJson.filename()} has been updated")
109+
self._log.i(f"[{track.id}] -> {UpdateJson.filename()} has been updated")
110110
update_json.write(update_json_file)
111111

112112
def ids(self, module_ids=None, new=False):
@@ -129,7 +129,7 @@ def ids(self, module_ids=None, new=False):
129129
continue
130130

131131
if not self._check_folder(track, online_module.id):
132-
self._log.i(f"ids: [{old_id}] -> track has been migrated to {track.id}")
132+
self._log.i(f"[{old_id}] -> track has been migrated to {track.id}")
133133
module_folder = self._modules_folder.joinpath(track.id)
134134
track_json_file = module_folder.joinpath(TrackJson.filename())
135135
track.write(track_json_file)
@@ -140,11 +140,48 @@ def ids(self, module_ids=None, new=False):
140140

141141
update_json = UpdateJson.load(update_json_file)
142142
if not self._check_update_json(track, update_json, True):
143-
self._log.i(f"ids: [{track.id}] -> {UpdateJson.filename()} has been updated")
143+
self._log.i(f"[{track.id}] -> {UpdateJson.filename()} has been updated")
144144
update_json.write(update_json_file)
145145

146-
def empty_values(self, module_ids=None, new=False):
146+
def empty(self, module_ids=None, new=False):
147147
for track in self._get_tracks(module_ids, new):
148148
module_folder = self._modules_folder.joinpath(track.id)
149149
track_json_file = module_folder.joinpath(TrackJson.filename())
150150
track.write(track_json_file)
151+
152+
def old(self, module_ids=None, new=False):
153+
for track in self._get_tracks(module_ids, new):
154+
module_folder = self._modules_folder.joinpath(track.id)
155+
update_json_file = module_folder.joinpath(UpdateJson.filename())
156+
if not update_json_file.exists():
157+
continue
158+
159+
update_json = UpdateJson.load(update_json_file)
160+
161+
max_num = self._config.max_num
162+
if track.max_num is not None:
163+
max_num = track.max_num
164+
165+
if len(update_json.versions) <= max_num:
166+
continue
167+
168+
old_versions = update_json.versions[:-max_num]
169+
for old_item in old_versions:
170+
update_json.versions.remove(old_item)
171+
zipfile = module_folder.joinpath(old_item.zipfile_name)
172+
changelog = module_folder.joinpath(old_item.changelog_filename)
173+
174+
for path in [zipfile, changelog]:
175+
if not (path.exists() and path.is_file()):
176+
continue
177+
178+
self._log.d(f"[{track.id}] -> remove {path.name}")
179+
path.unlink()
180+
181+
self._log.i(f"[{track.id}] -> {UpdateJson.filename()} has been updated")
182+
update_json.write(update_json_file)
183+
184+
self._log.i(f"[{track.id}] -> {TrackJson.filename()} has been updated")
185+
track_json_file = module_folder.joinpath(TrackJson.filename())
186+
track.versions = len(update_json.versions)
187+
track.write(track_json_file)

Diff for: sync/core/Check.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ class Check:
2828
def get_online_module(self, module_id: str, zip_file: Path) -> Optional[OnlineModule]: ...
2929
def url(self, module_ids: Optional[List[str]] = ..., new: bool = ...): ...
3030
def ids(self, module_ids: Optional[List[str]] = ..., new: bool = ...): ...
31-
def empty_values(self, module_ids: Optional[List[str]] = ..., new: bool = ...): ...
31+
def empty(self, module_ids: Optional[List[str]] = ..., new: bool = ...): ...
32+
def old(self, module_ids: Optional[List[str]] = ..., new: bool = ...): ...

0 commit comments

Comments
 (0)