-
Notifications
You must be signed in to change notification settings - Fork 54
/
update_v8.py
executable file
·185 lines (140 loc) · 4.67 KB
/
update_v8.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
182
183
184
185
#!/usr/bin/env python
from os.path import abspath, dirname, exists, join
import argparse
import json
import os
import re
import subprocess
import sys
DATABASE = 'update_v8.json'
options = None
dry_run = False
try:
unicode
except NameError:
unicode = str # Fix-up for python 3.
PY3 = (unicode == str)
def git(*args, **kwargs):
cmd = [options.git] + list(args)
print(' '.join(cmd))
try:
if kwargs.pop('dry_run'): return
except KeyError:
pass
if kwargs.get('check_output'):
del kwargs['check_output']
if PY3: kwargs['encoding'] = 'utf-8'
return subprocess.check_output(cmd, **kwargs).strip()
subprocess.check_call(cmd, **kwargs)
def isv8(dep):
return '' == dep['path']
def repodir(dep):
return abspath(join(options.tmpdir, 'v8', dep['path'].replace('/', '_')))
def repodir_exists(dep):
return exists(join(repodir(dep), 'config'))
def update_one(dep):
cwd = abspath('.')
url = dep['url']
path = dep['path']
branch = dep['branch']
commit = dep['commit']
clonedir = repodir(dep)
if not repodir_exists(dep):
git('clone', '--bare', url, clonedir, cwd=options.tmpdir, dry_run=dry_run)
what = commit
if isv8(dep):
what = '+refs/{}:refs/{}'.format(branch, branch)
git('fetch', url, what, cwd=clonedir, dry_run=dry_run)
def update_all():
with open(DATABASE) as fp:
deps = json.load(fp)
assert len(deps) > 0
assert isinstance(deps, list)
for dep in deps:
assert isinstance(dep, dict)
assert isinstance(dep.get('branch'), unicode)
assert isinstance(dep.get('commit'), unicode)
assert isinstance(dep.get('path'), unicode)
assert isinstance(dep.get('url'), unicode)
v8 = deps[0] # must be first
assert isv8(v8)
update_one(v8)
v8['commit'] = (
git('rev-parse', v8['branch'], check_output=True, cwd=repodir(v8)))
# Now for some arbitrary code execution...
what = '{}:DEPS'.format(v8['commit'])
source = git('show', what, check_output=True, cwd=repodir(v8))
code = compile('def Var(k): return vars[k]\ndef Str(k): return str(k)\n' + source, 'DEPS', 'exec')
globls = {}
eval(code, globls)
v8_deps = globls['deps']
assert isinstance(v8_deps, dict)
for dep in deps[1:]: # skip v8 itself
changed = options.force or not repodir_exists(dep)
path = dep['path']
url_and_commit = v8_deps.get(path)
if not url_and_commit:
raise Exception('{} missing from DEPS'.format(path))
if isinstance(url_and_commit, dict):
url_and_commit = url_and_commit.get('url')
if not isinstance(url_and_commit, str):
raise Exception('{} is not a string or dict in DEPS'.format(path))
url, commit = url_and_commit.split('@', 2)
if url != dep['url']:
print('url changed: {} -> {}'.format(dep['url'], url))
dep['url'] = url
changed = True
if commit != dep['commit']:
print('commit changed: {} -> {}'.format(dep['commit'], commit))
dep['commit'] = commit
changed = True
if changed:
update_one(dep)
arg = '-n' if dry_run else '-q'
git('rm', arg, '-r', 'v8')
for dep in deps:
cmd = '(cd {} && {} archive --format=tar --prefix=v8/{}/ {}) | {} x'.format(
repodir(dep), options.git, dep['path'], dep['commit'], options.tar)
if dry_run:
print(cmd)
else:
subprocess.check_call([cmd], shell=True)
for filename in sorted(os.listdir('patches')):
if filename.endswith('.patch'):
git('apply', '--reject', join('patches', filename), dry_run=dry_run)
for path, _, files in os.walk('v8'):
for filename in files:
if filename.endswith('.pyc'):
os.remove(join(path, filename))
git('add', '-f', 'v8', dry_run=dry_run)
git('log', '-1', '--oneline', v8['commit'], cwd=repodir(v8))
newdeps = json.dumps(deps, indent=2)
newdeps = re.sub(r'\s+$', '\n', newdeps)
if dry_run:
print(newdeps)
else:
with open(DATABASE, 'w') as fp:
fp.write(newdeps)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Sync with upstream V8')
parser.add_argument('--dry-run', default=False, action='store_true')
parser.add_argument('--force', default=False, action='store_true')
parser.add_argument('--git', default='git')
parser.add_argument('--tar', default='tar')
parser.add_argument('--tmpdir', default=os.environ.get('TMPDIR', '/tmp'))
parser.add_argument('--workspace', default=abspath(dirname(__file__)))
options = parser.parse_args()
dry_run = options.dry_run
os.chdir(options.workspace)
lockfile_name = '{}.lock'.format(DATABASE)
try:
lockfile = open(lockfile_name, 'x') # python 3
except ValueError:
lockfile = open(lockfile_name, 'wx') # python 2
try:
update_all()
finally:
try:
os.unlink(lockfile_name)
except:
pass