Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update source attributes other than src #202

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions nix_update/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ def parse_args(args: list[str]) -> Options:
help="Attribute name within the file evaluated",
)

parser.add_argument(
"--src-attr",
help="Src attribute",
default="src",
)

a = parser.parse_args(args)
return Options(
import_path=os.path.realpath(a.file),
Expand All @@ -101,6 +107,7 @@ def parse_args(args: list[str]) -> Options:
version=a.version,
version_preference=VersionPreference.from_str(a.version),
attribute=a.attribute,
source_attribute=a.src_attr,
test=a.test,
version_regex=a.version_regex,
review=a.review,
Expand Down
32 changes: 24 additions & 8 deletions nix_update/eval.py
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently fails with

nix_update.errors.VersionError: Please specify the version. We can only get the latest version from codeberg/crates.io/gitea/github/gitlab/notabug/pypi/savannah/sourcehut/rubygems projects right now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output of the pprint(package)

Package(attribute='catppuccin',
        name='catppuccin-unstable-2023-10-09',
        old_version='unstable-2023-10-09',
        filename='/home/artturin/nixgits/my-nixpkgs/pkgs/data/themes/catppuccin/default.nix',
        line=19,
        urls=['https://github.com/catppuccin/bat/archive/00bd462e8fab5f74490335dcf881ebe7784d23fa.tar.gz'],
        url='https://github.com/catppuccin/bat/archive/00bd462e8fab5f74490335dcf881ebe7784d23fa.tar.gz',
        src_homepage='https://github.com/catppuccin/bat',
        changelog=None,
        rev='00bd462e8fab5f74490335dcf881ebe7784d23fa',
        hash='sha256-yzn+1IXxQaKcCK7fBdjtVohns0kbN+gcqbWVE4Bx7G8=',
        go_modules=None,
        go_modules_old=None,
        cargo_deps=None,
        npm_deps=None,
        yarn_deps=None,
        tests=[],
        has_update_script=False,
        parsed_url=ParseResult(scheme='https', netloc='github.com', path='/catppuccin/bat/archive/00bd462e8fab5f74490335dcf881ebe7784d23fa.tar.gz', params='', query='', fragment=''),
        new_version=None,
        version_position=Position(file='/home/artturin/nixgits/my-nixpkgs/pkgs/data/themes/catppuccin/default.nix',
                                  line=66,
                                  column=3),
        cargo_lock=<nix_update.eval.NoCargoLock object at 0x7fc032a9e150>,
        diff_url=None)

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
from dataclasses import InitVar, dataclass, field
from pprint import pprint
from textwrap import dedent, indent
from typing import Any, Literal
from urllib.parse import ParseResult, urlparse
Expand Down Expand Up @@ -91,10 +92,20 @@ def __post_init__(


def eval_expression(
escaped_import_path: str, attr: str, flake: bool, system: str | None
escaped_import_path: str,
attr: str,
source_attr: str,
flake: bool,
system: str | None,
) -> str:
system = f'"{system}"' if system else "builtins.currentSystem"

source_attrs = source_attr.rpartition(".")
source_attr_last = source_attrs[-1] or source_attr
source_attr_all_but_last = (
f".{source_attrs[0]}" if source_attr_last != source_attr else ""
)

if flake:
let_bindings = f"""
inherit (builtins) getFlake stringLength substring;
Expand Down Expand Up @@ -132,17 +143,17 @@ def eval_expression(
else if pkg ? isPhpExtension then
raw_version_position
else
sanitizePosition (builtins.unsafeGetAttrPos "src" pkg);
sanitizePosition (builtins.unsafeGetAttrPos "{source_attr_last}" pkg{source_attr_all_but_last});
in {{
name = pkg.name;
old_version = pkg.version or (builtins.parseDrvName pkg.name).version;
inherit raw_version_position;
filename = position.file;
line = position.line;
urls = pkg.src.urls or null;
url = pkg.src.url or null;
rev = pkg.src.rev or null;
hash = pkg.src.outputHash or null;
urls = pkg.{source_attr}.urls or null;
url = pkg.{source_attr}.url or null;
rev = pkg.{source_attr}.rev or null;
hash = pkg.{source_attr}.outputHash or null;
go_modules = pkg.goModules.outputHash or null;
go_modules_old = pkg.go-modules.outputHash or null;
cargo_deps = pkg.cargoDeps.outputHash or null;
Expand All @@ -161,14 +172,18 @@ def eval_expression(
yarn_deps = pkg.offlineCache.outputHash or null;
tests = builtins.attrNames (pkg.passthru.tests or {{}});
has_update_script = {has_update_script};
src_homepage = pkg.src.meta.homepage or null;
src_homepage = pkg.{source_attr}.meta.homepage or null;
changelog = pkg.meta.changelog or null;
}}"""


def eval_attr(opts: Options) -> Package:
expr = eval_expression(
opts.escaped_import_path, opts.escaped_attribute, opts.flake, opts.system
opts.escaped_import_path,
opts.escaped_attribute,
opts.source_attribute,
opts.flake,
opts.system,
)
cmd = [
"nix",
Expand All @@ -181,6 +196,7 @@ def eval_attr(opts: Options) -> Package:
res = run(cmd)
out = json.loads(res.stdout)
package = Package(attribute=opts.attribute, import_path=opts.import_path, **out)
pprint(package)
if opts.override_filename is not None:
package.filename = opts.override_filename
if opts.url is not None:
Expand Down
4 changes: 4 additions & 0 deletions nix_update/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@dataclass
class Options:
attribute: str
source_attribute: str = "src"
flake: bool = False
version: str = "stable"
version_preference: VersionPreference = VersionPreference.STABLE
Expand All @@ -29,4 +30,7 @@ class Options:

def __post_init__(self) -> None:
self.escaped_attribute = ".".join(map(json.dumps, self.attribute.split(".")))
self.escaped_source_attribute = ".".join(
map(json.dumps, self.source_attribute.split("."))
)
self.escaped_import_path = json.dumps(self.import_path)
2 changes: 1 addition & 1 deletion nix_update/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def git_prefetch(x: tuple[str, tuple[str, str]]) -> tuple[str, str]:


def update_src_hash(opts: Options, filename: str, current_hash: str) -> None:
target_hash = nix_prefetch(opts, "src")
target_hash = nix_prefetch(opts, opts.source_attribute)
replace_hash(filename, current_hash, target_hash)


Expand Down