unify version parsing in Version.py
authorMartin Hermant <martin.hermant@gmail.com>
Mon, 13 Mar 2017 18:49:56 +0000 (19:49 +0100)
committerMartin Hermant <martin.hermant@gmail.com>
Mon, 13 Mar 2017 18:49:56 +0000 (19:49 +0100)
Version.py [new file with mode: 0644]
python/lib/moresetuptools.py
setup.py
wscript

diff --git a/Version.py b/Version.py
new file mode 100644 (file)
index 0000000..d3637e2
--- /dev/null
@@ -0,0 +1,137 @@
+
+import os
+for l in open('VERSION').readlines(): exec (l.strip())
+
+# def get_git_revision_hash( short=True):
+#     import os
+#     def which(program):
+#         def is_exe(fpath):
+#             return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
+
+#         fpath, fname = os.path.split(program)
+#         if fpath:
+#             if is_exe(program):
+#                 return program
+#         else:
+#             for path in os.environ["PATH"].split(os.pathsep):
+#                 path = path.strip('"')
+#                 exe_file = os.path.join(path, program)
+#                 if is_exe(exe_file):
+#                     return exe_file
+#         return None
+
+#     if not which('git') :
+#         # print('no git found on this system : can\'t get sha')
+#         return ""
+#     if not os.path.isdir('.git'):
+#         # print('Version : not in git repository : can\'t get sha')
+#         return ""
+
+#     import subprocess
+#     aubio_dir = os.path.abspath(os.curdir)
+#     if not os.path.exists(aubio_dir):
+#         raise SystemError("git / root folder not found")
+#     gitcmd = ['git','-C',aubio_dir ,'rev-parse']
+#     if short:
+#       gitcmd.append('--short')
+#     gitcmd.append('HEAD')
+#     outCmd = subprocess.check_output(gitcmd).strip().decode('utf8')
+#     return outCmd
+
+
+def get_aubio_version():
+    # read from VERSION
+    this_file_dir = os.path.dirname(os.path.abspath(__file__))
+    version_file = os.path.join(this_file_dir,  'VERSION')
+
+    if not os.path.isfile(version_file):
+        raise SystemError("VERSION file not found.")
+
+    for l in open(version_file).readlines():
+        #exec (l.strip())
+        if l.startswith('AUBIO_MAJOR_VERSION'):
+            AUBIO_MAJOR_VERSION = int(l.split('=')[1])
+        if l.startswith('AUBIO_MINOR_VERSION'):
+            AUBIO_MINOR_VERSION = int(l.split('=')[1])
+        if l.startswith('AUBIO_PATCH_VERSION'):
+            AUBIO_PATCH_VERSION = int(l.split('=')[1])
+        if l.startswith('AUBIO_VERSION_STATUS'):
+            AUBIO_VERSION_STATUS = l.split('=')[1].strip()[1:-1]
+
+    if AUBIO_MAJOR_VERSION is None or AUBIO_MINOR_VERSION is None \
+            or AUBIO_PATCH_VERSION is None:
+        raise SystemError("Failed parsing VERSION file.")
+
+    verstr = '.'.join(map(str, [AUBIO_MAJOR_VERSION,
+                                     AUBIO_MINOR_VERSION,
+                                     AUBIO_PATCH_VERSION]))
+
+    
+    # append sha to version in alpha release
+    # MAJ.MIN.PATCH.{~git<sha> , ''}
+    if '~alpha' in AUBIO_VERSION_STATUS :
+        AUBIO_GIT_SHA = get_git_revision_hash()
+        if AUBIO_GIT_SHA:
+            AUBIO_VERSION_STATUS = '~git'+AUBIO_GIT_SHA
+
+    if AUBIO_VERSION_STATUS is not None :
+        verstr += AUBIO_VERSION_STATUS
+    return verstr
+
+def get_aubio_pyversion():
+    # convert to version for python according to pep 440
+    # see https://www.python.org/dev/peps/pep-0440/
+    verstr = get_aubio_version()
+    spl = verstr.split('~')
+    if len(spl)==2:
+        verstr = spl[0] + '+a1.'+spl[1]
+
+    # TODO: add rc, .dev, and .post suffixes, add numbering
+    return verstr
+
+
+
+def get_git_revision_hash( short=True):
+    def which(program):
+    
+        def is_exe(fpath):
+            return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
+
+        fpath, fname = os.path.split(program)
+        if fpath:
+            if is_exe(program):
+                return program
+        else:
+            for path in os.environ["PATH"].split(os.pathsep):
+                path = path.strip('"')
+                exe_file = os.path.join(path, program)
+                if is_exe(exe_file):
+                    return exe_file
+
+        return None
+
+    if not which('git') :
+        # print('no git found on this system : can\'t get sha')
+        return ""
+    if not os.path.isdir('.git'):
+        # print('Version : not in git repository : can\'t get sha')
+        return ""
+
+    import subprocess
+    aubio_dir = os.path.dirname(os.path.abspath(__file__))
+    if not os.path.exists(aubio_dir):
+        raise SystemError("git / root folder not found")
+    gitcmd = ['git','-C',aubio_dir ,'rev-parse']
+    if short:
+      gitcmd.append('--short')
+    gitcmd.append('HEAD')
+    outCmd = subprocess.check_output(gitcmd).strip().decode('utf8')
+    return outCmd
+
+
+
+# append sha to version in alpha release
+if AUBIO_VERSION_STATUS and '~alpha' in AUBIO_VERSION_STATUS :
+    AUBIO_GIT_SHA = get_git_revision_hash()
+    if AUBIO_GIT_SHA:
+        AUBIO_VERSION_STATUS = '~git'+AUBIO_GIT_SHA
\ No newline at end of file
index 97d1aa3..17dc78c 100644 (file)
@@ -4,92 +4,7 @@ import sys, os, glob, subprocess
 import distutils, distutils.command.clean, distutils.dir_util
 from .gen_external import generate_external, header, output_path
 
