-
Notifications
You must be signed in to change notification settings - Fork 6
/
update_version.py
83 lines (73 loc) · 3.95 KB
/
update_version.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Version updater for Sample Applications
This script helps cut a new release for ROS applications which are not intended to be released via bloom, and is meant to be invoked automatically as part of a Travis CI job.
It takes in a path indicating where the version.json file should be created, and the current version of the application (typically provided by `git describe`).
It also relies on the environment variables TRAVIS_BUILD_DIR and SA_PACKAGE_NAME being preset (the latter implies which package manifest should be updated).
The tool performs the following actions:
1. Determines what is the new version (bumps the patch component by default).
2. Creates a version.json file with the new version string.
3. Updates package.xml if necessary.
"""
import os
import sys
import subprocess
import xml.etree.ElementTree as ET
from distutils.version import StrictVersion
VERSION_COMPONENTS_TO_IDX = {'major': 0, 'minor': 1, 'patch': 2}
FIRST_VERSION = '1.0.0'
# If the repository already contains tags in an unsupported format (e.g. b1.0.58.0.1.0.51.0), bump to the following version.
FIRST_VERSION_WITH_EXISTING_OBSOLETE_TAGS = '1.1.0'
def get_incremented_version(version, increment_type='patch'):
components = version.split('.')
if len(components) != 3:
# Either there was no release yet (no tags) or the tag format is invalid.
if 'No names found' in version:
return FIRST_VERSION
else:
return FIRST_VERSION_WITH_EXISTING_OBSOLETE_TAGS
else:
# Bump version according to increment_type
components[VERSION_COMPONENTS_TO_IDX[increment_type]] = str(int(components[VERSION_COMPONENTS_TO_IDX[increment_type]]) + 1)
return '.'.join(components)
def update_version(file_path, current_version):
new_version = get_incremented_version(current_version)
# Create ephemeral version.json in order to tell the internal build system which version should be built.
with open(file_path, 'wb') as f:
f.write('{"application_version": "%s"}\n' % (new_version, ))
# Update version in package.xml if needed.
package_xml_path = get_path_to_package_xml()
if not package_xml_path:
print 'Skipping package.xml update as it could not be located (Does the environment contain TRAVIS_BUILD_DIR and SA_PACKAGE_NAME?)'
return
xml_tree = ET.parse(package_xml_path)
xml_root = xml_tree.getroot()
version_element = xml_root.findall('version')[0]
if StrictVersion(version_element.text) == StrictVersion(new_version):
print 'package.xml already up to date with version %s, not updating.' % (new_version, )
elif StrictVersion(version_element.text) > StrictVersion(new_version):
raise ValueError('The version in package.xml (%s) is newer than the version intended to be released (%s)' % (version_element.text, new_version, ))
else:
version_element.text = new_version
xml_tree.write(package_xml_path)
# TODO: git commit, push / open PR.
def get_current_package_xml_version():
package_xml_path = get_path_to_package_xml()
if not package_xml_path:
return None
xml_tree = ET.parse(package_xml_path)
xml_root = xml_tree.getroot()
version_element = xml_root.findall('version')[0]
return version_element.text
def get_path_to_package_xml():
"""
@note assumes the environment contains TRAVIS_BUILD_DIR and SA_PACKAGE_NAME.
"""
if 'TRAVIS_BUILD_DIR' in os.environ and 'SA_PACKAGE_NAME' in os.environ:
search_cmd = 'find %s -name "package.xml" -path "*/%s/*"' % (os.environ['TRAVIS_BUILD_DIR'], os.environ['SA_PACKAGE_NAME'], )
return subprocess.check_output(search_cmd, shell=True).strip()
else:
return None
if __name__ == '__main__':
if len(sys.argv) < 3:
raise ValueError("Missing parameters. Usage: update_version.py <version file path> <current version>")
version_file_path, current_version = sys.argv[1:]
update_version(version_file_path, current_version)