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

Commit

Permalink
Add a check for max_num
Browse files Browse the repository at this point in the history
close #29
  • Loading branch information
SanmerDev committed Sep 3, 2023
1 parent 92ef6e4 commit 375682d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
18 changes: 11 additions & 7 deletions sync/cli/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,8 @@ def check(cls) -> int:
root_folder = Path(cls._args.root_folder).resolve()
Log.set_log_level(logging.INFO)

if not (cls._args.check_id or cls._args.check_url or cls._args.remove_empty):
return cls.CODE_FAILURE
else:
config = Config(root_folder)
check = Check(root_folder=root_folder, config=config)
config = Config(root_folder)
check = Check(root_folder=root_folder, config=config)

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

if cls._args.remove_empty:
check.empty_values(module_ids=cls._args.module_ids)
check.empty(module_ids=cls._args.module_ids)

return cls.CODE_SUCCESS
if cls._args.remove_old:
check.old(module_ids=cls._args.module_ids)

_tracks: LocalTracks = getattr(check, "_tracks")
if _tracks.size == 0:
return cls.CODE_FAILURE
else:
return cls.CODE_SUCCESS


def print_error(msg):
Expand Down
12 changes: 9 additions & 3 deletions sync/cli/Parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,23 +347,29 @@ def configure_parser_check(cls, sub_parsers):
help="Ids of modules to check, default is all."
)
p.add_argument(
"-c",
"-I",
"--check-id",
action="store_true",
help="Check id of the module in all json."
)
p.add_argument(
"-u",
"-U",
"--check-url",
action="store_true",
help=f"Check urls of files in {UpdateJson.filename()}."
)
p.add_argument(
"-e",
"-E",
"--remove-empty",
action="store_true",
help=f"Remove empty values in {TrackJson.filename()}."
)
p.add_argument(
"-O",
"--remove-old",
action="store_true",
help=f"Remove old versions based on max_num."
)

cls.add_parser_env(p)

Expand Down
47 changes: 42 additions & 5 deletions sync/core/Check.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _get_file_url(self, module_id, file):
return func(self, module_id, file)

def _get_tracks(self, module_ids, new):
if new or len(self._tracks.tracks) == 0:
if new or self._tracks.size == 0:
return self._tracks.get_tracks(module_ids)

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

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

def ids(self, module_ids=None, new=False):
Expand All @@ -129,7 +129,7 @@ def ids(self, module_ids=None, new=False):
continue

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

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

def empty_values(self, module_ids=None, new=False):
def empty(self, module_ids=None, new=False):
for track in self._get_tracks(module_ids, new):
module_folder = self._modules_folder.joinpath(track.id)
track_json_file = module_folder.joinpath(TrackJson.filename())
track.write(track_json_file)

def old(self, module_ids=None, new=False):
for track in self._get_tracks(module_ids, new):
module_folder = self._modules_folder.joinpath(track.id)
update_json_file = module_folder.joinpath(UpdateJson.filename())
if not update_json_file.exists():
continue

update_json = UpdateJson.load(update_json_file)

max_num = self._config.max_num
if track.max_num is not None:
max_num = track.max_num

if len(update_json.versions) <= max_num:
continue

old_versions = update_json.versions[:-max_num]
for old_item in old_versions:
update_json.versions.remove(old_item)
zipfile = module_folder.joinpath(old_item.zipfile_name)
changelog = module_folder.joinpath(old_item.changelog_filename)

for path in [zipfile, changelog]:
if not (path.exists() and path.is_file()):
continue

self._log.d(f"[{track.id}] -> remove {path.name}")
path.unlink()

self._log.i(f"[{track.id}] -> {UpdateJson.filename()} has been updated")
update_json.write(update_json_file)

self._log.i(f"[{track.id}] -> {TrackJson.filename()} has been updated")
track_json_file = module_folder.joinpath(TrackJson.filename())
track.versions = len(update_json.versions)
track.write(track_json_file)
3 changes: 2 additions & 1 deletion sync/core/Check.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ class Check:
def get_online_module(self, module_id: str, zip_file: Path) -> Optional[OnlineModule]: ...
def url(self, module_ids: Optional[List[str]] = ..., new: bool = ...): ...
def ids(self, module_ids: Optional[List[str]] = ..., new: bool = ...): ...
def empty_values(self, module_ids: Optional[List[str]] = ..., new: bool = ...): ...
def empty(self, module_ids: Optional[List[str]] = ..., new: bool = ...): ...
def old(self, module_ids: Optional[List[str]] = ..., new: bool = ...): ...

0 comments on commit 375682d

Please sign in to comment.