aboutsummaryrefslogtreecommitdiff
path: root/fetcher/fetcher_lib.py
blob: 0ec017318832788f675ab4ad2587b17824faa6f9 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""Provides helper functions for fetching artifacts."""

import io
import os
import re
import sys
import sysconfig
import time

# This is a workaround to put '/usr/lib/python3.X' ahead of googleapiclient
# Using embedded_launcher won't work since py3-cmd doesn't contain _ssl module.
if sys.version_info.major == 3:
  sys.path.insert(0, os.path.dirname(sysconfig.get_paths()['purelib']))

# pylint: disable=import-error,g-bad-import-order,g-import-not-at-top
import apiclient
from googleapiclient.discovery import build
from six.moves import http_client

import httplib2
from oauth2client.service_account import ServiceAccountCredentials

_SCOPE_URL = 'https://www.googleapis.com/auth/androidbuild.internal'
_DEF_JSON_KEYFILE = '.config/gcloud/application_default_credentials.json'


# 20 MB default chunk size -- used in Buildbot
_DEFAULT_CHUNK_SIZE = 20 * 1024 * 1024

# HTTP errors -- used in Builbot
_DEFAULT_MASKED_ERRORS = [404]
_DEFAULT_RETRIED_ERRORS = [503]
_DEFAULT_RETRIES = 10


def _create_http_from_p12(robot_credentials_file, robot_username):
  """Creates a credentialed HTTP object for requests.

  Args:
    robot_credentials_file: The path to the robot credentials file.
    robot_username: A string containing the username of the robot account.

  Returns:
    An authorized httplib2.Http object.
  """
  try:
    credentials = ServiceAccountCredentials.from_p12_keyfile(
        service_account_email=robot_username,
        filename=robot_credentials_file,
        scopes=_SCOPE_URL)
  except AttributeError:
    raise ValueError('Machine lacks openssl or pycrypto support')
  http = httplib2.Http()
  return credentials.authorize(http)


def _simple_execute(http_request,
                    masked_errors=None,
                    retried_errors=None,
                    retry_delay_seconds=5,
                    max_tries=_DEFAULT_RETRIES):
  """Execute http request and return None on specified errors.

  Args:
    http_request: the apiclient provided http request
    masked_errors: list of errors to return None on
    retried_errors: list of erros to retry the request on
    retry_delay_seconds: how many seconds to sleep before retrying
    max_tries: maximum number of attmpts to make request

  Returns:
    The result on success or None on masked errors.
  """
  if not masked_errors:
    masked_errors = _DEFAULT_MASKED_ERRORS
  if not retried_errors:
    retried_errors = _DEFAULT_RETRIED_ERRORS

  last_error = None
  for _ in range(max_tries):
    try:
      return http_request.execute()
    except http_client.errors.HttpError as e:
      last_error = e
      if e.resp.status in masked_errors:
        return None
      elif e.resp.status in retried_errors:
        time.sleep(retry_delay_seconds)
      else:
        # Server Error is server error
        raise e

  # We've gone through the max_retries, raise the last error
  raise last_error  # pylint: disable=raising-bad-type


def create_client(http):
  """Creates an Android build api client from an authorized http object.

  Args:
     http: An authorized httplib2.Http object.

  Returns:
    An authorized android build api client.
  """
  return build(serviceName='androidbuildinternal', version='v2beta1', http=http)


def create_client_from_json_keyfile(json_keyfile_name=None):
  """Creates an Android build api client from a json keyfile.

  Args:
    json_keyfile_name: The location of the keyfile, if None is provided use
                       default location.

  Returns:
    An authorized android build api client.
  """
  if not json_keyfile_name:
    json_keyfile_name = os.path.join(os.getenv('HOME'), _DEF_JSON_KEYFILE)

  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      filename=json_keyfile_name, scopes=_SCOPE_URL)
  http = httplib2.Http()
  credentials.authorize(http)
  return create_client(http)


def create_client_from_p12(robot_credentials_file, robot_username):
  """Creates an Android build api client from a config file.

  Args:
    robot_credentials_file: The path to the robot credentials file.
    robot_username: A string containing the username of the robot account.

  Returns:
    An authorized android build api client.
  """
  http = _create_http_from_p12(robot_credentials_file, robot_username)
  return create_client(http)


def fetch_artifact(client, build_id, target, resource_id, dest):
  """Fetches an artifact.

  Args:
    client: An authorized android build api client.
    build_id: AB build id
    target: the target name to download from
    resource_id: the resource id of the artifact
    dest: path to store the artifact
  """
  out_dir = os.path.dirname(dest)
  if not os.path.exists(out_dir):
    os.makedirs(out_dir)

  dl_req = client.buildartifact().get_media(
      buildId=build_id,
      target=target,
      attemptId='latest',
      resourceId=resource_id)

  print('Fetching %s to %s...' % (resource_id, dest))
  with io.FileIO(dest, mode='wb') as fh:
    downloader = apiclient.http.MediaIoBaseDownload(
        fh, dl_req, chunksize=_DEFAULT_CHUNK_SIZE)
    done = False
    while not done:
      status, done = downloader.next_chunk(num_retries=_DEFAULT_RETRIES)
      print('Fetching...' + str(status.progress() * 100))

  print('Done Fetching %s to %s' % (resource_id, dest))


def get_build_list(client, **kwargs):
  """Get a list of builds from the android build api that matches parameters.

  Args:
    client: An authorized android build api client.
    **kwargs: keyworded arguments to pass to build api.

  Returns:
    Response from build api.
  """
  build_request = client.build().list(**kwargs)

  return _simple_execute(build_request)


def list_artifacts(client, regex, **kwargs):
  """List artifacts from the android build api that matches parameters.

  Args:
    client: An authorized android build api client.
    regex: Regular expression pattern to match artifact name.
    **kwargs: keyworded arguments to pass to buildartifact.list api.

  Returns:
    List of matching artifact names.
  """
  matching_artifacts = []
  kwargs.setdefault('attemptId', 'latest')
  regex = re.compile(regex)
  req = client.buildartifact().list(**kwargs)
  while req:
    result = _simple_execute(req)
    if result and 'artifacts' in result:
      for a in result['artifacts']:
        if regex.match(a['name']):
          matching_artifacts.append(a['name'])
    req = client.buildartifact().list_next(req, result)
  return matching_artifacts


def fetch_artifacts(client, out_dir, target, pattern, build_id):
  """Fetches target files artifacts matching patterns.

  Args:
    client: An authorized instance of an android build api client for making
      requests.
    out_dir: The directory to store the fetched artifacts to.
    target: The target name to download from.
    pattern: A regex pattern to match to artifacts filename.
    build_id: The Android Build id.
  """
  if not os.path.exists(out_dir):
    os.makedirs(out_dir)

  # Build a list of needed artifacts
  artifacts = list_artifacts(
      client=client,
      regex=pattern,
      buildId=build_id,
      target=target)

  for artifact in artifacts:
    fetch_artifact(
        client=client,
        build_id=build_id,
        target=target,
        resource_id=artifact,
        dest=os.path.join(out_dir, artifact))


def get_latest_build_id(client, branch, target):
  """Get the latest build id.

  Args:
    client: An authorized instance of an android build api client for making
      requests.
    branch: The branch to download from
    target: The target name to download from.
  Returns:
    The build id.
  """
  build_response = get_build_list(
      client=client,
      branch=branch,
      target=target,
      maxResults=1,
      successful=True,
      buildType='submitted')

  if not build_response:
    raise ValueError('Unable to determine latest build ID!')

  return build_response['builds'][0]['buildId']


def fetch_latest_artifacts(client, out_dir, target, pattern, branch):
  """Fetches target files artifacts matching patterns from the latest build.

  Args:
    client: An authorized instance of an android build api client for making
      requests.
    out_dir: The directory to store the fetched artifacts to.
    target: The target name to download from.
    pattern: A regex pattern to match to artifacts filename
    branch: The branch to download from
  """
  build_id = get_latest_build_id(
      client=client, branch=branch, target=target)

  fetch_artifacts(client, out_dir, target, pattern, build_id)