21702: Merge branch 'main' into 21702-keep-web-replace_files
[arvados.git] / build / run-build-packages-python-and-ruby.sh
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 COLUMNS=80
7
8 . `dirname "$(readlink -f "$0")"`/run-library.sh
9
10 read -rd "\000" helpmessage <<EOF
11 $(basename $0): Build Arvados Python packages and Ruby gems
12
13 Syntax:
14         WORKSPACE=/path/to/arvados $(basename $0) [options]
15
16 Options:
17
18 --debug
19     Output debug information (default: false)
20 --upload
21     If the build and test steps are successful, upload the python
22     packages to pypi and the gems to rubygems (default: false)
23 --ruby <true|false>
24     Build ruby gems (default: true)
25 --python <true|false>
26     Build python packages (default: true)
27
28 WORKSPACE=path         Path to the Arvados source tree to build packages from
29
30 EOF
31
32 exit_cleanly() {
33     trap - INT
34     report_outcomes
35     exit ${#failures[@]}
36 }
37
38 gem_wrapper() {
39   local gem_name="$1"; shift
40   local gem_directory="$1"; shift
41
42   title "Start $gem_name gem build"
43   timer_reset
44
45   cd "$gem_directory"
46   handle_ruby_gem $gem_name
47
48   checkexit $? "$gem_name gem build"
49   title "End of $gem_name gem build (`timer`)"
50 }
51
52 handle_python_package () {
53   # This function assumes the current working directory is the python package directory
54   local -a pkg_fmts=()
55   local version="$(nohash_version_from_git)"
56   if [[ -z "$(find dist -name "*-$version.tar.gz" -print -quit)" ]]; then
57     pkg_fmts+=(sdist)
58   fi
59   if [[ -z "$(find dist -name "*-$version-py*.whl" -print -quit)" ]]; then
60     pkg_fmts+=(bdist_wheel)
61   fi
62   if [[ "${#pkg_fmts[@]}" -eq 0 ]]; then
63     echo "This package doesn't need rebuilding."
64   else
65     python3 setup.py $DASHQ_UNLESS_DEBUG "${pkg_fmts[@]}"
66   fi
67 }
68
69 python_wrapper() {
70   local package_name="$1"; shift
71   local package_directory="$1"; shift
72
73   title "Start $package_name python package build"
74   timer_reset
75
76   cd "$package_directory"
77   if [[ $DEBUG > 0 ]]; then
78     echo `pwd`
79   fi
80   handle_python_package
81
82   checkexit $? "$package_name python package build"
83   title "End of $package_name python package build (`timer`)"
84 }
85
86 TARGET=
87 UPLOAD=0
88 RUBY=1
89 PYTHON=1
90 DEBUG=${ARVADOS_DEBUG:-0}
91
92 PARSEDOPTS=$(getopt --name "$0" --longoptions \
93     help,debug,ruby:,python:,upload,target: \
94     -- "" "$@")
95 if [ $? -ne 0 ]; then
96     exit 1
97 fi
98
99 eval set -- "$PARSEDOPTS"
100 while [ $# -gt 0 ]; do
101     case "$1" in
102         --help)
103             echo >&2 "$helpmessage"
104             echo >&2
105             exit 1
106             ;;
107         --target)
108             TARGET="$2"; shift
109             ;;
110         --ruby)
111             RUBY="$2"; shift
112             if [ "$RUBY" != "true" ] && [ "$RUBY" != "1" ]; then
113               RUBY=0
114             else
115               RUBY=1
116             fi
117             ;;
118         --python)
119             PYTHON="$2"; shift
120             if [ "$PYTHON" != "true" ] && [ "$PYTHON" != "1" ]; then
121               PYTHON=0
122             else
123               PYTHON=1
124             fi
125             ;;
126         --upload)
127             UPLOAD=1
128             ;;
129         --debug)
130             DEBUG=1
131             ;;
132         --)
133             if [ $# -gt 1 ]; then
134                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
135                 exit 1
136             fi
137             ;;
138     esac
139     shift
140 done
141
142 if ! [[ -n "$WORKSPACE" ]]; then
143   echo >&2 "$helpmessage"
144   echo >&2
145   echo >&2 "Error: WORKSPACE environment variable not set"
146   echo >&2
147   exit 1
148 fi
149
150 STDOUT_IF_DEBUG=/dev/null
151 STDERR_IF_DEBUG=/dev/null
152 DASHQ_UNLESS_DEBUG=-q
153 if [[ "$DEBUG" != 0 ]]; then
154     STDOUT_IF_DEBUG=/dev/stdout
155     STDERR_IF_DEBUG=/dev/stderr
156     DASHQ_UNLESS_DEBUG=
157 fi
158
159 RUN_BUILD_PACKAGES_PATH="`dirname \"$0\"`"
160 RUN_BUILD_PACKAGES_PATH="`( cd \"$RUN_BUILD_PACKAGES_PATH\" && pwd )`"  # absolutized and normalized
161 if [ -z "$RUN_BUILD_PACKAGES_PATH" ] ; then
162   # error; for some reason, the path is not accessible
163   # to the script (e.g. permissions re-evaled after suid)
164   exit 1  # fail
165 fi
166
167 debug_echo "$0 is running from $RUN_BUILD_PACKAGES_PATH"
168 debug_echo "Workspace is $WORKSPACE"
169
170 if [ $RUBY -eq 0 ] && [ $PYTHON -eq 0 ]; then
171   echo "Nothing to do!"
172   exit 0
173 fi
174
175 # Make all files world-readable -- jenkins runs with umask 027, and has checked
176 # out our git tree here
177 chmod o+r "$WORKSPACE" -R
178
179 # More cleanup - make sure all executables that we'll package are 755
180 cd "$WORKSPACE"
181 find -type d -name 'bin' |xargs -I {} find {} -type f |xargs -I {} chmod 755 {}
182
183 # Now fix our umask to something better suited to building and publishing
184 # gems and packages
185 umask 0022
186
187 debug_echo "umask is" `umask`
188
189 GEM_BUILD_FAILURES=0
190 if [ $RUBY -eq 1 ]; then
191   debug_echo "Building Ruby gems"
192   gem_wrapper arvados "$WORKSPACE/sdk/ruby"
193   gem_wrapper arvados-cli "$WORKSPACE/sdk/cli"
194   gem_wrapper arvados-login-sync "$WORKSPACE/services/login-sync"
195   if [ ${#failures[@]} -ne 0 ]; then
196     GEM_BUILD_FAILURES=${#failures[@]}
197   fi
198 fi
199
200 PYTHON_BUILD_FAILURES=0
201 if [ $PYTHON -eq 1 ]; then
202   debug_echo "Building Python packages"
203   python_wrapper arvados-python-client "$WORKSPACE/sdk/python"
204   python_wrapper arvados-cwl-runner "$WORKSPACE/sdk/cwl"
205   python_wrapper arvados_fuse "$WORKSPACE/services/fuse"
206   python_wrapper crunchstat_summary "$WORKSPACE/tools/crunchstat-summary"
207   python_wrapper arvados-user-activity "$WORKSPACE/tools/user-activity"
208
209   if [ $((${#failures[@]} - $GEM_BUILD_FAILURES)) -ne 0 ]; then
210     PYTHON_BUILD_FAILURES=$((${#failures[@]} - $GEM_BUILD_FAILURES))
211   fi
212 fi
213
214 if [ $UPLOAD -ne 0 ]; then
215   echo "Uploading"
216
217   if [ $DEBUG > 0 ]; then
218     EXTRA_UPLOAD_FLAGS=" --verbose"
219   else
220     EXTRA_UPLOAD_FLAGS=""
221   fi
222
223   if [ ! -e "$WORKSPACE/packages" ]; then
224     mkdir -p "$WORKSPACE/packages"
225   fi
226
227   if [ $PYTHON -eq 1 ]; then
228     title "Start upload python packages"
229     timer_reset
230
231     if [ $PYTHON_BUILD_FAILURES -eq 0 ]; then
232       /usr/local/arvados-dev/jenkins/run_upload_packages.py $EXTRA_UPLOAD_FLAGS --workspace $WORKSPACE python
233     else
234       echo "Skipping python packages upload, there were errors building the packages"
235     fi
236     checkexit $? "upload python packages"
237     title "End of upload python packages (`timer`)"
238   fi
239
240   if [ $RUBY -eq 1 ]; then
241     title "Start upload ruby gems"
242     timer_reset
243
244     if [ $GEM_BUILD_FAILURES -eq 0 ]; then
245       /usr/local/arvados-dev/jenkins/run_upload_packages.py $EXTRA_UPLOAD_FLAGS --workspace $WORKSPACE gems
246     else
247       echo "Skipping ruby gem upload, there were errors building the packages"
248     fi
249     checkexit $? "upload ruby gems"
250     title "End of upload ruby gems (`timer`)"
251   fi
252 fi
253
254 exit_cleanly