1#! /usr/bin/env nix-shell
2#! nix-shell -i python3 -p python3 nix
3
4import csv
5import fileinput
6import json
7import os
8import re
9import subprocess
10import sys
11
12from codecs import iterdecode
13from datetime import datetime
14from urllib.request import urlopen, Request
15
16
17DEFAULT_NIX = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'default.nix')
18
19
20def get_latest_chromium_build():
21 RELEASES_URL = 'https://versionhistory.googleapis.com/v1/chrome/platforms/linux/channels/dev/versions/all/releases?filter=endtime=none&order_by=version%20desc'
22 print(f'GET {RELEASES_URL}')
23 with urlopen(RELEASES_URL) as resp:
24 return json.load(resp)['releases'][0]
25
26
27def get_file_revision(revision, file_path):
28 """Fetches the requested Git revision of the given Chromium file."""
29 url = f'https://raw.githubusercontent.com/chromium/chromium/{revision}/{file_path}'
30 with urlopen(url) as http_response:
31 return http_response.read().decode()
32
33
34def get_commit(ref):
35 url = f'https://api.github.com/repos/llvm/llvm-project/commits/{ref}'
36 headers = {'Accept': 'application/vnd.github.v3+json'}
37 request = Request(url, headers=headers)
38 with urlopen(request) as http_response:
39 return json.loads(http_response.read().decode())
40
41
42def get_current_revision():
43 """Get the current revision of llvmPackages_git."""
44 with open(DEFAULT_NIX) as f:
45 for line in f:
46 rev = re.search(r'^ rev = "(.*)";', line)
47 if rev:
48 return rev.group(1)
49 sys.exit(1)
50
51
52def nix_prefetch_url(url, algo='sha256'):
53 """Prefetches the content of the given URL."""
54 print(f'nix-prefetch-url {url}')
55 out = subprocess.check_output(['nix-prefetch-url', '--type', algo, '--unpack', url])
56 return out.decode('utf-8').rstrip()
57
58
59chromium_build = get_latest_chromium_build()
60chromium_version = chromium_build['version']
61print(f'chromiumDev version: {chromium_version}')
62print('Getting LLVM commit...')
63clang_update_script = get_file_revision(chromium_version, 'tools/clang/scripts/update.py')
64clang_revision = re.search(r"^CLANG_REVISION = '(.+)'$", clang_update_script, re.MULTILINE).group(1)
65clang_commit_short = re.search(r"llvmorg-[0-9]+-init-[0-9]+-g([0-9a-f]{8})", clang_revision).group(1)
66release_version = re.search(r"^RELEASE_VERSION = '(.+)'$", clang_update_script, re.MULTILINE).group(1)
67commit = get_commit(clang_commit_short)
68if get_current_revision() == commit["sha"]:
69 print('No new update available.')
70 sys.exit(0)
71date = datetime.fromisoformat(commit['commit']['committer']['date'].rstrip('Z')).date().isoformat()
72version = f'unstable-{date}'
73print('Prefetching source tarball...')
74hash = nix_prefetch_url(f'https://github.com/llvm/llvm-project/archive/{commit["sha"]}.tar.gz')
75print('Updating default.nix...')
76with fileinput.FileInput(DEFAULT_NIX, inplace=True) as f:
77 for line in f:
78 if match := re.search(r'^ rev-version = "unstable-(.+)";', line):
79 old_date = match.group(1)
80 result = line
81 result = re.sub(r'^ version = ".+";', f' version = "{release_version}";', result)
82 result = re.sub(r'^ rev = ".*";', f' rev = "{commit["sha"]}";', result)
83 result = re.sub(r'^ rev-version = ".+";', f' rev-version = "{version}";', result)
84 result = re.sub(r'^ sha256 = ".+";', f' sha256 = "{hash}";', result)
85 print(result, end='')
86# Commit the result:
87commit_message = f"llvmPackages_git: {old_date} -> {date}"
88subprocess.run(['git', 'add', DEFAULT_NIX], check=True)
89subprocess.run(['git', 'commit', '--file=-'], input=commit_message.encode(), check=True)