From 0c85ed62f247cd04dd1baeea40ebe63efc7a3a06 Mon Sep 17 00:00:00 2001
From: Christoph Groth <christoph.groth@cea.fr>
Date: Thu, 8 Oct 2015 12:12:07 +0200
Subject: [PATCH] setup.py: get rid of duplication of get_version_from_git

---
 kwant/_common.py |  1 -
 setup.py         | 54 +++++++-----------------------------------------
 2 files changed, 8 insertions(+), 47 deletions(-)

diff --git a/kwant/_common.py b/kwant/_common.py
index f8ddc03c..84909373 100644
--- a/kwant/_common.py
+++ b/kwant/_common.py
@@ -13,7 +13,6 @@ __all__ = ['version', 'KwantDeprecationWarning']
 
 distr_root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
 
-# When changing this function, remember to also change its twin in ../setup.py.
 def get_version_from_git():
     try:
         p = subprocess.Popen(['git', 'rev-parse', '--show-toplevel'],
diff --git a/setup.py b/setup.py
index f5843e3c..683b4418 100755
--- a/setup.py
+++ b/setup.py
@@ -14,6 +14,7 @@ import sys
 import re
 import os
 import glob
+import imp
 import subprocess
 import ConfigParser
 from distutils.core import setup, Extension, Command
@@ -25,6 +26,11 @@ from distutils.command.sdist import sdist as distutils_sdist
 
 import numpy
 
+_dont_write_bytecode_saved = sys.dont_write_bytecode
+sys.dont_write_bytecode = True
+kwant_common = imp.load_source('kwant_common', 'kwant/_common.py')
+sys.dont_write_bytecode = _dont_write_bytecode_saved
+
 CONFIG_FILE = 'build.conf'
 README_FILE = 'README.rst'
 README_END_BEFORE = 'See also in this directory:'
@@ -226,53 +232,9 @@ with a comment).  It may well be incomplete.""",
                   banner(),
                   sep='\n', file=sys.stderr)
 
-# Other than the "if not use_git" clause in the beginning, this is an exact copy
-# of the function from kwant/_common.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():
-    if not use_git:
-        return
-
-    try:
-        p = subprocess.Popen(['git', 'rev-parse', '--show-toplevel'],
-                             cwd=distr_root,
-                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-    except OSError:
-        return
-    if p.wait() != 0:
-        return
-    # TODO: use os.path.samefile once we depend on Python >= 3.3.
-    if os.path.normpath(p.communicate()[0].rstrip('\n')) != distr_root:
-        # The top-level directory of the current Git repository is not the same
-        # as the root directory of the Kwant distribution: do not extract the
-        # version from Git.
-        return
-
-    # git describe --first-parent does not take into account tags from branches
-    # that were merged-in.
-    for opts in [['--first-parent'], []]:
-        try:
-            p = subprocess.Popen(['git', 'describe'] + opts, cwd=distr_root,
-                                 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-        except OSError:
-            return
-        if p.wait() == 0:
-            break
-    else:
-        return
-    version = p.communicate()[0].rstrip('\n')
-
-    if version[0] == 'v':
-        version = version[1:]
-
-    try:
-        p = subprocess.Popen(['git', 'diff', '--quiet'], cwd=distr_root)
-    except OSError:
-        version += '-confused'  # This should never happen.
-    else:
-        if p.wait() == 1:
-            version += '-dirty'
-    return version
+    if use_git:
+        return kwant_common.get_version_from_git()
 
 
 def read_static_version():
-- 
GitLab