remove redundant DCHECK that a size_t variable >= 0
[chromium-blink-merge.git] / tools / bisect_utils.py
blob4447f48ecc20fee3aeb02e938c73ec3d6b7d97c3
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Set of operations/utilities related to checking out the depot, and
6 outputting annotations on the buildbot waterfall. These are intended to be
7 used by the bisection scripts."""
9 import errno
10 import os
11 import subprocess
14 GCLIENT_SPEC = """
15 solutions = [
16 { "name" : "src",
17 "url" : "https://chromium.googlesource.com/chromium/src.git",
18 "deps_file" : ".DEPS.git",
19 "managed" : True,
20 "custom_deps" : {
21 "src/data/page_cycler": "https://chrome-internal.googlesource.com/" +
22 "chrome/data/page_cycler/.git",
23 "src/tools/perf/data": "https://chrome-internal.googlesource.com/" +
24 "chrome/tools/perf/data/.git",
25 "src/v8_bleeding_edge": "git://github.com/v8/v8.git",
27 "safesync_url": "",
30 """
31 GCLIENT_SPEC = ''.join([l for l in GCLIENT_SPEC.splitlines()])
34 def OutputAnnotationStepStart(name):
35 """Outputs appropriate annotation to signal the start of a step to
36 a trybot.
38 Args:
39 name: The name of the step.
40 """
41 print
42 print '@@@SEED_STEP %s@@@' % name
43 print '@@@STEP_CURSOR %s@@@' % name
44 print '@@@STEP_STARTED@@@'
45 print
48 def OutputAnnotationStepClosed():
49 """Outputs appropriate annotation to signal the closing of a step to
50 a trybot."""
51 print
52 print '@@@STEP_CLOSED@@@'
53 print
56 def CreateAndChangeToSourceDirectory(working_directory):
57 """Creates a directory 'bisect' as a subdirectory of 'working_directory'. If
58 the function is successful, the current working directory will change to that
59 of the new 'bisect' directory.
61 Returns:
62 True if the directory was successfully created (or already existed).
63 """
64 cwd = os.getcwd()
65 os.chdir(working_directory)
66 try:
67 os.mkdir('bisect')
68 except OSError, e:
69 if e.errno != errno.EEXIST:
70 return False
71 os.chdir('bisect')
72 return True
75 def RunGClient(params):
76 """Runs gclient with the specified parameters.
78 Args:
79 params: A list of parameters to pass to gclient.
81 Returns:
82 The return code of the call.
83 """
84 if os.name == 'nt':
85 # "HOME" isn't normally defined on windows, but is needed
86 # for git to find the user's .netrc file.
87 if not os.getenv('HOME'):
88 os.environ['HOME'] = os.environ['USERPROFILE']
90 shell = os.name == 'nt'
91 cmd = ['gclient'] + params
92 return subprocess.call(cmd, shell=shell)
95 def RunGClientAndCreateConfig():
96 """Runs gclient and creates a config containing both src and src-internal.
98 Returns:
99 The return code of the call.
101 return_code = RunGClient(
102 ['config', '--spec=%s' % GCLIENT_SPEC, '--git-deps'])
103 return return_code
106 def RunGClientAndSync(reset):
107 """Runs gclient and does a normal sync.
109 Args:
110 reset: Whether to reset any changes to the depot.
112 Returns:
113 The return code of the call.
115 params = ['sync', '--verbose']
116 if reset:
117 params.extend(['--reset', '--force', '--delete_unversioned_trees'])
118 return RunGClient(params)
121 def SetupGitDepot(output_buildbot_annotations, reset):
122 """Sets up the depot for the bisection. The depot will be located in a
123 subdirectory called 'bisect'.
125 Args:
126 reset: Whether to reset any changes to the depot.
128 Returns:
129 True if gclient successfully created the config file and did a sync, False
130 otherwise.
132 name = 'Setting up Bisection Depot'
134 if output_buildbot_annotations:
135 OutputAnnotationStepStart(name)
137 passed = False
139 if not RunGClientAndCreateConfig():
140 if not RunGClientAndSync(reset):
141 passed = True
143 if output_buildbot_annotations:
144 print
145 OutputAnnotationStepClosed()
147 return passed
150 def CreateBisectDirectoryAndSetupDepot(opts, reset=False):
151 """Sets up a subdirectory 'bisect' and then retrieves a copy of the depot
152 there using gclient.
154 Args:
155 opts: The options parsed from the command line through parse_args().
156 reset: Whether to reset any changes to the depot.
158 Returns:
159 Returns 0 on success, otherwise 1.
161 if not CreateAndChangeToSourceDirectory(opts.working_directory):
162 print 'Error: Could not create bisect directory.'
163 print
164 return 1
166 if not SetupGitDepot(opts.output_buildbot_annotations, reset):
167 print 'Error: Failed to grab source.'
168 print
169 return 1
171 return 0