OLD | NEW |
(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, *args, **kwargs): |
| 52 self._properties = {} |
| 53 super(BotUpdateApi, self).__init__(*args, **kwargs) |
| 54 |
| 55 def __call__(self, name, cmd, **kwargs): |
| 56 """Wrapper for easy calling of bot_update.""" |
| 57 assert isinstance(cmd, (list, tuple)) |
| 58 bot_update_path = self.resource('bot_update.py') |
| 59 kwargs.setdefault('infra_step', True) |
| 60 return self.m.python(name, bot_update_path, cmd, **kwargs) |
| 61 |
| 62 @property |
| 63 def properties(self): |
| 64 return self._properties |
| 65 |
| 66 def ensure_checkout(self, gclient_config=None, suffix=None, |
| 67 patch=True, update_presentation=True, |
| 68 force=False, patch_root=None, no_shallow=False, |
| 69 with_branch_heads=False, refs=None, |
| 70 patch_project_roots=None, patch_oauth2=False, |
| 71 output_manifest=True, clobber=False, |
| 72 root_solution_revision=None, **kwargs): |
| 73 refs = refs or [] |
| 74 # We can re-use the gclient spec from the gclient module, since all the |
| 75 # data bot_update needs is already configured into the gclient spec. |
| 76 cfg = gclient_config or self.m.gclient.c |
| 77 spec_string = jsonish_to_python(cfg.as_jsonish(), True) |
| 78 |
| 79 # Used by bot_update to determine if we want to run or not. |
| 80 master = self.m.properties['mastername'] |
| 81 builder = self.m.properties['buildername'] |
| 82 slave = self.m.properties['slavename'] |
| 83 |
| 84 # Construct our bot_update command. This basically be inclusive of |
| 85 # everything required for bot_update to know: |
| 86 root = patch_root |
| 87 if root is None: |
| 88 root = cfg.solutions[0].name |
| 89 additional = self.m.rietveld.calculate_issue_root(patch_project_roots) |
| 90 if additional: |
| 91 root = self.m.path.join(root, additional) |
| 92 |
| 93 if patch: |
| 94 issue = self.m.properties.get('issue') |
| 95 patchset = self.m.properties.get('patchset') |
| 96 patch_url = self.m.properties.get('patch_url') |
| 97 gerrit_repo = self.m.properties.get('repository') |
| 98 gerrit_ref = self.m.properties.get('event.patchSet.ref') |
| 99 else: |
| 100 # The trybot recipe sometimes wants to de-apply the patch. In which case |
| 101 # we pretend the issue/patchset/patch_url never existed. |
| 102 issue = patchset = patch_url = email_file = key_file = None |
| 103 gerrit_repo = gerrit_ref = None |
| 104 |
| 105 # Issue and patchset must come together. |
| 106 if issue: |
| 107 assert patchset |
| 108 if patchset: |
| 109 assert issue |
| 110 if patch_url: |
| 111 # If patch_url is present, bot_update will actually ignore issue/ps. |
| 112 issue = patchset = None |
| 113 |
| 114 # The gerrit_ref and gerrit_repo must be together or not at all. If one is |
| 115 # missing, clear both of them. |
| 116 if not gerrit_ref or not gerrit_repo: |
| 117 gerrit_repo = gerrit_ref = None |
| 118 assert (gerrit_ref != None) == (gerrit_repo != None) |
| 119 |
| 120 # Point to the oauth2 auth files if specified. |
| 121 # These paths are where the bots put their credential files. |
| 122 if patch_oauth2: |
| 123 email_file = self.m.path['build'].join( |
| 124 'site_config', '.rietveld_client_email') |
| 125 key_file = self.m.path['build'].join( |
| 126 'site_config', '.rietveld_secret_key') |
| 127 else: |
| 128 email_file = key_file = None |
| 129 |
| 130 rev_map = {} |
| 131 if self.m.gclient.c: |
| 132 rev_map = self.m.gclient.c.got_revision_mapping.as_jsonish() |
| 133 |
| 134 flags = [ |
| 135 # 1. Do we want to run? (master/builder/slave). |
| 136 ['--master', master], |
| 137 ['--builder', builder], |
| 138 ['--slave', slave], |
| 139 |
| 140 # 2. What do we want to check out (spec/root/rev/rev_map). |
| 141 ['--spec', spec_string], |
| 142 ['--root', root], |
| 143 ['--revision_mapping_file', self.m.json.input(rev_map)], |
| 144 |
| 145 # 3. How to find the patch, if any (issue/patchset/patch_url). |
| 146 ['--issue', issue], |
| 147 ['--patchset', patchset], |
| 148 ['--patch_url', patch_url], |
| 149 ['--rietveld_server', self.m.properties.get('rietveld')], |
| 150 ['--gerrit_repo', gerrit_repo], |
| 151 ['--gerrit_ref', gerrit_ref], |
| 152 ['--apply_issue_email_file', email_file], |
| 153 ['--apply_issue_key_file', key_file], |
| 154 |
| 155 # 4. Hookups to JSON output back into recipes. |
| 156 ['--output_json', self.m.json.output()],] |
| 157 |
| 158 |
| 159 # Collect all fixed revisions to simulate them in the json output. |
| 160 # Fixed revision are the explicit input revisions of bot_update.py, i.e. |
| 161 # every command line parameter "--revision name@value". |
| 162 fixed_revisions = {} |
| 163 |
| 164 revisions = {} |
| 165 for solution in cfg.solutions: |
| 166 if solution.revision: |
| 167 revisions[solution.name] = solution.revision |
| 168 elif solution == cfg.solutions[0]: |
| 169 revisions[solution.name] = ( |
| 170 self.m.properties.get('parent_got_revision') or |
| 171 self.m.properties.get('revision') or |
| 172 'HEAD') |
| 173 if self.m.gclient.c and self.m.gclient.c.revisions: |
| 174 revisions.update(self.m.gclient.c.revisions) |
| 175 if cfg.solutions and root_solution_revision: |
| 176 revisions[cfg.solutions[0].name] = root_solution_revision |
| 177 # Allow for overrides required to bisect into rolls. |
| 178 revisions.update(self.m.properties.get('deps_revision_overrides', {})) |
| 179 for name, revision in sorted(revisions.items()): |
| 180 fixed_revision = self.m.gclient.resolve_revision(revision) |
| 181 if fixed_revision: |
| 182 fixed_revisions[name] = fixed_revision |
| 183 flags.append(['--revision', '%s@%s' % (name, fixed_revision)]) |
| 184 |
| 185 # Add extra fetch refspecs. |
| 186 for ref in refs: |
| 187 flags.append(['--refs', ref]) |
| 188 |
| 189 # Filter out flags that are None. |
| 190 cmd = [item for flag_set in flags |
| 191 for item in flag_set if flag_set[1] is not None] |
| 192 |
| 193 if clobber: |
| 194 cmd.append('--clobber') |
| 195 if force: |
| 196 cmd.append('--force') |
| 197 if no_shallow: |
| 198 cmd.append('--no_shallow') |
| 199 if output_manifest: |
| 200 cmd.append('--output_manifest') |
| 201 if with_branch_heads or cfg.with_branch_heads: |
| 202 cmd.append('--with_branch_heads') |
| 203 |
| 204 # Inject Json output for testing. |
| 205 git_mode = self.m.properties.get('mastername') not in SVN_MASTERS |
| 206 first_sln = cfg.solutions[0].name |
| 207 step_test_data = lambda: self.test_api.output_json( |
| 208 master, builder, slave, root, first_sln, rev_map, git_mode, force, |
| 209 self.m.properties.get('fail_patch', False), |
| 210 output_manifest=output_manifest, fixed_revisions=fixed_revisions) |
| 211 |
| 212 # Add suffixes to the step name, if specified. |
| 213 name = 'bot_update' |
| 214 if not patch: |
| 215 name += ' (without patch)' |
| 216 if suffix: |
| 217 name += ' - %s' % suffix |
| 218 |
| 219 # Ah hah! Now that everything is in place, lets run bot_update! |
| 220 try: |
| 221 # 87 and 88 are the 'patch failure' codes for patch download and patch |
| 222 # apply, respectively. We don't actually use the error codes, and instead |
| 223 # rely on emitted json to determine cause of failure. |
| 224 self(name, cmd, step_test_data=step_test_data, |
| 225 ok_ret=(0, 87, 88), **kwargs) |
| 226 finally: |
| 227 step_result = self.m.step.active_result |
| 228 self._properties = step_result.json.output.get('properties', {}) |
| 229 |
| 230 if update_presentation: |
| 231 # Set properties such as got_revision. |
| 232 for prop_name, prop_value in self.properties.iteritems(): |
| 233 step_result.presentation.properties[prop_name] = prop_value |
| 234 # Add helpful step description in the step UI. |
| 235 if 'step_text' in step_result.json.output: |
| 236 step_text = step_result.json.output['step_text'] |
| 237 step_result.presentation.step_text = step_text |
| 238 # Add log line output. |
| 239 if 'log_lines' in step_result.json.output: |
| 240 for log_name, log_lines in step_result.json.output['log_lines']: |
| 241 step_result.presentation.logs[log_name] = log_lines.splitlines() |
| 242 |
| 243 # Set the "checkout" path for the main solution. |
| 244 # This is used by the Chromium module to figure out where to look for |
| 245 # the checkout. |
| 246 # If there is a patch failure, emit another step that said things failed. |
| 247 if step_result.json.output.get('patch_failure'): |
| 248 return_code = step_result.json.output.get('patch_apply_return_code') |
| 249 if return_code == 3: |
| 250 # This is download failure, hence an infra failure. |
| 251 # Sadly, python.failing_step doesn't support kwargs. |
| 252 self.m.python.inline( |
| 253 'Patch failure', |
| 254 ('import sys;' |
| 255 'print "Patch download failed. See bot_update step for details";' |
| 256 'sys.exit(1)'), |
| 257 infra_step=True, |
| 258 step_test_data=lambda: self.m.raw_io.test_api.output( |
| 259 'Patch download failed. See bot_update step for details', |
| 260 retcode=1) |
| 261 ) |
| 262 else: |
| 263 # This is actual patch failure. |
| 264 self.m.tryserver.set_patch_failure_tryjob_result() |
| 265 self.m.python.failing_step( |
| 266 'Patch failure', 'Check the bot_update step for details') |
| 267 |
| 268 # bot_update actually just sets root to be the folder name of the |
| 269 # first solution. |
| 270 if step_result.json.output['did_run']: |
| 271 co_root = step_result.json.output['root'] |
| 272 cwd = kwargs.get('cwd', self.m.path['slave_build']) |
| 273 if 'checkout' not in self.m.path: |
| 274 self.m.path['checkout'] = cwd.join(*co_root.split(self.m.path.sep)) |
| 275 |
| 276 return step_result |
OLD | NEW |