xref: /aosp_15_r20/external/pytorch/tools/stats/upload_artifacts.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Workerimport argparse
2*da0073e9SAndroid Build Coastguard Workerimport os
3*da0073e9SAndroid Build Coastguard Workerimport re
4*da0073e9SAndroid Build Coastguard Workerfrom tempfile import TemporaryDirectory
5*da0073e9SAndroid Build Coastguard Worker
6*da0073e9SAndroid Build Coastguard Workerfrom tools.stats.upload_stats_lib import download_gha_artifacts, upload_file_to_s3
7*da0073e9SAndroid Build Coastguard Worker
8*da0073e9SAndroid Build Coastguard Worker
9*da0073e9SAndroid Build Coastguard WorkerARTIFACTS = [
10*da0073e9SAndroid Build Coastguard Worker    "sccache-stats",
11*da0073e9SAndroid Build Coastguard Worker    "test-jsons",
12*da0073e9SAndroid Build Coastguard Worker    "test-reports",
13*da0073e9SAndroid Build Coastguard Worker    "usage-log",
14*da0073e9SAndroid Build Coastguard Worker]
15*da0073e9SAndroid Build Coastguard WorkerBUCKET_NAME = "gha-artifacts"
16*da0073e9SAndroid Build Coastguard WorkerFILENAME_REGEX = r"-runattempt\d+"
17*da0073e9SAndroid Build Coastguard Worker
18*da0073e9SAndroid Build Coastguard Worker
19*da0073e9SAndroid Build Coastguard Workerdef get_artifacts(repo: str, workflow_run_id: int, workflow_run_attempt: int) -> None:
20*da0073e9SAndroid Build Coastguard Worker    with TemporaryDirectory() as temp_dir:
21*da0073e9SAndroid Build Coastguard Worker        print("Using temporary directory:", temp_dir)
22*da0073e9SAndroid Build Coastguard Worker        os.chdir(temp_dir)
23*da0073e9SAndroid Build Coastguard Worker
24*da0073e9SAndroid Build Coastguard Worker        for artifact in ARTIFACTS:
25*da0073e9SAndroid Build Coastguard Worker            artifact_paths = download_gha_artifacts(
26*da0073e9SAndroid Build Coastguard Worker                artifact, workflow_run_id, workflow_run_attempt
27*da0073e9SAndroid Build Coastguard Worker            )
28*da0073e9SAndroid Build Coastguard Worker
29*da0073e9SAndroid Build Coastguard Worker            for artifact_path in artifact_paths:
30*da0073e9SAndroid Build Coastguard Worker                # GHA artifact is named as follows: NAME-runattempt${{ github.run_attempt }}-SUFFIX.zip
31*da0073e9SAndroid Build Coastguard Worker                # and we want remove the run_attempt to conform with the naming convention on S3, i.e.
32*da0073e9SAndroid Build Coastguard Worker                # pytorch/pytorch/WORKFLOW_ID/RUN_ATTEMPT/artifact/NAME-SUFFIX.zip
33*da0073e9SAndroid Build Coastguard Worker                s3_filename = re.sub(FILENAME_REGEX, "", artifact_path.name)
34*da0073e9SAndroid Build Coastguard Worker                upload_file_to_s3(
35*da0073e9SAndroid Build Coastguard Worker                    file_name=str(artifact_path.resolve()),
36*da0073e9SAndroid Build Coastguard Worker                    bucket=BUCKET_NAME,
37*da0073e9SAndroid Build Coastguard Worker                    key=f"{repo}/{workflow_run_id}/{workflow_run_attempt}/artifact/{s3_filename}",
38*da0073e9SAndroid Build Coastguard Worker                )
39*da0073e9SAndroid Build Coastguard Worker
40*da0073e9SAndroid Build Coastguard Worker
41*da0073e9SAndroid Build Coastguard Workerif __name__ == "__main__":
42*da0073e9SAndroid Build Coastguard Worker    parser = argparse.ArgumentParser(description="Upload test artifacts from GHA to S3")
43*da0073e9SAndroid Build Coastguard Worker    parser.add_argument(
44*da0073e9SAndroid Build Coastguard Worker        "--workflow-run-id",
45*da0073e9SAndroid Build Coastguard Worker        type=int,
46*da0073e9SAndroid Build Coastguard Worker        required=True,
47*da0073e9SAndroid Build Coastguard Worker        help="id of the workflow to get artifacts from",
48*da0073e9SAndroid Build Coastguard Worker    )
49*da0073e9SAndroid Build Coastguard Worker    parser.add_argument(
50*da0073e9SAndroid Build Coastguard Worker        "--workflow-run-attempt",
51*da0073e9SAndroid Build Coastguard Worker        type=int,
52*da0073e9SAndroid Build Coastguard Worker        required=True,
53*da0073e9SAndroid Build Coastguard Worker        help="which retry of the workflow this is",
54*da0073e9SAndroid Build Coastguard Worker    )
55*da0073e9SAndroid Build Coastguard Worker    parser.add_argument(
56*da0073e9SAndroid Build Coastguard Worker        "--repo",
57*da0073e9SAndroid Build Coastguard Worker        type=str,
58*da0073e9SAndroid Build Coastguard Worker        required=True,
59*da0073e9SAndroid Build Coastguard Worker        help="which GitHub repo this workflow run belongs to",
60*da0073e9SAndroid Build Coastguard Worker    )
61*da0073e9SAndroid Build Coastguard Worker    args = parser.parse_args()
62*da0073e9SAndroid Build Coastguard Worker    get_artifacts(args.repo, args.workflow_run_id, args.workflow_run_attempt)
63