Merge pull request #5319 from soltanmm/banhammer

Exclude cygrpc.so from the Python package
diff --git a/PYTHON-MANIFEST.in b/PYTHON-MANIFEST.in
index 072089a..25aba87 100644
--- a/PYTHON-MANIFEST.in
+++ b/PYTHON-MANIFEST.in
@@ -1,4 +1,5 @@
-graft src/python/grpcio/grpc
+recursive-include src/python/grpcio/grpc *.c *.h *.py *.pyx *.pxd *.pxi *.python *.pem
+recursive-exclude src/python/grpcio/grpc/_cython *.so *.pyd
 graft src/python/grpcio/tests
 graft src/core
 graft include/grpc
diff --git a/setup.py b/setup.py
index 01faa4c..46ad75f 100644
--- a/setup.py
+++ b/setup.py
@@ -210,7 +210,6 @@
     'grpc._cython': [
         '_windows/grpc_c.32.python',
         '_windows/grpc_c.64.python',
-        'cygrpc.so',
     ],
 }
 if INSTALL_TESTS:
diff --git a/src/python/grpcio/precompiled.py b/src/python/grpcio/precompiled.py
index 05c651b..ae2a0c8 100644
--- a/src/python/grpcio/precompiled.py
+++ b/src/python/grpcio/precompiled.py
@@ -100,3 +100,5 @@
     sys.stderr.write(
         'could not write precompiled extension to directory: {} -> {}\n'
             .format(url, target_path))
+    return
+  setup_arguments['package_data']['grpc._cython'].append('cygrpc.so')
diff --git a/tools/run_tests/build_artifact_python.sh b/tools/run_tests/build_artifact_python.sh
index 6e7ab91..7ba04d7 100755
--- a/tools/run_tests/build_artifact_python.sh
+++ b/tools/run_tests/build_artifact_python.sh
@@ -39,6 +39,14 @@
   pip install -rrequirements.txt
 fi
 
+# Build the source distribution first because MANIFEST.in cannot override
+# exclusion of built shared objects among package resources (for some
+# inexplicable reason).
+GRPC_PYTHON_USE_CUSTOM_BDIST=0  \
+GRPC_PYTHON_BUILD_WITH_CYTHON=1 \
+${SETARCH_CMD} python setup.py  \
+    sdist
+
 # The bdist_wheel_grpc_custom command is finicky about command output ordering
 # and thus ought to be run in a shell command separate of others. Further, it
 # trashes the actual bdist_wheel output, so it should be run first so that
@@ -48,11 +56,12 @@
 ${SETARCH_CMD} python setup.py  \
     build_tagged_ext
 
+# Wheel has a bug where directories don't get excluded.
+# https://bitbucket.org/pypa/wheel/issues/99/cannot-exclude-directory
 GRPC_PYTHON_USE_CUSTOM_BDIST=0  \
 GRPC_PYTHON_BUILD_WITH_CYTHON=1 \
 ${SETARCH_CMD} python setup.py  \
-    bdist_wheel                 \
-    sdist
+    bdist_wheel
 
 mkdir -p artifacts