forked from tdm/android-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepo-bisect
executable file
·262 lines (227 loc) · 7.76 KB
/
repo-bisect
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
#!/usr/bin/python
import os
import sys
import getopt
import subprocess
import urllib2
import json
import re
import datetime
import xml.etree.ElementTree as ElementTree
cfg = dict()
cfg['verbose'] = 0
cfg['nosync'] = False
cwd = os.getcwd()
if not os.path.exists('.repo'):
sys.stderr.write("Not a repo\n")
sys.exit(1)
def verbose(s):
if cfg['verbose'] > 0:
sys.stderr.write(s)
# Valid formats:
# YYYY-MM-DD-HH-MM-SS
# YYYY-MM-DD-HH
# YYYY-MM-DD
def string_to_datetime(s):
fields = s.split('-')
if len(fields) == 6:
return datetime.datetime(int(fields[0]), int(fields[1]), int(fields[2]),
int(fields[3]), int(fields[4]), int(fields[5]))
if len(fields) == 4:
return datetime.datetime(int(fields[0]), int(fields[1]), int(fields[2]),
int(fields[3]))
if len(fields) == 3:
return datetime.datetime(int(fields[0]), int(fields[1]), int(fields[2]))
return None
def datetime_to_string(dt):
if dt.hour == 0 and dt.minute == 0 and dt.second == 0:
return dt.strftime("%Y-%m-%d")
return dt.strftime("%Y-%m-%d-%H-%M-%S")
# Find mid point between two datetime objects.
#
# Granularity is 1 hour.
#
# If the objects differ by less than 2 hours, exit.
#
# If the objects differ by at least 2 days and both hour components are
# zero, the resulting hour component will be zero.
def datetime_mid(d1, d2):
delta = (d2 - d1) / 2
if delta.total_seconds() < 3600:
print "No more dates left to test"
sys.exit(0)
mid = d1 + delta
if mid.minute != 0 or mid.second != 0:
mid = mid.replace(minute=0, second=0)
if delta.total_seconds() > 86400 and d1.hour == 0 and d2.hour == 0:
if mid.hour != 0:
mid = mid.replace(hour=0)
return mid
def git_config(key):
args = ['git', 'config', key]
child = subprocess.Popen(args, stdin=None, stdout=subprocess.PIPE, stderr=None)
out, err = child.communicate()
if child.returncode != 0:
sys.stderr.write('Failed to read git config\n')
sys.exit(1)
return out.strip()
def git_reset_hard(rev):
args = ['git', 'reset', '--hard', rev]
child = subprocess.Popen(args, stdin=None, stdout=subprocess.PIPE, stderr=None)
out, err = child.communicate()
if child.returncode != 0:
sys.stderr.write('Failed to reset git tree\n')
sys.exit(1)
def git_checkout(rev):
args = ['git', 'checkout', rev]
child = subprocess.Popen(args, stdin=None, stdout=subprocess.PIPE, stderr=None)
out, err = child.communicate()
if child.returncode != 0:
sys.stderr.write('Failed to checkout git tree\n')
sys.exit(1)
def git_rev_by_date(date, branch):
args = ['git', 'rev-list', '--max-count=1']
args.append('--until=%s' % (date))
args.append(branch)
child = subprocess.Popen(args, stdin=None, stdout=subprocess.PIPE, stderr=None)
out, err = child.communicate()
if child.returncode != 0:
sys.stderr.write('Failed to read git log\n')
sys.exit(1)
if not out:
sys.stderr.write('Failed to find rev')
sys.exit(1)
return out.strip()
def repo_manifest():
args = []
args.append('repo')
args.append('manifest')
child = subprocess.Popen(args, stdin=None, stdout=subprocess.PIPE, stderr=None)
out, err = child.communicate()
if child.returncode != 0:
sys.stderr.write('Failed to read manifest\n')
sys.exit(1)
return ElementTree.fromstring(out)
def repo_sync_to_date(date):
print "bisect: sync to %s" % (date)
# Set manifest revision
os.chdir('.repo/manifests')
remote = git_config('branch.default.remote')
branch = git_config('branch.default.merge')
manifest_rev = git_rev_by_date(date, '%s/%s' % (remote, branch))
verbose("manifest revision %s\n" % (manifest_rev))
git_reset_hard(manifest_rev)
os.chdir(cwd)
# Fetch the manifest
manifest = repo_manifest()
# Find default remote and revision
for elem in manifest.findall('default'):
def_remote = elem.get('remote')
def_revision = elem.get('revision')
if def_revision.startswith('refs/heads/'):
# refs/heads/branch => branch
def_revision = def_revision.split('/')[2]
# Update manifest revisions
for elem in manifest.findall('project'):
project_name = elem.get('name')
project_path = elem.get('path', project_name)
project_remote = elem.get('remote', def_remote)
project_revision = elem.get('revision', def_revision)
if project_revision.startswith('refs/tags'):
manifest_revision = project_revision.split('/')[2]
else:
manifest_revision = "%s/%s" % (project_remote, project_revision)
os.chdir(".repo/projects/%s.git" % (project_path))
rev = git_rev_by_date(date, manifest_revision)
os.chdir(cwd)
elem.set('revision', rev)
# Write new manifest
pathname = "%s/.repo/manifests/bisect-%s.xml" % (cwd, date)
ElementTree.ElementTree(manifest).write(pathname)
# Sync the working tree. Note:
#
# - Both "repo manifest" and "repo sync -m" include local manifests,
# so we must move the local manifests out of the way temporarily
# to avoid duplicate project errors.
#
# - "repo sync" always syncs up the main manifest (even with -m), so
# we must reset the main manifest after we sync.
have_local = os.path.exists('.repo/local_manifests')
if have_local:
os.rename('.repo/local_manifests', '.repo/local_manifests.hide')
args = ['repo', 'sync', '-l', '-m', pathname]
child = subprocess.Popen(args, stdin=None, stdout=subprocess.PIPE, stderr=None)
out, err = child.communicate()
if child.returncode != 0:
sys.stderr.write('Failed to sync\n')
if have_local:
os.rename('.repo/local_manifests.hide', '.repo/local_manifests')
os.chdir('.repo/manifests')
git_reset_hard(manifest_rev)
os.chdir(cwd)
def state_to_mid(state):
d1 = string_to_datetime(state['start'])
d2 = string_to_datetime(state['end'])
mid = datetime_mid(d1, d2)
return datetime_to_string(mid)
def repo_sync_to_mid(state):
mid = state_to_mid(state)
repo_sync_to_date(mid)
def state_read():
s = dict()
f = open('.repo/bisect', 'r')
for line in f:
k,v = line.strip().split('=')
s[k] = v
f.close()
return s
def state_write(s):
f = open('.repo/bisect', 'w')
f.write("start=%s\n" % (s['start']))
f.write("end=%s\n" % (s['end']))
f.close()
def state_delete():
os.unlink('.repo/bisect')
def usage():
print "Usage:"
print " repo-bisect [args] start yyyy-mm-dd yyyy-mm-dd"
print " repo-bisect [args] <good|bad|reset>"
print " --verbose Increase verbose level"
sys.exit(1)
### Main code ###
optargs, argv = getopt.getopt(sys.argv[1:], 'v',
['verbose', 'nosync'])
for k, v in optargs:
if k in ('-n', '--nosync'):
cfg['nosync'] = True
if k in ('-v', '--verbose'):
cfg['verbose'] += 1
if len(argv) < 1:
usage()
action = argv[0]
if action == 'start':
if len(argv) < 3:
usage()
state = dict()
state['start'] = argv[1]
state['end'] = argv[2]
state_write(state)
if not cfg['nosync']:
repo_sync_to_mid(state)
if action == 'good':
state = state_read()
state['start'] = state_to_mid(state)
state_write(state)
print "bisect: start=%s, end=%s" % (state['start'], state['end'])
if not cfg['nosync']:
repo_sync_to_mid(state)
if action == 'bad':
state = state_read()
state['end'] = state_to_mid(state)
state_write(state)
print "bisect: start=%s, end=%s" % (state['start'], state['end'])
if not cfg['nosync']:
repo_sync_to_mid(state)
if action == 'reset':
state_delete()
sys.exit(0)