@@ -0,0 +1,85 @@
1+#! /usr/bin/env nix-shell
2+#! nix-shell -p python3 -p nix-prefetch-git -i python
3+
4+import base64
5+import csv
6+import json
7+import re
8+import subprocess
9+import xml.etree.ElementTree as ElementTree
10+from codecs import iterdecode
11+from operator import itemgetter
12+from os.path import dirname, splitext
13+from urllib.request import urlopen
14+
15+# ChromiumOS components required to build crosvm.
16+components = ['chromiumos/platform/crosvm', 'chromiumos/third_party/adhd']
17+
18+git_root = 'https://chromium.googlesource.com/'
19+manifest_versions = f'{git_root}chromiumos/manifest-versions'
20+buildspecs_url = f'{manifest_versions}/+/refs/heads/master/paladin/buildspecs/'
21+
22+# CrOS version numbers look like this:
23+# [<chrome-major-version>.]<tip-build>.<branch-build>.<branch-branch-build>
24+#
25+# As far as I can tell, branches are where internal Google
26+# modifications are added to turn Chromium OS into Chrome OS, and
27+# branch branches are used for fixes for specific devices. So for
28+# Chromium OS they will always be 0. This is a best guess, and is not
29+# documented.
30+with urlopen('https://cros-omahaproxy.appspot.com/all') as resp:
31+ versions = csv.DictReader(iterdecode(resp, 'utf-8'))
32+ stables = filter(lambda v: v['track'] == 'stable-channel', versions)
33+ stable = sorted(stables, key=itemgetter('chrome_version'), reverse=True)[0]
34+
35+chrome_major_version = re.match(r'\d+', stable['chrome_version'])[0]
36+chromeos_tip_build = re.match(r'\d+', stable['chromeos_version'])[0]
37+
38+# Find the most recent buildspec for the stable Chrome version and
39+# Chromium OS build number. Its branch build and branch branch build
40+# numbers will (almost?) certainly be 0. It will then end with an rc
41+# number -- presumably these are release candidates, one of which
42+# becomes the final release. Presumably the one with the highest rc
43+# number.
44+with urlopen(f'{buildspecs_url}{chrome_major_version}/?format=TEXT') as resp:
45+ listing = base64.decodebytes(resp.read()).decode('utf-8')
46+ buildspecs = [(line.split('\t', 1)[1]) for line in listing.splitlines()]
47+ buildspecs = [s for s in buildspecs if s.startswith(chromeos_tip_build)]
48+ buildspecs.sort(reverse=True)
49+ buildspec = splitext(buildspecs[0])[0]
50+
51+revisions = {}
52+
53+# Read the buildspec, and extract the git revisions for each component.
54+with urlopen(f'{buildspecs_url}{chrome_major_version}/{buildspec}.xml?format=TEXT') as resp:
55+ xml = base64.decodebytes(resp.read()).decode('utf-8')
56+ root = ElementTree.fromstring(xml)
57+ for project in root.findall('project'):
58+ revisions[project.get('name')] = project.get('revision')
59+
60+# Initialize the data that will be output from this script. Leave the
61+# rc number in buildspec so nobody else is subject to the same level
62+# of confusion I have been.
63+data = {'version': f'{chrome_major_version}.{buildspec}', 'components': {}}
64+
65+# Fill in the 'components' dictionary with the output from
66+# nix-prefetch-git, which can be passed straight to fetchGit when
67+# imported by Nix.
68+for component in components:
69+ argv = ['nix-prefetch-git',
70+ '--url', git_root + component,
71+ '--rev', revisions[component]]
72+
73+ output = subprocess.check_output(argv)
74+ data['components'][component] = json.loads(output.decode('utf-8'))
75+
76+# Find the path to crosvm's default.nix, so the srcs data can be
77+# written into the same directory.
78+argv = ['nix-instantiate', '--eval', '--json', '-A', 'crosvm.meta.position']
79+position = json.loads(subprocess.check_output(argv).decode('utf-8'))
80+filename = re.match(r'[^:]*', position)[0]
81+
82+# Finally, write the output.
83+with open(dirname(filename) + '/upstream-info.json', 'w') as out:
84+ json.dump(data, out, indent=2)
85+ out.write('\n')