1#! /usr/bin/env nix-shell
2#! nix-shell -p common-updater-scripts python3
3#! nix-shell -i python
4
5import csv
6import json
7import re
8import shlex
9import subprocess
10from os.path import abspath, dirname, splitext
11from urllib.request import urlopen
12
13# CrOS version numbers look like this:
14# [<chrome-major-version>.]<tip-build>.<branch-build>.<branch-branch-build>
15#
16# As far as I can tell, branches are where internal Google
17# modifications are added to turn Chromium OS into Chrome OS, and
18# branch branches are used for fixes for specific devices. So for
19# Chromium OS they will always be 0. This is a best guess, and is not
20# documented.
21with urlopen('https://chromiumdash.appspot.com/cros/download_serving_builds_csv?deviceCategory=ChromeOS') as resp:
22 reader = csv.reader(map(bytes.decode, resp))
23 header = reader.__next__()
24 cr_stable_index = header.index('cr_stable')
25 cros_stable_index = header.index('cros_stable')
26 chrome_version = []
27 platform_version = []
28
29 for line in reader:
30 if line[cr_stable_index] == "no update":
31 continue
32 this_chrome_version = list(map(int, line[cr_stable_index].split('.')))
33 this_platform_version = list(map(int, line[cros_stable_index].split('.')))
34 chrome_version = max(chrome_version, this_chrome_version)
35 platform_version = max(platform_version, this_platform_version)
36
37chrome_major_version = chrome_version[0]
38chromeos_tip_build = platform_version[0]
39release_branch = f'release-R{chrome_major_version}-{chromeos_tip_build}.B'
40
41# Determine the git revision.
42with urlopen(f'https://chromium.googlesource.com/chromiumos/platform/crosvm/+/refs/heads/{release_branch}?format=JSON') as resp:
43 resp.readline() # Remove )]}' header
44 rev = json.load(resp)['commit']
45
46# Determine the patch version by counting the commits that have been
47# added to the release branch since it forked off the chromeos branch.
48with urlopen(f'https://chromium.googlesource.com/chromiumos/platform/crosvm/+log/refs/heads/chromeos..{rev}?format=JSON') as resp:
49 resp.readline() # Remove )]}' header
50 branch_commits = json.load(resp)['log']
51 version = f'{chrome_major_version}.{len(branch_commits)}'
52
53# Update the version, git revision, and hash in crosvm's default.nix.
54subprocess.run(['update-source-version', 'crosvm', f'--rev={rev}', version])