blob: e8c13c2b8f04d2c5fbad0d407e2d8047844f603f [file] [log] [blame]
Kuang-che Wu875c89a2020-01-08 14:30:55 +08001#!/usr/bin/env python3
Kuang-che Wu41e8b592018-09-25 17:01:30 +08002# -*- coding: utf-8 -*-
3# Copyright 2018 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6"""Helper script to prepare source trees for ChromeOS bisection.
7
8Typical usage:
9
10 Initial setup:
11 $ %(prog)s init --chromeos
12 $ %(prog)s init --chrome
13 $ %(prog)s init --android=pi-arc-dev
14
15 Sync code if necessary:
16 $ %(prog)s sync
17
18 Create source trees for bisection
19 $ %(prog)s new --session=12345
20
21 After bisection finished, delete trees
22 $ %(prog)s delete --session=12345
23"""
24from __future__ import print_function
25import argparse
26import csv
Zheng-Jie Changcd424c12020-01-10 14:32:08 +080027import glob
Kuang-che Wu999893c2020-04-13 22:06:22 +080028import io
Kuang-che Wu41e8b592018-09-25 17:01:30 +080029import logging
30import os
Kuang-che Wu22f207e2019-02-23 12:53:53 +080031import subprocess
Kuang-che Wu67be74b2018-10-15 14:17:26 +080032import time
Kuang-che Wu7d0c7592019-09-16 09:59:28 +080033import xml.etree.ElementTree
Kuang-che Wu999893c2020-04-13 22:06:22 +080034import urllib.parse
35import urllib.request
Kuang-che Wua7ddf9b2019-11-25 18:59:57 +080036
Kuang-che Wu41e8b592018-09-25 17:01:30 +080037from bisect_kit import common
38from bisect_kit import configure
39from bisect_kit import gclient_util
40from bisect_kit import git_util
Kuang-che Wudc714412018-10-17 16:06:39 +080041from bisect_kit import locking
Kuang-che Wu41e8b592018-09-25 17:01:30 +080042from bisect_kit import repo_util
43from bisect_kit import util
44
45DEFAULT_MIRROR_BASE = os.path.expanduser('~/git-mirrors')
46DEFAULT_WORK_BASE = os.path.expanduser('~/bisect-workdir')
47CHECKOUT_TEMPLATE_NAME = 'template'
Kuang-che Wucc2870b2021-02-18 15:36:00 +080048MANIFEST_FOR_DELETED = 'deleted-repos.xml'
Kuang-che Wu41e8b592018-09-25 17:01:30 +080049
50logger = logging.getLogger(__name__)
51
52
Kuang-che Wu23192ad2020-03-11 18:12:46 +080053class DefaultProjectPathFactory:
Kuang-che Wu41e8b592018-09-25 17:01:30 +080054 """Factory for chromeos/chrome/android source tree paths."""
55
56 def __init__(self, mirror_base, work_base, session):
57 self.mirror_base = mirror_base
58 self.work_base = work_base
59 self.session = session
60
61 def get_chromeos_mirror(self):
62 return os.path.join(self.mirror_base, 'chromeos')
63
64 def get_chromeos_tree(self):
65 return os.path.join(self.work_base, self.session, 'chromeos')
66
67 def get_android_mirror(self, branch):
68 return os.path.join(self.mirror_base, 'android.%s' % branch)
69
70 def get_android_tree(self, branch):
71 return os.path.join(self.work_base, self.session, 'android.%s' % branch)
72
73 def get_chrome_cache(self):
74 return os.path.join(self.mirror_base, 'chrome')
75
76 def get_chrome_tree(self):
77 return os.path.join(self.work_base, self.session, 'chrome')
78
79
80def subvolume_or_makedirs(opts, path):
81 if os.path.exists(path):
82 return
83
84 path = os.path.abspath(path)
85 if opts.btrfs:
86 dirname, basename = os.path.split(path)
87 if not os.path.exists(dirname):
88 os.makedirs(dirname)
89 util.check_call('btrfs', 'subvolume', 'create', basename, cwd=dirname)
90 else:
91 os.makedirs(path)
92
93
94def is_btrfs_subvolume(path):
95 if util.check_output('stat', '-f', '--format=%T', path).strip() != 'btrfs':
96 return False
97 return util.check_output('stat', '--format=%i', path).strip() == '256'
98
99
100def snapshot_or_copytree(src, dst):
101 assert os.path.isdir(src), '%s does not exist' % src
102 assert os.path.isdir(os.path.dirname(dst))
103
104 # Make sure dst do not exist, otherwise it becomes "dst/name" (one extra
105 # depth) instead of "dst".
106 assert not os.path.exists(dst)
107
108 if is_btrfs_subvolume(src):
109 util.check_call('btrfs', 'subvolume', 'snapshot', src, dst)
110 else:
111 # -a for recursion and preserve all attributes.
112 util.check_call('cp', '-a', src, dst)
113
114
Kuang-che Wubfa64482018-10-16 11:49:49 +0800115def collect_removed_manifest_repos(repo_dir, last_sync_time, only_branch=None):
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800116 manifest_dir = os.path.join(repo_dir, '.repo', 'manifests')
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800117 manifest_path = 'default.xml'
118 manifest_full_path = os.path.join(manifest_dir, manifest_path)
119 # hack for chromeos symlink
120 if os.path.islink(manifest_full_path):
121 manifest_path = os.readlink(manifest_full_path)
122
123 parser = repo_util.ManifestParser(manifest_dir)
Kuang-che Wudedc5922020-12-17 17:19:23 +0800124 git_rev = git_util.get_commit_hash(manifest_dir, 'HEAD')
125 root = parser.parse_xml_recursive(git_rev, manifest_path)
126 latest_all = parser.process_parsed_result(root, group_constraint='all')
127 latest_default = parser.process_parsed_result(
128 root, group_constraint='default')
129
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800130 removed = {}
131 for _, git_rev in reversed(
132 parser.enumerate_manifest_commits(last_sync_time, None, manifest_path)):
Kuang-che Wu7d0c7592019-09-16 09:59:28 +0800133 try:
134 root = parser.parse_xml_recursive(git_rev, manifest_path)
135 except xml.etree.ElementTree.ParseError:
136 logger.warning('%s %s@%s syntax error, skip', manifest_dir, manifest_path,
137 git_rev[:12])
138 continue
Kuang-che Wubfa64482018-10-16 11:49:49 +0800139 if (only_branch and root.find('default') is not None and
140 root.find('default').get('revision') != only_branch):
141 break
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800142 entries = parser.process_parsed_result(root)
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800143 for path, path_spec in entries.items():
Kuang-che Wudedc5922020-12-17 17:19:23 +0800144 if path in latest_default:
145 continue
146 if path in latest_all:
147 logger.warning(
148 'path=%s was removed from default group; assume skip is harmless',
149 path)
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800150 continue
151 if path in removed:
152 continue
153 removed[path] = path_spec
154
155 return removed
156
157
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800158def setup_chromeos_repos(opts, path_factory):
159 chromeos_mirror = path_factory.get_chromeos_mirror()
160 chromeos_tree = path_factory.get_chromeos_tree()
161 subvolume_or_makedirs(opts, chromeos_mirror)
162 subvolume_or_makedirs(opts, chromeos_tree)
163
164 manifest_url = (
165 'https://chrome-internal.googlesource.com/chromeos/manifest-internal')
166 repo_url = 'https://chromium.googlesource.com/external/repo.git'
167
168 if os.path.exists(os.path.join(chromeos_mirror, '.repo', 'manifests')):
169 logger.warning(
170 '%s has already been initialized, assume it is setup properly',
171 chromeos_mirror)
172 else:
173 logger.info('repo init for chromeos mirror')
174 repo_util.init(
175 chromeos_mirror,
176 manifest_url=manifest_url,
177 repo_url=repo_url,
178 mirror=True)
179
Kuang-che Wucc2870b2021-02-18 15:36:00 +0800180 local_manifest_dir = os.path.join(chromeos_mirror,
181 repo_util.LOCAL_MANIFESTS_DIR)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800182 os.mkdir(local_manifest_dir)
183 with open(os.path.join(local_manifest_dir, 'manifest-versions.xml'),
184 'w') as f:
Kuang-che Wuae6824b2019-08-27 22:20:01 +0800185 f.write("""<?xml version="1.0" encoding="UTF-8"?>
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800186 <manifest>
187 <project name="chromeos/manifest-versions" remote="cros-internal" />
188 </manifest>
Kuang-che Wuae6824b2019-08-27 22:20:01 +0800189 """)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800190
191 logger.info('repo init for chromeos tree')
192 repo_util.init(
193 chromeos_tree,
194 manifest_url=manifest_url,
195 repo_url=repo_url,
196 reference=chromeos_mirror)
197
Kuang-che Wudc714412018-10-17 16:06:39 +0800198 with locking.lock_file(
199 os.path.join(chromeos_mirror, locking.LOCK_FILE_FOR_MIRROR_SYNC)):
200 logger.info('repo sync for chromeos mirror (this takes hours; be patient)')
201 repo_util.sync(chromeos_mirror)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800202
203 logger.info('repo sync for chromeos tree')
204 repo_util.sync(chromeos_tree)
205
206
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800207def read_last_sync_time(repo_dir):
208 timestamp_path = os.path.join(repo_dir, 'last_sync_time')
209 if os.path.exists(timestamp_path):
210 with open(timestamp_path) as f:
211 return int(f.read())
212 else:
213 # 4 months should be enough for most bisect cases.
214 return int(time.time()) - 86400 * 120
215
216
217def write_sync_time(repo_dir, sync_time):
218 timestamp_path = os.path.join(repo_dir, 'last_sync_time')
219 with open(timestamp_path, 'w') as f:
220 f.write('%d\n' % sync_time)
221
222
223def write_extra_manifest_to_mirror(repo_dir, removed):
Kuang-che Wucc2870b2021-02-18 15:36:00 +0800224 local_manifest_dir = os.path.join(repo_dir, repo_util.LOCAL_MANIFESTS_DIR)
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800225 if not os.path.exists(local_manifest_dir):
226 os.mkdir(local_manifest_dir)
Kuang-che Wucc2870b2021-02-18 15:36:00 +0800227 with open(os.path.join(local_manifest_dir, MANIFEST_FOR_DELETED), 'w') as f:
Kuang-che Wuae6824b2019-08-27 22:20:01 +0800228 f.write("""<?xml version="1.0" encoding="UTF-8"?>\n<manifest>\n""")
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800229 remotes = {}
230 for path_spec in removed.values():
Kuang-che Wua7ddf9b2019-11-25 18:59:57 +0800231 scheme, netloc, remote_path = urllib.parse.urlsplit(
232 path_spec.repo_url)[:3]
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800233 assert remote_path[0] == '/'
234 remote_path = remote_path[1:]
235 if (scheme, netloc) not in remotes:
236 remote_name = 'remote_for_deleted_repo_%s' % (scheme + netloc)
237 remotes[scheme, netloc] = remote_name
Kuang-che Wuae6824b2019-08-27 22:20:01 +0800238 f.write(""" <remote name="%s" fetch="%s" />\n""" %
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800239 (remote_name, '%s://%s' % (scheme, netloc)))
Kuang-che Wubfa64482018-10-16 11:49:49 +0800240 f.write(
Kuang-che Wuae6824b2019-08-27 22:20:01 +0800241 """ <project name="%s" path="%s" remote="%s" revision="%s" />\n""" %
Kuang-che Wubfa64482018-10-16 11:49:49 +0800242 (remote_path, path_spec.path, remotes[scheme, netloc], path_spec.at))
Kuang-che Wuae6824b2019-08-27 22:20:01 +0800243 f.write("""</manifest>\n""")
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800244
245
Kuang-che Wucc2870b2021-02-18 15:36:00 +0800246def delete_extra_manifest(repo_dir):
247 path = os.path.join(repo_dir, repo_util.LOCAL_MANIFESTS_DIR,
248 MANIFEST_FOR_DELETED)
249 if os.path.exists(path):
250 os.unlink(path)
251
252
Kuang-che Wubfa64482018-10-16 11:49:49 +0800253def generate_extra_manifest_for_deleted_repo(repo_dir, only_branch=None):
254 last_sync_time = read_last_sync_time(repo_dir)
255 removed = collect_removed_manifest_repos(
256 repo_dir, last_sync_time, only_branch=only_branch)
257 write_extra_manifest_to_mirror(repo_dir, removed)
258 logger.info('since last sync, %d repo got removed', len(removed))
Kuang-che Wub76b7f62019-09-16 10:06:18 +0800259 return len(removed)
Kuang-che Wubfa64482018-10-16 11:49:49 +0800260
261
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800262def sync_chromeos_code(opts, path_factory):
263 del opts # unused
264
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800265 start_sync_time = int(time.time())
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800266 chromeos_mirror = path_factory.get_chromeos_mirror()
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800267
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800268 logger.info('repo sync for chromeos mirror')
Kuang-che Wucc2870b2021-02-18 15:36:00 +0800269 delete_extra_manifest(chromeos_mirror)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800270 repo_util.sync(chromeos_mirror)
Kuang-che Wub76b7f62019-09-16 10:06:18 +0800271 # If there are repos deleted after last sync, generate custom manifest and
272 # sync again for those repos. So we can mirror commits just before the repo
273 # deletion.
274 if generate_extra_manifest_for_deleted_repo(chromeos_mirror) != 0:
275 logger.info('repo sync again')
276 repo_util.sync(chromeos_mirror)
Kuang-che Wua7f29352020-03-02 17:07:53 +0800277
278 # Work around for b/149883148: 'repo' tool does not fetch all branches in
279 # mirror mode.
280 chromiumos_overlay_mirror = os.path.join(
281 chromeos_mirror, 'chromiumos/overlays/chromiumos-overlay.git')
282 git_util.fetch(chromiumos_overlay_mirror, 'cros')
283
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800284 write_sync_time(chromeos_mirror, start_sync_time)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800285
286 logger.info('repo sync for chromeos tree')
287 chromeos_tree = path_factory.get_chromeos_tree()
288 repo_util.sync(chromeos_tree)
289
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800290
291def query_chrome_latest_branch():
Kuang-che Wud3a4e842019-12-11 12:15:23 +0800292 result = 0
Kuang-che Wua7ddf9b2019-11-25 18:59:57 +0800293 r = urllib.request.urlopen('https://omahaproxy.appspot.com/all')
Kuang-che Wu6bf6ac32020-04-15 01:14:32 +0800294 for row in csv.DictReader(io.TextIOWrapper(r, encoding='utf-8')):
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800295 if row['true_branch'].isdigit():
296 result = max(result, int(row['true_branch']))
297 return result
298
299
300def setup_chrome_repos(opts, path_factory):
301 chrome_cache = path_factory.get_chrome_cache()
302 subvolume_or_makedirs(opts, chrome_cache)
303 chrome_tree = path_factory.get_chrome_tree()
304 subvolume_or_makedirs(opts, chrome_tree)
305
306 latest_branch = query_chrome_latest_branch()
307 logger.info('latest chrome branch is %d', latest_branch)
308 assert latest_branch
Kuang-che Wuae6824b2019-08-27 22:20:01 +0800309 spec = """
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800310solutions = [
311 { "name" : "buildspec",
312 "url" : "https://chrome-internal.googlesource.com/a/chrome/tools/buildspec.git",
313 "deps_file" : "branches/%d/DEPS",
314 "custom_deps" : {
315 },
316 "custom_vars": {'checkout_src_internal': True},
317 },
318]
319target_os = ['chromeos']
320cache_dir = %r
Kuang-che Wuae6824b2019-08-27 22:20:01 +0800321""" % (latest_branch, chrome_cache)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800322
Kuang-che Wudc714412018-10-17 16:06:39 +0800323 with locking.lock_file(
324 os.path.join(chrome_cache, locking.LOCK_FILE_FOR_MIRROR_SYNC)):
325 logger.info('gclient config for chrome')
326 gclient_util.config(chrome_tree, spec=spec)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800327
Kuang-che Wudc714412018-10-17 16:06:39 +0800328 is_first_sync = not os.listdir(chrome_cache)
329 if is_first_sync:
330 logger.info('gclient sync for chrome (this takes hours; be patient)')
331 else:
332 logger.info('gclient sync for chrome')
Kuang-che Wu6ee24b52020-11-02 11:33:49 +0800333 gclient_util.sync(chrome_tree, with_branch_heads=True, with_tags=True)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800334
Kuang-che Wudc714412018-10-17 16:06:39 +0800335 # It's possible that some repos are removed from latest branch and thus
336 # their commit history is not fetched in recent gclient sync. So we call
337 # 'git fetch' for all existing git mirrors.
338 # TODO(kcwu): only sync repos not in DEPS files of latest branch
339 logger.info('additional sync for chrome mirror')
340 for git_repo_name in os.listdir(chrome_cache):
341 # another gclient is running or leftover of previous run; skip
342 if git_repo_name.startswith('_cache_tmp'):
343 continue
344 git_repo = os.path.join(chrome_cache, git_repo_name)
Kuang-che Wu08366542019-01-12 12:37:49 +0800345 if not git_util.is_git_bare_dir(git_repo):
Kuang-che Wudc714412018-10-17 16:06:39 +0800346 continue
Kuang-che Wu2b1286b2019-05-20 20:37:26 +0800347 git_util.fetch(git_repo)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800348
Kuang-che Wu1e49f512018-12-06 15:27:42 +0800349 # Some repos were removed from the DEPS and won't be synced here. They will
350 # be synced during DEPS file processing because the necessary information
351 # requires full DEPS parsing. (crbug.com/902238)
352
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800353
354def sync_chrome_code(opts, path_factory):
355 # The sync step is identical to the initial gclient config step.
356 setup_chrome_repos(opts, path_factory)
357
358
359def setup_android_repos(opts, path_factory, branch):
360 android_mirror = path_factory.get_android_mirror(branch)
361 android_tree = path_factory.get_android_tree(branch)
362 subvolume_or_makedirs(opts, android_mirror)
363 subvolume_or_makedirs(opts, android_tree)
364
365 manifest_url = ('persistent-https://googleplex-android.git.corp.google.com'
366 '/platform/manifest')
367 repo_url = 'https://gerrit.googlesource.com/git-repo'
368
369 if os.path.exists(os.path.join(android_mirror, '.repo', 'manifests')):
370 logger.warning(
371 '%s has already been initialized, assume it is setup properly',
372 android_mirror)
373 else:
374 logger.info('repo init for android mirror branch=%s', branch)
375 repo_util.init(
376 android_mirror,
377 manifest_url=manifest_url,
378 repo_url=repo_url,
379 manifest_branch=branch,
380 mirror=True)
381
382 logger.info('repo init for android tree branch=%s', branch)
383 repo_util.init(
384 android_tree,
385 manifest_url=manifest_url,
386 repo_url=repo_url,
387 manifest_branch=branch,
388 reference=android_mirror)
389
390 logger.info('repo sync for android mirror (this takes hours; be patient)')
391 repo_util.sync(android_mirror, current_branch=True)
392
393 logger.info('repo sync for android tree branch=%s', branch)
394 repo_util.sync(android_tree, current_branch=True)
395
396
397def sync_android_code(opts, path_factory, branch):
398 del opts # unused
Kuang-che Wu67be74b2018-10-15 14:17:26 +0800399 start_sync_time = int(time.time())
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800400 android_mirror = path_factory.get_android_mirror(branch)
401 android_tree = path_factory.get_android_tree(branch)
402
Kuang-che Wudc714412018-10-17 16:06:39 +0800403 with locking.lock_file(
404 os.path.join(android_mirror, locking.LOCK_FILE_FOR_MIRROR_SYNC)):
405 logger.info('repo sync for android mirror branch=%s', branch)
Kuang-che Wucc2870b2021-02-18 15:36:00 +0800406 delete_extra_manifest(android_mirror)
Kuang-che Wub76b7f62019-09-16 10:06:18 +0800407 repo_util.sync(android_mirror, current_branch=True)
Kuang-che Wudc714412018-10-17 16:06:39 +0800408 # Android usually big jump between milestone releases and add/delete lots of
409 # repos when switch releases. Because it's infeasible to bisect between such
410 # big jump, the deleted repo is useless. In order to save disk, do not sync
411 # repos deleted in other branches.
Kuang-che Wub76b7f62019-09-16 10:06:18 +0800412 if generate_extra_manifest_for_deleted_repo(
413 android_mirror, only_branch=branch) != 0:
414 logger.info('repo sync again')
415 repo_util.sync(android_mirror, current_branch=True)
Kuang-che Wudc714412018-10-17 16:06:39 +0800416 write_sync_time(android_mirror, start_sync_time)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800417
418 logger.info('repo sync for android tree branch=%s', branch)
419 repo_util.sync(android_tree, current_branch=True)
420
421
422def cmd_init(opts):
423 path_factory = DefaultProjectPathFactory(opts.mirror_base, opts.work_base,
424 CHECKOUT_TEMPLATE_NAME)
425
426 if opts.chromeos:
427 setup_chromeos_repos(opts, path_factory)
428 if opts.chrome:
429 setup_chrome_repos(opts, path_factory)
430 for branch in opts.android:
431 setup_android_repos(opts, path_factory, branch)
432
433
434def enumerate_android_branches_available(base):
435 branches = []
436 for name in os.listdir(base):
437 if name.startswith('android.'):
438 branches.append(name.partition('.')[2])
439 return branches
440
441
Kuang-che Wu22f207e2019-02-23 12:53:53 +0800442def do_sync(opts):
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800443 path_factory = DefaultProjectPathFactory(opts.mirror_base, opts.work_base,
444 CHECKOUT_TEMPLATE_NAME)
445
446 sync_all = False
447 if not opts.chromeos and not opts.chrome and not opts.android:
448 logger.info('sync trees for all')
449 sync_all = True
450
451 if sync_all or opts.chromeos:
452 sync_chromeos_code(opts, path_factory)
453 if sync_all or opts.chrome:
454 sync_chrome_code(opts, path_factory)
455
456 if sync_all:
457 android_branches = enumerate_android_branches_available(opts.mirror_base)
458 else:
459 android_branches = opts.android
460 for branch in android_branches:
461 sync_android_code(opts, path_factory, branch)
462
463
Kuang-che Wu22f207e2019-02-23 12:53:53 +0800464def cmd_sync(opts):
465 try:
466 do_sync(opts)
467 except subprocess.CalledProcessError:
468 # Sync may fail due to network or server issues.
469 logger.exception('do_sync failed, will retry one minute later')
470 time.sleep(60)
471 do_sync(opts)
472
473
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800474def cmd_new(opts):
475 work_dir = os.path.join(opts.work_base, opts.session)
476 if not os.path.exists(work_dir):
477 os.makedirs(work_dir)
478
479 template_factory = DefaultProjectPathFactory(opts.mirror_base, opts.work_base,
480 CHECKOUT_TEMPLATE_NAME)
481 path_factory = DefaultProjectPathFactory(opts.mirror_base, opts.work_base,
482 opts.session)
483
484 prepare_all = False
485 if not opts.chromeos and not opts.chrome and not opts.android:
486 logger.info('prepare trees for all')
487 prepare_all = True
488
489 chromeos_template = template_factory.get_chromeos_tree()
490 if (prepare_all and os.path.exists(chromeos_template)) or opts.chromeos:
491 logger.info('prepare tree for chromeos, %s',
492 path_factory.get_chromeos_tree())
493 snapshot_or_copytree(chromeos_template, path_factory.get_chromeos_tree())
494
495 chrome_template = template_factory.get_chrome_tree()
496 if (prepare_all and os.path.exists(chrome_template)) or opts.chrome:
497 logger.info('prepare tree for chrome, %s', path_factory.get_chrome_tree())
498 snapshot_or_copytree(chrome_template, path_factory.get_chrome_tree())
499
500 if prepare_all:
501 android_branches = enumerate_android_branches_available(opts.mirror_base)
502 else:
503 android_branches = opts.android
504 for branch in android_branches:
505 logger.info('prepare tree for android branch=%s, %s', branch,
506 path_factory.get_android_tree(branch))
507 snapshot_or_copytree(
508 template_factory.get_android_tree(branch),
509 path_factory.get_android_tree(branch))
510
511
512def delete_tree(path):
513 if is_btrfs_subvolume(path):
Kuang-che Wu9d3ccde2019-01-03 17:06:09 +0800514 # btrfs should be mounted with 'user_subvol_rm_allowed' option and thus
515 # normal user permission is enough.
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800516 util.check_call('btrfs', 'subvolume', 'delete', path)
517 else:
Kuang-che Wu9d3ccde2019-01-03 17:06:09 +0800518 util.check_call('sudo', 'rm', '-rf', path)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800519
520
521def cmd_list(opts):
522 print('%-20s %s' % ('Session', 'Path'))
523 for name in os.listdir(opts.work_base):
524 if name == CHECKOUT_TEMPLATE_NAME:
525 continue
526 path = os.path.join(opts.work_base, name)
527 print('%-20s %s' % (name, path))
528
529
530def cmd_delete(opts):
531 assert opts.session
532 path_factory = DefaultProjectPathFactory(opts.mirror_base, opts.work_base,
533 opts.session)
534
535 chromeos_tree = path_factory.get_chromeos_tree()
536 if os.path.exists(chromeos_tree):
537 if os.path.exists(os.path.join(chromeos_tree, 'chromite')):
538 # ignore error
539 util.call('cros_sdk', '--unmount', cwd=chromeos_tree)
540 delete_tree(chromeos_tree)
541
542 chrome_tree = path_factory.get_chrome_tree()
543 if os.path.exists(chrome_tree):
544 delete_tree(chrome_tree)
545
546 android_branches = enumerate_android_branches_available(opts.mirror_base)
547 for branch in android_branches:
548 android_tree = path_factory.get_android_tree(branch)
549 if os.path.exists(android_tree):
550 delete_tree(android_tree)
551
552 os.rmdir(os.path.join(opts.work_base, opts.session))
553
Zheng-Jie Changcd424c12020-01-10 14:32:08 +0800554 # remove caches
555 chromeos_root = os.getenv('DEFAULT_CHROMEOS_ROOT')
556 if chromeos_root:
557 path = os.path.join(chromeos_root, 'devserver/static')
558 if os.path.exists(path):
Kuang-che Wud2747442020-07-01 16:50:24 +0800559 logger.debug('remove cache (cros flash): %s', path)
Zheng-Jie Changcd424c12020-01-10 14:32:08 +0800560 util.call('cros', 'clean', '--flash', cwd=path)
561
562 for path in glob.glob(os.path.join(chromeos_root, 'chroot/tmp/*')):
Kuang-che Wud2747442020-07-01 16:50:24 +0800563 logger.debug('remove cache (chroot/tmp): %s', path)
Zheng-Jie Changcd424c12020-01-10 14:32:08 +0800564 delete_tree(path)
565
Zheng-Jie Changd164da42020-03-12 10:33:23 +0800566 for path in glob.glob(os.path.join(chromeos_root, 'tmp/*')):
Kuang-che Wud2747442020-07-01 16:50:24 +0800567 logger.debug('remove cache (chromeos root tmp): %s', path)
Zheng-Jie Changd164da42020-03-12 10:33:23 +0800568 delete_tree(path)
569
Zheng-Jie Changcd424c12020-01-10 14:32:08 +0800570 for path in glob.glob(
571 os.path.join(path_factory.get_chrome_cache(), '_cache_*')):
Kuang-che Wud2747442020-07-01 16:50:24 +0800572 logger.debug('remove cache (chrome gclient cache): %s', path)
Zheng-Jie Changcd424c12020-01-10 14:32:08 +0800573 delete_tree(path)
574
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800575
576def create_parser():
Kuang-che Wud2d6e412021-01-28 16:26:41 +0800577 base_parser = argparse.ArgumentParser(add_help=False)
578 base_parser.add_argument(
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800579 '--mirror_base',
580 metavar='MIRROR_BASE',
581 default=configure.get('MIRROR_BASE', DEFAULT_MIRROR_BASE),
582 help='Directory for mirrors (default: %(default)s)')
Kuang-che Wud2d6e412021-01-28 16:26:41 +0800583 base_parser.add_argument(
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800584 '--work_base',
585 metavar='WORK_BASE',
586 default=configure.get('WORK_BASE', DEFAULT_WORK_BASE),
Kuang-che Wud2d6e412021-01-28 16:26:41 +0800587 help='Directory for bisection working directories'
588 ' (default: %(default)s)')
589 parents_session_optional = [
590 common.common_argument_parser, common.session_optional_parser, base_parser
591 ]
592 parents_session_required = [
593 common.common_argument_parser, common.session_required_parser, base_parser
594 ]
595
596 parser = argparse.ArgumentParser(
597 formatter_class=argparse.RawDescriptionHelpFormatter, description=__doc__)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800598 subparsers = parser.add_subparsers(
Kuang-che Wud2d6e412021-01-28 16:26:41 +0800599 dest='command', title='commands', metavar='<command>', required=True)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800600
601 parser_init = subparsers.add_parser(
Kuang-che Wud2d6e412021-01-28 16:26:41 +0800602 'init',
603 help='Mirror source trees and create template checkout',
604 parents=parents_session_optional)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800605 parser_init.add_argument(
606 '--chrome', action='store_true', help='init chrome mirror and tree')
607 parser_init.add_argument(
608 '--chromeos', action='store_true', help='init chromeos mirror and tree')
609 parser_init.add_argument(
610 '--android',
611 metavar='BRANCH',
612 action='append',
613 default=[],
614 help='init android mirror and tree of BRANCH')
615 parser_init.add_argument(
616 '--btrfs',
617 action='store_true',
618 help='create btrfs subvolume for source tree')
619 parser_init.set_defaults(func=cmd_init)
620
621 parser_sync = subparsers.add_parser(
622 'sync',
623 help='Sync source trees',
624 description='Sync all if no projects are specified '
Kuang-che Wud2d6e412021-01-28 16:26:41 +0800625 '(--chrome, --chromeos, or --android)',
626 parents=parents_session_optional)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800627 parser_sync.add_argument(
628 '--chrome', action='store_true', help='sync chrome mirror and tree')
629 parser_sync.add_argument(
630 '--chromeos', action='store_true', help='sync chromeos mirror and tree')
631 parser_sync.add_argument(
632 '--android',
633 metavar='BRANCH',
634 action='append',
635 default=[],
636 help='sync android mirror and tree of BRANCH')
637 parser_sync.set_defaults(func=cmd_sync)
638
639 parser_new = subparsers.add_parser(
640 'new',
641 help='Create new source checkout for bisect',
642 description='Create for all if no projects are specified '
Kuang-che Wud2d6e412021-01-28 16:26:41 +0800643 '(--chrome, --chromeos, or --android)',
644 parents=parents_session_required)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800645 parser_new.add_argument(
646 '--chrome', action='store_true', help='create chrome checkout')
647 parser_new.add_argument(
648 '--chromeos', action='store_true', help='create chromeos checkout')
649 parser_new.add_argument(
650 '--android',
651 metavar='BRANCH',
652 action='append',
653 default=[],
654 help='create android checkout of BRANCH')
655 parser_new.set_defaults(func=cmd_new)
656
657 parser_list = subparsers.add_parser(
Kuang-che Wud2d6e412021-01-28 16:26:41 +0800658 'list',
659 help='List existing sessions with source checkout',
660 parents=parents_session_optional)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800661 parser_list.set_defaults(func=cmd_list)
662
Kuang-che Wud2d6e412021-01-28 16:26:41 +0800663 parser_delete = subparsers.add_parser(
664 'delete', help='Delete source checkout', parents=parents_session_required)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800665 parser_delete.set_defaults(func=cmd_delete)
666
667 return parser
668
669
670def main():
671 common.init()
672 parser = create_parser()
673 opts = parser.parse_args()
674 common.config_logging(opts)
Kuang-che Wu41e8b592018-09-25 17:01:30 +0800675 opts.func(opts)
676
677
678if __name__ == '__main__':
679 main()