1#!/bin/bash
2# Copyright 2016 gRPC authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16set -ex
17
18cd "$(dirname "$0")/../../.."
19
20export GRPC_PYTHON_BUILD_WITH_CYTHON=1
21export PYTHON=${PYTHON:-python}
22export PIP=${PIP:-pip}
23export AUDITWHEEL=${AUDITWHEEL:-auditwheel}
24
25mkdir -p "${ARTIFACTS_OUT}"
26ARTIFACT_DIR="$PWD/${ARTIFACTS_OUT}"
27
28# Build the source distribution first because MANIFEST.in cannot override
29# exclusion of built shared objects among package resources (for some
30# inexplicable reason).
31${SETARCH_CMD} "${PYTHON}" setup.py sdist
32
33# Wheel has a bug where directories don't get excluded.
34# https://bitbucket.org/pypa/wheel/issues/99/cannot-exclude-directory
35${SETARCH_CMD} "${PYTHON}" setup.py bdist_wheel
36
37GRPCIO_STRIP_TEMPDIR=$(mktemp -d)
38GRPCIO_TAR_GZ_LIST=( dist/grpcio-*.tar.gz )
39GRPCIO_TAR_GZ=${GRPCIO_TAR_GZ_LIST[0]}
40GRPCIO_STRIPPED_TAR_GZ=$(mktemp -t "XXXXXXXXXX.tar.gz")
41
42clean_non_source_files() {
43( cd "$1"
44  find . -type f \
45    | grep -v '\.c$' | grep -v '\.cc$' | grep -v '\.cpp$' \
46    | grep -v '\.h$' | grep -v '\.hh$' \
47    | grep -v '\.s$' | grep -v '\.py$' \
48    | while read -r file; do
49      rm -f "$file" || true
50    done
51  find . -type d -empty -delete
52)
53}
54
55tar xzf "${GRPCIO_TAR_GZ}" -C "${GRPCIO_STRIP_TEMPDIR}"
56( cd "${GRPCIO_STRIP_TEMPDIR}"
57  find . -type d -name .git -exec rm -fr {} \; || true
58  for dir in */third_party/*; do
59    clean_non_source_files "${dir}" || true
60  done
61  tar czf "${GRPCIO_STRIPPED_TAR_GZ}" -- *
62)
63mv "${GRPCIO_STRIPPED_TAR_GZ}" "${GRPCIO_TAR_GZ}"
64
65# Build gRPC tools package distribution
66"${PYTHON}" tools/distrib/python/make_grpcio_tools.py
67
68# Build gRPC tools package source distribution
69${SETARCH_CMD} "${PYTHON}" tools/distrib/python/grpcio_tools/setup.py sdist
70
71# Build gRPC tools package binary distribution
72${SETARCH_CMD} "${PYTHON}" tools/distrib/python/grpcio_tools/setup.py bdist_wheel
73
74if [ "$GRPC_BUILD_MANYLINUX_WHEEL" != "" ]
75then
76  for wheel in dist/*.whl; do
77    "${AUDITWHEEL}" repair "$wheel" -w "$ARTIFACT_DIR"
78    rm "$wheel"
79  done
80  for wheel in tools/distrib/python/grpcio_tools/dist/*.whl; do
81    "${AUDITWHEEL}" repair "$wheel" -w "$ARTIFACT_DIR"
82    rm "$wheel"
83  done
84fi
85
86# We need to use the built grpcio-tools/grpcio to compile the health proto
87# Wheels are not supported by setup_requires/dependency_links, so we
88# manually install the dependency.  Note we should only do this if we
89# are in a docker image or in a virtualenv.
90if [ "$GRPC_BUILD_GRPCIO_TOOLS_DEPENDENTS" != "" ]
91then
92  "${PIP}" install -rrequirements.txt
93
94  if [ "$("$PYTHON" -c "import sys; print(sys.version_info[0])")" == "2" ]
95  then
96    "${PIP}" install futures>=2.2.0
97  fi
98
99  "${PIP}" install grpcio --no-index --find-links "file://$ARTIFACT_DIR/"
100  "${PIP}" install grpcio-tools --no-index --find-links "file://$ARTIFACT_DIR/"
101
102  # Build grpcio_testing source distribution
103  ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_testing/setup.py sdist
104  cp -r src/python/grpcio_testing/dist/* "$ARTIFACT_DIR"
105
106  # Build grpcio_health_checking source distribution
107  ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_health_checking/setup.py \
108      preprocess build_package_protos sdist
109  cp -r src/python/grpcio_health_checking/dist/* "$ARTIFACT_DIR"
110
111  # Build grpcio_reflection source distribution
112  ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_reflection/setup.py \
113      preprocess build_package_protos sdist
114  cp -r src/python/grpcio_reflection/dist/* "$ARTIFACT_DIR"
115fi
116
117cp -r dist/* "$ARTIFACT_DIR"
118cp -r tools/distrib/python/grpcio_tools/dist/* "$ARTIFACT_DIR"
119