forked from simonswine/pyroscope-ebpf-testdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_python.sh
More file actions
executable file
·178 lines (150 loc) · 4.5 KB
/
Copy pathbuild_python.sh
File metadata and controls
executable file
·178 lines (150 loc) · 4.5 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env bash
set -euf -o pipefail
size=5; check=0; install=0; archs="amd64 arm64";
while getopts 'hcip:a:' opt
do
case "$opt" in
c)
check=1
;;
i)
install=1
;;
p)
size=$OPTARG
;;
a)
archs=$OPTARG
;;
*)
echo "Usage: $0 [-c] [-i] [-p] [-a]"
echo " -c: check versions"
echo " -i: install and configure asdf"
echo " -p: number of parallel builds"
echo " -a: architectures [options: amd64, arm64]"
exit 1
;;
esac
done
# Sanitary checks
if [ ${size} -le 0 ]
then
echo "Number of parallel builds -p should be more than or equal to 1. Current value is ${size}"
exit 1
fi
if [ -z "${archs}" ]
then
echo "-a should not be empty. Accepted values are amd64, arm64 or both"
exit 1
fi
# Check if archs are valid
VALID_ARCHS=("amd64" "arm64")
for arch in $archs; do
if ! printf '%s\0' "${VALID_ARCHS[@]}" | grep -Fqxz -- "${arch}"; then
echo "Not a valid arch value. Accepted values are amd64, arm64 or both"
exit 1
fi
done
function join() {
local IFS="$1"
shift
echo "$*"
}
function build_image() {
local version=$1
local arch=$2
version_minor=$(echo ${version} | cut -d. -f1-2)
if [ "${arch}" == "amd64" ]
then
output_dir="python-x64/${version}/"
else
output_dir="python-arm64/${version}/"
fi
docker build \
--platform=linux/${arch} \
--build-arg=PYTHON_VERSION_PATCH=${version} \
--build-arg=PYTHON_VERSION_MINOR=${version_minor} \
-f dockerfiles/python.Dockerfile \
--output=${output_dir} \
dockerfiles
}
# Install and configure asdf
if [ ${install} -ne 0 ]
then
wget -O /tmp/asdf.tar.gz https://github.com/asdf-vm/asdf/releases/download/v0.19.0/asdf-v0.19.0-linux-amd64.tar.gz >/dev/null 2>&1
tar -xvf /tmp/asdf.tar.gz >/dev/null 2>&1
# Ensure $HOME/.local/bin exists and move asdf there
mkdir -p $HOME/.local/bin
mv asdf $HOME/.local/bin
# Add python plugin
asdf plugin add python https://github.com/danhper/asdf-python.git >/dev/null 2>&1
echo "asdf installed at $HOME/.local/bin and configured"
fi
# Make sure $HOME/.local/bin is on PATH
PATH="$HOME/.local/bin:$PATH"
# Check if asdf is available
if ! command -v asdf >/dev/null 2>&1
then
echo "asdf could not be found. Use $0 -i to install and configure asdf"
exit 1
fi
# Get all versions of Python
PYTHON_ALL_VERSIONS=$(asdf list all python | grep "^3\." | grep -v dev \
| grep -v "^3\.[0-5]\." | grep -v "t" | grep -v "a" | grep -v "b" | grep -v "rc")
# Initialise missing versions
MISSING_VERSIONS_AMD64=()
MISSING_VERSIONS_ARM64=()
# Check missing versions
for version in $PYTHON_ALL_VERSIONS; do
stat "python-x64/$version" >/dev/null 2>&1 || MISSING_VERSIONS_AMD64=("${MISSING_VERSIONS_AMD64[@]}" "$version")
stat "python-arm64/$version" >/dev/null 2>&1 || MISSING_VERSIONS_ARM64=("${MISSING_VERSIONS_ARM64[@]}" "$version")
done
# If only dry run, print missing versions and exit
if [ ${check} -ne 0 ]
then
echo "Missing version for amd64: ${MISSING_VERSIONS_AMD64[@]}"
echo "Missing version for arm64: ${MISSING_VERSIONS_ARM64[@]}"
exit 0
fi
if [ ${#MISSING_VERSIONS_AMD64[@]} -eq 0 ] && [ ${#MISSING_VERSIONS_ARM64[@]} -eq 0 ]; then
echo "No missing version for amd64 and arm64 archs. Exiting..."
exit 0
fi
# Make platforms
PLATFORMS=()
for arch in $archs; do
PLATFORMS=("${PLATFORMS[@]}" "linux/${arch}")
done
PLATFORMS=$(join , "${PLATFORMS[@]}")
# Make base image
docker buildx build \
--platform=${PLATFORMS} \
-f dockerfiles/python.base.Dockerfile \
-t python-base:latest \
dockerfiles
# Build Python libs for AMD64
if [[ $archs =~ "amd64" ]]; then
index=0
while [[ $index -lt ${#MISSING_VERSIONS_AMD64[@]} ]]; do
for version in "${MISSING_VERSIONS_AMD64[@]:$index:$size}"; do
echo "Building library for version ${version} and arch amd64"
build_image "${version}" "amd64" &
done
# Wait for all processes to finish before moving on to next chunk
wait
index=$((index + size))
done
fi
# Build Python libs foor ARM64
if [[ $archs =~ "arm64" ]]; then
index=0
while [[ $index -lt ${#MISSING_VERSIONS_ARM64[@]} ]]; do
for version in "${MISSING_VERSIONS_ARM64[@]:$index:$size}"; do
echo "Building library for version ${version} and arch arm64"
build_image "${version}" "arm64" &
done
# Wait for all processes to finish before moving on to next chunk
wait
index=$((index + size))
done
fi