|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved. |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +from multiprocessing import Pool |
| 6 | +import os |
| 7 | +import sys |
| 8 | +from typing import List |
| 9 | + |
| 10 | +import requests |
| 11 | + |
| 12 | +gh_token = os.environ["GITHUB_TOKEN"] |
| 13 | + |
| 14 | + |
| 15 | +def upload_file(args): |
| 16 | + try: |
| 17 | + url, base, name = args |
| 18 | + |
| 19 | + headers = { |
| 20 | + "Authentication": f"Bearer {gh_token}", |
| 21 | + } |
| 22 | + r = requests.put(url, headers=headers, allow_redirects=False) |
| 23 | + if not r.ok: |
| 24 | + return name, f"Unable to get signed url HTTP_{r.status_code} - {r.text}" |
| 25 | + |
| 26 | + url = r.headers["location"] |
| 27 | + path = os.path.join(base, name) |
| 28 | + r = requests.put( |
| 29 | + url, data=open(path, "rb"), headers={"Content-type": "application/octet-stream"} |
| 30 | + ) |
| 31 | + if not r.ok: |
| 32 | + return name, f"Unable to upload content HTTP_{r.status_code} - {r.text}" |
| 33 | + |
| 34 | + return name, None |
| 35 | + except Exception as e: |
| 36 | + return name, str(e) |
| 37 | + |
| 38 | + |
| 39 | +def get_files_to_publish(path: str) -> List[str]: |
| 40 | + paths = [] |
| 41 | + for root, dirs, files in os.walk(path): |
| 42 | + for file in files: |
| 43 | + paths.append(os.path.join(root, file)[len(path) :]) |
| 44 | + return paths |
| 45 | + |
| 46 | + |
| 47 | +def main(artifacts_dir: str, base_url: str): |
| 48 | + paths = get_files_to_publish(artifacts_dir) |
| 49 | + print(f"= Found {len(paths)} files to publish", flush=True) |
| 50 | + |
| 51 | + failed = False |
| 52 | + work = [(f"{base_url}{x}", artifacts_dir, x) for x in paths] |
| 53 | + with Pool(5) as p: |
| 54 | + results = p.imap_unordered(upload_file, work) |
| 55 | + for i, res in enumerate(results): |
| 56 | + name, err = res |
| 57 | + print(f"= {i+1} of {len(work)} - {name}", flush=True) |
| 58 | + if err: |
| 59 | + print(f"|-> ERROR: {err}", flush=True) |
| 60 | + failed = True |
| 61 | + |
| 62 | + if failed: |
| 63 | + sys.exit(1) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + BUILD_DIR = os.environ["BUILD_DIR"] |
| 68 | + if BUILD_DIR[-1] != "/": |
| 69 | + BUILD_DIR = BUILD_DIR + "/" |
| 70 | + |
| 71 | + URL = os.environ["URL"] |
| 72 | + if URL[-1] != "/": |
| 73 | + URL = URL + "/" |
| 74 | + |
| 75 | + main(BUILD_DIR, URL) |
0 commit comments