From 68505781e333e1a5df0a7c32f86df5b53df7a712 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Tue, 25 Oct 2022 14:17:44 +0200 Subject: [PATCH] gnome.updateScript: More Python This was prompted by the need to capture exit status of `find-latest-version.py` in the next commit. I had to drop `errexit` (if I did not want to result to even worse hacks) but that concealed errors like the following, when I accidentally used an incorrect equals operator in numeric comparison: line 24: ((: 1 = 1 : attempted assignment to non-variable (error token is "= 1 ") Converting the plumbing in `gnome/update.nix` to Python also makes it slightly easier to read. For now, `find-latest-version.py` is still invoked as a separate process (rather than being imported as a Python module), as `update.nix` might be replaced by `genericUpdater` in the future. --- pkgs/desktops/gnome/update.nix | 62 +++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/pkgs/desktops/gnome/update.nix b/pkgs/desktops/gnome/update.nix index e945e005d48d..2f6384c5aa76 100644 --- a/pkgs/desktops/gnome/update.nix +++ b/pkgs/desktops/gnome/update.nix @@ -1,4 +1,4 @@ -{ stdenv, bash, pkgs, lib, writeScript, python3, common-updater-scripts }: +{ stdenv, pkgs, lib, writeScript, python3, common-updater-scripts }: { packageName, attrPath ? packageName, versionPolicy ? "tagged", freeze ? false }: let @@ -20,24 +20,54 @@ let else throw "“freeze” argument needs to be either a boolean, or a version string."; updateScript = writeScript "gnome-update-script" '' - #!${bash}/bin/bash - set -o errexit - attr_path="$1" - package_name="$2" - package_version="$3" - version_policy="$4" + #!${python}/bin/python + import json + import os + import subprocess + import sys - flvFlags=("$package_name" "$version_policy" "''${GNOME_UPDATE_STABILITY:-stable}") + _, attr_path, package_name, package_version, version_policy, *remaining_args = sys.argv - if (( $# >= 5 )); then - upper_bound="$5" - flvFlags+=("--upper-bound=$upper_bound") - fi + flv_args = [ + package_name, + version_policy, + os.environ.get("GNOME_UPDATE_STABILITY", "stable"), + ] - PATH=${lib.makeBinPath [ common-updater-scripts python ]} - latest_tag=$(python "${./find-latest-version.py}" "''${flvFlags[@]}") - update-source-version "$attr_path" "$latest_tag" - echo '[ { "commitBody": "https://gitlab.gnome.org/GNOME/'$package_name'/-/compare/'$package_version'...'$latest_tag'" } ]' + match remaining_args: + case []: + pass + case [upper_bound]: + flv_args.append(f"--upper-bound={upper_bound}") + case other: + print("gnome-update-script: Received too many arguments.", file=sys.stderr) + sys.exit(1) + + latest_tag = subprocess.check_output( + [ + "${python}/bin/python", + "${./find-latest-version.py}", + *flv_args, + ], + encoding="utf-8", + ) + latest_tag = latest_tag.strip() + subprocess.run( + [ + "${common-updater-scripts}/bin/update-source-version", + attr_path, + latest_tag, + ], + check=True, + ) + + report = [ + { + "commitBody": f"https://gitlab.gnome.org/GNOME/{package_name}/-/compare/{package_version}...{latest_tag}", + }, + ] + + print(json.dumps(report)) ''; in { name = "gnome-update-script";