Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / pkgs / development / compilers / llvm / update-git.py
blobb5e900df502bb57d070494343ae55513ea4a0d04
1 #! /usr/bin/env nix-shell
2 #! nix-shell -i python3 -p python3 nix
4 import csv
5 import fileinput
6 import json
7 import os
8 import re
9 import subprocess
10 import sys
12 from codecs import iterdecode
13 from datetime import datetime
14 from urllib.request import urlopen, Request
17 DEFAULT_NIX = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'git/default.nix')
20 def 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]
27 def 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()
34 def 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())
42 def 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)
52 def 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()
59 chromium_build = get_latest_chromium_build()
60 chromium_version = chromium_build['version']
61 print(f'chromiumDev version: {chromium_version}')
62 print('Getting LLVM commit...')
63 clang_update_script = get_file_revision(chromium_version, 'tools/clang/scripts/update.py')
64 clang_revision = re.search(r"^CLANG_REVISION = '(.+)'$", clang_update_script, re.MULTILINE).group(1)
65 clang_commit_short = re.search(r"llvmorg-[0-9]+-init-[0-9]+-g([0-9a-f]{8})", clang_revision).group(1)
66 release_version = re.search(r"^RELEASE_VERSION = '(.+)'$", clang_update_script, re.MULTILINE).group(1)
67 commit = get_commit(clang_commit_short)
68 if get_current_revision() == commit["sha"]:
69 print('No new update available.')
70 sys.exit(0)
71 date = datetime.fromisoformat(commit['commit']['committer']['date'].rstrip('Z')).date().isoformat()
72 version = f'unstable-{date}'
73 print('Prefetching source tarball...')
74 hash = nix_prefetch_url(f'https://github.com/llvm/llvm-project/archive/{commit["sha"]}.tar.gz')
75 print('Updating default.nix...')
76 with 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 = re.sub(r'^ release_version = ".+";', f' release_version = "{release_version}";', line)
81 result = re.sub(r'^ rev = ".*";', f' rev = "{commit["sha"]}";', result)
82 result = re.sub(r'^ rev-version = ".+";', f' rev-version = "{version}";', result)
83 result = re.sub(r'^ sha256 = ".+";', f' sha256 = "{hash}";', result)
84 print(result, end='')
85 # Commit the result:
86 commit_message = f"llvmPackages_git: {old_date} -> {date}"
87 subprocess.run(['git', 'add', DEFAULT_NIX], check=True)
88 subprocess.run(['git', 'commit', '--file=-'], input=commit_message.encode(), check=True)