summarylogtreecommitdiffstats
path: root/put_version_into_build_xml.py
blob: 2408c86c8dc2717592c6dc8fa67ef6717b18f7f7 (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
55
56
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'])
    except subprocess.CalledProcessError:
        print('Call to git failed. Skipping...', file=sys.stderr)
        return

    mobj = re.match(
        r'''(?x)
            version
            (?P<major>\d)\.
            (?P<minor>\d)\.
            (?P<release>\d)-
            (?P<build>\d+)-.*''',
        git_version.decode('utf-8'))

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

    versions = {
        'major': mobj.group('major'),
        'minor': mobj.group('minor'),
        'release': mobj.group('release'),
        'build': mobj.group('build'),
    }

    print('Set version to %s.%s.%s' % (
        versions['major'], versions['minor'], versions['release']))

    for version_type in versions.keys():
        path = './target/property[@name="version.%s"]' % version_type
        for element in tree.findall(path):
            element.set('value', str(versions[version_type]))

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

if __name__ == '__main__':
    main()