1*344a7f5eSAndroid Build Coastguard Worker#!/usr/bin/python3 2*344a7f5eSAndroid Build Coastguard Worker 3*344a7f5eSAndroid Build Coastguard Workerimport os 4*344a7f5eSAndroid Build Coastguard Workerimport subprocess 5*344a7f5eSAndroid Build Coastguard Workerimport zipfile 6*344a7f5eSAndroid Build Coastguard Worker 7*344a7f5eSAndroid Build Coastguard Workerfrom utils import print_e, mv 8*344a7f5eSAndroid Build Coastguard Worker 9*344a7f5eSAndroid Build Coastguard Worker# See go/fetch_artifact for details on this script. 10*344a7f5eSAndroid Build Coastguard WorkerFETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact' 11*344a7f5eSAndroid Build Coastguard WorkerFETCH_ARTIFACT_BEYOND_CORP = '/usr/bin/fetch_artifact' 12*344a7f5eSAndroid Build Coastguard Worker 13*344a7f5eSAndroid Build Coastguard Worker 14*344a7f5eSAndroid Build Coastguard Workerclass BuildId(object): 15*344a7f5eSAndroid Build Coastguard Worker def __init__(self, url_id, fs_id): 16*344a7f5eSAndroid Build Coastguard Worker # id when used in build server urls 17*344a7f5eSAndroid Build Coastguard Worker self.url_id = url_id 18*344a7f5eSAndroid Build Coastguard Worker # id when used in build commands 19*344a7f5eSAndroid Build Coastguard Worker self.fs_id = fs_id 20*344a7f5eSAndroid Build Coastguard Worker 21*344a7f5eSAndroid Build Coastguard Worker 22*344a7f5eSAndroid Build Coastguard Workerdef fetch_artifact(target, build_id, artifact_path, beyond_corp): 23*344a7f5eSAndroid Build Coastguard Worker download_to = os.path.join('.', os.path.dirname(artifact_path)) 24*344a7f5eSAndroid Build Coastguard Worker print(f'Fetching {artifact_path} from {target}...') 25*344a7f5eSAndroid Build Coastguard Worker if not os.path.exists(download_to): 26*344a7f5eSAndroid Build Coastguard Worker os.makedirs(download_to) 27*344a7f5eSAndroid Build Coastguard Worker if beyond_corp: 28*344a7f5eSAndroid Build Coastguard Worker fetch_cmd = [FETCH_ARTIFACT_BEYOND_CORP, '--use_oauth2', 29*344a7f5eSAndroid Build Coastguard Worker '--bid', str(build_id), '--target', target, artifact_path, download_to] 30*344a7f5eSAndroid Build Coastguard Worker else: 31*344a7f5eSAndroid Build Coastguard Worker fetch_cmd = [FETCH_ARTIFACT, 32*344a7f5eSAndroid Build Coastguard Worker '--bid', str(build_id), '--target', target, artifact_path, download_to] 33*344a7f5eSAndroid Build Coastguard Worker print('Running: ' + ' '.join(fetch_cmd)) 34*344a7f5eSAndroid Build Coastguard Worker try: 35*344a7f5eSAndroid Build Coastguard Worker subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT) 36*344a7f5eSAndroid Build Coastguard Worker except subprocess.CalledProcessError: 37*344a7f5eSAndroid Build Coastguard Worker print_e(f'FAIL: Unable to retrieve {artifact_path} artifact for build ID {build_id}') 38*344a7f5eSAndroid Build Coastguard Worker print_e('Please make sure you are authenticated for build server access!') 39*344a7f5eSAndroid Build Coastguard Worker return None 40*344a7f5eSAndroid Build Coastguard Worker return artifact_path 41*344a7f5eSAndroid Build Coastguard Worker 42*344a7f5eSAndroid Build Coastguard Worker 43*344a7f5eSAndroid Build Coastguard Workerdef fetch_artifacts(target, build_id, artifact_dict, beyond_corp): 44*344a7f5eSAndroid Build Coastguard Worker for artifact, target_path in artifact_dict.items(): 45*344a7f5eSAndroid Build Coastguard Worker artifact_path = fetch_artifact(target, build_id.url_id, artifact, beyond_corp) 46*344a7f5eSAndroid Build Coastguard Worker if not artifact_path: 47*344a7f5eSAndroid Build Coastguard Worker return False 48*344a7f5eSAndroid Build Coastguard Worker mv(artifact_path, target_path) 49*344a7f5eSAndroid Build Coastguard Worker return True 50*344a7f5eSAndroid Build Coastguard Worker 51*344a7f5eSAndroid Build Coastguard Worker 52*344a7f5eSAndroid Build Coastguard Workerdef extract_artifact(artifact_path): 53*344a7f5eSAndroid Build Coastguard Worker # Unzip the repo archive into a separate directory. 54*344a7f5eSAndroid Build Coastguard Worker repo_dir = os.path.splitext(artifact_path)[0] 55*344a7f5eSAndroid Build Coastguard Worker with zipfile.ZipFile(artifact_path) as zipFile: 56*344a7f5eSAndroid Build Coastguard Worker zipFile.extractall(repo_dir) 57*344a7f5eSAndroid Build Coastguard Worker return repo_dir 58*344a7f5eSAndroid Build Coastguard Worker 59*344a7f5eSAndroid Build Coastguard Worker 60*344a7f5eSAndroid Build Coastguard Workerdef fetch_and_extract(target, build_id, file, beyond_corp, artifact_path=None): 61*344a7f5eSAndroid Build Coastguard Worker if not artifact_path: 62*344a7f5eSAndroid Build Coastguard Worker artifact_path = fetch_artifact(target, build_id, file, beyond_corp) 63*344a7f5eSAndroid Build Coastguard Worker if not artifact_path: 64*344a7f5eSAndroid Build Coastguard Worker return None 65*344a7f5eSAndroid Build Coastguard Worker return extract_artifact(artifact_path) 66*344a7f5eSAndroid Build Coastguard Worker 67*344a7f5eSAndroid Build Coastguard Worker 68*344a7f5eSAndroid Build Coastguard Workerdef parse_build_id(source): 69*344a7f5eSAndroid Build Coastguard Worker # must be in the format 12345 or P12345 70*344a7f5eSAndroid Build Coastguard Worker number_text = source 71*344a7f5eSAndroid Build Coastguard Worker presubmit = False 72*344a7f5eSAndroid Build Coastguard Worker if number_text.startswith('P'): 73*344a7f5eSAndroid Build Coastguard Worker presubmit = True 74*344a7f5eSAndroid Build Coastguard Worker number_text = number_text[1:] 75*344a7f5eSAndroid Build Coastguard Worker if not number_text.isnumeric(): 76*344a7f5eSAndroid Build Coastguard Worker return None 77*344a7f5eSAndroid Build Coastguard Worker url_id = source 78*344a7f5eSAndroid Build Coastguard Worker fs_id = url_id 79*344a7f5eSAndroid Build Coastguard Worker if presubmit: 80*344a7f5eSAndroid Build Coastguard Worker fs_id = '0' 81*344a7f5eSAndroid Build Coastguard Worker return BuildId(url_id, fs_id) 82