-def get_aubio_version():
-    # read from VERSION
-    this_file_dir = os.path.dirname(os.path.abspath(__file__))
-    version_file = os.path.join(this_file_dir, '..', '..', 'VERSION')
-
-    if not os.path.isfile(version_file):
-        raise SystemError("VERSION file not found.")
-
-    for l in open(version_file).readlines():
-        #exec (l.strip())
-        if l.startswith('AUBIO_MAJOR_VERSION'):
-            AUBIO_MAJOR_VERSION = int(l.split('=')[1])
-        if l.startswith('AUBIO_MINOR_VERSION'):
-            AUBIO_MINOR_VERSION = int(l.split('=')[1])
-        if l.startswith('AUBIO_PATCH_VERSION'):
-            AUBIO_PATCH_VERSION = int(l.split('=')[1])
-        if l.startswith('AUBIO_VERSION_STATUS'):
-            AUBIO_VERSION_STATUS = l.split('=')[1].strip()[1:-1]
-
-    if AUBIO_MAJOR_VERSION is None or AUBIO_MINOR_VERSION is None \
-            or AUBIO_PATCH_VERSION is None:
-        raise SystemError("Failed parsing VERSION file.")
-
-    verstr = '.'.join(map(str, [AUBIO_MAJOR_VERSION,
-                                     AUBIO_MINOR_VERSION,
-                                     AUBIO_PATCH_VERSION]))
-
-    
-    # append sha to version in alpha release
-    # MAJ.MIN.PATCH.{~git<sha> , ''}
-    if '~alpha' in AUBIO_VERSION_STATUS :
-        AUBIO_GIT_SHA = get_git_revision_hash()
-        if AUBIO_GIT_SHA:
-            AUBIO_VERSION_STATUS = '~git'+AUBIO_GIT_SHA
-
-    if AUBIO_VERSION_STATUS is not None :
-        verstr += AUBIO_VERSION_STATUS
-    return verstr
-
-def get_aubio_pyversion():
-    # convert to version for python according to pep 440
-    # see https://www.python.org/dev/peps/pep-0440/
-    verstr = get_aubio_version()
-    if '~alpha' in verstr or '~git' in verstr:
-        verstr = verstr.split('~')[0] + '+a1'
-        gitsha = get_git_revision_hash(short=True)
-        if gitsha:
-            verstr+='.git.'+gitsha
-    # TODO: add rc, .dev, and .post suffixes, add numbering
-    return verstr
-
-
-
-def get_git_revision_hash( short=True):
-    def which(program):
-    
-        def is_exe(fpath):
-            return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
-
-        fpath, fname = os.path.split(program)
-        if fpath:
-            if is_exe(program):
-                return program
-        else:
-            for path in os.environ["PATH"].split(os.pathsep):
-                path = path.strip('"')
-                exe_file = os.path.join(path, program)
-                if is_exe(exe_file):
-                    return exe_file
-
-        return None
-    if not which('git'):
-        print ('Version : no git found on this system : can\'t get sha')
-        return ""
-
-    import subprocess
-    this_file_dir = os.path.dirname(os.path.abspath(__file__))
-    aubio_dir = os.path.join(this_file_dir, '..', '..')
-    aubio_dir = os.path.abspath(aubio_dir)
-    if not os.path.exists(aubio_dir):
-        raise SystemError("git / root folder not found")
-    gitcmd = ['git','-C',aubio_dir ,'rev-parse']
-    if short:
-      gitcmd.append('--short')
-    gitcmd.append('HEAD')
-    return subprocess.check_output(gitcmd).strip()
+from Version import get_aubio_version
 
 # inspired from https://gist.github.com/abergmeier/9488990
 def add_packages(packages, ext=None, **kw):
