rustdesk-flutter: add pipewire gstreame plugin (#379587)
[NixPkgs.git] / pkgs / os-specific / linux / kernel / update-zen.py
blob5fe19ef8cb846bfb7a6fbfa5e8bd3aabebc09a91
1 #! /usr/bin/env nix-shell
2 #! nix-shell -i python3 -p python3 nix nix-prefetch-git
4 import fileinput
5 import json
6 import os
7 import sys
8 import re
9 import subprocess
11 from datetime import datetime
12 from urllib.request import urlopen, Request
15 def panic(exc):
16 raise Exception(exc)
19 DIR = os.path.dirname(os.path.abspath(__file__))
20 HEADERS = {'Accept': 'application/vnd.github.v3+json'}
23 def github_api_request(endpoint):
24 base_url = 'https://api.github.com/'
25 request = Request(base_url + endpoint, headers=HEADERS)
26 with urlopen(request) as http_response:
27 return json.loads(http_response.read().decode('utf-8'))
30 def get_commit_date(repo, sha):
31 url = f'https://api.github.com/repos/{repo}/commits/{sha}'
32 request = Request(url, headers=HEADERS)
33 with urlopen(request) as http_response:
34 commit = json.loads(http_response.read().decode())
35 date = commit['commit']['committer']['date'].rstrip('Z')
36 date = datetime.fromisoformat(date).date().isoformat()
37 return 'unstable-' + date
40 def nix_prefetch_git(url, rev):
41 """Prefetches the requested Git revision (incl. submodules) of the given repository URL."""
42 print(f'nix-prefetch-git {url} {rev}')
43 out = subprocess.check_output([
44 'nix-prefetch-git', '--quiet',
45 '--url', url,
46 '--rev', rev,
47 '--fetch-submodules'])
48 return json.loads(out)['sha256']
51 def nix_prefetch_url(url, unpack=False):
52 """Prefetches the content of the given URL."""
53 print(f'nix-prefetch-url {url}')
54 options = ['--type', 'sha256']
55 if unpack:
56 options += ['--unpack']
57 out = subprocess.check_output(['nix-prefetch-url'] + options + [url])
58 return out.decode('utf-8').rstrip()
61 def update_file(relpath, variant, version, suffix, sha256):
62 file_path = os.path.join(DIR, relpath)
63 with fileinput.FileInput(file_path, inplace=True) as f:
64 for line in f:
65 result = line
66 result = re.sub(
67 fr'^ version = ".+"; # {variant}',
68 f' version = "{version}"; # {variant}',
69 result)
70 result = re.sub(
71 fr'^ suffix = ".+"; # {variant}',
72 f' suffix = "{suffix}"; # {variant}',
73 result)
74 result = re.sub(
75 fr'^ sha256 = ".+"; # {variant}',
76 f' sha256 = "{sha256}"; # {variant}',
77 result)
78 print(result, end='')
81 def read_file(relpath, variant):
82 file_path = os.path.join(DIR, relpath)
83 re_version = re.compile(fr'^\s*version = "(.+)"; # {variant}')
84 re_suffix = re.compile(fr'^\s*suffix = "(.+)"; # {variant}')
85 version = None
86 suffix = None
87 with fileinput.FileInput(file_path, mode='r') as f:
88 for line in f:
89 version_match = re_version.match(line)
90 if version_match:
91 version = version_match.group(1)
92 continue
94 suffix_match = re_suffix.match(line)
95 if suffix_match:
96 suffix = suffix_match.group(1)
97 continue
99 if version and suffix:
100 break
101 return version, suffix
104 if __name__ == "__main__":
105 if len(sys.argv) == 1:
106 panic("Update variant expected")
107 variant = sys.argv[1]
108 if variant not in ("zen", "lqx"):
109 panic(f"Unexepected variant instead of 'zen' or 'lqx': {sys.argv[1]}")
110 pattern = re.compile(fr"v(\d+\.\d+\.?\d*)-({variant}\d+)")
111 zen_tags = github_api_request('repos/zen-kernel/zen-kernel/releases')
112 for tag in zen_tags:
113 zen_match = pattern.match(tag['tag_name'])
114 if zen_match:
115 zen_tag = zen_match.group(0)
116 zen_version = zen_match.group(1)
117 zen_suffix = zen_match.group(2)
118 break
119 old_version, old_suffix = read_file('zen-kernels.nix', variant)
120 if old_version != zen_version or old_suffix != zen_suffix:
121 zen_hash = nix_prefetch_git('https://github.com/zen-kernel/zen-kernel.git', zen_tag)
122 update_file('zen-kernels.nix', variant, zen_version, zen_suffix, zen_hash)