release.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/bin/bash
  2. set -ex
  3. function get_source_version() {
  4. grep "__version__ = '.*'" python/google/protobuf/__init__.py | sed -r "s/__version__ = '(.*)'/\1/"
  5. }
  6. function run_install_test() {
  7. local VERSION=$1
  8. local PYTHON=$2
  9. local PYPI=$3
  10. # Setuptools 45.0 removed support for Python 2, so to test with Python 2 we
  11. # pass --no-setuptools here and then install an older setuptools version
  12. # below.
  13. virtualenv -p `which $PYTHON` --no-setuptools test-venv
  14. # Intentionally put a broken protoc in the path to make sure installation
  15. # doesn't require protoc installed.
  16. touch test-venv/bin/protoc
  17. chmod +x test-venv/bin/protoc
  18. source test-venv/bin/activate
  19. pip install "setuptools<45"
  20. pip install -i ${PYPI} protobuf==${VERSION} --no-cache-dir
  21. deactivate
  22. rm -fr test-venv
  23. }
  24. [ $# -lt 1 ] && {
  25. echo "Usage: $0 VERSION ["
  26. echo ""
  27. echo "Examples:"
  28. echo " Test 3.3.0 release using version number 3.3.0.dev1:"
  29. echo " $0 3.0.0 dev1"
  30. echo " Actually release 3.3.0 to PyPI:"
  31. echo " $0 3.3.0"
  32. exit 1
  33. }
  34. VERSION=$1
  35. DEV=$2
  36. # Make sure we are in a protobuf source tree.
  37. [ -f "python/google/protobuf/__init__.py" ] || {
  38. echo "This script must be ran under root of protobuf source tree."
  39. exit 1
  40. }
  41. # Make sure all files are world-readable.
  42. find python -type d -exec chmod a+r,a+x {} +
  43. find python -type f -exec chmod a+r {} +
  44. umask 0022
  45. # Check that the supplied version number matches what's inside the source code.
  46. SOURCE_VERSION=`get_source_version`
  47. [ "${VERSION}" == "${SOURCE_VERSION}" -o "${VERSION}.${DEV}" == "${SOURCE_VERSION}" ] || {
  48. echo "Version number specified on the command line ${VERSION} doesn't match"
  49. echo "the actual version number in the source code: ${SOURCE_VERSION}"
  50. exit 1
  51. }
  52. TESTING_ONLY=1
  53. TESTING_VERSION=${VERSION}.${DEV}
  54. if [ -z "${DEV}" ]; then
  55. read -p "You are releasing ${VERSION} to PyPI. Are you sure? [y/n]" -r
  56. echo
  57. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  58. exit 1
  59. fi
  60. TESTING_ONLY=0
  61. TESTING_VERSION=${VERSION}
  62. else
  63. # Use dev version number for testing.
  64. sed -i -r "s/__version__ = '.*'/__version__ = '${VERSION}.${DEV}'/" python/google/protobuf/__init__.py
  65. fi
  66. cd python
  67. # Run tests locally.
  68. python setup.py build
  69. python setup.py test
  70. # Deploy source package to testing PyPI
  71. python setup.py sdist
  72. twine upload --skip-existing -r testpypi -u protobuf-wheel-test dist/*
  73. # Test locally with different python versions.
  74. run_install_test ${TESTING_VERSION} python2.7 https://test.pypi.org/simple
  75. run_install_test ${TESTING_VERSION} python3 https://test.pypi.org/simple
  76. # Deploy egg/wheel packages to testing PyPI and test again.
  77. python setup.py clean build bdist_wheel
  78. twine upload --skip-existing -r testpypi -u protobuf-wheel-test dist/*
  79. run_install_test ${TESTING_VERSION} python2.7 https://test.pypi.org/simple
  80. run_install_test ${TESTING_VERSION} python3 https://test.pypi.org/simple
  81. echo "All install tests have passed using testing PyPI."
  82. if [ $TESTING_ONLY -eq 0 ]; then
  83. read -p "Publish to PyPI? [y/n]" -r
  84. echo
  85. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  86. exit 1
  87. fi
  88. echo "Publishing to PyPI..."
  89. # Be sure to run build before sdist, because otherwise sdist will not include
  90. # well-known types.
  91. python setup.py clean build sdist
  92. twine upload --skip-existing -u protobuf-packages dist/*
  93. # Be sure to run clean before bdist_xxx, because otherwise bdist_xxx will
  94. # include files you may not want in the package. E.g., if you have built
  95. # and tested with --cpp_implemenation, bdist_xxx will include the _message.so
  96. # file even when you no longer pass the --cpp_implemenation flag. See:
  97. # https://github.com/protocolbuffers/protobuf/issues/3042
  98. python setup.py clean build bdist_wheel
  99. twine upload --skip-existing -u protobuf-packages dist/*
  100. else
  101. # Set the version number back (i.e., remove dev suffix).
  102. sed -i -r "s/__version__ = '.*'/__version__ = '${VERSION}'/" google/protobuf/__init__.py
  103. fi