Work around for side effects in setup.py script

Disables setup_requires and related magic for setup.py commands
clean, egg_info and sdist and the options --version and --help.

See also https://github.com/pyca/cryptography/issues/1253
diff --git a/setup.py b/setup.py
index 347dbe8..7465c31 100644
--- a/setup.py
+++ b/setup.py
@@ -140,6 +140,37 @@
         sys.exit(errno)
 
 
+def keywords_with_side_effects():
+    """
+    Get a dictionary with setup keywords that (can) have side effects.
+
+    This setup.py script uses the setuptools 'setup_requires' feature because
+    this is required by the cffi package to compile extension modules. The
+    purpose of ``keywords_with_side_effects()`` is to avoid triggering the cffi
+    build process as a result of any of the following ``setup.py`` invocations:
+
+    - ``python setup.py --help-commands``
+    - ``python setup.py --help``
+    - ``python setup.py --version``
+    - ``python setup.py clean``
+    - ``python setup.py egg_info``
+
+    This function is based on the `setup.py script of SciPy`_ (see also the
+    discussion in `pip issue #25`_).
+
+    .. _pip issue #25: https://github.com/pypa/pip/issues/25
+    .. _setup.py script of SciPy: https://github.com/scipy/scipy/blob/master/setup.py
+    """
+    if len(sys.argv) >= 2 and ('--help' in sys.argv[1:] or
+            sys.argv[1] in ('--help-commands', '--version', 'clean', 'egg_info')):
+        return {}
+    else:
+        return dict(setup_requires=requirements,
+                    cmdclass=dict(build=CFFIBuild,
+                                  install=CFFIInstall,
+                                  test=PyTest))
+
+
 with open(os.path.join(base_dir, "README.rst")) as f:
     long_description = f.read()
 
@@ -182,19 +213,13 @@
     include_package_data=True,
 
     install_requires=requirements,
-    setup_requires=requirements,
     tests_require=test_requirements,
 
     # for cffi
     zip_safe=False,
     ext_package="cryptography",
-    cmdclass={
-        "build": CFFIBuild,
-        "install": CFFIInstall,
-        "test": PyTest,
-    },
-
     entry_points={
         "cryptography.backends": backends,
-    }
+    },
+    **keywords_with_side_effects()
 )