From f8a1c2f3cfcfa54af85de145367b1e0108c96b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Mon, 1 May 2023 08:13:08 +0200 Subject: [PATCH] nur: add eval command for local testing --- ci/nur/__init__.py | 5 +++ ci/nur/eval.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++ ci/nur/update.py | 54 ++------------------------------ 3 files changed, 83 insertions(+), 52 deletions(-) create mode 100644 ci/nur/eval.py diff --git a/ci/nur/__init__.py b/ci/nur/__init__.py index ff03df55d..70d29b07e 100644 --- a/ci/nur/__init__.py +++ b/ci/nur/__init__.py @@ -8,6 +8,7 @@ from .format_manifest import format_manifest_command from .index import index_command from .path import ROOT from .update import update_command +from .eval import eval_command LOG_LEVELS = dict( debug=logging.DEBUG, @@ -37,6 +38,10 @@ def parse_arguments(argv: List[str]) -> argparse.Namespace: update = subparsers.add_parser("update") update.set_defaults(func=update_command) + eval = subparsers.add_parser("eval") + eval.add_argument("directory", default=".") + eval.set_defaults(func=eval_command) + index = subparsers.add_parser("index") index.add_argument("directory", default=ROOT) index.set_defaults(func=index_command) diff --git a/ci/nur/eval.py b/ci/nur/eval.py new file mode 100644 index 000000000..66db5be4a --- /dev/null +++ b/ci/nur/eval.py @@ -0,0 +1,76 @@ +import logging +import subprocess +import tempfile +import os +from argparse import Namespace +from pathlib import Path +from urllib.parse import urlparse + +from .error import EvalError +from .manifest import Repo +from .path import EVALREPO_PATH, nixpkgs_path + +logger = logging.getLogger(__name__) + + +def eval_repo(repo: Repo, repo_path: Path) -> None: + with tempfile.TemporaryDirectory() as d: + eval_path = Path(d).joinpath("default.nix") + with open(eval_path, "w") as f: + f.write( + f""" + with import {{}}; +import {EVALREPO_PATH} {{ + name = "{repo.name}"; + url = "{repo.url}"; + src = {repo_path.joinpath(repo.file)}; + inherit pkgs lib; +}} +""" + ) + + # fmt: off + cmd = [ + "nix-env", + "-f", str(eval_path), + "-qa", "*", + "--meta", + "--xml", + "--allowed-uris", "https://static.rust-lang.org", + "--option", "restrict-eval", "true", + "--option", "allow-import-from-derivation", "true", + "--drv-path", + "--show-trace", + "-I", f"nixpkgs={nixpkgs_path()}", + "-I", str(repo_path), + "-I", str(eval_path), + "-I", str(EVALREPO_PATH), + ] + # fmt: on + + logger.info(f"Evaluate repository {repo.name}") + env = dict(PATH=os.environ["PATH"], NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM="1") + proc = subprocess.Popen(cmd, env=env, stdout=subprocess.DEVNULL) + try: + res = proc.wait(15) + except subprocess.TimeoutExpired: + raise EvalError(f"evaluation for {repo.name} timed out of after 15 seconds") + if res != 0: + raise EvalError(f"{repo.name} does not evaluate:\n$ {' '.join(cmd)}") + + +def eval_command(args: Namespace) -> None: + logging.basicConfig(level=logging.INFO) + + repo_path = Path(args.directory) + name = repo_path.name + repo = Repo( + name, + urlparse("localhost"), + False, + None, + None, + None, + None, + ) + eval_repo(repo, repo_path) diff --git a/ci/nur/update.py b/ci/nur/update.py index 51b16b1a3..15cbf904d 100644 --- a/ci/nur/update.py +++ b/ci/nur/update.py @@ -1,64 +1,14 @@ import logging -import os -import subprocess -import tempfile from argparse import Namespace -from pathlib import Path -from .error import EvalError +from .eval import EvalError, eval_repo from .manifest import Repo, load_manifest, update_lock_file -from .path import EVALREPO_PATH, LOCK_PATH, MANIFEST_PATH, nixpkgs_path +from .path import LOCK_PATH, MANIFEST_PATH from .prefetch import prefetch logger = logging.getLogger(__name__) -def eval_repo(repo: Repo, repo_path: Path) -> None: - with tempfile.TemporaryDirectory() as d: - eval_path = Path(d).joinpath("default.nix") - with open(eval_path, "w") as f: - f.write( - f""" - with import {{}}; -import {EVALREPO_PATH} {{ - name = "{repo.name}"; - url = "{repo.url}"; - src = {repo_path.joinpath(repo.file)}; - inherit pkgs lib; -}} -""" - ) - - # fmt: off - cmd = [ - "nix-env", - "-f", str(eval_path), - "-qa", "*", - "--meta", - "--xml", - "--allowed-uris", "https://static.rust-lang.org", - "--option", "restrict-eval", "true", - "--option", "allow-import-from-derivation", "true", - "--drv-path", - "--show-trace", - "-I", f"nixpkgs={nixpkgs_path()}", - "-I", str(repo_path), - "-I", str(eval_path), - "-I", str(EVALREPO_PATH), - ] - # fmt: on - - logger.info(f"Evaluate repository {repo.name}") - env = dict(PATH=os.environ["PATH"], NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM="1") - proc = subprocess.Popen(cmd, env=env, stdout=subprocess.DEVNULL) - try: - res = proc.wait(15) - except subprocess.TimeoutExpired: - raise EvalError(f"evaluation for {repo.name} timed out of after 15 seconds") - if res != 0: - raise EvalError(f"{repo.name} does not evaluate:\n$ {' '.join(cmd)}") - - def update(repo: Repo) -> Repo: repo, locked_version, repo_path = prefetch(repo)