Skip to content

Commit 3b13a7d

Browse files
committed
Add uninstall option
Signed-off-by: Alireza Poodineh <[email protected]>
1 parent 5379427 commit 3b13a7d

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

tools/pybite/handlers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,29 @@ def handle_bite_update(host: Host, args: argparse.Namespace, extras: List[str])
132132

133133
if not sources:
134134
print("No modules to update.")
135+
136+
def handle_bite_uninstall(host: Host, args: argparse.Namespace, extras: List[str]) -> None:
137+
"""
138+
Handle the 'uninstall' command, uninstalling module.
139+
"""
140+
141+
if extras:
142+
host.get_argparser().error(f"Invalid arguments: {extras}")
143+
144+
from . import module
145+
146+
modules = getattr(args, 'modules', [])
147+
force = args.force
148+
149+
mods: List[module.Module] = []
150+
if modules:
151+
hmods = host.get_modules()
152+
for id in modules:
153+
mod = hmods.get(id)
154+
if mod is None:
155+
host.get_argparser().error(f"Module '{id}' not found.")
156+
else:
157+
host.get_argparser().error("No modules specified for uninstall.")
158+
159+
for mod in mods:
160+
module.uninstall(mod, force=force)

tools/pybite/host.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ def get_argparser(self) -> argparse.ArgumentParser:
176176
update_parser.add_argument('-a', '--all', action='store_true', default=False, help='Update all eligible modules')
177177
self.register_handler('update', handlers.handle_bite_update)
178178

179+
uninstall_parser = subparsers.add_parser(
180+
'uninstall',
181+
help='Uninstall bite module',
182+
usage=self.argparser_usage.replace('command', 'uninstall') + ' [module ...]',
183+
)
184+
185+
uninstall_parser.add_argument('modules', nargs='*', help='Module IDs to uninstall')
186+
uninstall_parser.add_argument('-f', '--force', action='store_true', default=False, help='Force uninstall even if module has invalid statructure. By default, modules with invalid structure are not uninstalled to prevent data loss')
187+
self.register_handler('uninstall', handlers.handle_bite_uninstall)
188+
179189
# Register run command only if bite.proj exists
180190
if os.path.isfile(self.BITE_PROJ_PATH):
181191
run_parser = subparsers.add_parser(

tools/pybite/module.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,16 @@ def parse_version(v):
164164
if not modules:
165165
print(f"No valid modules found in {temp_dir}.")
166166

167-
def uninstall(module: Module):
167+
def uninstall(module: Module, force: bool = False) -> None:
168168
"""
169169
Uninstall a module.
170170
"""
171171
print(f"Uninstalling module {module.id}...")
172-
if not module.valid:
172+
if not force and not module.valid:
173173
raise ValueError(f"Module {module.id} is not valid.")
174+
174175
if not os.path.exists(module.path):
175176
raise ValueError(f"Module path does not exist: {module.path}")
177+
176178
shutil.rmtree(module.path)
177179
print(f"Module {module.id} uninstalled successfully.")

0 commit comments

Comments
 (0)