• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 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
6from recipe_engine import recipe_api
7
8
9INFRA_GO_PKG = 'go.skia.org/infra'
10UPDATE_GO_ATTEMPTS = 5
11UPLOAD_ATTEMPTS = 5
12
13
14class InfraApi(recipe_api.RecipeApi):
15  @property
16  def goroot(self):
17    return self.m.vars.slave_dir.join('go', 'go')
18
19  @property
20  def go_bin(self):
21    return self.goroot.join('bin')
22
23  @property
24  def go_exe(self):
25    return self.go_bin.join('go')
26
27  @property
28  def go_env(self):
29    return {
30        'GOCACHE': self.m.vars.cache_dir.join('go_cache'),
31        'GOPATH': self.gopath,
32        'GOROOT': self.goroot,
33        'PATH': self.m.path.pathsep.join([
34            str(self.go_bin), str(self.gopath.join('bin')), '%(PATH)s']),
35    }
36
37  @property
38  def gopath(self):
39    return self.m.vars.cache_dir.join('gopath')
40
41  def go_version(self):
42    """Print the Go version."""
43    env = self.m.context.env
44    env.update(self.go_env)
45    with self.m.context(env=env):
46      self.m.run(
47          self.m.step,
48          'go version',
49          cmd=[self.go_exe, 'version'])
50      self.m.run(
51          self.m.step,
52          'env go version',
53          cmd=['go', 'version'])
54
55  class MetadataFetch():
56    def __init__(self, api, metadata_key, local_file, **kwargs):
57      self.m = api
58      self._key = metadata_key
59      self._local_file = local_file
60
61    def __enter__(self):
62      return self.m.python.inline(
63          'download ' + self._local_file,
64        """
65import os
66import urllib2
67
68TOKEN_FILE = '%s'
69TOKEN_URL = 'http://metadata/computeMetadata/v1/project/attributes/%s'
70
71req = urllib2.Request(TOKEN_URL, headers={'Metadata-Flavor': 'Google'})
72contents = urllib2.urlopen(req).read()
73
74home = os.path.expanduser('~')
75token_file = os.path.join(home, TOKEN_FILE)
76
77with open(token_file, 'w') as f:
78  f.write(contents)
79        """ % (self._local_file, self._key),
80      )
81
82    def __exit__(self, t, v, tb):
83      self.m.python.inline(
84          'cleanup ' + self._local_file,
85        """
86import os
87
88
89TOKEN_FILE = '%s'
90
91
92home = os.path.expanduser('~')
93token_file = os.path.join(home, TOKEN_FILE)
94if os.path.isfile(token_file):
95  os.remove(token_file)
96          """ % (self._local_file),
97      )
98      return v is None
99