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