|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Note: This script is intended to be called from the CocoaPods package release pipeline or a similar context. |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import argparse |
| 8 | +import os |
| 9 | +import shlex |
| 10 | +import subprocess |
| 11 | +from enum import Enum |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | + |
| 15 | +class Task(Enum): |
| 16 | + upload_pod_archive = 1 |
| 17 | + update_podspec = 2 |
| 18 | + |
| 19 | + |
| 20 | +def _run(command: list[str], **kwargs): |
| 21 | + print(f"Running command: {shlex.join(command)}", flush=True) |
| 22 | + kwargs.setdefault("check", True) |
| 23 | + return subprocess.run(command, **kwargs) # noqa: PLW1510 # we add 'check' to kwargs if not present |
| 24 | + |
| 25 | + |
| 26 | +def upload_pod_archive(pod_archive_path: Path): |
| 27 | + env = os.environ.copy() |
| 28 | + env.update( |
| 29 | + { |
| 30 | + # configure azcopy to use managed identity |
| 31 | + "AZCOPY_AUTO_LOGIN_TYPE": "MSI", |
| 32 | + "AZCOPY_MSI_CLIENT_ID": "63b63039-6328-442f-954b-5a64d124e5b4", |
| 33 | + } |
| 34 | + ) |
| 35 | + |
| 36 | + storage_account_name = "onnxruntimepackages" |
| 37 | + storage_account_container_name = "$web" |
| 38 | + dest_url = f"https://{storage_account_name}.blob.core.windows.net/{storage_account_container_name}/" |
| 39 | + |
| 40 | + upload_command = ["azcopy", "cp", str(pod_archive_path), dest_url] |
| 41 | + |
| 42 | + _run(upload_command, env=env) |
| 43 | + |
| 44 | + |
| 45 | +def update_podspec(pod_archive_path: Path, podspec_path: Path): |
| 46 | + storage_url = f"https://download.onnxruntime.ai/{pod_archive_path.name}" |
| 47 | + |
| 48 | + podspec_content = podspec_path.read_text() |
| 49 | + podspec_content = podspec_content.replace("file:///http_source_placeholder", storage_url) |
| 50 | + podspec_path.write_text(podspec_content) |
| 51 | + |
| 52 | + |
| 53 | +def _parse_args(): |
| 54 | + parser = argparse.ArgumentParser( |
| 55 | + description="Helper script to perform release tasks. " |
| 56 | + "Mostly useful for the CocoaPods package release pipeline.", |
| 57 | + ) |
| 58 | + |
| 59 | + parser.add_argument( |
| 60 | + "--pod-archive-path", |
| 61 | + type=Path, |
| 62 | + help="Pod archive path.", |
| 63 | + ) |
| 64 | + |
| 65 | + parser.add_argument( |
| 66 | + "--podspec-path", |
| 67 | + type=Path, |
| 68 | + help="Podspec path.", |
| 69 | + ) |
| 70 | + |
| 71 | + parser.add_argument( |
| 72 | + "task", |
| 73 | + choices=[task.name for task in Task], |
| 74 | + help="Specify the task to run.", |
| 75 | + ) |
| 76 | + |
| 77 | + return parser.parse_args() |
| 78 | + |
| 79 | + |
| 80 | +def _validate_args( |
| 81 | + args: argparse.Namespace, require_pod_archive_path: bool = False, require_podspec_path: bool = False |
| 82 | +): |
| 83 | + if require_pod_archive_path: |
| 84 | + assert args.pod_archive_path is not None, "--pod-archive-path must be specified." |
| 85 | + args.pod_archive_path = args.pod_archive_path.resolve(strict=True) |
| 86 | + |
| 87 | + if require_podspec_path: |
| 88 | + assert args.podspec_path is not None, "--podspec-path must be specified." |
| 89 | + args.podspec_path = args.podspec_path.resolve(strict=True) |
| 90 | + |
| 91 | + |
| 92 | +def main(): |
| 93 | + args = _parse_args() |
| 94 | + |
| 95 | + task = Task[args.task] |
| 96 | + |
| 97 | + if task == Task.update_podspec: |
| 98 | + _validate_args(args, require_pod_archive_path=True, require_podspec_path=True) |
| 99 | + update_podspec(args.pod_archive_path, args.podspec_path) |
| 100 | + |
| 101 | + elif task == Task.upload_pod_archive: |
| 102 | + _validate_args(args, require_pod_archive_path=True) |
| 103 | + upload_pod_archive(args.pod_archive_path) |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + main() |
0 commit comments