-
Notifications
You must be signed in to change notification settings - Fork 23
/
update.py
executable file
·181 lines (146 loc) · 5.46 KB
/
update.py
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
#!/usr/bin/env python3
import subprocess
import os
from pathlib import Path
# =============================================================================
DESTINATION = Path(__file__).parent
ALLOWLIST = DESTINATION / 'ALLOWLIST.txt'
TOOLS_GIT = DESTINATION
V8_GIT = DESTINATION / '.v8'
OUT_DIR = DESTINATION / 'gen'
OUT_DIR.mkdir(exist_ok=True)
# V8 8.6 is the first version with tools
OLDEST_VERSION=(8, 6)
# =============================================================================
def run(*command, capture=False, cwd=None):
command = list(map(str, command))
print(f'CMD: {" ".join(command)}')
stdout = subprocess.PIPE if capture else None
result = subprocess.run(command, stdout=stdout, cwd=cwd)
result.check_returncode()
if capture:
return result.stdout.decode('utf-8')
return None
def git(*command, capture=False, repository=V8_GIT):
return run('git', '-C', repository, *command, capture=capture)
class Step:
def __init__(self, title):
self.title = title
def __enter__(self):
print('=' * 80)
print("::group::" + self.title)
print('-' * 80)
def __exit__(self, type, value, tb):
print("::endgroup::")
# =============================================================================
with Step(f'Getting V8 checkout in: {V8_GIT}'):
if not V8_GIT.exists():
run('git', 'clone', '--depth=1', 'https://chromium.googlesource.com/v8/v8',
V8_GIT)
def map_branch_name(branch):
version = branch.split('-')[0]
if version == 'lkgr' or version == 'main':
return 'head'
return f"v{version}"
def filter_branch_name(name):
return name.endswith("-lkgr") or name == 'main'
with Step('List Branches'):
# Find the refs and SHA of all remote branches.
BRANCHES = git('ls-remote', '--heads', 'origin',
capture=True).rstrip().split("\n")
BRANCHES = [ref.split("\t") for ref in BRANCHES]
BRANCHES = [(branch.split('/')[-1], sha) for sha, branch in BRANCHES]
# Only keep release branches
BRANCHES = filter(lambda each: filter_branch_name(each[0]), BRANCHES)
BRANCHES = [(map_branch_name(branch), branch, sha)
for branch, sha in BRANCHES]
# Sort branches from old to new:
def branch_sort_key(version_branch_sha):
if version_branch_sha[0] == 'head':
return (float("inf"), )
return tuple(map(int, version_branch_sha[0][1:].split('.')))
BRANCHES.sort(key=branch_sort_key)
print(BRANCHES)
def filter_by_stamp(values):
version, branch, sha = values
stamp = OUT_DIR / version / '.sha'
if not stamp.exists():
print(f'{version} needs update: no stamp file {stamp}')
return True
stamp_mtime = stamp.stat().st_mtime
if stamp_mtime <= Path(__file__).stat().st_mtime:
print(f'{version} needs update: stamp file older than update script')
return True
stamp_sha = stamp.read_text()
if stamp_sha != sha:
print(
f'{version} needs update: stamp SHA does not match branch SHA ({stamp_sha} vs. {sha})'
)
return True
return False
with Step("Fetch Filtered Branches"):
# Fetch only the required branches
BRANCHES = list(filter(filter_by_stamp, BRANCHES))
# Only update the last 3 branches
BRANCHES = BRANCHES[-3:]
print(f"BRANCHES {BRANCHES}")
git("fetch", "--depth=1", "origin",
*(branch for version, branch, sha in BRANCHES))
for version, branch, sha in BRANCHES:
with Step(f'Generating Branch: {branch}'):
branch_dir = OUT_DIR / version
branch_dir.mkdir(exist_ok=True)
stamp = branch_dir / '.sha'
stamp.write_text(sha)
git('switch', '--force', '--detach', sha)
git('clean', '--force', '-d')
source = V8_GIT / 'tools'
run('rsync', '--itemize-changes', f'--include-from={ALLOWLIST}',
'--exclude=*', '--recursive', '--checksum', f'{source}{os.sep}',
f'{branch_dir}{os.sep}')
turbolizer_dir = branch_dir / 'turbolizer'
if (turbolizer_dir / 'package.json').exists():
with Step(f'Building turbolizer: {turbolizer_dir}'):
run('rm', '-rf', turbolizer_dir / 'build')
try:
run('npm', 'install', cwd=turbolizer_dir)
run('npm', 'run-script', 'build', cwd=turbolizer_dir)
# We don't need to deploy the cached node_modules folder
run('rm', '-rf', turbolizer_dir / "node_modules")
except Exception as e:
print(f'Error occured: {e}')
INDEX_HTML = OUT_DIR / "index.html"
with Step("Update versions.txt"):
versions_file = OUT_DIR / 'versions.txt'
with open(versions_file, mode='w') as f:
versions = OUT_DIR.glob('v*')
versions = list(filter(lambda file: file.name != 'versions.txt', versions))
versions.sort(
key=lambda file: list(map(int, file.name[1:].split('.')))
)
for version_dir in versions:
f.write(version_dir.name)
f.write('\n')
run("cp", DESTINATION / "index.html.template",INDEX_HTML )
GTAG = """
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-EQNXD43G9N"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-EQNXD43G9N');
</script>
"""
PLACEHOLDER = "<!-- ANALYTICS_PLACEHOLDER -->"
def inject_analytics(html_file):
with html_file.open() as f:
contents = f.read()
contents = contents.replace(PLACEHOLDER, GTAG)
with html_file.open('w+') as f:
f.write(contents)
with Step("Inject Analytics"):
inject_analytics(INDEX_HTML)
for version, branch, sha in BRANCHES:
for html_file in OUT_DIR.glob("**/*.html"):
inject_analytics(html_file)