From e4830940e2c6fadf9876947fab7ca2e44fac97ad Mon Sep 17 00:00:00 2001
From: Christoph Groth <christoph.groth@cea.fr>
Date: Mon, 1 Jul 2013 15:30:39 +0200
Subject: [PATCH] generate MANIFEST from the output of "git ls-files" (less
 error-prone)

---
 MANIFEST.in | 23 --------------------
 setup.py    | 61 +++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 57 insertions(+), 27 deletions(-)
 delete mode 100644 MANIFEST.in

diff --git a/MANIFEST.in b/MANIFEST.in
deleted file mode 100644
index 77a31e49..00000000
--- a/MANIFEST.in
+++ /dev/null
@@ -1,23 +0,0 @@
-# This file specifies the files to be included in the source distribution
-# in addition to the default ones.
-
-include MANIFEST.in
-include INSTALL LICENSE AUTHORS ACKNOWLEDGEMENTS TODO README_WINDOWS.txt
-
-# We explicitly include both pyx and c files, so that both are shipped in
-# source tarballs independently of whether "setup.py sdist" is run with
-# --no-cython or not.
-recursive-include kwant *.pyx
-recursive-include kwant *.pxd
-recursive-include kwant *.c
-recursive-include kwant *.h
-recursive-include examples *.py
-
-include doc/other/*[a-zA-Z]
-include doc/Makefile
-recursive-include doc/source *.rst *.py *.svg *.sh *.diff
-recursive-include doc/source/_static *[a-zA-Z]
-recursive-exclude doc/source/images [a-zA-Z]*.py
-recursive-include doc/templates *[a-zA-Z]
-prune doc/source/reference/generated
-recursive-include doc/sphinxext *.py *.txt *.in
diff --git a/setup.py b/setup.py
index 2c63448b..c5178fca 100755
--- a/setup.py
+++ b/setup.py
@@ -48,6 +48,8 @@ if cythonize and cython_version:
 else:
     from distutils.command.build_ext import build_ext
 
+kwant_dir = os.path.dirname(os.path.abspath(__file__))
+
 
 class kwant_build_ext(build_ext):
     def run(self):
@@ -96,7 +98,18 @@ class build_tut(Command):
 # that the tutorial is present.
 class kwant_build(distutils_build):
     sub_commands = [('build_tut', None)] + distutils_build.sub_commands
-    pass
+
+
+def git_lsfiles():
+    try:
+        p = subprocess.Popen(['git', 'ls-files'], cwd=kwant_dir,
+                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+    except OSError:
+        return
+
+    if p.wait() != 0:
+        return
+    return p.communicate()[0].split('\n')[:-1]
 
 
 # Make the command "sdist" depend on "build".  This verifies that the
@@ -105,14 +118,54 @@ class kwant_build(distutils_build):
 # distribution and that they will be up-to-date.
 class kwant_sdist(distutils_sdist):
     sub_commands = [('build', None)] + distutils_sdist.sub_commands
-    pass
+
+    def run(self):
+        names = git_lsfiles()
+        trustworthy = True
+        if names is None:
+            # Check that MANIFEST exists and has not been generated by
+            # distutils.
+            try:
+                with open(kwant_dir + '/MANIFEST', 'r') as f:
+                    line = f.read()
+            except IOError:
+                print >>sys.stderr, "error: MANIFEST file is missing and " \
+                    "Git is not available to regenerate it."
+                exit(1)
+            trustworthy = not line.strip().startswith('#')
+        else:
+            # Generate MANIFEST file.
+            with open(kwant_dir + '/MANIFEST', 'w') as f:
+                for name in names:
+                    a, sep, b = name.rpartition('/')
+                    if b == '.gitignore':
+                        continue
+                    stem, dot, extension = b.rpartition('.')
+                    if extension == 'pyx':
+                        f.write(''.join([a, sep, stem, dot, 'c', '\n']))
+                    f.write(name + '\n')
+                f.write(STATIC_VERSION_FILE + '\n')
+                f.write('MANIFEST')
+
+        distutils_sdist.run(self)
+
+        if names is None:
+            print >>sys.stderr, \
+    """**************** Warning ****************
+Git was not available for re-generating the MANIFEST file (the list of file
+names to be included in the source distribution).  The old MANIFEST was used."""
+
+        if not trustworthy:
+            print >>sys.stderr, \
+    """**************** Warning ****************
+The existing MANIFEST file seems to have been generated by distutils (it begins
+with a comment).  It may well be incomplete."""
 
 
 # This is an exact copy of the function from kwant/version.py.  We can't import
 # it here (because kwant is not yet built when this scipt is run), so we just
 # include a copy.
 def get_version_from_git():
-    kwant_dir = os.path.dirname(os.path.abspath(__file__))
     try:
         p = subprocess.Popen(['git', 'describe'], cwd=kwant_dir,
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@@ -297,7 +350,7 @@ def ext_modules(extensions):
                                         for f in cythonized_files)
             except OSError:
                 print >>sys.stderr, \
-                    "Error: Cython-generated file {0} is missing.".format(f)
+                    "error: Cython-generated file {0} is missing.".format(f)
                 if cythonize:
                     print >>sys.stderr, "Install Cython so it can be made" \
                     " or use a source distribution of kwant."
-- 
GitLab