From cc1d6fcbcb83608f7a1c86c103e55bb1728c1ad6 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Mon, 25 Dec 2017 10:46:52 +0100 Subject: [PATCH] gnome3: add updateScript --- pkgs/desktops/gnome-3/default.nix | 8 ++- pkgs/desktops/gnome-3/find-latest-version.py | 58 ++++++++++++++++++++ pkgs/desktops/gnome-3/update.nix | 11 ++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 pkgs/desktops/gnome-3/find-latest-version.py create mode 100644 pkgs/desktops/gnome-3/update.nix diff --git a/pkgs/desktops/gnome-3/default.nix b/pkgs/desktops/gnome-3/default.nix index 9388c9198966..80e9058bfb1c 100644 --- a/pkgs/desktops/gnome-3/default.nix +++ b/pkgs/desktops/gnome-3/default.nix @@ -1,4 +1,4 @@ -{ pkgs }: +{ pkgs, lib }: let @@ -13,6 +13,12 @@ let callPackage = pkgs.newScope self; + # Convert a version to branch (3.26.18 → 3.26) + # Used for finding packages on GNOME mirrors + versionBranch = version: builtins.concatStringsSep "." (lib.take 2 (lib.splitString "." version)); + + updateScript = callPackage ./update.nix { }; + version = "3.26"; maintainers = with pkgs.lib.maintainers; [ lethalman jtojnar ]; diff --git a/pkgs/desktops/gnome-3/find-latest-version.py b/pkgs/desktops/gnome-3/find-latest-version.py new file mode 100644 index 000000000000..e6089c69c042 --- /dev/null +++ b/pkgs/desktops/gnome-3/find-latest-version.py @@ -0,0 +1,58 @@ +import argparse +import json +import requests +import sys + +def version_to_list(version): + return list(map(int, version.split('.'))) + +def odd_unstable(version_str, selected): + version = version_to_list(version_str) + if len(version) < 2: + return True + + even = version[1] % 2 == 0 + if selected == 'stable': + return even + else: + return not even + +def no_policy(version, selected): + return True + +version_policies = { + 'odd-unstable': odd_unstable, + 'none': no_policy, +} + +def make_version_policy(version_predicate, selected): + return lambda version: version_predicate(version, selected) + +parser = argparse.ArgumentParser(description='Find latest version for a GNOME package by crawling their release server.') +parser.add_argument('package-name', help='Name of the directory in https://ftp.gnome.org/pub/GNOME/sources/ containing the package.') +parser.add_argument('version-policy', help='Policy determining which versions are considered stable. For most GNOME packages, odd minor versions are unstable but there are exceptions.', choices=version_policies.keys(), nargs='?', default='odd-unstable') +parser.add_argument('requested-release', help='Most of the time, we will want to update to stable version but sometimes it is useful to test.', choices=['stable', 'unstable'], nargs='?', default='stable') + + +if __name__ == '__main__': + args = parser.parse_args() + + package_name = getattr(args, 'package-name') + requested_release = getattr(args, 'requested-release') + version_predicate = version_policies[getattr(args, 'version-policy')] + version_policy = make_version_policy(version_predicate, requested_release) + + # The structure of cache.json: https://gitlab.gnome.org/Infrastructure/sysadmin-bin/blob/master/ftpadmin#L762 + cache = json.loads(requests.get('https://ftp.gnome.org/pub/GNOME/sources/{}/cache.json'.format(package_name)).text) + if type(cache) != list or cache[0] != 4: + print('Unknown format of cache.json file.', file=sys.stderr) + sys.exit(1) + + versions = cache[2][package_name] + versions = sorted(filter(version_policy, versions), key=version_to_list) + + if len(versions) == 0: + print('No versions matched.', file=sys.stderr) + sys.exit(1) + + print(versions[-1]) diff --git a/pkgs/desktops/gnome-3/update.nix b/pkgs/desktops/gnome-3/update.nix new file mode 100644 index 000000000000..b7a6ce16d11f --- /dev/null +++ b/pkgs/desktops/gnome-3/update.nix @@ -0,0 +1,11 @@ +{ lib, writeScript, python3, common-updater-scripts, coreutils, gnugrep, gnused }: +{ packageName, attrPath ? packageName, versionPolicy ? "odd-unstable" }: + +let + python = python3.withPackages (p: [ p.requests ]); +in writeScript "update-${packageName}" '' + set -o errexit + PATH=${lib.makeBinPath [ common-updater-scripts coreutils gnugrep gnused python ]} + latest_tag=$(python "${./find-latest-version.py}" "${packageName}" "${versionPolicy}" "stable") + update-source-version "${attrPath}" "$latest_tag" +''