unify version parsing in Version.py
[aubio.git] / Version.py
1
2 import os
3 for l in open('VERSION').readlines(): exec (l.strip())
4
5 # def get_git_revision_hash( short=True):
6 #     import os
7 #     def which(program):
8 #         def is_exe(fpath):
9 #             return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
10
11 #         fpath, fname = os.path.split(program)
12 #         if fpath:
13 #             if is_exe(program):
14 #                 return program
15 #         else:
16 #             for path in os.environ["PATH"].split(os.pathsep):
17 #                 path = path.strip('"')
18 #                 exe_file = os.path.join(path, program)
19 #                 if is_exe(exe_file):
20 #                     return exe_file
21 #         return None
22
23 #     if not which('git') :
24 #         # print('no git found on this system : can\'t get sha')
25 #         return ""
26 #     if not os.path.isdir('.git'):
27 #         # print('Version : not in git repository : can\'t get sha')
28 #         return ""
29
30 #     import subprocess
31 #     aubio_dir = os.path.abspath(os.curdir)
32 #     if not os.path.exists(aubio_dir):
33 #         raise SystemError("git / root folder not found")
34 #     gitcmd = ['git','-C',aubio_dir ,'rev-parse']
35 #     if short:
36 #       gitcmd.append('--short')
37 #     gitcmd.append('HEAD')
38 #     outCmd = subprocess.check_output(gitcmd).strip().decode('utf8')
39 #     return outCmd
40
41
42 def get_aubio_version():
43     # read from VERSION
44     this_file_dir = os.path.dirname(os.path.abspath(__file__))
45     version_file = os.path.join(this_file_dir,  'VERSION')
46
47     if not os.path.isfile(version_file):
48         raise SystemError("VERSION file not found.")
49
50     for l in open(version_file).readlines():
51         #exec (l.strip())
52         if l.startswith('AUBIO_MAJOR_VERSION'):
53             AUBIO_MAJOR_VERSION = int(l.split('=')[1])
54         if l.startswith('AUBIO_MINOR_VERSION'):
55             AUBIO_MINOR_VERSION = int(l.split('=')[1])
56         if l.startswith('AUBIO_PATCH_VERSION'):
57             AUBIO_PATCH_VERSION = int(l.split('=')[1])
58         if l.startswith('AUBIO_VERSION_STATUS'):
59             AUBIO_VERSION_STATUS = l.split('=')[1].strip()[1:-1]
60
61     if AUBIO_MAJOR_VERSION is None or AUBIO_MINOR_VERSION is None \
62             or AUBIO_PATCH_VERSION is None:
63         raise SystemError("Failed parsing VERSION file.")
64
65     verstr = '.'.join(map(str, [AUBIO_MAJOR_VERSION,
66                                      AUBIO_MINOR_VERSION,
67                                      AUBIO_PATCH_VERSION]))
68
69     
70     # append sha to version in alpha release
71     # MAJ.MIN.PATCH.{~git<sha> , ''}
72     if '~alpha' in AUBIO_VERSION_STATUS :
73         AUBIO_GIT_SHA = get_git_revision_hash()
74         if AUBIO_GIT_SHA:
75             AUBIO_VERSION_STATUS = '~git'+AUBIO_GIT_SHA
76
77     if AUBIO_VERSION_STATUS is not None :
78         verstr += AUBIO_VERSION_STATUS
79     return verstr
80
81 def get_aubio_pyversion():
82     # convert to version for python according to pep 440
83     # see https://www.python.org/dev/peps/pep-0440/
84     verstr = get_aubio_version()
85     spl = verstr.split('~')
86     if len(spl)==2:
87         verstr = spl[0] + '+a1.'+spl[1]
88
89     # TODO: add rc, .dev, and .post suffixes, add numbering
90     return verstr
91
92
93
94 def get_git_revision_hash( short=True):
95     def which(program):
96     
97         def is_exe(fpath):
98             return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
99
100         fpath, fname = os.path.split(program)
101         if fpath:
102             if is_exe(program):
103                 return program
104         else:
105             for path in os.environ["PATH"].split(os.pathsep):
106                 path = path.strip('"')
107                 exe_file = os.path.join(path, program)
108                 if is_exe(exe_file):
109                     return exe_file
110
111         return None
112
113     if not which('git') :
114         # print('no git found on this system : can\'t get sha')
115         return ""
116     if not os.path.isdir('.git'):
117         # print('Version : not in git repository : can\'t get sha')
118         return ""
119
120     import subprocess
121     aubio_dir = os.path.dirname(os.path.abspath(__file__))
122     if not os.path.exists(aubio_dir):
123         raise SystemError("git / root folder not found")
124     gitcmd = ['git','-C',aubio_dir ,'rev-parse']
125     if short:
126       gitcmd.append('--short')
127     gitcmd.append('HEAD')
128     outCmd = subprocess.check_output(gitcmd).strip().decode('utf8')
129     return outCmd
130
131
132
133 # append sha to version in alpha release
134 if AUBIO_VERSION_STATUS and '~alpha' in AUBIO_VERSION_STATUS :
135     AUBIO_GIT_SHA = get_git_revision_hash()
136     if AUBIO_GIT_SHA:
137         AUBIO_VERSION_STATUS = '~git'+AUBIO_GIT_SHA