summarylogtreecommitdiffstats
path: root/put_version_into_build_xml.py
blob: f5e0eabd55e4016fdb593294d1a34f519d75bacd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import re
import subprocess
import sys
import xml.etree.ElementTree as ET


def main():
    build_xml_filename = 'build.xml'
    if not os.access(build_xml_filename, os.R_OK | os.W_OK):
        print(
            '%s not found. Skipping' % build_xml_filename,
            file=sys.stderr)
        return

    tree = ET.parse(build_xml_filename)

    try:
        git_version = subprocess.check_output(
            ['git', 'describe', '--long', '--tags', '--match', 'version*'])
    except subprocess.CalledProcessError:
        print('Call to git failed. Skipping...', file=sys.stderr)
        return

    mobj = re.match(
        r'''(?x)
            version
            (?P<version>[^-]+)-
            (?P<revcount>\d+)-
            g(?P<hash>[0-9a-f]+)''',
        git_version.decode('utf-8'))

    if not mobj:
        print('unexpected git version %s. Skipping...' % git_version)
        return

    versions = {
        'version': mobj.group('version'),
        'revcount': mobj.group('revcount'),
        'hash': mobj.group('hash'),
    }

    print('Set version to %s.r%s (commit %s)' % (
        versions['version'], versions['revcount'], versions['hash']))

    for version_key, value in zip(['major', 'minor', 'release', 'build'], versions['version'].split('.')):
        path = './target/property[@name="version.%s"]' % version_key
        for element in tree.findall(path):
            element.set('value', value)

    tree.write(build_xml_filename, encoding='UTF-8', xml_declaration=True)

if __name__ == '__main__':
    main()