Skip to content

Commit 61e23d3

Browse files
committed
Add update option
Signed-off-by: Alireza Poodineh <itsaeliux@gmail.com>
1 parent 21b4489 commit 61e23d3

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

tools/pybite/handlers.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def handle_bite_list(host: Host, args: argparse.Namespace, extras: List[str]) ->
8484

8585
def handle_bite_install(host: Host, args: argparse.Namespace, extras: List[str]) -> None:
8686
"""
87-
Handle the 'install' command, installing a module.
87+
Handle the 'install' command, installing module.
8888
"""
8989
if extras:
9090
host.get_argparser().error(f"Invalid arguments: {extras}")
@@ -95,3 +95,40 @@ def handle_bite_install(host: Host, args: argparse.Namespace, extras: List[str])
9595
upgrade = args.upgrade
9696

9797
module.install(source, host.MODULES_DIR, upgrade=upgrade)
98+
99+
def handle_bite_update(host: Host, args: argparse.Namespace, extras: List[str]) -> None:
100+
"""
101+
Handle the 'update' command, updating module.
102+
"""
103+
104+
if extras:
105+
host.get_argparser().error(f"Invalid arguments: {extras}")
106+
107+
from . import module
108+
109+
modules = getattr(args, 'modules', [])
110+
all_modules = args.all
111+
112+
sources: List[str] = []
113+
hmods = host.get_modules()
114+
if all_modules:
115+
for mod in hmods.values():
116+
if mod.updatable:
117+
sources.append(mod.update_url)
118+
elif modules:
119+
for id in modules:
120+
mod = hmods.get(id)
121+
if mod is None:
122+
host.get_argparser().error(f"Module '{id}' not found.")
123+
if mod.updatable:
124+
sources.append(mod.update_url)
125+
else:
126+
host.get_argparser().error(f"Module '{id}' is not updatable.")
127+
else:
128+
host.get_argparser().error("No modules specified for update.")
129+
130+
for source in sources:
131+
module.install(source, host.MODULES_DIR, upgrade=True)
132+
133+
if not sources:
134+
print("No modules to update.")

tools/pybite/host.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ def get_argparser(self) -> argparse.ArgumentParser:
166166
install_parser.add_argument('-u', '--upgrade', action='store_true', default=False, help='Upgrade existing modules')
167167
self.register_handler('install', handlers.handle_bite_install)
168168

169+
update_parser = subparsers.add_parser(
170+
'update',
171+
help='Update bite modules',
172+
usage=self.argparser_usage.replace('command', 'update') + ' [module ...]',
173+
)
174+
175+
update_parser.add_argument('modules', nargs='*', help='Module IDs to update')
176+
update_parser.add_argument('-a', '--all', action='store_true', default=False, help='Update all eligible modules')
177+
self.register_handler('update', handlers.handle_bite_update)
178+
169179
# Register run command only if bite.proj exists
170180
if os.path.isfile(self.BITE_PROJ_PATH):
171181
run_parser = subparsers.add_parser(

tools/pybite/module.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ def _get_modules(path: str) -> None:
139139
if not old_module.valid:
140140
print(f"Can't update module {module.id}, invalid structure.")
141141
continue
142+
# Compare versions and skip update if not newer
143+
if old_module.version and module.version:
144+
def parse_version(v):
145+
return tuple(int(x) for x in v.split('.'))
146+
try:
147+
if parse_version(module.version) <= parse_version(old_module.version):
148+
print(f"Current version of {module.id} {old_module.version} is newer or equal to {module.version}. Skipping update.")
149+
continue
150+
except Exception as e:
151+
print(f"Error comparing versions: {e}. Proceeding with update.")
142152
uninstall(old_module)
143153
print(f"Installing module {module.id}...")
144154
module.update_url = parsed_url.geturl()

0 commit comments

Comments
 (0)