Skip to content

Commit

Permalink
automatic ruff fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic92 committed Jan 11, 2025
1 parent c2f4539 commit b524356
Show file tree
Hide file tree
Showing 24 changed files with 109 additions and 100 deletions.
61 changes: 35 additions & 26 deletions nix_update/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -197,17 +199,33 @@ 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:
with tempfile.NamedTemporaryFile(mode="w") as f:
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,
)

Expand Down Expand Up @@ -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}")
Expand All @@ -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:
Expand All @@ -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:
Expand Down
20 changes: 6 additions & 14 deletions nix_update/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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
47 changes: 17 additions & 30 deletions nix_update/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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="")
Expand Down Expand Up @@ -335,8 +323,8 @@ def disable_copystat():
"--print-out-paths",
"--expr",
getSrcAndBin,
]
+ opts.extra_flags,
*opts.extra_flags,
],
)
src = Path(res.stdout.strip())

Expand All @@ -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,
)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion nix_update/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 6 additions & 7 deletions nix_update/version/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
3 changes: 2 additions & 1 deletion nix_update/version/crate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
8 changes: 5 additions & 3 deletions nix_update/version/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
8 changes: 5 additions & 3 deletions nix_update/version/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion nix_update/version/npm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
3 changes: 2 additions & 1 deletion nix_update/version/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Loading

0 comments on commit b524356

Please sign in to comment.