Skip to content
Snippets Groups Projects

Remove cmd

Merged Ashwin Kumar Karnad requested to merge continue-with-remove-cmd into main
Files
2
@@ -1071,17 +1071,41 @@ def install_environment(
def remove_environment(mpsd_release, root_dir, package_sets="NONE", force_remove=False):
"""Remove release from installation."""
"""Remove release from installation.
Handle 3 situations :
1. remove dosent specify what to remove
-> warn and exit
2. remove all package_sets from release
-> remove release folder except logs
3. remove specific package_sets from release
-> remove spack environments via spack commands
Parameters
----------
mpsd_release : str
A string representing the MPSD release version.
root_dir : pathlib.Path
A Path object representing the path to the directory where
the release and package_sets will be installed.
package_sets : list of str
A list of strings representing the package_sets to install
(e.g., "foss2021a-mpi", "global_generic", "ALL").
force_remove : bool, optional
A boolean indicating whether to force remove the release.
If False, the user will be prompted to confirm the removal.
Defaults to False.
Raises
------
ValueError
"""
msg = (
f"Removing release {mpsd_release}"
f" with package_sets {package_sets} from {root_dir}"
)
logging.warning(msg)
# Handle 3 situations :
# 1. remove dosent specify what to remove -> warn and exit
# 2. remove all package_sets from release -> remove release folder except logs
# 3. remove specific package_sets from release
# -> remove spack environments via spack commands
if package_sets == "NONE":
logging.warning(
@@ -1116,16 +1140,46 @@ def remove_environment(mpsd_release, root_dir, package_sets="NONE", force_remove
if package_set not in ["global_packages", "global"]:
remove_spack_environment(dir_to_remove / "spack", package_set)
else:
# TODO remove global packages by calling remove_spack_package
pass
# list all specs from the global_packages.list
spe_folder = root_dir / mpsd_release / "spack-environments"
package_list_file = (
spe_folder / "toolchains" / package_set / "global_packages.list"
)
with open(package_list_file, "r") as f:
package_dump = f.read()
# remove all content from # to the end of the line
package_dump = re.sub(r"#.*\n", "\n", package_dump)
# replace \\n with "" to remove line breaks
package_list = package_dump.replace("\\\n", "").split("\n")
# remove all empty lines
package_list = [line for line in package_list if line != ""]
# remove all packages in package_list
for package in package_list:
logging.info(f"Removing package {package} from installation")
remove_spack_package(dir_to_remove / "spack", package)
def remove_spack_environment(spack_dir, environment_name):
"""Remove spack environment."""
"""Remove spack environment including packages exclusive to it.
First activate the environment,
then uninstall all packages exclusive to the environment,
then deactivate the environment,
and finally remove the environment.
Parameters
----------
spack_dir : pathlib.Path
A Path object representing the path to the spack directory.
environment_name : str
A string representing the name of the spack environment to remove.
"""
logging.warning(f"Removing spack environment {environment_name}")
spack_env = spack_dir / "share" / "spack" / "setup-env.sh"
commands_to_execute = [
f"source {spack_env}",
f"export SPACK_DIR={spack_dir}", # need to set SPACK_DIR in dash and sh
f". {spack_env}",
f"spack env activate {environment_name}",
f"for spec in $(spack -e {environment_name} find" # this line continues
r' --format "{name}@{version}%{compiler.name}@{compiler.version}");do'
@@ -1137,7 +1191,18 @@ def remove_spack_environment(spack_dir, environment_name):
def remove_spack_package(spack_dir, package):
"""Remove spack package."""
"""Remove spack package.
Used to remove global packages.
Parameters
----------
spack_dir : pathlib.Path
A Path object representing the path to the spack directory.
package : str
A string representing the name of the spack package to remove.
"""
logging.info(f"Removing spack package {package}")
spack_env = spack_dir / "share" / "spack" / "setup-env.sh"
commands_to_execute = [
Loading