-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget-dependencies.sh
executable file
·162 lines (127 loc) · 2.94 KB
/
get-dependencies.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env bash
# shellcheck disable=SC2016
set -euo pipefail
ARCH="$(uname -m)"
GITREPO=""
GITREF=""
OPT_DEPSPEC=()
ROOT=$(git rev-parse --show-toplevel 2>/dev/null || dirname "$(readlink -f "${0}")")
CONFIG="${ROOT}/config.yml"
declare -A DEPS=(
[jq]=jq
[yq]=yq
[docker]=docker
)
_OPTS=$(getopt --name "$0" --long 'help,arch:,gitrepo:,gitref:' --options 'help,a:' -- "$@")
eval set -- "${_OPTS}"
unset _OPTS
# ----
SELF=$(basename -- "$(readlink -f -- "${0}")")
log() {
echo "[${SELF}]" "$@"
}
err() {
log >&2 "$@"
exit 1
}
print_help() {
echo "Usage: ${0} [options] [depspec]"
echo
echo "Options:"
echo " -a, --arch <arch> Target architecture"
echo " --gitrepo <url> Source"
echo " --gitref <ref> Git branch/tag/commit"
exit 0
}
# ----
while true; do
case "${1}" in
-h | --help)
print_help
;;
-a | --arch)
ARCH="${2}"
shift 2
;;
--gitrepo)
GITREPO="${2}"
shift 2
;;
--gitref)
GITREF="${2}"
shift 2
;;
--)
shift
break
;;
*)
err "Invalid option: ${1}"
;;
esac
done
OPT_DEPSPEC+=("${@}")
# ----
for dep in "${!DEPS[@]}"; do
command -v "${dep}" >/dev/null 2>&1 || err "Missing dependency: ${DEPS["${dep}"]}"
done
CONFIGJSON=$(cat "${CONFIG}")
yq -e --arg a "${ARCH}" '.builds[$a]' >/dev/null <<< "${CONFIGJSON}" \
|| err "Unsupported arch"
read -r gitrepo gitref \
< <(yq -r '.git | "\(.repo) \(.ref)"' <<< "${CONFIGJSON}")
read -r image abi \
< <(yq -r --arg a "${ARCH}" '.builds[$a] | "\(.image) \(.abi)"' <<< "${CONFIGJSON}")
# shellcheck disable=SC2207
dependency_override=($(yq -r --arg a "${ARCH}" '.builds[$a].dependency_override[]' <<< "${CONFIGJSON}"))
gitrepo="${GITREPO:-${gitrepo}}"
gitref="${GITREF:-${gitref}}"
# ----
get_docker_image() {
log "Getting docker image"
[[ -n "$(docker image ls -q "${image}")" ]] \
|| docker image pull "${image}"
}
get_deps() {
log "Finding dependencies (${ARCH} / ${abi}) for ${gitrepo}@${gitref}"
local deps script
deps=("git+${gitrepo}@${gitref}" "${dependency_override[@]}" "${OPT_DEPSPEC[@]}")
script=$(cat <<EOF
PYTHON="/opt/python/${abi}/bin/python"
REPORT=\$(mktemp)
"\${PYTHON}" -m pip install \
--disable-pip-version-check \
--root-user-action=ignore \
--isolated \
--no-cache-dir \
--check-build-dependencies \
--ignore-installed \
--dry-run \
--report="\${REPORT}" \
"\$@"
yq -y -C \
'
[
.install[]
| select(.is_direct != true)
| .download_info.archive_info.hash |= sub("^(?<hash>[^=]+)="; "\(.hash):")
| {
key: .metadata.name,
value: "\(.metadata.version) --hash=\(.download_info.archive_info.hash)"
}
]
| sort_by(.key | ascii_upcase)
| from_entries
| {"dependencies": .}
' \
"\${REPORT}"
EOF
)
docker run \
--interactive \
--rm \
"${image}" \
/usr/bin/bash /dev/stdin "${deps[@]}" <<< "${script}"
}
get_docker_image
get_deps