incus: port updateScript from nushell to python

This commit is contained in:
Adam Stephens 2024-09-19 11:13:36 -04:00
parent e82be94deb
commit 06c8f2833b
No known key found for this signature in database
4 changed files with 90 additions and 32 deletions

View File

@ -128,8 +128,8 @@ buildGoModule rec {
ui = callPackage ./ui.nix { };
updateScript = writeScript "ovs-update.nu" ''
${./update.nu} ${updateScriptArgs}
updateScript = writeScript "ovs-update.py" ''
${./update.py} ${updateScriptArgs}
'';
};

View File

@ -9,5 +9,5 @@ import ./generic.nix {
./0c37b7e3ec65b4d0e166e2127d9f1835320165b8.patch
];
lts = true;
updateScriptArgs = "--lts=true --regex '6.0.*'";
updateScriptArgs = "--lts --regex '6.0.*'";
}

View File

@ -1,29 +0,0 @@
#!/usr/bin/env nix-shell
#!nix-shell -i nu -p nushell common-updater-scripts gnused
def main [--lts = false, --regex: string] {
let attr = $"incus(if $lts {"-lts"})"
let file = $"(pwd)/pkgs/by-name/in/incus/(if $lts { "lts" } else { "package" }).nix"
let tags = list-git-tags --url=https://github.com/lxc/incus | lines | sort --natural | str replace v ''
let latest_tag = if $regex == null { $tags } else { $tags | find --regex $regex } | last
let current_version = nix eval --raw -f default.nix $"($attr).version" | str trim
if $latest_tag != $current_version {
print $"Updating: new ($latest_tag) != old ($current_version)"
update-source-version $attr $latest_tag $"--file=($file)"
let oldVendorHash = nix-instantiate . --eval --strict -A $"($attr).goModules.drvAttrs.outputHash" --json | from json
let checkBuild = do { nix-build -A $"($attr).goModules" } | complete
let vendorHash = $checkBuild.stderr | lines | str trim | find --regex 'got:[[:space:]]*sha256' | split row ' ' | last
if $vendorHash != null {
open $file | str replace $oldVendorHash $vendorHash | save --force $file
} else {
print $checkBuild.stderr
exit 1
}
}
{"lts?": $lts, before: $current_version, after: $latest_tag}
}

87
pkgs/by-name/in/incus/update.py Executable file
View File

@ -0,0 +1,87 @@
#!/usr/bin/env nix-shell
#!nix-shell -i python -p python3 python3Packages.looseversion common-updater-scripts nurl
import argparse
import json
import os
import re
from looseversion import LooseVersion
from subprocess import run
parser = argparse.ArgumentParser()
parser.add_argument("--lts", action="store_true")
parser.add_argument("--regex")
args = parser.parse_args()
nixpkgs_path = os.environ["PWD"]
attr = "incus"
file = f"pkgs/by-name/in/incus/package.nix"
if args.lts:
attr = "incus-lts"
file = f"pkgs/by-name/in/incus/lts.nix"
tags = (
run(["list-git-tags", "--url=https://github.com/lxc/incus"], capture_output=True)
.stdout.decode("utf-8")
.splitlines()
)
tags = [t.lstrip("v") for t in tags]
latest_version = "0"
for tag in tags:
if args.regex != None and not re.match(args.regex, tag):
continue
if LooseVersion(tag) > LooseVersion(latest_version):
latest_version = tag
current_version = (
run(
["nix", "eval", "--raw", "-f", "default.nix", f"{attr}.version"],
capture_output=True,
)
.stdout.decode("utf-8")
.strip()
)
if LooseVersion(latest_version) <= LooseVersion(current_version):
print("No update available")
exit(0)
print(f"Found new version {latest_version} > {current_version}")
run(["update-source-version", attr, latest_version, f"--file={file}"])
current_vendor_hash = (
run(
[
"nix-instantiate",
".",
"--eval",
"--strict",
"-A",
f"{attr}.goModules.drvAttrs.outputHash",
"--json",
],
capture_output=True,
)
.stdout.decode("utf-8")
.strip()
.strip('"')
)
latest_vendor_hash = (
run(
["nurl", "--expr", f"(import {nixpkgs_path} {{}}).{attr}.goModules"],
capture_output=True,
)
.stdout.decode("utf-8")
.strip()
)
with open(file, "r+") as f:
file_content = f.read()
file_content = re.sub(current_vendor_hash, latest_vendor_hash, file_content)
f.seek(0)
f.write(file_content)