source: this_version.py @ 71b6437

feature/crepe
Last change on this file since 71b6437 was ddc9733, checked in by Paul Brossier <piem@piem.org>, 8 years ago

this_version.py: add options to get only libaubio or python-aubio version

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[255fe0a]1#! python
[aab682e]2import os
[8250214]3import sys
[638be5f]4
[255fe0a]5__version_info = {} # keep a reference to parse VERSION once
[07bfe2d2]6
[255c4c8]7def get_version_info():
8    # read from VERSION
9    # return dictionary filled with content of version
10    if not __version_info:
[c23d45f]11        this_file_dir = os.path.dirname(os.path.abspath(__file__))
[07bfe2d2]12        version_file = os.path.join(this_file_dir, 'VERSION')
[c23d45f]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():
[07bfe2d2]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.")
[c23d45f]37
38        # switch version status with commit sha in alpha releases
39        if __version_info['AUBIO_VERSION_STATUS'] and \
[07bfe2d2]40                '~alpha' in __version_info['AUBIO_VERSION_STATUS']:
[c23d45f]41            AUBIO_GIT_SHA = get_git_revision_hash()
42            if AUBIO_GIT_SHA:
[7b9b6ba]43                __version_info['AUBIO_VERSION_STATUS'] = '~git+' + AUBIO_GIT_SHA
[255c4c8]44
45    return __version_info
46
47def get_libaubio_version():
[255fe0a]48    verfmt = '%(LIBAUBIO_LT_CUR)s.%(LIBAUBIO_LT_REV)s.%(LIBAUBIO_LT_AGE)s'
49    return str(verfmt % get_version_info())
[07bfe2d2]50
[255fe0a]51def 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())
[aab682e]54
[255fe0a]55def get_aubio_pyversion():
[aab682e]56    # convert to version for python according to pep 440
57    # see https://www.python.org/dev/peps/pep-0440/
[87a4a5a]58    # outputs MAJ.MIN.PATCH[a0[+git.<sha>[.mods]]]
[255fe0a]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:
[f5bb097]64        verstr = aubio_version.replace('~alpha', 'a0')
65    else:
66        verstr = aubio_version
[aab682e]67    return verstr
68
[07bfe2d2]69def get_git_revision_hash(short=True):
[255fe0a]70    # get commit id, with +mods if local tree is not clean
[aab682e]71    if not os.path.isdir('.git'):
72        # print('Version : not in git repository : can\'t get sha')
[638be5f]73        return None
[aab682e]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")
[07bfe2d2]78    gitcmd = ['git', '-C', aubio_dir, 'rev-parse']
[aab682e]79    if short:
[c23d45f]80        gitcmd.append('--short')
[aab682e]81    gitcmd.append('HEAD')
[638be5f]82    try:
[f7b7a35]83        gitsha = subprocess.check_output(gitcmd).strip().decode('utf8')
[638be5f]84    except Exception as e:
[8250214]85        sys.stderr.write('git command error :%s\n' % e)
[c23d45f]86        return None
[f7b7a35]87    # check if we have a clean tree
[115b452]88    gitcmd = ['git', '-C', aubio_dir, 'status', '--porcelain']
[f7b7a35]89    try:
[115b452]90        output = subprocess.check_output(gitcmd).decode('utf8')
91        if len(output):
[8250214]92            sys.stderr.write('Info: current tree is not clean\n\n')
93            sys.stderr.write(output + '\n')
[115b452]94            gitsha += '+mods'
95    except subprocess.CalledProcessError as e:
[8250214]96        sys.stderr.write('git command error :%s\n' % e)
[115b452]97        pass
[f7b7a35]98    return gitsha
[255fe0a]99
100if __name__ == '__main__':
[ddc9733]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())
Note: See TracBrowser for help on using the repository browser.