index e02e48b..0610831 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -5,6 +5,7 @@ from setuptools import setup, Extension
 from python.lib.moresetuptools import *
 # function to generate gen/*.{c,h}
 from python.lib.gen_external import generate_external, header, output_path
+from Version import get_aubio_version, get_aubio_pyversion
 
 __version__ = get_aubio_version()
 __pip_version__ = get_aubio_pyversion()
diff --git a/wscript b/wscript
index 2c9f790..c97b7cb 100644 (file)
--- a/wscript
+++ b/wscript
@@ -15,48 +15,9 @@ import sys
 APPNAME = 'aubio'
 
 # source VERSION
-for l in open('VERSION').readlines(): exec (l.strip())
-
-def get_git_revision_hash( short=True):
-    import os
-    def which(program):
-        def is_exe(fpath):
-            return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
-
-        fpath, fname = os.path.split(program)
-        if fpath:
-            if is_exe(program):
-                return program
-        else:
-            for path in os.environ["PATH"].split(os.pathsep):
-                path = path.strip('"')
-                exe_file = os.path.join(path, program)
-                if is_exe(exe_file):
-                    return exe_file
-        return None
-
-    if not which('git'):
-        print('no git found on this system : can\'t get sha')
-        return ""
-
-    import subprocess
-    aubio_dir = os.path.abspath(os.curdir)
-    if not os.path.exists(aubio_dir):
-        raise SystemError("git / root folder not found")
-    gitcmd = ['git','-C',aubio_dir ,'rev-parse']
-    if short:
-      gitcmd.append('--short')
-    gitcmd.append('HEAD')
-    return str(subprocess.check_output(gitcmd).strip())
-
-
-# append sha to version in alpha release
-if AUBIO_VERSION_STATUS and '~alpha' in AUBIO_VERSION_STATUS :
-    AUBIO_GIT_SHA = get_git_revision_hash()
-    if AUBIO_GIT_SHA:
-        AUBIO_VERSION_STATUS = '~git'+AUBIO_GIT_SHA
 
 
+from Version import *
 
 
 VERSION = '.'.join ([str(x) for x in [
@@ -178,7 +139,7 @@ def configure(ctx):
     ctx.define('AUBIO_MINOR_VERSION',AUBIO_MINOR_VERSION)
     ctx.define('AUBIO_PATCH_VERSION',AUBIO_PATCH_VERSION)
     ctx.define('AUBIO_VERSION_STATUS',AUBIO_VERSION_STATUS)
-    # ctx.define('AUBIO_GIT_SHA',AUBIO_GIT_SHA)
+    
     if ctx.options.build_type == "debug":
         ctx.define('DEBUG', 1)
     else: