From 45df4614ce33d03e1d21a64a791d8f99327f7ac0 Mon Sep 17 00:00:00 2001 From: Juan Jose Garcia-Ripoll Date: Mon, 11 Nov 2024 16:45:47 -0800 Subject: [PATCH] Added a new build script based on python --- scripts/make.cmd | 21 -------- scripts/make.py | 126 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 21 deletions(-) delete mode 100644 scripts/make.cmd create mode 100644 scripts/make.py diff --git a/scripts/make.cmd b/scripts/make.cmd deleted file mode 100644 index d452af12..00000000 --- a/scripts/make.cmd +++ /dev/null @@ -1,21 +0,0 @@ -@echo off -if "x%1"=="xclean" goto :clean -if "x%1"=="xcheck" goto :check -if "x%1"=="xinstall" goto :install -echo Unrecognized option %1 -goto :eof - -:clean -for /r . %%i in (SeeMPS.egg-info __pycache__) do if exist "%%i" rmdir /S /Q "%%i" -for %%i in (build dist) do if exist %%i rmdir /S /Q "%%i" -for /r . %%i in (*.pyd) do if exist "%%i" del /S /Q "%%i" -goto :eof - -:install -pip install --upgrade . - -:check -mypy src/seemps -ruff check src -rem flake8 src/seemps --count --select=E9,F63,F7,F82 --show-source --statistics -rem flake8 src/seemps --count --exit-zero --max-complexity=10 --max-line-length=150 --ignore W503,E741,E203,C901 --statistics diff --git a/scripts/make.py b/scripts/make.py new file mode 100644 index 00000000..873fa053 --- /dev/null +++ b/scripts/make.py @@ -0,0 +1,126 @@ +#!/usr/bin/python3 +import os +import re +import sys +import shutil +import subprocess + +incremental: bool = True +use_sanitizer: str = "no" if "SANITIZE" not in os.environ else os.environ["SANITIZE"] +ld_preload: str = "" +valgrind: list[str] = [] +python: str = "python" if sys.platform == "win32" else "python3" + + +def run(command: list[str], *args, **kwdargs) -> bool: + print(f"Running command:\n{' '.join(command)}") + return subprocess.run(command, *args, **kwdargs).returncode == 0 + + +def asan_library() -> str: + if sys.platform in ["win32", "cygwin"]: + return "" + else: + return run_output( + [ + "/bin/sh", + "-c", + r"ldconfig -p |grep asan | sed 's/^\(.*=> \)\(.*\)$/\2/g' | sed '/^[[:space:]]*$/d'", + ] + ) + + +def run_many(commands: list[list[str]], *args, chain: bool = True, **kwdargs) -> bool: + ok = True + for c in commands: + if not run(c, *args, **kwdargs): + ok = False + if chain: + return ok + return ok + + +def run_output(command: str, *args, **kwdargs) -> str: + s = str(subprocess.check_output(command, *args, **kwdargs), encoding="utf-8") + return s.rstrip(" \n\r") + + +def delete_directories(patterns: list[str], root: str = "."): + to_delete = [] + for dirname, _, _ in os.walk("."): + if os.path.basename(dirname) in patterns: + to_delete.append(dirname) + for path in to_delete: + if os.path.exists(path): + print(f"Removing {path}") + shutil.rmtree(path) + + +def delete_files(patterns: list[str], root: str = "."): + regexp = [re.compile(p) for p in patterns] + for root, _, files in os.walk("."): + for f in files: + if any(p.match(f) for p in regexp): + path = root + "/" + f + print(f"Removing {path}") + os.remove(path) + + +def clean() -> None: + for path in ["build", "dist"]: + if os.path.exists(path): + print(f"Removing {path}") + shutil.rmtree(path) + delete_directories(["SeeMPS.egg-info", "__pycache__"]) + delete_files([r".*\.so", r".*\.pyd", r".*\.pyc"]) + + +def check() -> bool: + env = os.environ.copy() + if use_sanitizer != "no": + env["LD_PRELOAD"] = asan_library() + return run_many( + [ + valgrind + [python, "-m", "unittest", "discover", "-f"], + ["mypy", "src/seemps"], + ["ruff", "check", "src"], + ], + env=env, + ) + + +def build() -> bool: + env = os.environ.copy() + extra = [] + if use_sanitizer != "no": + env["SANITIZE"] = use_sanitizer + if incremental: + extra = ["--no-build-isolation", "-ve"] + extra + return run(["pip", "install"] + extra + ["."], env=env) + + +def install() -> bool: + return run(["pip", "install", "."]) + + +for option in sys.argv[1:]: + match option: + case "here": + incremental = True + case "leak": + use_sanitizer = "leak" + case "asan": + use_sanitizer = "address" + case "clean": + clean() + case "build": + if not build(): + raise Exception("Build failed") + case "install": + if not install(): + raise Exception("Install failed") + case "check": + if not check(): + raise Exception("Tests failed") + case _: + raise Exception(f"Unknown option: {option}")