Skip to content
Snippets Groups Projects
setup-base-dir 4.66 KiB
Newer Older
#!/usr/bin/env python3

import os
import stat
import sys
import subprocess
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"

DIRECTORIES = [
    RESULT_DIR,
    BIN_ENV_DIR,
    DATA_ENV_DIR,
    BENCHMARK_DIR,
    """setup a venv and install poulpe in it"""
    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",
    ]

    commands = [install_venv, update_pip, install_poulpe]

    for command in commands:
        subprocess.run(command, check=True)


ACTIVATE = """#!/usr/bin/bash
export POULPE_BASE_DIR="%s"
. "$POULPE_BASE_DIR"/.venv/bin/activate
"""


POULPE_PROXY = """#!/usr/bin/bash
base="`dirname $0`"
. "$base"/activate
poulpe "$@"
BASH_PROXY = """#!/usr/bin/bash
base="`dirname $0`"
def setup_quick_access(base_dir):
    print('Creating a "bin/" directory with some utility')
    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",
        ])
def setup_base_dir(path):
    base_path = Path(path).absolute()
    print("Creating folders")
    base_path.mkdir(parents=True)
    for d in DIRECTORIES:
        base_path.joinpath(d).mkdir()

    old_dir = os.curdir
    os.chdir(base_path)
    try:
        setup_quick_access(base_path)
        print("Create symlinks to Poulpe")
        tooling = Path(TOOLING)
        tooling.mkdir()
        local_poulpe = tooling / "poulpe"
        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.name
            os.symlink('..' / b, bench / suite_name)
    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)
                    clone_one_repo(repos, *spec)

    print("updating environment variable")
    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("# from %s\n" % source)
            with open(env) as e:
                f.write(e.read())


if __name__ == "__main__":
    if len(sys.argv) != 2:
        print(USAGE, file=sys.stderr)
        sys.exit(128)
    else:
        ret = setup_base_dir(sys.argv[1])
        sys.exit(ret)