|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import datetime |
| 5 | +import glob |
| 6 | +import inspect |
| 7 | +import os |
| 8 | +import subprocess |
| 9 | +import yaml |
| 10 | + |
| 11 | +def get_script_dir(): |
| 12 | + return os.path.dirname(inspect.getabsfile(get_script_dir)) |
| 13 | + |
| 14 | +SCRIPTDIR = get_script_dir() |
| 15 | + |
| 16 | +parser = argparse.ArgumentParser(description='Convert a batch config to a collection of run scripts') |
| 17 | +parser.add_argument('config', type=str, help='config file' ) |
| 18 | +parser.add_argument('outdir', type=str, help='directory of output') |
| 19 | +parser.add_argument('-v', '--verbose', action='store_true', default=False) |
| 20 | + |
| 21 | +args = parser.parse_args() |
| 22 | + |
| 23 | + |
| 24 | +SCRIPT_PREAMBLE = '''#!/bin/sh |
| 25 | +
|
| 26 | +# don't allow unset variables |
| 27 | +set -o nounset |
| 28 | +
|
| 29 | +# be verbose as we execute |
| 30 | +set -x |
| 31 | +
|
| 32 | +TIMESTAMP=`date +'%Y%m%d_%H%M%S'` |
| 33 | +
|
| 34 | +# make sure python log files are in order |
| 35 | +PYTHONUNBUFFERED=true |
| 36 | +
|
| 37 | +''' |
| 38 | + |
| 39 | +SCRIPT_PARAMS = ''' |
| 40 | +SCRIPTDIR={scriptdir} |
| 41 | +SCRATCHDIR={scratchdir} |
| 42 | +
|
| 43 | +BENCH_CORE={bench_core} |
| 44 | +ENVIRONMENT={environment} |
| 45 | +EXEC_SPEC={exec_spec} |
| 46 | +CODESPEED_URL={codespeed_url} |
| 47 | +CODESPEED_DB={ocamlspeed_dir}/data/data.db |
| 48 | +ARCHIVE_DIR={ocamlspeed_dir}/artifacts/ |
| 49 | +
|
| 50 | +GITHUB_USER={github_user} |
| 51 | +GITHUB_REPO={github_repo} |
| 52 | +BRANCH={branch} |
| 53 | +FIRST_COMMIT={first_commit} |
| 54 | +OCAML_VERSION={ocaml_version} |
| 55 | +RUN_PATH_TAG={run_path_tag} |
| 56 | +CODESPEED_NAME={codespeed_name} |
| 57 | +
|
| 58 | +''' |
| 59 | + |
| 60 | +SCRIPT_BODY = ''' |
| 61 | +RUNDIR=${SCRATCHDIR}/${RUN_PATH_TAG} |
| 62 | +
|
| 63 | +RUN_STAGES=setup,bench,archive,upload |
| 64 | +
|
| 65 | +
|
| 66 | +# needed to get the path to include a dune binary |
| 67 | +# NB: a full eval $(opam config env) breaks the sandmark build in a strange way... |
| 68 | +eval $(opam config env | grep ^PATH=) |
| 69 | +
|
| 70 | +mkdir -p ${ARCHIVE_DIR} |
| 71 | +
|
| 72 | +## STAGES: |
| 73 | +## - get local copy of git repo |
| 74 | +## - setup target codespeed db to see project |
| 75 | +## - run backfill script to do it |
| 76 | +
|
| 77 | +
|
| 78 | +cd $SCRIPTDIR |
| 79 | +
|
| 80 | +## get local copy of git repo |
| 81 | +REPO=${GITHUB_USER}__${GITHUB_REPO} |
| 82 | +if [ ! -d ${REPO} ]; then |
| 83 | + git clone https://github.com/${GITHUB_USER}/${GITHUB_REPO}.git ${REPO} |
| 84 | +fi |
| 85 | +
|
| 86 | +## setup target codespeed db to see project |
| 87 | +sqlite3 ${CODESPEED_DB} "INSERT INTO codespeed_project (name,repo_type,repo_path,repo_user,repo_pass,commit_browsing_url,track,default_branch) SELECT '${CODESPEED_NAME}', 'G', 'https://github.com/${GITHUB_USER}/ocaml', '${GITHUB_USER}', '', 'https://github.com/${GITHUB_USER}/${GITHUB_REPO}/commit/{commitid}',1,'${BRANCH}' WHERE NOT EXISTS(SELECT 1 FROM codespeed_project WHERE name = '${CODESPEED_NAME}')" |
| 88 | +
|
| 89 | +
|
| 90 | +## run backfill script |
| 91 | +./run_sandmark_backfill.py --run_stages ${RUN_STAGES} --branch ${BRANCH} --main_branch ${BRANCH} --repo ${REPO} --repo_pull --repo_reset_hard --use_repo_reference --incremental_hashes --commit_choice_method from_hash=${FIRST_COMMIT} --executable_spec=${EXEC_SPEC} --environment ${ENVIRONMENT} --sandmark_comp_fmt https://github.com/${GITHUB_USER}/${GITHUB_REPO}/archive/{tag}.tar.gz --sandmark_tag_override ${OCAML_VERSION} --sandmark_iter 1 --sandmark_pre_exec="'taskset --cpu-list "${BENCH_CORE}" setarch `uname -m` --addr-no-randomize'" --archive_dir ${ARCHIVE_DIR} --codespeed_url ${CODESPEED_URL} --upload_project_name ${CODESPEED_NAME} -v ${RUNDIR} |
| 92 | +
|
| 93 | +
|
| 94 | +''' |
| 95 | + |
| 96 | +def shell_exec(cmd, verbose=args.verbose, check=False, stdout=None, stderr=None): |
| 97 | + if verbose: |
| 98 | + print('+ %s'%cmd) |
| 99 | + return subprocess.run(cmd, shell=True, check=check, stdout=stdout, stderr=stderr) |
| 100 | + |
| 101 | +outdir = os.path.abspath(args.outdir) |
| 102 | +if args.verbose: print('making directory: %s'%outdir) |
| 103 | +shell_exec('mkdir -p %s'%outdir) |
| 104 | + |
| 105 | +global_conf = { |
| 106 | + 'scriptdir': SCRIPTDIR, |
| 107 | +} |
| 108 | + |
| 109 | +# read in yaml config |
| 110 | +with open(args.config, 'r') as stream: |
| 111 | + try: |
| 112 | + conf = yaml.safe_load(stream) |
| 113 | + except yaml.YAMLError as exc: |
| 114 | + print('YAMLError: %s'%exc) |
| 115 | + sys.exit(1) |
| 116 | + |
| 117 | +# output the script |
| 118 | +for run_conf in conf['tracked_branches']: |
| 119 | + fname = os.path.join(outdir, '%s.sh'%run_conf['codespeed_name']) |
| 120 | + with open(fname, 'w') as outfile: |
| 121 | + outfile.write(SCRIPT_PREAMBLE) |
| 122 | + conf_str = SCRIPT_PARAMS.format(**{**global_conf, **conf, **run_conf}) |
| 123 | + outfile.write(conf_str) |
| 124 | + outfile.write(SCRIPT_BODY) |
| 125 | + shell_exec('chmod +x %s'%fname) |
| 126 | + |
| 127 | + |
0 commit comments