xref: /aosp_15_r20/kernel/tests/tools/fetch_artifact.sh (revision 2f2c4c7ab4226c71756b9c31670392fdd6887c4f)
1#!/usr/bin/env bash
2# SPDX-License-Identifier: GPL-2.0
3
4# fetch_artifact .sh is a handy tool dedicated to download artifacts from ci.
5# The fetch_artifact binary is needed for this script. Please see more info at:
6#    go/fetch_artifact,
7#    or
8#    https://android.googlesource.com/tools/fetch_artifact/
9# Will use x20 binary: /google/data/ro/projects/android/fetch_artifact by default.
10# Can install fetch_artifact locally with:
11# sudo glinux-add-repo android stable && \
12# sudo apt update && \
13# sudo apt install android-fetch-artifact#
14#
15DEFAULT_FETCH_ARTIFACT=/google/data/ro/projects/android/fetch_artifact
16BOLD="$(tput bold)"
17END="$(tput sgr0)"
18GREEN="$(tput setaf 2)"
19RED="$(tput setaf 198)"
20YELLOW="$(tput setaf 3)"
21BLUE="$(tput setaf 34)"
22
23function print_info() {
24    echo "[$MY_NAME]: ${GREEN}$1${END}"
25}
26
27function print_warn() {
28    echo "[$MY_NAME]: ${YELLOW}$1${END}"
29}
30
31function print_error() {
32    echo -e "[$MY_NAME]: ${RED}$1${END}"
33    exit 1
34}
35
36function binary_checker() {
37    if which fetch_artifact &> /dev/null; then
38        FETCH_CMD="fetch_artifact"
39    elif [ ! -z "${FETCH_ARTIFACT}" ] && [ -f "${FETCH_ARTIFACT}" ]; then
40        FETCH_CMD="${FETCH_ARTIFACT}"
41    elif [ -f "$DEFAULT_FETCH_ARTIFACT" ]; then
42        FETCH_CMD="$DEFAULT_FETCH_ARTIFACT"
43    else
44        print_error "\n${RED} fetch_artifact is not found${END}"
45        echo -e "\n${RED} Please see go/fetch_artifact${END} or
46        https://android.googlesource.com/tools/fetch_artifact/+/refs/heads/main"
47        exit 1
48    fi
49}
50
51
52binary_checker
53
54fetch_cli="$FETCH_CMD"
55
56BUILD_INFO=
57BUILD_FORMAT="ab://<branch>/<build_target>/<build_id>/<file_name>"
58EXTRA_OPTIONS=
59
60MY_NAME="${0##*/}"
61
62for i in "$@"; do
63    case $i in
64        "ab://"*)
65        BUILD_INFO=$i
66        ;;
67        *)
68        EXTRA_OPTIONS+=" $i"
69        ;;
70    esac
71done
72if [ -z "$BUILD_INFO" ]; then
73    print_error "$0 didn't come with the expected $BUILD_FORMAT"
74fi
75
76IFS='/' read -ra array <<< "$BUILD_INFO"
77if [ ${#array[@]} -lt 6 ]; then
78    print_error "Invalid build format: $BUILD_INFO. Needs to be: $BUILD_FORMAT"
79elif [ ${#array[@]} -gt 7 ]; then
80    print_error "Invalid TEST_DIR format: $BUILD_INFO. Needs to be: $BUILD_FORMAT"
81else
82    fetch_cli+=" --branch ${array[2]}"
83    fetch_cli+=" --target ${array[3]}"
84    if [[ "${array[4]}" != latest* ]]; then
85        fetch_cli+=" --bid ${array[4]}"
86    else
87        fetch_cli+=" --latest"
88    fi
89    fetch_cli+="$EXTRA_OPTIONS"
90    fetch_cli+=" '${array[5]}'"
91fi
92
93print_info "Run: $fetch_cli"
94eval "$fetch_cli"
95