diff --git a/bin/refresh-poulpe-den b/bin/refresh-poulpe-den new file mode 100755 index 0000000000000000000000000000000000000000..bf4de3f281ea147cf1e211339db11c758386a200_YmluL3JlZnJlc2gtcG91bHBlLWRlbg== --- /dev/null +++ b/bin/refresh-poulpe-den @@ -0,0 +1,206 @@ +#!/usr/bin/env python3 + +import os +import stat +import sys +import subprocess +import glob +from pathlib import Path + + +USAGE = "USAGE: %s ROOTPATH" % os.path.basename(sys.argv[0]) + +POULPE_DIR = Path(__file__).absolute().parent.parent + +RESULT_DIR = "results" +BIN_ENV_DIR = "bin-envs" +DATA_ENV_DIR = "data-envs" +BENCHMARK_DIR = "benchmarks" +REPOSITORIES_DIR = "repositories" + +DIRECTORIES = [ + RESULT_DIR, + BIN_ENV_DIR, + DATA_ENV_DIR, + BENCHMARK_DIR, + REPOSITORIES_DIR, +] + +SUITE_DIR = "suites" +TOOLING = "tooling" + + +def re_install_poulpe(poulpe_dir): + """reset any venv and install poulpe in it""" + venv = Path(".venv") + if venv.exists(): + print("Deleting existing venv") + shutil.rmtree(venv) + print("Installing poulpe (editable) into a new venv") + + install_venv = [sys.executable, "-m", "venv", ".venv"] + update_pip = [ + str(Path(".venv") / "bin" / "pip"), + "install", + "--quiet", + "--upgrade", + "pip", + ] + install_poulpe = [ + str(Path(".venv") / "bin" / "pip"), + "install", + "--quiet", + "--editable", + str(poulpe_dir), + ] + + # TODO fix the symlink support thing + commands = [install_venv, update_pip, install_poulpe] + + for command in commands: + subprocess.run(command, check=True) + + +ACTIVATE = """#!/bin/bash +export POULPE_BASE_DIR="%s" +. "$POULPE_BASE_DIR"/.venv/bin/activate + +# setup autocomplete +if [ ! -z ${ZSH_VERSION+x} ]; then + eval "$(_POULPE_COMPLETE=zsh_source poulpe)" +elif [ ! -z ${BASH_VERSION+x} ]; then + eval "$(_POULPE_COMPLETE=bash_source poulpe)" +fi +""" + + +POULPE_PROXY = """#!/bin/bash +base="`dirname $0`" +. "$base"/activate +poulpe "$@" +""" + +BASH_PROXY = """#!/bin/bash +base="`dirname $0`" +. "$base"/activate +"$@" +""" + + +def setup_quick_access(base_dir): + print('Creating a "bin/" directory with a few utilities') + Path("bin").mkdir(parents=True) + + dir_bin = Path("bin") + + poulpe_proxy = dir_bin / "poulpe" + poulpe_proxy.write_text(POULPE_PROXY) + poulpe_proxy.chmod(poulpe_proxy.stat().st_mode | stat.S_IEXEC) + + activate = dir_bin / "activate" + activate.write_text(ACTIVATE % base_dir) + + bash_proxy = dir_bin / "shell" + bash_proxy.write_text(BASH_PROXY) + bash_proxy.chmod(bash_proxy.stat().st_mode | stat.S_IEXEC) + + +def clone_one_repo(repo_dir, repo_type, url, dest): + dest = repo_dir / dest + if repo_type != "hg": + raise NotImplementedError(repo_type) + clone_hg(url, dest) + + +def clone_hg(source, dest): + environ = os.environ.copy() + environ["HGRCPATH"] = "" + environ["HGPLAIN"] = "" + command = [ + "hg", + "clone", + "--quiet", + source, + dest, + ] + if "TESTTMP" in environ: + command.extend( + [ + "--config", + "extensions.share=", + "--config", + "share.poolnaming=remote", + "--config", + f"share.pool={POULPE_DIR}/.repos_caches", + ] + ) + subprocess.run(command, check=True) + + +def setup_base_dir(path): + base_path = Path(path).absolute() + print("Creating folders") + base_path.mkdir(parents=True, exist_ok=True) + for d in DIRECTORIES: + base_path.joinpath(d).mkdir(exist_ok=True) + + old_dir = os.curdir + os.chdir(base_path) + try: + tooling = Path(TOOLING) + tooling.mkdir(exist_ok=True) + local_poulpe = tooling / "poulpe" + print("Create symlinks to Poulpe") + if not local_poulpe.exists(): + os.symlink(POULPE_DIR.resolve(), local_poulpe) + os.symlink(local_poulpe / SUITE_DIR, SUITE_DIR) + + bench = base_path / BENCHMARK_DIR + bench_glob = local_poulpe.absolute() / SUITE_DIR / '*' / 'benchmarks' + for p in glob.glob(str(bench_glob)): + b = Path(p).relative_to(base_path) + suite_name = b.parent.name + if not (bench / suite_name).exists(): + os.symlink('..' / b, bench / suite_name) + + re_install_poulpe(local_poulpe) + setup_quick_access(base_path) + + finally: + os.chdir(old_dir) + + print("Cloning repositories to benchmark") + repos = base_path / REPOSITORIES_DIR + for listing in glob.glob(str(base_path / 'suites' / '*' / 'repositories')): + with open(listing) as f: + for line in f: + line = line.strip() + if not line.startswith('#'): + spec = line.split() + if len(spec) != 3: + msg = "ignoring malformated repository line: %r" + msg %= line + print(msg, file=sys.stderr) + repo_type, url, dest = spec + if not (repos / dest).exists(): + clone_one_repo(repos, repo_type, url, dest) + + print("Updating environment variables") + repos = base_path / REPOSITORIES_DIR + activate = base_path / "bin" / "activate" + for env in glob.glob(str(base_path / 'suites' / '*' / 'environment')): + source = Path(p).relative_to(base_path) + with activate.open(mode="a") as f: + f.write("\n# from %s\n" % source) + with open(env) as e: + f.write(e.read()) + print(f'Poulpe Den ready to use in "{base_path}"') + + +if __name__ == "__main__": + if len(sys.argv) != 2 or sys.argv[1].startswith('-'): + print(USAGE, file=sys.stderr) + sys.exit(128) + else: + ret = setup_base_dir(sys.argv[1]) + sys.exit(ret)