|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from argparse import ArgumentParser, Namespace |
| 4 | +from pathlib import Path |
| 5 | +from typing import Mapping, Optional |
| 6 | +import sys |
| 7 | + |
| 8 | +import requests |
| 9 | + |
| 10 | +# Our scripts/ is not a proper Python package so we need to modify PYTHONPATH to import from it |
| 11 | +# pragma pylint: disable=import-error,wrong-import-position |
| 12 | +SCRIPTS_DIR = Path(__file__).parent.parent |
| 13 | +sys.path.insert(0, str(SCRIPTS_DIR)) |
| 14 | + |
| 15 | +from common.git_helpers import git_current_branch, git_commit_hash |
| 16 | +from common.rest_api_helpers import APIHelperError, CircleCI, Github, download_file |
| 17 | +# pragma pylint: enable=import-error,wrong-import-position |
| 18 | + |
| 19 | + |
| 20 | +def process_commandline() -> Namespace: |
| 21 | + script_description = ( |
| 22 | + "Downloads benchmark results attached as artifacts to the c_ext_benchmarks job on CircleCI. " |
| 23 | + "If no options are specified, downloads results for the currently checked out git branch." |
| 24 | + ) |
| 25 | + |
| 26 | + parser = ArgumentParser(description=script_description) |
| 27 | + |
| 28 | + target_definition = parser.add_mutually_exclusive_group() |
| 29 | + target_definition.add_argument( |
| 30 | + '--branch', |
| 31 | + dest='branch', |
| 32 | + help="Git branch that the job ran on.", |
| 33 | + ) |
| 34 | + target_definition.add_argument( |
| 35 | + '--pr', |
| 36 | + dest='pull_request_id', |
| 37 | + type=int, |
| 38 | + help="Github PR ID that the job ran on.", |
| 39 | + ) |
| 40 | + target_definition.add_argument( |
| 41 | + '--base-of-pr', |
| 42 | + dest='base_of_pr', |
| 43 | + type=int, |
| 44 | + help="ID of a Github PR that's based on top of the branch we're interested in." |
| 45 | + ) |
| 46 | + |
| 47 | + parser.add_argument( |
| 48 | + '--any-commit', |
| 49 | + dest='ignore_commit_hash', |
| 50 | + default=False, |
| 51 | + action='store_true', |
| 52 | + help="Include pipelines that ran on a different commit as long as branch/PR matches." |
| 53 | + ) |
| 54 | + parser.add_argument( |
| 55 | + '--overwrite', |
| 56 | + dest='overwrite', |
| 57 | + default=False, |
| 58 | + action='store_true', |
| 59 | + help="If artifacts already exist on disk, overwrite them.", |
| 60 | + ) |
| 61 | + parser.add_argument( |
| 62 | + '--debug-requests', |
| 63 | + dest='debug_requests', |
| 64 | + default=False, |
| 65 | + action='store_true', |
| 66 | + help="Print detailed info about performed API requests and received responses.", |
| 67 | + ) |
| 68 | + |
| 69 | + return parser.parse_args() |
| 70 | + |
| 71 | + |
| 72 | +def download_benchmark_artifact( |
| 73 | + artifacts: Mapping[str, dict], |
| 74 | + benchmark_name: str, |
| 75 | + branch: str, |
| 76 | + commit_hash: str, |
| 77 | + overwrite: bool, |
| 78 | + silent: bool = False |
| 79 | +): |
| 80 | + if not silent: |
| 81 | + print(f"Downloading artifact: {benchmark_name}-{branch}-{commit_hash[:8]}.json.") |
| 82 | + |
| 83 | + artifact_path = f'reports/externalTests/{benchmark_name}.json' |
| 84 | + |
| 85 | + if artifact_path not in artifacts: |
| 86 | + raise RuntimeError(f"Missing artifact: {artifact_path}.") |
| 87 | + |
| 88 | + download_file( |
| 89 | + artifacts[artifact_path]['url'], |
| 90 | + Path(f'{benchmark_name}-{branch}-{commit_hash[:8]}.json'), |
| 91 | + overwrite, |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def download_benchmarks( |
| 96 | + branch: Optional[str], |
| 97 | + pull_request_id: Optional[int], |
| 98 | + base_of_pr: Optional[int], |
| 99 | + ignore_commit_hash: bool = False, |
| 100 | + overwrite: bool = False, |
| 101 | + debug_requests: bool = False, |
| 102 | + silent: bool = False, |
| 103 | +): |
| 104 | + github = Github('ethereum/solidity', debug_requests) |
| 105 | + circleci = CircleCI('ethereum/solidity', debug_requests) |
| 106 | + |
| 107 | + expected_commit_hash = None |
| 108 | + if branch is None and pull_request_id is None and base_of_pr is None: |
| 109 | + branch = git_current_branch() |
| 110 | + expected_commit_hash = git_commit_hash() |
| 111 | + elif branch is not None: |
| 112 | + expected_commit_hash = git_commit_hash(branch) |
| 113 | + elif pull_request_id is not None: |
| 114 | + pr_info = github.pull_request(pull_request_id) |
| 115 | + branch = pr_info['head']['ref'] |
| 116 | + expected_commit_hash = pr_info['head']['sha'] |
| 117 | + elif base_of_pr is not None: |
| 118 | + pr_info = github.pull_request(base_of_pr) |
| 119 | + branch = pr_info['base']['ref'] |
| 120 | + expected_commit_hash = pr_info['base']['sha'] |
| 121 | + |
| 122 | + if not silent: |
| 123 | + print( |
| 124 | + f"Looking for pipelines that ran on branch {branch}" + |
| 125 | + (f", commit {expected_commit_hash}." if not ignore_commit_hash else " (any commit).") |
| 126 | + ) |
| 127 | + |
| 128 | + pipeline = circleci.latest_item(circleci.pipelines( |
| 129 | + branch, |
| 130 | + expected_commit_hash if not ignore_commit_hash else None, |
| 131 | + # Skip nightly workflows. They don't have the c_ext_benchmarks job and even if they did, |
| 132 | + # they would likely be running a different set of external tests. |
| 133 | + excluded_trigger_types=['schedule'], |
| 134 | + )) |
| 135 | + if pipeline is None: |
| 136 | + raise RuntimeError("No matching pipelines found.") |
| 137 | + |
| 138 | + actual_commit_hash = pipeline['vcs']['revision'] |
| 139 | + workflow_id = circleci.latest_item(circleci.workflows(pipeline['id']))['id'] |
| 140 | + benchmark_collector_job = circleci.job(workflow_id, 'c_ext_benchmarks', require_success=True) |
| 141 | + |
| 142 | + artifacts = circleci.artifacts(int(benchmark_collector_job['job_number'])) |
| 143 | + |
| 144 | + download_benchmark_artifact(artifacts, 'summarized-benchmarks', branch, actual_commit_hash, overwrite, silent) |
| 145 | + download_benchmark_artifact(artifacts, 'all-benchmarks', branch, actual_commit_hash, overwrite, silent) |
| 146 | + |
| 147 | + |
| 148 | +def main(): |
| 149 | + try: |
| 150 | + options = process_commandline() |
| 151 | + download_benchmarks( |
| 152 | + options.branch, |
| 153 | + options.pull_request_id, |
| 154 | + options.base_of_pr, |
| 155 | + options.ignore_commit_hash, |
| 156 | + options.overwrite, |
| 157 | + options.debug_requests, |
| 158 | + ) |
| 159 | + |
| 160 | + return 0 |
| 161 | + except APIHelperError as exception: |
| 162 | + print(f"[ERROR] {exception}", file=sys.stderr) |
| 163 | + return 1 |
| 164 | + except requests.exceptions.HTTPError as exception: |
| 165 | + print(f"[ERROR] {exception}", file=sys.stderr) |
| 166 | + return 1 |
| 167 | + except RuntimeError as exception: |
| 168 | + print(f"[ERROR] {exception}", file=sys.stderr) |
| 169 | + return 1 |
| 170 | + |
| 171 | +if __name__ == '__main__': |
| 172 | + sys.exit(main()) |
0 commit comments