Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(384)

Side by Side Diff: infra/recipe_modules/bot_update/api.py

Issue 1641363002: Adds bot_update to depot_tools. (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Don't delete the old files in this CL. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « infra/recipe_modules/bot_update/__init__.py ('k') | infra/recipe_modules/bot_update/example.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2014 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.
4
5
6 """Recipe module to ensure a checkout is consistant on a bot."""
7
8 from recipe_engine import recipe_api
9
10
11 # This is just for testing, to indicate if a master is using a Git scheduler
12 # or not.
13 SVN_MASTERS = (
14 'experimental.svn',
15 )
16
17
18 def jsonish_to_python(spec, is_top=False):
19 """Turn a json spec into a python parsable object.
20
21 This exists because Gclient specs, while resembling json, is actually
22 ingested using a python "eval()". Therefore a bit of plumming is required
23 to turn our newly constructed Gclient spec into a gclient-readable spec.
24 """
25 ret = ''
26 if is_top: # We're the 'top' level, so treat this dict as a suite.
27 ret = '\n'.join(
28 '%s = %s' % (k, jsonish_to_python(spec[k])) for k in sorted(spec)
29 )
30 else:
31 if isinstance(spec, dict):
32 ret += '{'
33 ret += ', '.join(
34 "%s: %s" % (repr(str(k)), jsonish_to_python(spec[k]))
35 for k in sorted(spec)
36 )
37 ret += '}'
38 elif isinstance(spec, list):
39 ret += '['
40 ret += ', '.join(jsonish_to_python(x) for x in spec)
41 ret += ']'
42 elif isinstance(spec, basestring):
43 ret = repr(str(spec))
44 else:
45 ret = repr(spec)
46 return ret
47
48
49 class BotUpdateApi(recipe_api.RecipeApi):
50
51 def __init__(self, deps_revision_overrides, issue, event_patchSet_ref,
52 fail_patch, failure_type, parent_got_revision, patchset,
53 patch_url, rietveld, repository, revision, *args, **kwargs):
54 # I love my properties
55 self._deps_revision_overrides = deps_revision_overrides
56 self._issue = issue
57 self._event_patchSet_ref = event_patchSet_ref
58 self._fail_patch = fail_patch
59 self._failure_type = failure_type
60 self._parent_got_revision = parent_got_revision
61 self._patchset = patchset
62 self._patch_url = patch_url
63 self._rietveld = rietveld
64 self._repository = repository
65 self._revision = revision
66
67 self._properties = {}
68 super(BotUpdateApi, self).__init__(*args, **kwargs)
69
70 def __call__(self, name, cmd, **kwargs):
71 """Wrapper for easy calling of bot_update."""
72 assert isinstance(cmd, (list, tuple))
73 bot_update_path = self.resource('bot_update.py')
74 kwargs.setdefault('infra_step', True)
75 return self.m.python(name, bot_update_path, cmd, **kwargs)
76
77 @property
78 def properties(self):
79 return self._properties
80
81 def ensure_checkout(self, gclient_config=None, suffix=None,
82 patch=True, update_presentation=True,
83 force=False, patch_root=None, no_shallow=False,
84 with_branch_heads=False, refs=None,
85 patch_project_roots=None, patch_oauth2=False,
86 output_manifest=True, clobber=False,
87 root_solution_revision=None, **kwargs):
88 refs = refs or []
89 # We can re-use the gclient spec from the gclient module, since all the
90 # data bot_update needs is already configured into the gclient spec.
91 cfg = gclient_config or self.m.gclient.c
92 spec_string = jsonish_to_python(cfg.as_jsonish(), True)
93
94 # Construct our bot_update command. This basically be inclusive of
95 # everything required for bot_update to know:
96 root = patch_root
97 if root is None:
98 root = cfg.solutions[0].name
99 additional = self.m.rietveld.calculate_issue_root(patch_project_roots)
100 if additional:
101 root = self.m.path.join(root, additional)
102
103 if patch:
104 issue = self._issue
105 patchset = self._patchset
106 patch_url = self._patch_url
107 gerrit_repo = self._repository
108 gerrit_ref = self._event_patchSet_ref
109 else:
110 # The trybot recipe sometimes wants to de-apply the patch. In which case
111 # we pretend the issue/patchset/patch_url never existed.
112 issue = patchset = patch_url = email_file = key_file = None
113 gerrit_repo = gerrit_ref = None
114
115 # Issue and patchset must come together.
116 if issue:
117 assert patchset
118 if patchset:
119 assert issue
120 if patch_url:
121 # If patch_url is present, bot_update will actually ignore issue/ps.
122 issue = patchset = None
123
124 # The gerrit_ref and gerrit_repo must be together or not at all. If one is
125 # missing, clear both of them.
126 if not gerrit_ref or not gerrit_repo:
127 gerrit_repo = gerrit_ref = None
128 assert (gerrit_ref != None) == (gerrit_repo != None)
129
130 # Point to the oauth2 auth files if specified.
131 # These paths are where the bots put their credential files.
132 if patch_oauth2:
133 email_file = self.m.path['build'].join(
134 'site_config', '.rietveld_client_email')
135 key_file = self.m.path['build'].join(
136 'site_config', '.rietveld_secret_key')
137 else:
138 email_file = key_file = None
139
140 rev_map = {}
141 if self.m.gclient.c:
142 rev_map = self.m.gclient.c.got_revision_mapping.as_jsonish()
143
144 flags = [
145 # 1. What do we want to check out (spec/root/rev/rev_map).
146 ['--spec', spec_string],
147 ['--root', root],
148 ['--revision_mapping_file', self.m.json.input(rev_map)],
149
150 # 2. How to find the patch, if any (issue/patchset/patch_url).
151 ['--issue', issue],
152 ['--patchset', patchset],
153 ['--patch_url', patch_url],
154 ['--rietveld_server', self._rietveld],
155 ['--gerrit_repo', gerrit_repo],
156 ['--gerrit_ref', gerrit_ref],
157 ['--apply_issue_email_file', email_file],
158 ['--apply_issue_key_file', key_file],
159
160 # 3. Hookups to JSON output back into recipes.
161 ['--output_json', self.m.json.output()],]
162
163
164 # Collect all fixed revisions to simulate them in the json output.
165 # Fixed revision are the explicit input revisions of bot_update.py, i.e.
166 # every command line parameter "--revision name@value".
167 fixed_revisions = {}
168
169 revisions = {}
170 for solution in cfg.solutions:
171 if solution.revision:
172 revisions[solution.name] = solution.revision
173 elif solution == cfg.solutions[0]:
174 revisions[solution.name] = (
175 self._parent_got_revision or
176 self._revision or
177 'HEAD')
178 if self.m.gclient.c and self.m.gclient.c.revisions:
179 revisions.update(self.m.gclient.c.revisions)
180 if cfg.solutions and root_solution_revision:
181 revisions[cfg.solutions[0].name] = root_solution_revision
182 # Allow for overrides required to bisect into rolls.
183 revisions.update(self._deps_revision_overrides)
184 for name, revision in sorted(revisions.items()):
185 fixed_revision = self.m.gclient.resolve_revision(revision)
186 if fixed_revision:
187 fixed_revisions[name] = fixed_revision
188 flags.append(['--revision', '%s@%s' % (name, fixed_revision)])
189
190 # Add extra fetch refspecs.
191 for ref in refs:
192 flags.append(['--refs', ref])
193
194 # Filter out flags that are None.
195 cmd = [item for flag_set in flags
196 for item in flag_set if flag_set[1] is not None]
197
198 if clobber:
199 cmd.append('--clobber')
200 if no_shallow:
201 cmd.append('--no_shallow')
202 if output_manifest:
203 cmd.append('--output_manifest')
204 if with_branch_heads or cfg.with_branch_heads:
205 cmd.append('--with_branch_heads')
206
207 # Inject Json output for testing.
208 # TODO(martinis, hinoka) remove me
209 git_mode = True
210 first_sln = cfg.solutions[0].name
211 step_test_data = lambda: self.test_api.output_json(
212 root, first_sln, rev_map, git_mode, force,
213 self._fail_patch,
214 output_manifest=output_manifest, fixed_revisions=fixed_revisions)
215
216 # Add suffixes to the step name, if specified.
217 name = 'bot_update'
218 if not patch:
219 name += ' (without patch)'
220 if suffix:
221 name += ' - %s' % suffix
222
223 # Ah hah! Now that everything is in place, lets run bot_update!
224 try:
225 # 87 and 88 are the 'patch failure' codes for patch download and patch
226 # apply, respectively. We don't actually use the error codes, and instead
227 # rely on emitted json to determine cause of failure.
228 self(name, cmd, step_test_data=step_test_data,
229 ok_ret=(0, 87, 88), **kwargs)
230 finally:
231 step_result = self.m.step.active_result
232 self._properties = step_result.json.output.get('properties', {})
233
234 if update_presentation:
235 # Set properties such as got_revision.
236 for prop_name, prop_value in self.properties.iteritems():
237 step_result.presentation.properties[prop_name] = prop_value
238 # Add helpful step description in the step UI.
239 if 'step_text' in step_result.json.output:
240 step_text = step_result.json.output['step_text']
241 step_result.presentation.step_text = step_text
242 # Add log line output.
243 if 'log_lines' in step_result.json.output:
244 for log_name, log_lines in step_result.json.output['log_lines']:
245 step_result.presentation.logs[log_name] = log_lines.splitlines()
246
247 # Set the "checkout" path for the main solution.
248 # This is used by the Chromium module to figure out where to look for
249 # the checkout.
250 # If there is a patch failure, emit another step that said things failed.
251 if step_result.json.output.get('patch_failure'):
252 return_code = step_result.json.output.get('patch_apply_return_code')
253 if return_code == 3:
254 # This is download failure, hence an infra failure.
255 # Sadly, python.failing_step doesn't support kwargs.
256 self.m.python.inline(
257 'Patch failure',
258 ('import sys;'
259 'print "Patch download failed. See bot_update step for details";'
260 'sys.exit(1)'),
261 infra_step=True,
262 step_test_data=lambda: self.m.raw_io.test_api.output(
263 'Patch download failed. See bot_update step for details',
264 retcode=1)
265 )
266 else:
267 # This is actual patch failure.
268 step_result.presentation.properties['failure_type'] = 'PATCH_FAILURE'
269 self.m.python.failing_step(
270 'Patch failure', 'Check the bot_update step for details')
271
272 # bot_update actually just sets root to be the folder name of the
273 # first solution.
274 if step_result.json.output['did_run']:
275 co_root = step_result.json.output['root']
276 cwd = kwargs.get('cwd', self.m.path['slave_build'])
277 if 'checkout' not in self.m.path:
278 self.m.path['checkout'] = cwd.join(*co_root.split(self.m.path.sep))
279
280 return step_result
OLDNEW
« no previous file with comments | « infra/recipe_modules/bot_update/__init__.py ('k') | infra/recipe_modules/bot_update/example.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698