1 #! /usr/bin/env nix-shell
2 #! nix-shell -i python3 -p python3 nix nix-prefetch-git
11 from datetime
import datetime
12 from urllib
.request
import urlopen
, Request
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',
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']
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
:
67 fr
'^ version = ".+"; # {variant}',
68 f
' version = "{version}"; # {variant}',
71 fr
'^ suffix = ".+"; # {variant}',
72 f
' suffix = "{suffix}"; # {variant}',
75 fr
'^ sha256 = ".+"; # {variant}',
76 f
' sha256 = "{sha256}"; # {variant}',
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}')
87 with fileinput
.FileInput(file_path
, mode
='r') as f
:
89 version_match
= re_version
.match(line
)
91 version
= version_match
.group(1)
94 suffix_match
= re_suffix
.match(line
)
96 suffix
= suffix_match
.group(1)
99 if version
and suffix
:
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')
113 zen_match
= pattern
.match(tag
['tag_name'])
115 zen_tag
= zen_match
.group(0)
116 zen_version
= zen_match
.group(1)
117 zen_suffix
= zen_match
.group(2)
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
)