1# Copyright (C) 2020 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# 15# Licensed under the Apache License, Version 2.0 (the "License"); 16# you may not use this file except in compliance with the License. 17# You may obtain a copy of the License at 18# 19# http://www.apache.org/licenses/LICENSE-2.0 20# 21# Unless required by applicable law or agreed to in writing, software 22# distributed under the License is distributed on an "AS IS" BASIS, 23# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 24# See the License for the specific language governing permissions and 25# limitations under the License. 26# 27# Licensed under the Apache License, Version 2.0 (the "License"); 28# you may not use this file except in compliance with the License. 29# You may obtain a copy of the License at 30# 31# http://www.apache.org/licenses/LICENSE-2.0 32# 33# Unless required by applicable law or agreed to in writing, software 34# distributed under the License is distributed on an "AS IS" BASIS, 35# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 36# See the License for the specific language governing permissions and 37# limitations under the License. 38 39 40""" Utility functions for logstorage. """ 41from __future__ import print_function 42 43import logging 44import constants 45 46# pylint: disable=import-error 47try: 48 import httplib2 49 from googleapiclient.discovery import build 50except ImportError as e: 51 logging.debug('Import error due to: %s', e) 52 53 54class BuildClient: 55 """Build api helper class.""" 56 57 def __init__(self, creds): 58 """Init BuildClient class. 59 Args: 60 creds: An oauth2client.OAuth2Credentials instance. 61 """ 62 http_auth = creds.authorize(httplib2.Http()) 63 self.client = build( 64 serviceName=constants.STORAGE_SERVICE_NAME, 65 version=constants.STORAGE_API_VERSION, 66 cache_discovery=False, 67 http=http_auth) 68 69 def list_branch(self): 70 """List all branch.""" 71 return self.client.branch().list(maxResults=10000).execute() 72 73 def list_target(self, branch): 74 """List all target in the branch.""" 75 return self.client.target().list(branch=branch, 76 maxResults=10000).execute() 77 78 def insert_local_build(self, external_id, target, branch): 79 """Insert a build record. 80 Args: 81 external_id: unique id of build record. 82 target: build target. 83 branch: build branch. 84 85 Returns: 86 An build record object. 87 """ 88 body = { 89 "buildId": "", 90 "externalId": external_id, 91 "branch": branch, 92 "target": { 93 "name": target, 94 "target": target 95 }, 96 "buildAttemptStatus": "complete", 97 } 98 return self.client.build().insert(buildType="local", 99 body=body).execute() 100 101 def insert_build_attempts(self, build_record): 102 """Insert a build attempt record. 103 Args: 104 build_record: build record. 105 106 Returns: 107 An build attempt object. 108 """ 109 build_attempt = { 110 "id": 0, 111 "status": "complete", 112 "successful": True 113 } 114 return self.client.buildattempt().insert( 115 buildId=build_record['buildId'], 116 target=build_record['target']['name'], 117 body=build_attempt).execute() 118 119 def insert_invocation(self, build_record): 120 """Insert a build invocation record. 121 Args: 122 build_record: build record. 123 124 Returns: 125 A build invocation object. 126 """ 127 invocation = { 128 "primaryBuild": { 129 "buildId": build_record['buildId'], 130 "buildTarget": build_record['target']['name'], 131 "branch": build_record['branch'], 132 }, 133 "schedulerState": "running" 134 } 135 return self.client.invocation().insert(body=invocation).execute() 136 137 def update_invocation(self, invocation): 138 """Insert a build invocation record. 139 Args: 140 invocation: invocation record. 141 142 Returns: 143 A invocation object. 144 """ 145 return self.client.invocation().update( 146 resourceId=invocation['invocationId'], 147 body=invocation).execute() 148 149 def insert_work_unit(self, invocation_record): 150 """Insert a workunit record. 151 Args: 152 invocation_record: invocation record. 153 154 Returns: 155 the workunit object. 156 """ 157 workunit = { 158 'invocationId': invocation_record['invocationId'] 159 } 160 return self.client.workunit().insert(body=workunit).execute() 161