aboutsummaryrefslogtreecommitdiffhomepage
path: root/infra/bots/recipe_modules/core/api.py
blob: a2591dcbee208e5295184af1170ba5607e131bd4 (plain)
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
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.


# pylint: disable=W0201


import json
import os
import re
import sys

from recipe_engine import recipe_api
from recipe_engine import config_types


class SkiaApi(recipe_api.RecipeApi):

  def setup(self):
    """Prepare the bot to run."""
    # Setup dependencies.
    self.m.vars.setup()

    # Check out the Skia code.
    self.checkout_steps()

    if not self.m.path.exists(self.m.vars.tmp_dir):
      self.m.run.run_once(self.m.file.makedirs,
                          'tmp_dir',
                          self.m.vars.tmp_dir,
                          infra_step=True)

    self.m.flavor.setup()

  def update_repo(self, parent_dir, repo):
    """Update an existing repo. This is safe to call without gen_steps."""
    repo_path = parent_dir.join(repo.name)
    if self.m.path.exists(repo_path):  # pragma: nocover
      if self.m.platform.is_win:
        git = 'git.bat'
      else:
        git = 'git'
      self.m.step('git remote set-url',
                  cmd=[git, 'remote', 'set-url', 'origin', repo.url],
                  cwd=repo_path,
                  infra_step=True)
      self.m.step('git fetch',
                  cmd=[git, 'fetch'],
                  cwd=repo_path,
                  infra_step=True)
      self.m.step('git reset',
                  cmd=[git, 'reset', '--hard', repo.revision],
                  cwd=repo_path,
                  infra_step=True)
      self.m.step('git clean',
                  cmd=[git, 'clean', '-d', '-f'],
                  cwd=repo_path,
                  infra_step=True)

  def checkout_steps(self):
    """Run the steps to obtain a checkout of Skia."""
    cfg_kwargs = {}
    if not self.m.vars.persistent_checkout:
      # We should've obtained the Skia checkout through isolates, so we don't
      # need to perform the checkout ourselves.
      return

    # Use a persistent gclient cache for Swarming.
    cfg_kwargs['CACHE_DIR'] = self.m.vars.gclient_cache

    # Create the checkout path if necessary.
    if not self.m.path.exists(self.m.vars.checkout_root):
      self.m.file.makedirs('checkout_path',
                           self.m.vars.checkout_root,
                           infra_step=True)

    # Initial cleanup.
    gclient_cfg = self.m.gclient.make_config(**cfg_kwargs)
    skia = gclient_cfg.solutions.add()
    skia.name = 'skia'
    skia.managed = False
    skia.url = 'https://skia.googlesource.com/skia.git'
    skia.revision = self.m.properties.get('revision') or 'origin/master'
    self.update_repo(self.m.vars.checkout_root, skia)

    # TODO(rmistry): Remove the below block after there is a solution for
    #                crbug.com/616443
    entries_file = self.m.vars.checkout_root.join('.gclient_entries')
    if self.m.path.exists(entries_file):
      self.m.file.remove('remove %s' % entries_file,
                         entries_file,
                         infra_step=True)  # pragma: no cover

    if self.m.vars.need_chromium_checkout:
      chromium = gclient_cfg.solutions.add()
      chromium.name = 'src'
      chromium.managed = False
      chromium.url = 'https://chromium.googlesource.com/chromium/src.git'
      chromium.revision = 'origin/lkgr'
      self.update_repo(self.m.vars.checkout_root, chromium)

    if self.m.vars.need_pdfium_checkout:
      pdfium = gclient_cfg.solutions.add()
      pdfium.name = 'pdfium'
      pdfium.managed = False
      pdfium.url = 'https://pdfium.googlesource.com/pdfium.git'
      pdfium.revision = 'origin/master'
      self.update_repo(self.m.vars.checkout_root, pdfium)

    # Run 'gclient sync'.
    gclient_cfg.got_revision_mapping['skia'] = 'got_revision'
    checkout_kwargs = {}
    checkout_kwargs['env'] = self.m.vars.default_env

    # api.gclient.revert() assumes things about the layout of the code, so it
    # fails for us. Run an appropriate revert sequence for trybots instead.
    gclient_file = self.m.vars.checkout_root.join('.gclient')
    if (self.m.tryserver.is_tryserver and
        self.m.path.exists(gclient_file)):  # pragma: no cover
      # These steps taken from:
      # https://chromium.googlesource.com/chromium/tools/build/+/
      #    81a696760ab7c25f6606c54fc781b90b8af9fdd2/scripts/slave/
      #    gclient_safe_revert.py
      if self.m.path.exists(entries_file):
        self.m.gclient('recurse', [
            'recurse', '-i', 'sh', '-c',
            'if [ -e .git ]; then git remote update; fi'])
      self.m.gclient(
          'revert',
          ['revert', '-v', '-v', '-v', '--nohooks', '--upstream'],
          cwd=self.m.vars.checkout_root)

    update_step = self.m.gclient.checkout(gclient_config=gclient_cfg,
                                          cwd=self.m.vars.checkout_root,
                                          revert=False,
                                          **checkout_kwargs)

    self.m.vars.got_revision = (
        update_step.presentation.properties['got_revision'])
    self.m.tryserver.maybe_apply_issue()
    if self.m.properties.get('patch_storage') == 'gerrit':
      self.m.bot_update.apply_gerrit_ref(
          root=str(self.m.vars.checkout_root.join('skia')))

    if self.m.vars.need_chromium_checkout:
      self.m.gclient.runhooks(cwd=self.m.vars.checkout_root,
                              env=self.m.vars.gclient_env)