diff --git a/nix_update/__init__.py b/nix_update/__init__.py index 2ea5d54..9d2dbcb 100644 --- a/nix_update/__init__.py +++ b/nix_update/__init__.py @@ -4,6 +4,7 @@ import shutil import sys import tempfile +from pathlib import Path from typing import NoReturn from .eval import CargoLockInSource, Package, eval_attr @@ -20,8 +21,8 @@ def die(msg: str) -> NoReturn: def parse_args(args: list[str]) -> Options: parser = argparse.ArgumentParser() - help = "File to import rather than default.nix. Examples, ./release.nix" - parser.add_argument("-f", "--file", default="./.", help=help) + help_msg = "File to import rather than default.nix. Examples, ./release.nix" + parser.add_argument("-f", "--file", default="./.", help=help_msg) parser.add_argument( "-F", "--flake", action="store_true", help="Update a flake attribute instead" ) @@ -94,7 +95,7 @@ def parse_args(args: list[str]) -> Options: parser.add_argument( "attribute", default=default_attribute, - nargs="?" if default_attribute else None, # type: ignore + nargs="?" if default_attribute else None, # type: ignore[arg-type] help="""Attribute name within the file evaluated (defaults to environment variable "UPDATE_NIX_ATTR_PATH")""", ) parser.add_argument( @@ -151,18 +152,19 @@ def nix_shell(options: Options) -> None: "nix", "shell", f"{options.import_path}#{options.attribute}", - ] - + options.extra_flags, + *options.extra_flags, + ], stdout=None, check=False, ) else: expr = f"let pkgs = import {options.escaped_import_path} {{}}; in pkgs.mkShell {{ buildInputs = [ pkgs.{options.escaped_attribute} ]; }}" with tempfile.TemporaryDirectory() as d: - path = os.path.join(d, "default.nix") - with open(path, "w") as f: - f.write(expr) - run(["nix-shell", path] + options.extra_flags, stdout=None, check=False) + path = Path(d) / "default.nix" + path.write_text(expr) + run( + ["nix-shell", str(path), *options.extra_flags], stdout=None, check=False + ) def git_has_diff(git_dir: str, package: Package) -> bool: @@ -197,8 +199,16 @@ def git_commit(git_dir: str, package: Package) -> None: or (new_version.rev and new_version.rev != package.rev) ): run( - ["git", "-C", git_dir, "commit", "--verbose", "--message", msg] - + files_changed, + [ + "git", + "-C", + git_dir, + "commit", + "--verbose", + "--message", + msg, + *files_changed, + ], stdout=None, ) else: @@ -206,8 +216,16 @@ def git_commit(git_dir: str, package: Package) -> None: f.write(msg) f.flush() run( - ["git", "-C", git_dir, "commit", "--verbose", "--template", f.name] - + files_changed, + [ + "git", + "-C", + git_dir, + "commit", + "--verbose", + "--template", + f.name, + *files_changed, + ], stdout=None, ) @@ -244,11 +262,7 @@ def validate_git_dir(import_path: str) -> str: def nix_run(options: Options) -> None: - cmd = [ - "nix", - "shell", - "-L", - ] + options.extra_flags + cmd = ["nix", "shell", "-L", *options.extra_flags] if options.flake: cmd.append(f"{options.import_path}#{options.attribute}") @@ -265,16 +279,11 @@ def nix_build_tool() -> str: "Return `nom` if found in $PATH" if shutil.which("nom"): return "nom" - else: - return "nix" + return "nix" def nix_build(options: Options) -> None: - cmd = [ - nix_build_tool(), - "build", - "-L", - ] + options.extra_flags + cmd = [nix_build_tool(), "build", "-L", *options.extra_flags] if options.flake: cmd.append(f"{options.import_path}#{options.attribute}") else: @@ -286,7 +295,7 @@ def nix_test(opts: Options, package: Package) -> None: if not package.tests: die(f"Package '{package.name}' does not define any tests") - cmd = [nix_build_tool(), "build", "-L"] + opts.extra_flags + cmd = [nix_build_tool(), "build", "-L", *opts.extra_flags] if opts.flake: for t in package.tests: diff --git a/nix_update/eval.py b/nix_update/eval.py index f3fc220..ddb2fcc 100644 --- a/nix_update/eval.py +++ b/nix_update/eval.py @@ -93,9 +93,9 @@ def __post_init__( if raw_cargo_lock is None: self.cargo_lock = NoCargoLock() - elif raw_cargo_lock is False: - self.cargo_lock = CargoLockInStore() - elif not os.path.realpath(raw_cargo_lock).startswith(import_path): + elif raw_cargo_lock is False or not os.path.realpath(raw_cargo_lock).startswith( + import_path + ): self.cargo_lock = CargoLockInStore() else: self.cargo_lock = CargoLockInSource(raw_cargo_lock) @@ -217,14 +217,7 @@ def eval_attr(opts: Options) -> Package: opts.system, opts.override_filename, ) - cmd = [ - "nix", - "eval", - "--json", - "--impure", - "--expr", - expr, - ] + opts.extra_flags + cmd = ["nix", "eval", "--json", "--impure", "--expr", expr, *opts.extra_flags] res = run(cmd) out = json.loads(res.stdout) if opts.override_filename is not None: @@ -233,8 +226,7 @@ def eval_attr(opts: Options) -> Package: out["url"] = opts.url package = Package(attribute=opts.attribute, import_path=opts.import_path, **out) if opts.version_preference != VersionPreference.SKIP and package.old_version == "": - raise UpdateError( - f"Nix's builtins.parseDrvName could not parse the version from {package.name}" - ) + msg = f"Nix's builtins.parseDrvName could not parse the version from {package.name}" + raise UpdateError(msg) return package diff --git a/nix_update/update.py b/nix_update/update.py index df7b734..b689607 100644 --- a/nix_update/update.py +++ b/nix_update/update.py @@ -69,9 +69,9 @@ def to_sri(hashstr: str) -> str: elif length == 40: # could be also base32 == 32, but we ignore this case and hope no one is using it prefix = "sha1:" - elif length == 64 or length == 52: + elif length in (64, 52): prefix = "sha256:" - elif length == 103 or length == 128: + elif length in (103, 128): prefix = "sha512:" else: return hashstr @@ -120,8 +120,8 @@ def nix_prefetch(opts: Options, attr: str) -> str: "nix-build", "--expr", f'let src = {expr}; in (src.overrideAttrs or (f: src // f src)) (_: {{ outputHash = ""; outputHashAlgo = "sha256"; }})', - ] - + opts.extra_flags, + *opts.extra_flags, + ], extra_env=extra_env, stderr=subprocess.PIPE, check=False, @@ -139,11 +139,9 @@ def nix_prefetch(opts: Options, attr: str) -> str: if got == "": print(stderr, file=sys.stderr) - raise UpdateError( - f"failed to retrieve hash when trying to update {opts.attribute}.{attr}" - ) - else: - return got + msg = f"failed to retrieve hash when trying to update {opts.attribute}.{attr}" + raise UpdateError(msg) + return got def disable_check_meta(opts: Options) -> str: @@ -196,19 +194,9 @@ def update_cargo_lock( "--impure", "--print-out-paths", "--expr", - f""" -{get_package(opts)}.overrideAttrs (old: {{ - cargoDeps = null; - postUnpack = '' - cp -r "$sourceRoot/${{old.cargoRoot or "."}}/Cargo.lock" $out - exit - ''; - outputs = [ "out" ]; - separateDebugInfo = false; -}}) -""", - ] - + opts.extra_flags, + f'\n{get_package(opts)}.overrideAttrs (old: {{\n cargoDeps = null;\n postUnpack = \'\'\n cp -r "$sourceRoot/${{old.cargoRoot or "."}}/Cargo.lock" $out\n exit\n \'\';\n outputs = [ "out" ];\n separateDebugInfo = false;\n}})\n', + *opts.extra_flags, + ], ) src = Path(res.stdout.strip()) if not src.is_file(): @@ -249,7 +237,7 @@ def update_cargo_lock( for line in f: print(line, end="") return - elif match := expanded.fullmatch(line): + if match := expanded.fullmatch(line): indent = match[1] path = match[2] print(line, end="") @@ -335,8 +323,8 @@ def disable_copystat(): "--print-out-paths", "--expr", getSrcAndBin, - ] - + opts.extra_flags, + *opts.extra_flags, + ], ) src = Path(res.stdout.strip()) @@ -347,7 +335,7 @@ def disable_copystat(): bin_path = (src / "nix-support" / f"{bin_name}-bin").read_text().rstrip("\n") run( - [bin_path] + cmd, + [bin_path, *cmd], cwd=tempdir, ) @@ -421,7 +409,8 @@ def update_version( new_version = Version(version) else: if not package.parsed_url: - raise UpdateError("Could not find a url in the derivations src attribute") + msg = "Could not find a url in the derivations src attribute" + raise UpdateError(msg) version_prefix = "" if preference != VersionPreference.BRANCH: @@ -568,9 +557,7 @@ def update(opts: Options) -> Package: if package.mix_deps: update_mix_deps_hash(opts, package.filename, package.mix_deps) - if isinstance(package.cargo_lock, CargoLockInSource) or isinstance( - package.cargo_lock, CargoLockInStore - ): + if isinstance(package.cargo_lock, CargoLockInSource | CargoLockInStore): if opts.generate_lockfile: generate_lockfile(opts, package.filename, "cargo") else: diff --git a/nix_update/utils.py b/nix_update/utils.py index 874e3f6..cbbb610 100644 --- a/nix_update/utils.py +++ b/nix_update/utils.py @@ -30,8 +30,10 @@ def run( stdout: None | int | IO[Any] = subprocess.PIPE, stderr: None | int | IO[Any] = None, check: bool = True, - extra_env: dict[str, str] = {}, + extra_env: dict[str, str] | None = None, ) -> "subprocess.CompletedProcess[str]": + if extra_env is None: + extra_env = {} info("$ " + shlex.join(command)) env = os.environ.copy() env.update(extra_env) diff --git a/nix_update/version/__init__.py b/nix_update/version/__init__.py index 9e94458..4cae8ce 100644 --- a/nix_update/version/__init__.py +++ b/nix_update/version/__init__.py @@ -4,7 +4,8 @@ from typing import Protocol from urllib.parse import ParseResult -from ..errors import VersionError +from nix_update.errors import VersionError + from .bitbucket import fetch_bitbucket_snapshots, fetch_bitbucket_versions from .crate import fetch_crate_versions from .gitea import fetch_gitea_snapshots, fetch_gitea_versions @@ -131,10 +132,8 @@ def fetch_latest_version( ) if unstable: - raise VersionError( - f"Found an unstable version {unstable[0]}, which is being ignored. To update to unstable version, please use '--version=unstable'" - ) + msg = f"Found an unstable version {unstable[0]}, which is being ignored. To update to unstable version, please use '--version=unstable'" + raise VersionError(msg) - raise VersionError( - "Please specify the version. We can only get the latest version from codeberg/crates.io/gitea/github/gitlab/pypi/savannah/sourcehut/rubygems/npm projects right now" - ) + msg = "Please specify the version. We can only get the latest version from codeberg/crates.io/gitea/github/gitlab/pypi/savannah/sourcehut/rubygems/npm projects right now" + raise VersionError(msg) diff --git a/nix_update/version/crate.py b/nix_update/version/crate.py index e399938..d1fe099 100644 --- a/nix_update/version/crate.py +++ b/nix_update/version/crate.py @@ -2,7 +2,8 @@ import urllib.request from urllib.parse import ParseResult -from ..utils import info +from nix_update.utils import info + from .version import Version diff --git a/nix_update/version/github.py b/nix_update/version/github.py index ba865f6..034e176 100644 --- a/nix_update/version/github.py +++ b/nix_update/version/github.py @@ -4,14 +4,16 @@ from urllib.parse import ParseResult, unquote, urlparse from xml.etree.ElementTree import Element -from ..errors import VersionError -from ..utils import info +from nix_update.errors import VersionError +from nix_update.utils import info + from .version import Version def version_from_entry(entry: Element) -> Version: if entry is None: - raise VersionError("No release found") + msg = "No release found" + raise VersionError(msg) link = entry.find("{http://www.w3.org/2005/Atom}link") assert link is not None href = link.attrib["href"] diff --git a/nix_update/version/gitlab.py b/nix_update/version/gitlab.py index 2bc0703..fb69a9a 100644 --- a/nix_update/version/gitlab.py +++ b/nix_update/version/gitlab.py @@ -4,8 +4,9 @@ from datetime import datetime from urllib.parse import ParseResult, quote_plus -from ..errors import VersionError -from ..utils import info +from nix_update.errors import VersionError +from nix_update.utils import info + from .version import Version GITLAB_API = re.compile( @@ -24,7 +25,8 @@ def fetch_gitlab_versions(url: ParseResult) -> list[Version]: resp = urllib.request.urlopen(gitlab_url) json_tags = json.loads(resp.read()) if len(json_tags) == 0: - raise VersionError("No git tags found") + msg = "No git tags found" + raise VersionError(msg) releases = [] tags = [] for tag in json_tags: diff --git a/nix_update/version/npm.py b/nix_update/version/npm.py index 217cd46..d76851b 100644 --- a/nix_update/version/npm.py +++ b/nix_update/version/npm.py @@ -2,7 +2,8 @@ import urllib.request from urllib.parse import ParseResult -from ..utils import info +from nix_update.utils import info + from .version import Version diff --git a/nix_update/version/pypi.py b/nix_update/version/pypi.py index 3463833..438435d 100644 --- a/nix_update/version/pypi.py +++ b/nix_update/version/pypi.py @@ -2,7 +2,8 @@ import urllib.request from urllib.parse import ParseResult -from ..utils import info +from nix_update.utils import info + from .version import Version diff --git a/nix_update/version/rubygems.py b/nix_update/version/rubygems.py index 9da358e..52a36c0 100644 --- a/nix_update/version/rubygems.py +++ b/nix_update/version/rubygems.py @@ -2,8 +2,9 @@ import urllib.request from urllib.parse import ParseResult -from ..errors import VersionError -from ..utils import info +from nix_update.errors import VersionError +from nix_update.utils import info + from .version import Version @@ -18,7 +19,8 @@ def fetch_rubygem_versions(url: ParseResult) -> list[Version]: resp = urllib.request.urlopen(versions_url) json_versions = json.load(resp) if len(json_versions) == 0: - raise VersionError("No versions found") + msg = "No versions found" + raise VersionError(msg) versions: list[Version] = [] for version in json_versions: diff --git a/nix_update/version/savannah.py b/nix_update/version/savannah.py index 8aa330f..600b9a1 100644 --- a/nix_update/version/savannah.py +++ b/nix_update/version/savannah.py @@ -4,7 +4,8 @@ from urllib.parse import ParseResult, urljoin, urlparse from xml.etree.ElementTree import Element -from ..utils import info +from nix_update.utils import info + from .version import Version filename_regex = re.compile(r"-(\d+(?:\.\d+)*(?:-[^-.]+)?)\.tar\.[^.]+$") diff --git a/nix_update/version/sourcehut.py b/nix_update/version/sourcehut.py index ceeb023..2aed2d3 100644 --- a/nix_update/version/sourcehut.py +++ b/nix_update/version/sourcehut.py @@ -3,14 +3,16 @@ from urllib.parse import ParseResult, urlparse from xml.etree.ElementTree import Element -from ..errors import VersionError -from ..utils import info +from nix_update.errors import VersionError +from nix_update.utils import info + from .version import Version def version_from_entry(entry: Element) -> Version: if entry is None: - raise VersionError("No release found") + msg = "No release found" + raise VersionError(msg) link = entry.find("link") assert link is not None url = urlparse(str(link.text)) diff --git a/nix_update/version/version.py b/nix_update/version/version.py index 061a573..3ce5194 100644 --- a/nix_update/version/version.py +++ b/nix_update/version/version.py @@ -20,12 +20,12 @@ class VersionPreference(StrEnum): @staticmethod def from_str(version: str) -> "VersionPreference": # auto is deprecated - if version == "auto" or version == "stable": + if version in ("auto", "stable"): return VersionPreference.STABLE - elif version == "unstable": + if version == "unstable": return VersionPreference.UNSTABLE - elif version == "skip": + if version == "skip": return VersionPreference.SKIP - elif version == "branch" or version.startswith("branch="): + if version == "branch" or version.startswith("branch="): return VersionPreference.BRANCH return VersionPreference.FIXED diff --git a/tests/test_branch.py b/tests/test_branch.py index 5bcb7e1..21b9a7e 100644 --- a/tests/test_branch.py +++ b/tests/test_branch.py @@ -15,8 +15,7 @@ def fake_urlopen(url: str) -> BinaryIO: if url.endswith("releases.atom"): return open(TEST_ROOT.joinpath("test_branch_releases.atom"), "rb") - else: - return open(TEST_ROOT.joinpath("test_branch_commits_master.atom"), "rb") + return open(TEST_ROOT.joinpath("test_branch_commits_master.atom"), "rb") def test_branch(helpers: conftest.Helpers) -> None: diff --git a/tests/test_composer.py b/tests/test_composer.py index b1647a8..2e0d80d 100644 --- a/tests/test_composer.py +++ b/tests/test_composer.py @@ -23,5 +23,6 @@ def test_update(helpers: conftest.Helpers) -> None: ], text=True, stdout=subprocess.PIPE, + check=False, ).stdout.strip() assert tuple(map(int, version.split("."))) >= (11, 3, 1) diff --git a/tests/test_composer_old.py b/tests/test_composer_old.py index 9dac8ea..99ec8d8 100644 --- a/tests/test_composer_old.py +++ b/tests/test_composer_old.py @@ -30,5 +30,6 @@ def test_update(helpers: conftest.Helpers) -> None: ], text=True, stdout=subprocess.PIPE, + check=False, ).stdout.strip() assert tuple(map(int, version.split("."))) >= (0, 11, 1) diff --git a/tests/test_maven.py b/tests/test_maven.py index d997bf2..f39a053 100644 --- a/tests/test_maven.py +++ b/tests/test_maven.py @@ -23,5 +23,6 @@ def test_update(helpers: conftest.Helpers) -> None: ], text=True, stdout=subprocess.PIPE, + check=False, ).stdout.strip() assert tuple(map(int, version.split("."))) > (3, 3, 0) diff --git a/tests/test_mix.py b/tests/test_mix.py index 8e76eff..a01262b 100644 --- a/tests/test_mix.py +++ b/tests/test_mix.py @@ -23,5 +23,6 @@ def test_update(helpers: conftest.Helpers) -> None: ], text=True, stdout=subprocess.PIPE, + check=False, ).stdout.strip() assert mix_hash != "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" diff --git a/tests/test_npm.py b/tests/test_npm.py index c718e0c..968ca8b 100644 --- a/tests/test_npm.py +++ b/tests/test_npm.py @@ -23,5 +23,6 @@ def test_update(helpers: conftest.Helpers) -> None: ], text=True, stdout=subprocess.PIPE, + check=False, ).stdout.strip() assert tuple(map(int, version.split("."))) > (10, 8, 6) diff --git a/tests/test_pnpm.py b/tests/test_pnpm.py index 9390480..bfa8b93 100644 --- a/tests/test_pnpm.py +++ b/tests/test_pnpm.py @@ -23,5 +23,6 @@ def test_update(helpers: conftest.Helpers) -> None: ], text=True, stdout=subprocess.PIPE, + check=False, ).stdout.strip() assert pnpm_hash != "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" diff --git a/tests/test_savanna.py b/tests/test_savanna.py index 34f1e5c..7199798 100644 --- a/tests/test_savanna.py +++ b/tests/test_savanna.py @@ -36,5 +36,6 @@ def test_update(helpers: conftest.Helpers) -> None: ], text=True, stdout=subprocess.PIPE, + check=False, ).stdout.strip() assert tuple(map(int, version.split("."))) >= (0, 6, 8) diff --git a/tests/test_sourcehut.py b/tests/test_sourcehut.py index 930ea6c..2e587b9 100644 --- a/tests/test_sourcehut.py +++ b/tests/test_sourcehut.py @@ -23,5 +23,6 @@ def test_update(helpers: conftest.Helpers) -> None: ], text=True, stdout=subprocess.PIPE, + check=False, ).stdout.strip() assert tuple(map(int, version.split("."))) >= (0, 3, 6) diff --git a/tests/test_subpackage.py b/tests/test_subpackage.py index 9fefa38..5e6b975 100644 --- a/tests/test_subpackage.py +++ b/tests/test_subpackage.py @@ -27,6 +27,7 @@ def get_attr(attr: str) -> str: ], text=True, stdout=subprocess.PIPE, + check=False, ).stdout.strip() subpackage_hash = get_attr("subpackage.autobrr-web.pnpmDeps.outputHash")