Skip to content
Snippets Groups Projects
Commit b5e7c510 authored by Ashwin Kumar Karnad's avatar Ashwin Kumar Karnad
Browse files

black with 120 char per line lim

parent c6e26258
No related branches found
No related tags found
2 merge requests!19Move linux-debian11 into main,!1Resolve "First draft for user interface for top level install command"
...@@ -16,20 +16,27 @@ The toolchains are built using the bash script spack_setup.sh, and the results a ...@@ -16,20 +16,27 @@ The toolchains are built using the bash script spack_setup.sh, and the results a
""" """
config_vars = { config_vars = {
"cmd_log_file": "logs/install-software-environment.log", "cmd_log_file": "logs/install-software-environment.log",
"build_log_file": f"build_toolchains_mpsd_spack_ver_{time.strftime('%Y%m%d-%H%M%S')}.log", # TODO: modify toolchains,mpsd_spack_ver when the variable is available "build_log_file": f"build_toolchains_mpsd_spack_ver_{time.strftime('%Y%m%d-%H%M%S')}.log", # TODO: modify toolchains,mpsd_spack_ver when the variable is available
"spack_environments_repo": "https://gitlab.gwdg.de/mpsd-cs/spack-environments.git", "spack_environments_repo": "https://gitlab.gwdg.de/mpsd-cs/spack-environments.git",
} }
shared_var ={ shared_var = {
"script_branch": subprocess.run(["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout=subprocess.PIPE).stdout.decode().strip(), "script_branch": subprocess.run(
"script_commit_hash": subprocess.run(["git", "rev-parse", "--short", "HEAD"], stdout=subprocess.PIPE).stdout.decode().strip(), ["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout=subprocess.PIPE
)
.stdout.decode()
.strip(),
"script_commit_hash": subprocess.run(
["git", "rev-parse", "--short", "HEAD"], stdout=subprocess.PIPE
)
.stdout.decode()
.strip(),
"spe_branch": None, "spe_branch": None,
"spe_commit_hash": None, "spe_commit_hash": None,
"available_toolchains": None, "available_toolchains": None,
"toolchain_base_dir": None, "toolchain_base_dir": None,
} }
# Helper class to change directory via context manager # Helper class to change directory via context manager
class os_chdir: class os_chdir:
def __init__(self, new_dir): def __init__(self, new_dir):
...@@ -42,6 +49,7 @@ class os_chdir: ...@@ -42,6 +49,7 @@ class os_chdir:
def __exit__(self, exc_type, exc_val, exc_tb): def __exit__(self, exc_type, exc_val, exc_tb):
os.chdir(self.saved_dir) os.chdir(self.saved_dir)
def setup_log_cmd(shared_var, msg=None): def setup_log_cmd(shared_var, msg=None):
# Create log directory if it doesn't exist # Create log directory if it doesn't exist
if not os.path.exists("logs"): if not os.path.exists("logs"):
...@@ -52,7 +60,7 @@ def setup_log_cmd(shared_var, msg=None): ...@@ -52,7 +60,7 @@ def setup_log_cmd(shared_var, msg=None):
# Software environment installer branch: script_branch (commit hash: script_commit_hash) # Software environment installer branch: script_branch (commit hash: script_commit_hash)
# Spack environments branch: dev-23a (commit hash: spe_commit_hash) # Spack environments branch: dev-23a (commit hash: spe_commit_hash)
# MSGs # MSGs
with open(config_vars['cmd_log_file'], "a") as f: with open(config_vars["cmd_log_file"], "a") as f:
if msg: if msg:
f.write(msg + "\n") f.write(msg + "\n")
else: else:
...@@ -66,8 +74,8 @@ def setup_log_cmd(shared_var, msg=None): ...@@ -66,8 +74,8 @@ def setup_log_cmd(shared_var, msg=None):
f"Spack environments branch: {shared_var['spe_branch']} (commit hash: {shared_var['spe_commit_hash']})\n" f"Spack environments branch: {shared_var['spe_branch']} (commit hash: {shared_var['spe_commit_hash']})\n"
) )
def prepare_environment(mpsd_release, script_dir):
def prepare_environment(mpsd_release, script_dir):
release_base_dir = os.path.join(script_dir, mpsd_release) release_base_dir = os.path.join(script_dir, mpsd_release)
if not os.path.exists(release_base_dir): if not os.path.exists(release_base_dir):
os.makedirs(release_base_dir) os.makedirs(release_base_dir)
...@@ -77,25 +85,22 @@ def prepare_environment(mpsd_release, script_dir): ...@@ -77,25 +85,22 @@ def prepare_environment(mpsd_release, script_dir):
with os_chdir(release_base_dir): with os_chdir(release_base_dir):
# Clone the spack-environments repo if it doesn't exist # Clone the spack-environments repo if it doesn't exist
if not os.path.exists("spack-environments"): if not os.path.exists("spack-environments"):
subprocess.run( subprocess.run(
[ [
"git", "git",
"clone", "clone",
config_vars['spack_environments_repo'], config_vars["spack_environments_repo"],
] ]
) )
with os_chdir("spack-environments"): with os_chdir("spack-environments"):
# Git fetch and checkout the release branch and git pull to be sure that the resulting repo is up to date # Git fetch and checkout the release branch and git pull to be sure that the resulting repo is up to date
subprocess.run(["git", "fetch", "--all"]) subprocess.run(["git", "fetch", "--all"])
subprocess.run(["git", "checkout", mpsd_release]) subprocess.run(["git", "checkout", mpsd_release])
subprocess.run(["git", "pull"]) subprocess.run(["git", "pull"])
# Get the branch and commit hash of the spack-environments repo and store them in shared_var # Get the branch and commit hash of the spack-environments repo and store them in shared_var
spe_commit_hash = ( spe_commit_hash = (
subprocess.run( subprocess.run(["git", "rev-parse", "HEAD"], stdout=subprocess.PIPE)
["git", "rev-parse", "HEAD"], stdout=subprocess.PIPE
)
.stdout.decode() .stdout.decode()
.strip() .strip()
) )
...@@ -110,54 +115,89 @@ def prepare_environment(mpsd_release, script_dir): ...@@ -110,54 +115,89 @@ def prepare_environment(mpsd_release, script_dir):
setup_log_cmd(shared_var) setup_log_cmd(shared_var)
return shared_var return shared_var
def install_environment(release, toolchains, target_dir,force_reinstall,skip_build_cache):
def install_environment(
release, toolchains, target_dir, force_reinstall, skip_build_cache
):
print(f"Installing release {release} with toolchains {toolchains} to {target_dir}") print(f"Installing release {release} with toolchains {toolchains} to {target_dir}")
shared_var = prepare_environment(target_dir, release, force_reinstall, shared_var) shared_var = prepare_environment(target_dir, release, force_reinstall, shared_var)
def remove_environment(release, toolchains, target_dir): def remove_environment(release, toolchains, target_dir):
print(f"Removing release {release} with toolchains {toolchains} from {target_dir}") print(f"Removing release {release} with toolchains {toolchains} from {target_dir}")
def start_new_environment(release, from_release, target_dir): def start_new_environment(release, from_release, target_dir):
print(f"Starting new release {release} from {from_release} to {target_dir}") print(f"Starting new release {release} from {from_release} to {target_dir}")
def main(): def main():
parser = argparse.ArgumentParser(description=about_tool) parser = argparse.ArgumentParser(description=about_tool)
subparsers = parser.add_subparsers(dest='action', title='actions', description='valid actions', required=True) subparsers = parser.add_subparsers(
dest="action", title="actions", description="valid actions", required=True
)
subparsers.required = True subparsers.required = True
list_of_cmds = [ list_of_cmds = [
('prepare', 'Prepare the environment for installation on the disk'), ("prepare", "Prepare the environment for installation on the disk"),
('install', 'Install a software environment'), ("install", "Install a software environment"),
('reinstall', 'Reinstall a software environment'), ("reinstall", "Reinstall a software environment"),
('remove', 'Remove a software environment or toolchains from an environment'), ("remove", "Remove a software environment or toolchains from an environment"),
('start-new', 'Start a new software environment version') ("start-new", "Start a new software environment version"),
] ]
for cmd, help_text in list_of_cmds: for cmd, help_text in list_of_cmds:
subp = subparsers.add_parser(cmd, help=help_text) subp = subparsers.add_parser(cmd, help=help_text)
if cmd == 'start-new': if cmd == "start-new":
subp.add_argument('--from-release', dest='from_release', type=str, required=True, help='Release version to start from') subp.add_argument(
subp.add_argument('--to-release', dest='to_release', type=str, required=True, help='Release version to create') "--from-release",
dest="from_release",
type=str,
required=True,
help="Release version to start from",
)
subp.add_argument(
"--to-release",
dest="to_release",
type=str,
required=True,
help="Release version to create",
)
else: else:
subp.add_argument('release', type=str, help='Release version to install or remove') subp.add_argument(
if cmd in ['install', 'reinstall']: "release", type=str, help="Release version to install or remove"
subp.add_argument('--toolchains', type=str, nargs='*', default='ALL', help='List of toolchains to install (use ALL to install all toolchains)') )
subp.add_argument('--enable-build-cache', action='store_true', help='Enable Spack build cache. Useful for reinstallation but consumes time and disk space') if cmd in ["install", "reinstall"]:
subp.add_argument(
"--toolchains",
type=str,
nargs="*",
default="ALL",
help="List of toolchains to install (use ALL to install all toolchains)",
)
subp.add_argument(
"--enable-build-cache",
action="store_true",
help="Enable Spack build cache. Useful for reinstallation but consumes time and disk space",
)
# Carry out the action # Carry out the action
args = parser.parse_args() args = parser.parse_args()
# target dir is the place where this script exists. the release `dev` in script_dir/dev-23a # target dir is the place where this script exists. the release `dev` in script_dir/dev-23a
target_dir = os.path.dirname(os.path.realpath(__file__)) target_dir = os.path.dirname(os.path.realpath(__file__))
# Check the command and run related function # Check the command and run related function
if args.action == 'remove': if args.action == "remove":
remove_environment(args.release, args.toolchains, target_dir) remove_environment(args.release, args.toolchains, target_dir)
elif args.action == 'start-new': elif args.action == "start-new":
start_new_environment(args.from_release, args.to_release, target_dir) start_new_environment(args.from_release, args.to_release, target_dir)
elif args.action == 'install': elif args.action == "install":
install_environment(args.release, args.toolchains, target_dir, False, args.skip_build_cache) install_environment(
elif args.action == 'prepare': args.release, args.toolchains, target_dir, False, args.skip_build_cache
)
elif args.action == "prepare":
prepare_environment(args.release, target_dir) prepare_environment(args.release, target_dir)
if __name__ == "__main__": if __name__ == "__main__":
main() main()
...@@ -35,7 +35,9 @@ def test_prepare_environment(tmp_path): ...@@ -35,7 +35,9 @@ def test_prepare_environment(tmp_path):
# check that the test directory does not exist # check that the test directory does not exist
assert not script_dir.exists() assert not script_dir.exists()
mod.prepare_environment(mpsd_release=mpsd_release_to_test, script_dir=str(script_dir)) mod.prepare_environment(
mpsd_release=mpsd_release_to_test, script_dir=str(script_dir)
)
# wait for 20 seconds for the git clone to finish # wait for 20 seconds for the git clone to finish
# time.sleep(20) # time.sleep(20)
# check if the directory now is created # check if the directory now is created
...@@ -55,7 +57,6 @@ def test_prepare_environment(tmp_path): ...@@ -55,7 +57,6 @@ def test_prepare_environment(tmp_path):
) )
def test_setup_log_cmd(tmp_path): def test_setup_log_cmd(tmp_path):
# check that logs/install-software-environment.log is updated when the module is run # check that logs/install-software-environment.log is updated when the module is run
log_file = "logs/install-software-environment.log" log_file = "logs/install-software-environment.log"
...@@ -65,7 +66,12 @@ def test_setup_log_cmd(tmp_path): ...@@ -65,7 +66,12 @@ def test_setup_log_cmd(tmp_path):
initial_bytes = 0 initial_bytes = 0
# run the prepare_env functionality # run the prepare_env functionality
mod.prepare_env(toolchain_base_dir=str(tmp_path), mpsd_spack_ver='dev-23a', skip_dir_check=False, shared_var=shared_var) mod.prepare_env(
toolchain_base_dir=str(tmp_path),
mpsd_spack_ver="dev-23a",
skip_dir_check=False,
shared_var=shared_var,
)
# check that logs/install-software-environment.log is updated # check that logs/install-software-environment.log is updated
assert os.path.exists("logs/install-software-environment.log") assert os.path.exists("logs/install-software-environment.log")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment