1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
|
#! /usr/bin/env nix-shell
#! nix-shell -i python -p python3.pkgs.joblib python3.pkgs.click python3.pkgs.click-log nix nix-prefetch-git nurl prefetch-yarn-deps prefetch-npm-deps
"""
electron updater
A script for updating both binary and source hashes.
It supports the following modes:
| Mode | Description |
|------------- | ----------------------------------------------- |
| `update` | for updating a specific Electron release |
| `update-all` | for updating all electron releases at once |
| `eval` | just print the necessary sources to fetch |
The `eval` and `update` commands accept an optional `--version` flag
to restrict the mechanism only to a given major release.
The `update` and `update-all` commands accept an optional `--commit`
flag to automatically commit the changes for you.
The `update` and `update-all` commands accept optional `--bin-only`
and `--source-only` flags to restict the update to binary or source
releases.
"""
import base64
import csv
import json
import logging
import os
import random
import re
import subprocess
import sys
import tempfile
import traceback
import urllib.request
from abc import ABC
from codecs import iterdecode
from datetime import datetime
from typing import Iterable, Optional, Tuple
from urllib.request import urlopen
import click
import click_log
from joblib import Parallel, delayed, Memory
depot_tools_checkout = tempfile.TemporaryDirectory()
subprocess.check_call(
[
"nix-prefetch-git",
"--builder",
"--quiet",
"--url",
"https://chromium.googlesource.com/chromium/tools/depot_tools",
"--out",
depot_tools_checkout.name,
"--rev",
"7a69b031d58081d51c9e8e89557b343bba8518b1",
]
)
sys.path.append(depot_tools_checkout.name)
import gclient_eval
import gclient_utils
# Relative path to the electron-source info.json
SOURCE_INFO_JSON = "info.json"
# Relatice path to the electron-bin info.json
BINARY_INFO_JSON = "binary/info.json"
# Number of spaces used for each indentation level
JSON_INDENT = 4
os.chdir(os.path.dirname(__file__))
memory: Memory = Memory("cache", verbose=0)
logger = logging.getLogger(__name__)
click_log.basic_config(logger)
nixpkgs_path = os.path.dirname(os.path.realpath(__file__)) + "/../../../.."
class Repo:
fetcher: str
args: dict
def __init__(self) -> None:
self.deps: dict = {}
self.hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
def get_deps(self, repo_vars: dict, path: str) -> None:
print(
"evaluating " + json.dumps(self, default=vars, sort_keys=True),
file=sys.stderr,
)
deps_file = self.get_file("DEPS")
evaluated = gclient_eval.Parse(deps_file, filename="DEPS")
repo_vars = dict(evaluated["vars"]) | repo_vars
prefix = f"{path}/" if evaluated.get("use_relative_paths", False) else ""
self.deps = {
prefix + dep_name: repo_from_dep(dep)
for dep_name, dep in evaluated["deps"].items()
if (
gclient_eval.EvaluateCondition(dep["condition"], repo_vars)
if "condition" in dep
else True
)
and repo_from_dep(dep) != None
}
for key in evaluated.get("recursedeps", []):
dep_path = prefix + key
if dep_path in self.deps and dep_path != "src/third_party/squirrel.mac":
self.deps[dep_path].get_deps(repo_vars, dep_path)
def prefetch(self) -> None:
self.hash = get_repo_hash(self.fetcher, self.args)
def prefetch_all(self) -> int:
return sum(
[dep.prefetch_all() for [_, dep] in self.deps.items()],
[delayed(self.prefetch)()],
)
def flatten_repr(self) -> dict:
return {"fetcher": self.fetcher, "hash": self.hash, **self.args}
def flatten(self, path: str) -> dict:
out = {path: self.flatten_repr()}
for dep_path, dep in self.deps.items():
out |= dep.flatten(dep_path)
return out
def get_file(self, filepath: str) -> str:
raise NotImplementedError
class GitRepo(Repo):
def __init__(self, url: str, rev: str) -> None:
super().__init__()
self.fetcher = "fetchgit"
self.args = {
"url": url,
"rev": rev,
}
class GitHubRepo(Repo):
def __init__(self, owner: str, repo: str, rev: str) -> None:
super().__init__()
self.fetcher = "fetchFromGitHub"
self.args = {
"owner": owner,
"repo": repo,
"rev": rev,
}
def get_file(self, filepath: str) -> str:
return (
urlopen(
f"https://raw.githubusercontent.com/{self.args['owner']}/{self.args['repo']}/{self.args['rev']}/{filepath}"
)
.read()
.decode("utf-8")
)
class GitilesRepo(Repo):
def __init__(self, url: str, rev: str) -> None:
super().__init__()
self.fetcher = "fetchFromGitiles"
# self.fetcher = 'fetchgit'
self.args = {
"url": url,
"rev": rev,
# "fetchSubmodules": "false",
}
if url == "https://chromium.googlesource.com/chromium/src.git":
self.args["postFetch"] = "rm -r $out/third_party/blink/web_tests; "
self.args["postFetch"] += "rm -r $out/third_party/hunspell/tests; "
self.args["postFetch"] += "rm -r $out/content/test/data; "
self.args["postFetch"] += "rm -r $out/courgette/testdata; "
self.args["postFetch"] += "rm -r $out/extensions/test/data; "
self.args["postFetch"] += "rm -r $out/media/test/data; "
def get_file(self, filepath: str) -> str:
return base64.b64decode(
urlopen(
f"{self.args['url']}/+/{self.args['rev']}/{filepath}?format=TEXT"
).read()
).decode("utf-8")
class ElectronBinRepo(GitHubRepo):
def __init__(self, owner: str, repo: str, rev: str) -> None:
super().__init__(owner, repo, rev)
self.systems = {
"i686-linux": "linux-ia32",
"x86_64-linux": "linux-x64",
"armv7l-linux": "linux-armv7l",
"aarch64-linux": "linux-arm64",
"x86_64-darwin": "darwin-x64",
"aarch64-darwin": "darwin-arm64",
}
def get_shasums256(self, version: str) -> list:
"""Returns the contents of SHASUMS256.txt"""
try:
called_process: subprocess.CompletedProcess = subprocess.run(
[
"nix-prefetch-url",
"--print-path",
f"https://github.com/electron/electron/releases/download/v{version}/SHASUMS256.txt",
],
capture_output=True,
check=True,
text=True,
)
hash_file_path = called_process.stdout.split("\n")[1]
with open(hash_file_path, "r") as f:
return f.read().split("\n")
except subprocess.CalledProcessError as err:
print(err.stderr)
sys.exit(1)
def get_headers(self, version: str) -> str:
"""Returns the hash of the release headers tarball"""
try:
called_process: subprocess.CompletedProcess = subprocess.run(
[
"nix-prefetch-url",
f"https://artifacts.electronjs.org/headers/dist/v{version}/node-v{version}-headers.tar.gz",
],
capture_output=True,
check=True,
text=True,
)
return called_process.stdout.split("\n")[0]
except subprocess.CalledProcessError as err:
print(err.stderr)
sys.exit(1)
def get_hashes(self, major_version: str) -> dict:
"""Returns a dictionary of hashes for a given major version"""
m, _ = get_latest_version(major_version)
version: str = m["version"]
out = {}
out[major_version] = {
"hashes": {},
"version": version,
}
hashes: list = self.get_shasums256(version)
for nix_system, electron_system in self.systems.items():
filename = f"*electron-v{version}-{electron_system}.zip"
if any([x.endswith(filename) for x in hashes]):
out[major_version]["hashes"][nix_system] = [
x.split(" ")[0] for x in hashes if x.endswith(filename)
][0]
out[major_version]["hashes"]["headers"] = self.get_headers(version)
return out
# Releases that have reached end-of-life no longer receive any updates
# and it is rather pointless trying to update those.
#
# https://endoflife.date/electron
@memory.cache
def supported_version_range() -> range:
"""Returns a range of electron releases that have not reached end-of-life yet"""
releases_json = json.loads(
urlopen("https://endoflife.date/api/electron.json").read()
)
supported_releases = [
int(x["cycle"])
for x in releases_json
if x["eol"] == False
or datetime.strptime(x["eol"], "%Y-%m-%d") > datetime.today()
]
return range(
min(supported_releases), # incl.
# We have also packaged the beta release in nixpkgs,
# but it is not tracked by endoflife.date
max(supported_releases) + 2, # excl.
1,
)
@memory.cache
def get_repo_hash(fetcher: str, args: dict) -> str:
expr = f'with import {nixpkgs_path} {{}};{fetcher}{{'
for key, val in args.items():
expr += f'{key}="{val}";'
expr += '}'
cmd = ["nurl", "-H", "--expr", expr]
print(" ".join(cmd), file=sys.stderr)
out = subprocess.check_output(cmd)
return out.decode("utf-8").strip()
@memory.cache
def _get_yarn_hash(path: str) -> str:
print(f"prefetch-yarn-deps", file=sys.stderr)
with tempfile.TemporaryDirectory() as tmp_dir:
with open(tmp_dir + "/yarn.lock", "w") as f:
f.write(path)
return (
subprocess.check_output(["prefetch-yarn-deps", tmp_dir + "/yarn.lock"])
.decode("utf-8")
.strip()
)
def get_yarn_hash(repo: Repo, yarn_lock_path: str = "yarn.lock") -> str:
return _get_yarn_hash(repo.get_file(yarn_lock_path))
@memory.cache
def _get_npm_hash(filename: str) -> str:
print(f"prefetch-npm-deps", file=sys.stderr)
with tempfile.TemporaryDirectory() as tmp_dir:
with open(tmp_dir + "/package-lock.json", "w") as f:
f.write(filename)
return (
subprocess.check_output(
["prefetch-npm-deps", tmp_dir + "/package-lock.json"]
)
.decode("utf-8")
.strip()
)
def get_npm_hash(repo: Repo, package_lock_path: str = "package-lock.json") -> str:
return _get_npm_hash(repo.get_file(package_lock_path))
def repo_from_dep(dep: dict) -> Optional[Repo]:
if "url" in dep:
url, rev = gclient_utils.SplitUrlRevision(dep["url"])
search_object = re.search(r"https://github.com/(.+)/(.+?)(\.git)?$", url)
if search_object:
return GitHubRepo(search_object.group(1), search_object.group(2), rev)
if re.match(r"https://.+.googlesource.com", url):
return GitilesRepo(url, rev)
return GitRepo(url, rev)
else:
# Not a git dependency; skip
return None
def get_gn_source(repo: Repo) -> dict:
gn_pattern = r"'gn_version': 'git_revision:([0-9a-f]{40})'"
gn_commit = re.search(gn_pattern, repo.get_file("DEPS")).group(1)
gn_prefetch: bytes = subprocess.check_output(
[
"nix-prefetch-git",
"--quiet",
"https://gn.googlesource.com/gn",
"--rev",
gn_commit,
]
)
gn: dict = json.loads(gn_prefetch)
return {
"gn": {
"version": datetime.fromisoformat(gn["date"]).date().isoformat(),
"url": gn["url"],
"rev": gn["rev"],
"hash": gn["hash"],
}
}
def get_latest_version(major_version: str) -> Tuple[str, str]:
"""Returns the latest version for a given major version"""
electron_releases: dict = json.loads(
urlopen("https://releases.electronjs.org/releases.json").read()
)
major_version_releases = filter(
lambda item: item["version"].startswith(f"{major_version}."), electron_releases
)
m = max(major_version_releases, key=lambda item: item["date"])
rev = f"v{m['version']}"
return (m, rev)
def get_electron_bin_info(major_version: str) -> Tuple[str, str, ElectronBinRepo]:
m, rev = get_latest_version(major_version)
electron_repo: ElectronBinRepo = ElectronBinRepo("electron", "electron", rev)
return (major_version, m, electron_repo)
def get_electron_info(major_version: str) -> Tuple[str, str, GitHubRepo]:
m, rev = get_latest_version(major_version)
electron_repo: GitHubRepo = GitHubRepo("electron", "electron", rev)
electron_repo.get_deps(
{
f"checkout_{platform}": platform == "linux"
for platform in ["ios", "chromeos", "android", "mac", "win", "linux"]
},
"src/electron",
)
return (major_version, m, electron_repo)
def get_update(repo: Tuple[str, str, Repo]) -> Tuple[str, dict]:
(major_version, m, electron_repo) = repo
tasks = electron_repo.prefetch_all()
a = lambda: (("electron_yarn_hash", get_yarn_hash(electron_repo)))
tasks.append(delayed(a)())
a = lambda: (
(
"chromium_npm_hash",
get_npm_hash(
electron_repo.deps["src"], "third_party/node/package-lock.json"
),
)
)
tasks.append(delayed(a)())
random.shuffle(tasks)
task_results = {
n[0]: n[1]
for n in Parallel(n_jobs=3, require="sharedmem", return_as="generator")(tasks)
if n != None
}
tree = electron_repo.flatten("src/electron")
return (
f"{major_version}",
{
"deps": tree,
**{key: m[key] for key in ["version", "modules", "chrome", "node"]},
"chromium": {
"version": m["chrome"],
"deps": get_gn_source(electron_repo.deps["src"]),
},
**task_results,
},
)
def load_info_json(path: str) -> dict:
"""Load the contents of a JSON file
Args:
path: The path to the JSON file
Returns: An empty dict if the path does not exist, otherwise the contents of the JSON file.
"""
try:
with open(path, "r") as f:
return json.loads(f.read())
except:
return {}
def save_info_json(path: str, content: dict) -> None:
"""Saves the given info to a JSON file
Args:
path: The path where the info should be saved
content: The content to be saved as JSON.
"""
with open(path, "w") as f:
f.write(json.dumps(content, indent=JSON_INDENT, default=vars, sort_keys=True))
f.write("\n")
def update_bin(major_version: str, commit: bool) -> None:
"""Update a given electron-bin release
Args:
major_version: The major version number, e.g. '27'
commit: Whether the updater should commit the result
"""
package_name = f"electron_{major_version}-bin"
print(f"Updating {package_name}")
electron_bin_info = get_electron_bin_info(major_version)
(_major_version, _version, repo) = electron_bin_info
old_info = load_info_json(BINARY_INFO_JSON)
new_info = repo.get_hashes(major_version)
out = old_info | new_info
save_info_json(BINARY_INFO_JSON, out)
old_version = (
old_info[major_version]["version"] if major_version in old_info else None
)
new_version = new_info[major_version]["version"]
if old_version == new_version:
print(f"{package_name} is up-to-date")
elif commit:
commit_result(package_name, old_version, new_version, BINARY_INFO_JSON)
def update_source(major_version: str, commit: bool) -> None:
"""Update a given electron-source release
Args:
major_version: The major version number, e.g. '27'
commit: Whether the updater should commit the result
"""
package_name = f"electron-source.electron_{major_version}"
print(f"Updating electron-source.electron_{major_version}")
old_info = load_info_json(SOURCE_INFO_JSON)
old_version = (
old_info[str(major_version)]["version"]
if str(major_version) in old_info
else None
)
electron_source_info = get_electron_info(major_version)
new_info = get_update(electron_source_info)
out = old_info | {new_info[0]: new_info[1]}
save_info_json(SOURCE_INFO_JSON, out)
new_version = new_info[1]["version"]
if old_version == new_version:
print(f"{package_name} is up-to-date")
elif commit:
commit_result(package_name, old_version, new_version, SOURCE_INFO_JSON)
def non_eol_releases(releases: Iterable[int]) -> Iterable[int]:
"""Returns a list of releases that have not reached end-of-life yet."""
return tuple(filter(lambda x: x in supported_version_range(), releases))
def update_all_source(commit: bool) -> None:
"""Update all eletron-source releases at once
Args:
commit: Whether to commit the result
"""
old_info = load_info_json(SOURCE_INFO_JSON)
filtered_releases = non_eol_releases(tuple(map(lambda x: int(x), old_info.keys())))
# This might take some time
repos = Parallel(n_jobs=2, require="sharedmem")(
delayed(get_electron_info)(major_version) for major_version in filtered_releases
)
new_info = {
n[0]: n[1]
for n in Parallel(n_jobs=2, require="sharedmem")(
delayed(get_update)(repo) for repo in repos
)
}
if commit:
for major_version in filtered_releases:
# Since the sources have been fetched at this point already,
# fetching them again will be much faster.
update_source(str(major_version), commit)
else:
out = old_info | {new_info[0]: new_info[1]}
save_info_json(SOURCE_INFO_JSON, out)
def parse_cve_numbers(tag_name: str) -> Iterable[str]:
"""Returns mentioned CVE numbers from a given release tag"""
cve_pattern = r"CVE-\d{4}-\d+"
url = f"https://api.github.com/repos/electron/electron/releases/tags/{tag_name}"
headers = {
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
request = urllib.request.Request(url=url, headers=headers)
release_note = ""
try:
with urlopen(request) as response:
release_note = json.loads(response.read().decode("utf-8"))["body"]
except:
print(
f"WARN: Fetching release note for {tag_name} from GitHub failed!",
file=sys.stderr,
)
return sorted(re.findall(cve_pattern, release_note))
def commit_result(
package_name: str, old_version: Optional[str], new_version: str, path: str
) -> None:
"""Creates a git commit with a short description of the change
Args:
package_name: The package name, e.g. `electron-source.electron-{major_version}`
or `electron_{major_version}-bin`
old_version: Version number before the update.
Can be left empty when initializing a new release.
new_version: Version number after the update.
path: Path to the lockfile to be committed
"""
assert (
isinstance(package_name, str) and len(package_name) > 0
), "Argument `package_name` cannot be empty"
assert (
isinstance(new_version, str) and len(new_version) > 0
), "Argument `new_version` cannot be empty"
if old_version != new_version:
major_version = new_version.split(".")[0]
cve_fixes_text = "\n".join(
list(
map(lambda cve: f"- Fixes {cve}", parse_cve_numbers(f"v{new_version}"))
)
)
init_msg = f"init at {new_version}"
update_msg = f"{old_version} -> {new_version}"
diff = f"- Diff: https://github.com/electron/electron/compare/refs/tags/v{old_version}...v{new_version}\n" if old_version != None else ""
commit_message = f"""{package_name}: {update_msg if old_version != None else init_msg}
- Changelog: https://github.com/electron/electron/releases/tag/v{new_version}
{diff}{cve_fixes_text}
"""
subprocess.run(
[
"git",
"add",
path,
]
)
subprocess.run(
[
"git",
"commit",
"-m",
commit_message,
]
)
@click.group()
def cli() -> None:
"""A script for updating electron-bin and electron-source hashes"""
pass
@cli.command(
"eval", help="Print the necessary sources to fetch for a given major release"
)
@click.option("--version", help="The major version, e.g. '23'")
def eval(version):
(_, _, repo) = electron_repo = get_electron_info(version)
tree = repo.flatten("src/electron")
print(json.dumps(tree, indent=JSON_INDENT, default=vars, sort_keys=True))
@cli.command("update", help="Update a single major release")
@click.option("-v", "--version", help="The major version, e.g. '23'")
@click.option(
"-b",
"--bin-only",
is_flag=True,
default=False,
help="Only update electron-bin packages",
)
@click.option(
"-s",
"--source-only",
is_flag=True,
default=False,
help="Only update electron-source packages",
)
@click.option("-c", "--commit", is_flag=True, default=False, help="Commit the result")
def update(version: str, bin_only: bool, source_only: bool, commit: bool) -> None:
assert isinstance(version, str) and len(version) > 0, "version must be non-empty"
if bin_only and source_only:
print(
"Error: Omit --bin-only and --source-only if you want to update both source and binary packages.",
file=sys.stderr,
)
sys.exit(1)
elif bin_only:
update_bin(version, commit)
elif source_only:
update_source(version, commit)
else:
update_bin(version, commit)
update_source(version, commit)
@cli.command("update-all", help="Update all releases at once")
@click.option(
"-b",
"--bin-only",
is_flag=True,
default=False,
help="Only update electron-bin packages",
)
@click.option(
"-s",
"--source-only",
is_flag=True,
default=False,
help="Only update electron-source packages",
)
@click.option("-c", "--commit", is_flag=True, default=False, help="Commit the result")
def update_all(bin_only: bool, source_only: bool, commit: bool) -> None:
# Filter out releases that have reached end-of-life
filtered_bin_info = dict(
filter(
lambda entry: int(entry[0]) in supported_version_range(),
load_info_json(BINARY_INFO_JSON).items(),
)
)
if bin_only and source_only:
print(
"Error: omit --bin-only and --source-only if you want to update both source and binary packages.",
file=sys.stderr,
)
sys.exit(1)
elif bin_only:
for major_version, _ in filtered_bin_info.items():
update_bin(major_version, commit)
elif source_only:
update_all_source(commit)
else:
for major_version, _ in filtered_bin_info.items():
update_bin(major_version, commit)
update_all_source(commit)
if __name__ == "__main__":
cli()
|