From 23e30c473443f952b9195720fa11bae439e6bd13 Mon Sep 17 00:00:00 2001 From: stealthycoin Date: Fri, 21 Oct 2022 09:38:48 -0700 Subject: [PATCH] Add flags to test scripts to allow intalling and running from another path --- scripts/ci/install-build-system | 68 +++++++++++++++++++++++-------- scripts/ci/run-build-system-tests | 25 ++++++++---- scripts/ci/run-tests | 17 +++++++- 3 files changed, 84 insertions(+), 26 deletions(-) diff --git a/scripts/ci/install-build-system b/scripts/ci/install-build-system index 4ae62c347fc7..094ae212a432 100755 --- a/scripts/ci/install-build-system +++ b/scripts/ci/install-build-system @@ -1,27 +1,61 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 +import argparse +import tarfile +import tempfile import os import glob -from subprocess import check_call import shutil -_dname = os.path.dirname +from pathlib import Path +from subprocess import check_call -REPO_ROOT = _dname(_dname(_dname(os.path.abspath(__file__)))) +REPO_ROOT = Path(__file__).parents[2] os.chdir(REPO_ROOT) -def run(command): - return check_call(command, shell=True) +def run(command, cwd=None): + print(f"Running command: {command}") + return check_call(command, shell=True, cwd=cwd) + + +def main(sdist_path=None): + run("pip install --no-build-isolation -r requirements-base.txt") + run("pip install --no-build-isolation -r requirements.txt") + run("pip install --no-build-isolation -r requirements-test.txt") + run( + "pip install --no-build-isolation -r requirements/download-deps/bootstrap-lock.txt" + ) + if sdist_path is None: + wheel_dist = _build_sdist_and_wheel() + else: + wheel_dist = _build_wheel(sdist_path) + run("pip install %s" % wheel_dist) + + +def _build_wheel(sdist_path): + build_dir = REPO_ROOT / "dist" + with tempfile.TemporaryDirectory() as tempdir: + with tarfile.open(sdist_path, "r:gz") as tar: + tar.extractall(tempdir) + unpacked_sdist = os.path.join(tempdir, os.listdir(tempdir)[0]) + run(f"python -m build -w -o {build_dir}", cwd=unpacked_sdist) + return _find_wheel_file() + return wheel_dist + + +def _build_sdist_and_wheel(): + if os.path.isdir("dist") and os.listdir("dist"): + shutil.rmtree("dist") + run("python -m build") + return _find_wheel_file() + + +def _find_wheel_file(): + return glob.glob(os.path.join("dist", "*.whl"))[0] -run("pip install --no-build-isolation -r requirements-base.txt") -run('pip install --no-build-isolation -r requirements.txt') -run('pip install --no-build-isolation -r requirements-test.txt') -run( - "pip install --no-build-isolation -r requirements/download-deps/bootstrap-lock.txt" -) -if os.path.isdir('dist') and os.listdir('dist'): - shutil.rmtree('dist') -run('python -m build') -wheel_dist = glob.glob(os.path.join('dist', '*.whl'))[0] -run('pip install %s' % wheel_dist) +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--use-sdist", default=None, type=os.path.abspath) + args = parser.parse_args() + main(args.use_sdist) diff --git a/scripts/ci/run-build-system-tests b/scripts/ci/run-build-system-tests index d959c90e918f..3fae903657e0 100755 --- a/scripts/ci/run-build-system-tests +++ b/scripts/ci/run-build-system-tests @@ -1,13 +1,14 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 +import argparse import os import sys + from contextlib import contextmanager +from pathlib import Path from subprocess import check_call -_dname = os.path.dirname - -REPO_ROOT = _dname(_dname(_dname(os.path.abspath(__file__)))) -RUN_TESTS_SCRIPTS = os.path.join(REPO_ROOT, 'scripts', 'ci', 'run-tests') +REPO_ROOT = Path(__file__).parents[2].absolute() +RUN_TESTS_SCRIPTS = os.path.join(REPO_ROOT, "scripts", "ci", "run-tests") @contextmanager @@ -25,9 +26,19 @@ def run(command): return check_call(command, shell=True) -if __name__ == "__main__": - with cd(os.path.join(REPO_ROOT, "tests")): +def main(tests_path=None): + if tests_path is None: + tests_path = REPO_ROOT / "tests" + with cd(tests_path): run( f"{sys.executable} {RUN_TESTS_SCRIPTS} " + f"--tests-path {tests_path} " f"backends/build_system/ --allow-repo-root-on-path" ) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--tests-path", default=None, type=os.path.abspath) + args = parser.parse_args() + main(args.tests_path) diff --git a/scripts/ci/run-tests b/scripts/ci/run-tests index cadf357a4450..663b7f1a506e 100755 --- a/scripts/ci/run-tests +++ b/scripts/ci/run-tests @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Don't run tests from the root repo dir. # We want to ensure we're importing from the installed # binary package not from the CWD. @@ -94,10 +94,23 @@ if __name__ == "__main__": "Ignore a test subdirectory. Can be specified multiple times." ) ) + parser.add_argument( + "--tests-path", + default=None, + type=os.path.abspath, + help=( + "Optional path to an alternate test directory to use." + ) + ) raw_args = parser.parse_args() test_runner, test_args, test_dirs = process_args(raw_args) + tests_path = raw_args.tests_path + if tests_path is None: + tests_path = os.path.join(REPO_ROOT, "tests") + cmd = f"{test_runner} {test_args}{test_dirs}" + print(f"CDing to {tests_path}") print(f"Running {cmd}...") - with cd(os.path.join(REPO_ROOT, "tests")): + with cd(tests_path): run(cmd, allow_repo_root_on_path=raw_args.allow_repo_root_on_path)