|
| 1 | +# Copyright 2023 Lawrence Livermore National Security, LLC and other |
| 2 | +# Benchpark Project Developers. See the top-level COPYRIGHT file for details. |
| 3 | +# |
| 4 | +# Copyright 2013-2023 Spack Project Developers. |
| 5 | +# |
| 6 | +# SPDX-License-Identifier: Apache-2.0 |
| 7 | + |
| 8 | +import os |
| 9 | +import os.path |
| 10 | +import pathlib |
| 11 | +import re |
| 12 | +import shutil |
| 13 | +import tempfile |
| 14 | + |
| 15 | +import benchpark.paths |
| 16 | +from benchpark.runtime import run_command, working_dir |
| 17 | + |
| 18 | + |
| 19 | +def _dry_run_command(cmd, *args, **kwargs): |
| 20 | + print(cmd) |
| 21 | + if args: |
| 22 | + print(f"\n\t{args}") |
| 23 | + if kwargs: |
| 24 | + print(f"\n\t{kwargs}") |
| 25 | + |
| 26 | + |
| 27 | +def copytree_part_of(basedir, dest, include): |
| 28 | + def _ignore(dirpath, dirlist): |
| 29 | + if pathlib.Path(dirpath) == pathlib.Path(basedir): |
| 30 | + return sorted(set(dirlist) - set(include)) |
| 31 | + else: |
| 32 | + return [] |
| 33 | + |
| 34 | + shutil.copytree(basedir, dest, ignore=_ignore) |
| 35 | + |
| 36 | + |
| 37 | +def delete_configs_in(basedir): |
| 38 | + collected = [] |
| 39 | + for fname in os.listdir(basedir): |
| 40 | + if fname.endswith(".yaml"): |
| 41 | + collected.append(os.path.join(basedir, fname)) |
| 42 | + for path in collected: |
| 43 | + run_command(f"rm {path}") |
| 44 | + |
| 45 | + |
| 46 | +def copytree_tracked(basedir, dest): |
| 47 | + tracked = set() |
| 48 | + with working_dir(basedir): |
| 49 | + if not os.path.isdir(os.path.join(basedir, ".git")): |
| 50 | + raise RuntimeError(f"Not a git repo: {basedir}") |
| 51 | + with tempfile.TemporaryDirectory() as tempdir: |
| 52 | + results_path = os.path.join(tempdir, "output.txt") |
| 53 | + with open(results_path, "w") as f: |
| 54 | + run_command("git ls-files", stdout=f) |
| 55 | + with open(results_path, "r") as f: |
| 56 | + for line in f.readlines(): |
| 57 | + tracked.add(pathlib.Path(line.strip()).parts[0]) |
| 58 | + |
| 59 | + tracked = sorted(tracked) |
| 60 | + copytree_part_of(basedir, dest, include=tracked + [".git"]) |
| 61 | + |
| 62 | + |
| 63 | +def locate_benchpark_workspace_parent_of_ramble_workspace(ramble_workspace_dir): |
| 64 | + ramble_workspace = pathlib.Path(ramble_workspace_dir) |
| 65 | + found_parent = None |
| 66 | + for parent in ramble_workspace.parents: |
| 67 | + if {"setup.sh", "spack", "ramble"} <= set(os.listdir(parent)): |
| 68 | + found_parent = parent |
| 69 | + break |
| 70 | + if not found_parent: |
| 71 | + raise RuntimeError( |
| 72 | + "Cannot locate Benchpark workspace as a parent of Ramble workspace" |
| 73 | + ) |
| 74 | + return found_parent, ramble_workspace.relative_to(found_parent) |
| 75 | + |
| 76 | + |
| 77 | +def find_one(basedir, basename): |
| 78 | + for root, dirs, files in os.walk(basedir): |
| 79 | + for x in dirs + files: |
| 80 | + if re.match(basename, x): |
| 81 | + return os.path.join(root, x) |
| 82 | + |
| 83 | + |
| 84 | +_CACHE_MARKER = ".benchpark-mirror-dir" |
| 85 | + |
| 86 | + |
| 87 | +def mirror_create(args): |
| 88 | + if args.dry_run: |
| 89 | + global run_command |
| 90 | + run_command = _dry_run_command |
| 91 | + |
| 92 | + dest = os.path.abspath(args.destdir) |
| 93 | + marker = os.path.join(dest, _CACHE_MARKER) |
| 94 | + |
| 95 | + ramble_workspace = os.path.realpath(os.path.abspath(args.workspace)) |
| 96 | + |
| 97 | + workspace, ramble_workspace_relative = ( |
| 98 | + locate_benchpark_workspace_parent_of_ramble_workspace(ramble_workspace) |
| 99 | + ) |
| 100 | + spack_instance = os.path.join(workspace, "spack") |
| 101 | + ramble_instance = os.path.join(workspace, "ramble") |
| 102 | + |
| 103 | + if not os.path.isdir(workspace): |
| 104 | + raise RuntimeError(f"{workspace} does not exist") |
| 105 | + |
| 106 | + if not os.path.exists(dest): |
| 107 | + os.makedirs(dest) |
| 108 | + with open(marker, "w"): |
| 109 | + pass |
| 110 | + elif not os.path.isdir(dest): |
| 111 | + raise RuntimeError(f"{dest} is not a directory") |
| 112 | + elif not os.path.exists(marker): |
| 113 | + raise RuntimeError( |
| 114 | + f"{dest} was not created by `benchpark mirror` (no {marker})" |
| 115 | + ) |
| 116 | + |
| 117 | + cache_storage = os.path.join(dest, "pip-cache") |
| 118 | + ramble_pip_reqs = os.path.join(benchpark.paths.benchpark_root, "requirements.txt") |
| 119 | + if not os.path.exists(cache_storage): |
| 120 | + run_command(f"pip download -r {ramble_pip_reqs} -d {cache_storage}") |
| 121 | + |
| 122 | + ramble_workspace_dest = os.path.join(dest, ramble_workspace_relative) |
| 123 | + penultimate = pathlib.Path(*pathlib.Path(ramble_workspace_dest).parts[:-1]) |
| 124 | + os.makedirs(penultimate, exist_ok=True) |
| 125 | + |
| 126 | + def _ignore(path, dir_list): |
| 127 | + if pathlib.Path(path) == pathlib.Path(ramble_workspace): |
| 128 | + # The ramble workspace contains a copy of the experiment binaries |
| 129 | + # in 'software/', and also puts dynamically generated logs for |
| 130 | + # workspace commands in 'logs/' (if the latter is not removed, |
| 131 | + # it generates an error on the destination) |
| 132 | + return ["software", "logs"] |
| 133 | + else: |
| 134 | + return [] |
| 135 | + |
| 136 | + if not os.path.exists(ramble_workspace_dest): |
| 137 | + shutil.copytree(ramble_workspace, ramble_workspace_dest, ignore=_ignore) |
| 138 | + |
| 139 | + spack_dest = os.path.join(dest, "spack") |
| 140 | + if not os.path.exists(spack_dest): |
| 141 | + copytree_tracked(spack_instance, spack_dest) |
| 142 | + |
| 143 | + ramble_dest = os.path.join(dest, "ramble") |
| 144 | + if not os.path.exists(ramble_dest): |
| 145 | + copytree_tracked(ramble_instance, ramble_dest) |
| 146 | + |
| 147 | + setup_dest = os.path.join(dest, "setup.sh") |
| 148 | + if not os.path.exists(setup_dest): |
| 149 | + with open(setup_dest, "w", encoding="utf-8") as f: |
| 150 | + f.write( |
| 151 | + """\ |
| 152 | +if [ -n "${_BENCHPARK_INITIALIZED:-}" ]; then |
| 153 | + return 0 |
| 154 | +fi |
| 155 | +
|
| 156 | +this_script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) |
| 157 | +
|
| 158 | +. $this_script_dir/spack/share/spack/setup-env.sh |
| 159 | +. $this_script_dir/ramble/share/ramble/setup-env.sh |
| 160 | +
|
| 161 | +export SPACK_DISABLE_LOCAL_CONFIG=1 |
| 162 | +
|
| 163 | +export _BENCHPARK_INITIALIZED=true |
| 164 | +""" |
| 165 | + ) |
| 166 | + |
| 167 | + env_dir = os.path.dirname(find_one(ramble_workspace, "spack.yaml")) |
| 168 | + git_repo_dst = os.path.join(dest, "git-repos") |
| 169 | + repo_copy_script = os.path.join( |
| 170 | + benchpark.paths.benchpark_root, "lib", "scripts", "env-collect-branch-tips.py" |
| 171 | + ) |
| 172 | + out, err = run_command( |
| 173 | + f"spack -e {env_dir} python {repo_copy_script} {git_repo_dst}" |
| 174 | + ) |
| 175 | + copied_pkgs = out.strip().split("\n") |
| 176 | + git_redirects = list() |
| 177 | + for pkg_name in copied_pkgs: |
| 178 | + git_url = f"$this_script_dir/git-repos/{pkg_name}" |
| 179 | + git_redirects.append( |
| 180 | + f"spack config --scope=site add packages:{pkg_name}:package_attributes:git:{git_url}" |
| 181 | + ) |
| 182 | + git_redirects = "\n".join(git_redirects) |
| 183 | + |
| 184 | + delete_configs_in(os.path.join(spack_dest, "etc", "spack")) |
| 185 | + delete_configs_in(os.path.join(ramble_dest, "etc", "ramble")) |
| 186 | + first_time_dest = os.path.join(dest, "first-time.sh") |
| 187 | + if not os.path.exists(first_time_dest): |
| 188 | + with open(first_time_dest, "w", encoding="utf-8") as f: |
| 189 | + f.write( |
| 190 | + f"""\ |
| 191 | +this_script_dir=$(cd "$(dirname "${{BASH_SOURCE[0]}}")" && pwd) |
| 192 | +
|
| 193 | +. $this_script_dir/setup.sh |
| 194 | +
|
| 195 | +spack uninstall -ay |
| 196 | +spack repo add --scope=site $this_script_dir/repo |
| 197 | +spack config --scope=site add "config:misc_cache:$this_script_dir/spack-misc-cache" |
| 198 | +spack bootstrap add --scope=site --trust local-sources "$this_script_dir/spack-bootstrap-mirror/metadata/sources/" |
| 199 | +# We store local copies of git repos for packages that install branch tips |
| 200 | +{git_redirects} |
| 201 | +
|
| 202 | +# We deleted the repo config because it may have absolute paths; |
| 203 | +# it is reinstantiated here |
| 204 | +ramble repo add --scope=site $this_script_dir/repo |
| 205 | +ramble repo add -t modifiers --scope=site $this_script_dir/modifiers |
| 206 | +ramble config --scope=site add "config:disable_progress_bar:true" |
| 207 | +ramble config --scope=site add \"config:spack:global:args:'-d'\" |
| 208 | +""" |
| 209 | + ) |
| 210 | + |
| 211 | + modifiers_dest = os.path.join(dest, "modifiers") |
| 212 | + modifiers_src = os.path.join(benchpark.paths.benchpark_root, "modifiers") |
| 213 | + if not os.path.exists(modifiers_dest): |
| 214 | + shutil.copytree(modifiers_src, modifiers_dest) |
| 215 | + |
| 216 | + bp_repo_dest = os.path.join(dest, "repo") |
| 217 | + bp_repo_src = os.path.join(benchpark.paths.benchpark_root, "repo") |
| 218 | + if not os.path.exists(bp_repo_dest): |
| 219 | + shutil.copytree(bp_repo_src, bp_repo_dest) |
| 220 | + |
| 221 | + spack_bootstrap_mirror_dest = os.path.join(dest, "spack-bootstrap-mirror") |
| 222 | + if not os.path.exists(spack_bootstrap_mirror_dest): |
| 223 | + run_command(f"spack bootstrap mirror {spack_bootstrap_mirror_dest}") |
| 224 | + |
| 225 | + ramble_workspace_mirror_dest = os.path.join(dest, "ramble-workspace-mirror") |
| 226 | + if not os.path.exists(ramble_workspace_mirror_dest): |
| 227 | + run_command( |
| 228 | + f"ramble --disable-progress-bar --workspace-dir {ramble_workspace} workspace mirror -d file://{ramble_workspace_mirror_dest}" |
| 229 | + ) |
| 230 | + |
| 231 | + |
| 232 | +def setup_parser(root_parser): |
| 233 | + mirror_subparser = root_parser.add_subparsers(dest="system_subcommand") |
| 234 | + |
| 235 | + create_parser = mirror_subparser.add_parser("create") |
| 236 | + create_parser.add_argument( |
| 237 | + "--dry-run", action="store_true", default=False, help="For debugging" |
| 238 | + ) |
| 239 | + create_parser.add_argument( |
| 240 | + "workspace", help="A benchpark workspace you want to copy" |
| 241 | + ) |
| 242 | + create_parser.add_argument("destdir", help="Put all needed resources here") |
| 243 | + |
| 244 | + |
| 245 | +def command(args): |
| 246 | + actions = { |
| 247 | + "create": mirror_create, |
| 248 | + } |
| 249 | + if args.system_subcommand in actions: |
| 250 | + actions[args.system_subcommand](args) |
| 251 | + else: |
| 252 | + raise ValueError(f"Unknown subcommand for 'system': {args.system_subcommand}") |
0 commit comments