Skip to content

Commit

Permalink
Implemented --ignore-missing and --uninstall-missing
Browse files Browse the repository at this point in the history
  • Loading branch information
muffato committed Nov 9, 2022
1 parent fbff784 commit 9d789b2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
14 changes: 11 additions & 3 deletions shpc/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,21 @@ def get_parser():
action="store_true",
)
reinstall.add_argument(
"--force",
"-f",
dest="force",
"--ignore-missing",
"-i",
dest="ignore_missing",
help="Ignore and leave intact the versions that don't exist in the registry anymore.",
default=False,
action="store_true",
)
reinstall.add_argument(
"--uninstall-missing",
"-u",
dest="uninstall_missing",
help="Uninstall the versions that don't exist in the registry anymore.",
default=False,
action="store_true",
)

# List installed modules
listing = subparsers.add_parser("list", description="list installed modules.")
Expand Down
12 changes: 11 additions & 1 deletion shpc/client/reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ def main(args, parser, extra, subparser):
# One option must be present
if not args.reinstall_recipe and not args.all:
logger.exit("Missing arguments: provide reinstall_recipe or --all.")
if args.ignore_missing and args.uninstall_missing:
logger.exit(
"Conflicting arguments --ignore-missing and --uninstall-missing, choose one."
)

# And do the reinstall
cli.reinstall(
args.reinstall_recipe,
force=args.force,
when_missing=(
"ignore"
if args.ignore_missing
else "uninstall"
if args.uninstall_missing
else None
),
)
28 changes: 21 additions & 7 deletions shpc/main/modules/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,37 +462,51 @@ def view_install(self, view_name, name, force=False):
view.confirm_install(module.module_dir, force=force)
view.install(module.module_dir)

def reinstall(self, module, force=False):
def reinstall(self, name, when_missing=None):
"""
Reinstall the module, or all modules
"""
if module:
module_name, _, version = module.partition(":")
if name:
module_name, _, version = name.partition(":")
# Find all the versions currently installed
installed_modules = self._get_module_lookup(
self.settings.module_base, self.modulefile, module_name
)
if (module_name not in installed_modules) or (
version and version not in installed_modules[module_name]
):
logger.exit("%s is not installed. Nothing to reinstall." % module)
logger.exit("%s is not installed. Nothing to reinstall." % name)
versions = [version] if version else installed_modules[module_name]
# Reinstall the required version(s) one by one
for version in versions:
self.install(module_name + ":" + version, allow_reinstall=True)
self._reinstall(module_name, version, when_missing)
else:
# Reinstall everything that is currently installed
installed_modules = self._get_module_lookup(
self.settings.module_base, self.modulefile
)
for module_name, versions in installed_modules.items():
for version in versions:
self.install(module_name + ":" + version, allow_reinstall=True)
self._reinstall(module_name, version, when_missing)

def _reinstall(self, module_name, versions, upgrade=False, force=False):
def _reinstall(self, module_name, version, when_missing):
"""
Reinstall (and possibly upgrade) all the current modules, possibly filtered by pattern.
"""
config = self.load_registry_config(module_name)
if version in config.tags:
self.install(module_name + ":" + version, allow_reinstall=True)
elif when_missing == "ignore":
pass
elif when_missing == "uninstall":
self.uninstall(module_name + ":" + version, force=True)
else:
logger.exit(
"%s is not in the Registry any more. Add --uninstall-missing or --ignore-missing."
% module_name
)

def _upgrade(self, module_name, versions, upgrade=False, force=False):
result = self.registry.find(module_name)
if result:

Expand Down

0 comments on commit 9d789b2

Please sign in to comment.