aboutsummaryrefslogtreecommitdiffhomepage
path: root/infra/bots/recipes/swarm_RecreateSKPs.py
blob: 6863b020b753ee453b08ee1dd59a4caf93e488f8 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# 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.


"""Recipe for the Skia RecreateSKPs Bot."""


DEPS = [
  'build/file',
  'depot_tools/gclient',
  'recipe_engine/path',
  'recipe_engine/properties',
  'recipe_engine/python',
  'recipe_engine/raw_io',
  'recipe_engine/step',
  'skia',
]


TEST_BUILDERS = {
  'client.skia.compile': {
    'skiabot-linux-swarm-000': [
      'Housekeeper-Nightly-RecreateSKPs_Canary',
      'Housekeeper-Weekly-RecreateSKPs',
    ],
  },
}


DEPOT_TOOLS_AUTH_TOKEN_FILE = '.depot_tools_oauth2_tokens'
DEPOT_TOOLS_AUTH_TOKEN_FILE_BACKUP = '.depot_tools_oauth2_tokens.old'
UPDATE_SKPS_KEY = 'depot_tools_auth_update_skps'


class depot_tools_auth(object):
  """Temporarily authenticate to depot_tools via GCE metadata."""
  def __init__(self, api, metadata_key):
    self.m = api
    self._key = metadata_key

  def __enter__(self):
    return self.m.python.inline(
        'depot-tools-auth login',
        """
import os
import urllib2

TOKEN_FILE = '%s'
TOKEN_FILE_BACKUP = '%s'
TOKEN_URL = 'http://metadata/computeMetadata/v1/project/attributes/%s'

req = urllib2.Request(TOKEN_URL, headers={'Metadata-Flavor': 'Google'})
contents = urllib2.urlopen(req).read()

home = os.path.expanduser('~')
token_file = os.path.join(home, TOKEN_FILE)
if os.path.isfile(token_file):
  os.rename(token_file, os.path.join(home, TOKEN_FILE_BACKUP))

with open(token_file, 'w') as f:
  f.write(contents)
        """ % (DEPOT_TOOLS_AUTH_TOKEN_FILE,
               DEPOT_TOOLS_AUTH_TOKEN_FILE_BACKUP,
               self._key),
    )

  def __exit__(self, t, v, tb):
    return self.m.python.inline(
        'depot-tools-auth logout',
        """
import os


TOKEN_FILE = '%s'
TOKEN_FILE_BACKUP = '%s'


home = os.path.expanduser('~')
token_file = os.path.join(home, TOKEN_FILE)
if os.path.isfile(token_file):
  os.remove(token_file)

backup_file = os.path.join(home, TOKEN_FILE_BACKUP)
if os.path.isfile(backup_file):
  os.rename(backup_file, token_file)
        """ % (DEPOT_TOOLS_AUTH_TOKEN_FILE,
               DEPOT_TOOLS_AUTH_TOKEN_FILE_BACKUP),
    )


def RunSteps(api):
  # Check out Chrome.
  api.skia.setup()
  api.gclient.runhooks(
      env={'CPPFLAGS': '-DSK_ALLOW_CROSSPROCESS_PICTUREIMAGEFILTERS=1'})

  src_dir = api.skia.checkout_root.join('src')

  # Call build/gyp_chromium
  api.step('gyp_chromium',
           ['build/gyp_chromium'],
           env={'CPPFLAGS': '-DSK_ALLOW_CROSSPROCESS_PICTUREIMAGEFILTERS=1',
                'GYP_GENERATORS': 'ninja'},
           cwd=src_dir)
  # Build Chrome.
  api.step('Build Chrome',
           ['ninja', '-C', 'out/Release', 'chrome'],
           cwd=src_dir)

  # Download boto file (needed by recreate_skps.py) to tmp dir.
  boto_file = api.path['slave_build'].join('tmp', '.boto')
  api.python.inline(
      'download boto file',
      """
import os
import urllib2

BOTO_URL = 'http://metadata/computeMetadata/v1/project/attributes/boto-file'

dest_path = '%s'
dest_dir = os.path.dirname(dest_path)
if not os.path.exists(dest_dir):
  os.makedirs(dest_dir)

req = urllib2.Request(BOTO_URL, headers={'Metadata-Flavor': 'Google'})
contents = urllib2.urlopen(req).read()

with open(dest_path, 'w') as f:
  f.write(contents)
        """ % boto_file)

  # Clean up the output dir.
  output_dir = api.path['slave_build'].join('skp_output')
  if api.path.exists(output_dir):
    api.file.rmtree('skp_output', output_dir)
  api.file.makedirs('skp_output', output_dir)

  # Capture the SKPs.
  path_var= api.path.pathsep.join([str(api.path['depot_tools']), '%(PATH)s'])
  env = {
      'CHROME_HEADLESS': '1',
      'PATH': path_var,
  }
  boto_env = {
      'AWS_CREDENTIAL_FILE': boto_file,
      'BOTO_CONFIG': boto_file,
  }
  recreate_skps_env = {}
  recreate_skps_env.update(env)
  recreate_skps_env.update(boto_env)
  asset_dir = api.skia.infrabots_dir.join('assets', 'skp')
  cmd = ['python', asset_dir.join('create.py'),
         '--chrome_src_path', src_dir,
         '--browser_executable', src_dir.join('out', 'Release', 'chrome'),
         '--target_dir', output_dir]
  if 'Canary' not in api.properties['buildername']:
    cmd.append('--upload_to_partner_bucket')
  api.step('Recreate SKPs',
           cmd=cmd,
           cwd=api.skia.skia_dir,
           env=recreate_skps_env)

  # Upload the SKPs.
  if 'Canary' not in api.properties['buildername']:
    cmd = ['python',
           api.skia.skia_dir.join('infra', 'bots', 'upload_skps.py'),
           '--target_dir', output_dir]
    with depot_tools_auth(api, UPDATE_SKPS_KEY):
      api.step('Upload SKPs',
               cmd=cmd,
               cwd=api.skia.skia_dir,
               env=env)


def GenTests(api):
  for mastername, slaves in TEST_BUILDERS.iteritems():
    for slavename, builders_by_slave in slaves.iteritems():
      for builder in builders_by_slave:
        test = (
            api.test(builder) +
            api.properties(buildername=builder,
                           mastername=mastername,
                           slavename=slavename,
                           revision='abc123',
                           buildnumber=2,
                           path_config='kitchen',
                           swarm_out_dir='[SWARM_OUT_DIR]') +
            api.path.exists(api.path['slave_build'].join('skp_output'))
        )
        yield test