14a1a7127697241d9ee4d6cc58f7e304e9057241
[aubio.git] / this_version.py
1
2 import os
3
4
5 __version_info = {}
6
7
8 def get_version_info():
9     # read from VERSION
10     # return dictionary filled with content of version
11
12     if not __version_info:
13         this_file_dir = os.path.dirname(os.path.abspath(__file__))
14         version_file = os.path.join(this_file_dir, 'VERSION')
15
16         if not os.path.isfile(version_file):
17             raise SystemError("VERSION file not found.")
18
19         for l in open(version_file).readlines():
20
21             if l.startswith('AUBIO_MAJOR_VERSION'):
22                 __version_info['AUBIO_MAJOR_VERSION'] = int(l.split('=')[1])
23             if l.startswith('AUBIO_MINOR_VERSION'):
24                 __version_info['AUBIO_MINOR_VERSION'] = int(l.split('=')[1])
25             if l.startswith('AUBIO_PATCH_VERSION'):
26                 __version_info['AUBIO_PATCH_VERSION'] = int(l.split('=')[1])
27             if l.startswith('AUBIO_VERSION_STATUS'):
28                 __version_info['AUBIO_VERSION_STATUS'] = \
29                     l.split('=')[1].strip()[1:-1]
30
31             if l.startswith('LIBAUBIO_LT_CUR'):
32                 __version_info['LIBAUBIO_LT_CUR'] = int(l.split('=')[1])
33             if l.startswith('LIBAUBIO_LT_REV'):
34                 __version_info['LIBAUBIO_LT_REV'] = int(l.split('=')[1])
35             if l.startswith('LIBAUBIO_LT_AGE'):
36                 __version_info['LIBAUBIO_LT_AGE'] = int(l.split('=')[1])
37
38         if len(__version_info) < 6:
39             raise SystemError("Failed parsing VERSION file.")
40
41         # switch version status with commit sha in alpha releases
42         if __version_info['AUBIO_VERSION_STATUS'] and \
43                 '~alpha' in __version_info['AUBIO_VERSION_STATUS']:
44             AUBIO_GIT_SHA = get_git_revision_hash()
45             if AUBIO_GIT_SHA:
46                 __version_info['AUBIO_VERSION_STATUS'] = '~git' + AUBIO_GIT_SHA
47
48     return __version_info
49
50
51 def get_aubio_version_tuple():
52     d = get_version_info()
53     return (d['AUBIO_MAJOR_VERSION'],
54             d['AUBIO_MINOR_VERSION'],
55             d['AUBIO_PATCH_VERSION'])
56
57
58 def get_libaubio_version_tuple():
59     d = get_version_info()
60     return (d['LIBAUBIO_LT_CUR'], d['LIBAUBIO_LT_REV'], d['LIBAUBIO_LT_AGE'])
61
62
63 def get_libaubio_version():
64     return '%s.%s.%s' % get_libaubio_version_tuple()
65
66
67 def get_aubio_version(add_status=True):
68     # return string formatted as MAJ.MIN.PATCH{~git<sha> , ''}
69     vdict = get_version_info()
70     verstr = '%s.%s.%s' % get_aubio_version_tuple()
71     if add_status and vdict['AUBIO_VERSION_STATUS']:
72         verstr += vdict['AUBIO_VERSION_STATUS']
73     return verstr
74
75
76 def get_aubio_pyversion(add_status=True):
77     # convert to version for python according to pep 440
78     # see https://www.python.org/dev/peps/pep-0440/
79     # outputs MAJ.MIN.PATCH+a0{.git<sha> , ''}
80     vdict = get_version_info()
81     verstr = '%s.%s.%s' % get_aubio_version_tuple()
82     if add_status and vdict['AUBIO_VERSION_STATUS']:
83         if '~git' in vdict['AUBIO_VERSION_STATUS']:
84             verstr += "+a0." + vdict['AUBIO_VERSION_STATUS'][1:]
85         elif '~alpha' in vdict['AUBIO_VERSION_STATUS']:
86             verstr += "+a0"
87         else:
88             raise SystemError("Aubio version statut not supported : %s" %
89                               vdict['AUBIO_VERSION_STATUS'])
90     return verstr
91
92
93 def get_git_revision_hash(short=True):
94
95     if not os.path.isdir('.git'):
96         # print('Version : not in git repository : can\'t get sha')
97         return None
98
99     import subprocess
100     aubio_dir = os.path.dirname(os.path.abspath(__file__))
101     if not os.path.exists(aubio_dir):
102         raise SystemError("git / root folder not found")
103     gitcmd = ['git', '-C', aubio_dir, 'rev-parse']
104     if short:
105         gitcmd.append('--short')
106     gitcmd.append('HEAD')
107     try:
108         outCmd = subprocess.check_output(gitcmd).strip().decode('utf8')
109     except Exception as e:
110         print('git command error :%s' % e)
111         return None
112     return outCmd