blob: fc88b08e3eee0e4cec2674cc0b19b6a055120267 [file] [log] [blame]
Jisi Liu09354db2017-07-18 15:38:30 -07001#!/bin/bash
2
3set -ex
4
5function get_source_version() {
6 grep "__version__ = '.*'" python/google/protobuf/__init__.py | sed -r "s/__version__ = '(.*)'/\1/"
7}
8
9function run_install_test() {
10 local VERSION=$1
11 local PYTHON=$2
12 local PYPI=$3
13
Adam Cozzette2ad43bf2020-08-14 08:57:23 -070014 # Setuptools 45.0 removed support for Python 2, so to test with Python 2 we
15 # pass --no-setuptools here and then install an older setuptools version
16 # below.
17 virtualenv -p `which $PYTHON` --no-setuptools test-venv
Jisi Liu09354db2017-07-18 15:38:30 -070018
19 # Intentionally put a broken protoc in the path to make sure installation
20 # doesn't require protoc installed.
21 touch test-venv/bin/protoc
22 chmod +x test-venv/bin/protoc
23
24 source test-venv/bin/activate
Adam Cozzette2ad43bf2020-08-14 08:57:23 -070025 pip install "setuptools<45"
Jisi Liu7ad8e7a2017-12-22 11:47:13 -080026 pip install -i ${PYPI} protobuf==${VERSION} --no-cache-dir
Jisi Liu09354db2017-07-18 15:38:30 -070027 deactivate
28 rm -fr test-venv
29}
30
31
32[ $# -lt 1 ] && {
33 echo "Usage: $0 VERSION ["
34 echo ""
35 echo "Examples:"
36 echo " Test 3.3.0 release using version number 3.3.0.dev1:"
37 echo " $0 3.0.0 dev1"
38 echo " Actually release 3.3.0 to PyPI:"
39 echo " $0 3.3.0"
40 exit 1
41}
42VERSION=$1
43DEV=$2
44
45# Make sure we are in a protobuf source tree.
46[ -f "python/google/protobuf/__init__.py" ] || {
47 echo "This script must be ran under root of protobuf source tree."
48 exit 1
49}
50
51# Make sure all files are world-readable.
52find python -type d -exec chmod a+r,a+x {} +
53find python -type f -exec chmod a+r {} +
Feng Xiao3a06fe12017-12-14 17:54:18 -080054umask 0022
Jisi Liu09354db2017-07-18 15:38:30 -070055
56# Check that the supplied version number matches what's inside the source code.
57SOURCE_VERSION=`get_source_version`
58
59[ "${VERSION}" == "${SOURCE_VERSION}" -o "${VERSION}.${DEV}" == "${SOURCE_VERSION}" ] || {
60 echo "Version number specified on the command line ${VERSION} doesn't match"
61 echo "the actual version number in the source code: ${SOURCE_VERSION}"
62 exit 1
63}
64
65TESTING_ONLY=1
66TESTING_VERSION=${VERSION}.${DEV}
67if [ -z "${DEV}" ]; then
68 read -p "You are releasing ${VERSION} to PyPI. Are you sure? [y/n]" -r
69 echo
70 if [[ ! $REPLY =~ ^[Yy]$ ]]; then
71 exit 1
72 fi
73 TESTING_ONLY=0
74 TESTING_VERSION=${VERSION}
75else
76 # Use dev version number for testing.
77 sed -i -r "s/__version__ = '.*'/__version__ = '${VERSION}.${DEV}'/" python/google/protobuf/__init__.py
78fi
79
80cd python
81
82# Run tests locally.
Adam Cozzetteb45ce5e2020-11-10 14:33:16 -080083python3 setup.py build
84python3 setup.py test
Jisi Liu09354db2017-07-18 15:38:30 -070085
86# Deploy source package to testing PyPI
Adam Cozzetteb45ce5e2020-11-10 14:33:16 -080087python3 setup.py sdist
Joshua Haberman0ff63992020-05-28 10:56:10 -070088twine upload --skip-existing -r testpypi -u protobuf-wheel-test dist/*
Jisi Liu09354db2017-07-18 15:38:30 -070089
90# Test locally with different python versions.
Jisi Liu19a7e202017-08-16 10:30:38 -070091run_install_test ${TESTING_VERSION} python2.7 https://test.pypi.org/simple
Paul Yang5b4ac532019-02-01 18:43:55 -080092run_install_test ${TESTING_VERSION} python3 https://test.pypi.org/simple
Jisi Liu09354db2017-07-18 15:38:30 -070093
94# Deploy egg/wheel packages to testing PyPI and test again.
Adam Cozzetteb45ce5e2020-11-10 14:33:16 -080095python3 setup.py clean build bdist_wheel
Joshua Haberman0ff63992020-05-28 10:56:10 -070096twine upload --skip-existing -r testpypi -u protobuf-wheel-test dist/*
Jisi Liu7ad8e7a2017-12-22 11:47:13 -080097
Jisi Liu19a7e202017-08-16 10:30:38 -070098run_install_test ${TESTING_VERSION} python2.7 https://test.pypi.org/simple
Paul Yang5b4ac532019-02-01 18:43:55 -080099run_install_test ${TESTING_VERSION} python3 https://test.pypi.org/simple
Jisi Liu09354db2017-07-18 15:38:30 -0700100
101echo "All install tests have passed using testing PyPI."
102
103if [ $TESTING_ONLY -eq 0 ]; then
104 read -p "Publish to PyPI? [y/n]" -r
105 echo
106 if [[ ! $REPLY =~ ^[Yy]$ ]]; then
107 exit 1
108 fi
109 echo "Publishing to PyPI..."
110 # Be sure to run build before sdist, because otherwise sdist will not include
111 # well-known types.
Adam Cozzetteb45ce5e2020-11-10 14:33:16 -0800112 python3 setup.py clean build sdist
Joshua Haberman0ff63992020-05-28 10:56:10 -0700113 twine upload --skip-existing -u protobuf-packages dist/*
Jisi Liu09354db2017-07-18 15:38:30 -0700114 # Be sure to run clean before bdist_xxx, because otherwise bdist_xxx will
115 # include files you may not want in the package. E.g., if you have built
116 # and tested with --cpp_implemenation, bdist_xxx will include the _message.so
117 # file even when you no longer pass the --cpp_implemenation flag. See:
Feng Xiaoafe98de2018-08-22 11:55:30 -0700118 # https://github.com/protocolbuffers/protobuf/issues/3042
Adam Cozzetteb45ce5e2020-11-10 14:33:16 -0800119 python3 setup.py clean build bdist_wheel
Joshua Haberman0ff63992020-05-28 10:56:10 -0700120 twine upload --skip-existing -u protobuf-packages dist/*
Jisi Liu09354db2017-07-18 15:38:30 -0700121else
122 # Set the version number back (i.e., remove dev suffix).
123 sed -i -r "s/__version__ = '.*'/__version__ = '${VERSION}'/" google/protobuf/__init__.py
124fi