gnome3.updateScript: format with black

This commit is contained in:
Jan Tojnar 2020-04-25 00:26:49 +02:00
parent 974f11cb29
commit b0a2fb1e03
No known key found for this signature in database
GPG Key ID: 7FAB2A15F7A607A4

View File

@ -4,8 +4,10 @@ import json
import requests import requests
import sys import sys
def version_to_list(version): def version_to_list(version):
return list(map(int, version.split('.'))) return list(map(int, version.split(".")))
def odd_unstable(version_str, selected): def odd_unstable(version_str, selected):
version = version_to_list(version_str) version = version_to_list(version_str)
@ -15,19 +17,22 @@ def odd_unstable(version_str, selected):
even = version[1] % 2 == 0 even = version[1] % 2 == 0
prerelease = (version[1] >= 90 and version[1] < 100) or (version[1] >= 900 and version[1] < 1000) prerelease = (version[1] >= 90 and version[1] < 100) or (version[1] >= 900 and version[1] < 1000)
stable = even and not prerelease stable = even and not prerelease
if selected == 'stable': if selected == "stable":
return stable return stable
else: else:
return True return True
def no_policy(version, selected): def no_policy(version, selected):
return True return True
version_policies = { version_policies = {
'odd-unstable': odd_unstable, "odd-unstable": odd_unstable,
'none': no_policy, "none": no_policy,
} }
def make_version_policy(version_predicate, selected, upper_bound): def make_version_policy(version_predicate, selected, upper_bound):
if not upper_bound: if not upper_bound:
upper_bound = [math.inf, math.inf] upper_bound = [math.inf, math.inf]
@ -36,33 +41,34 @@ def make_version_policy(version_predicate, selected, upper_bound):
return lambda version: version_predicate(version, selected) and version_to_list(version) < upper_bound return lambda version: version_predicate(version, selected) and version_to_list(version) < upper_bound
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 = argparse.ArgumentParser(description="Find latest version for a GNOME package by crawling their release server.")
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("package-name", help="Name of the directory in https://ftp.gnome.org/pub/GNOME/sources/ containing the package.")
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') 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('--upper-bound', dest='upper-bound', help='Only look for versions older than this one (useful for pinning dependencies).') 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")
parser.add_argument("--upper-bound", dest="upper-bound", help="Only look for versions older than this one (useful for pinning dependencies).")
if __name__ == '__main__': if __name__ == "__main__":
args = parser.parse_args() args = parser.parse_args()
package_name = getattr(args, 'package-name') package_name = getattr(args, "package-name")
requested_release = getattr(args, 'requested-release') requested_release = getattr(args, "requested-release")
upper_bound = getattr(args, 'upper-bound') upper_bound = getattr(args, "upper-bound")
version_predicate = version_policies[getattr(args, 'version-policy')] version_predicate = version_policies[getattr(args, "version-policy")]
version_policy = make_version_policy(version_predicate, requested_release, upper_bound) version_policy = make_version_policy(version_predicate, requested_release, upper_bound)
# The structure of cache.json: https://gitlab.gnome.org/Infrastructure/sysadmin-bin/blob/master/ftpadmin#L762 # 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) cache = json.loads(requests.get(f"https://ftp.gnome.org/pub/GNOME/sources/{package_name}/cache.json").text)
if type(cache) != list or cache[0] != 4: if type(cache) != list or cache[0] != 4:
print('Unknown format of cache.json file.', file=sys.stderr) print("Unknown format of cache.json file.", file=sys.stderr)
sys.exit(1) sys.exit(1)
versions = cache[2][package_name] versions = cache[2][package_name]
versions = sorted(filter(version_policy, versions), key=version_to_list) versions = sorted(filter(version_policy, versions), key=version_to_list)
if len(versions) == 0: if len(versions) == 0:
print('No versions matched.', file=sys.stderr) print("No versions matched.", file=sys.stderr)
sys.exit(1) sys.exit(1)
print(versions[-1]) print(versions[-1])