source: this_version.py @ 9189c42

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampler
Last change on this file since 9189c42 was c203601, checked in by Paul Brossier <piem@piem.org>, 7 years ago

this_version.py: show status if tree is not clean

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