-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_version.py
executable file
·79 lines (63 loc) · 2.65 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
#!/usr/bin/env python3
# Copyright (C) 2017 SignalFx, Inc. All rights reserved.
# This script is used to update the versions of all the artifacts, the
# User-Agent version of the library, and the documented version in the README
# file, all at once, as part of the release process.
import logging
import os
import re
import subprocess
from subprocess import PIPE
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logger = logging.getLogger(os.path.basename(__file__))
def match_all(v):
return True
def no_snapshots(v):
return 'SNAPSHOT' not in v
FILE_REPLACES = {
'signalflow-client/src/main/java/com/signalfx/signalflow/client/connection/AbstractHttpReceiverConnection.java': [
(match_all, re.compile(r'public static final String VERSION_NUMBER = "(.*?)"'),
'public static final String VERSION_NUMBER = "%s"')
],
}
def execute(cmd, expected_code=None, stdin=None, background=False):
logger.info('Executing in %s: %s', os.getcwd(), ' '.join(cmd))
proc = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
if background:
return ('', '', 0) # In background
stdout, stderr = proc.communicate(stdin)
logger.debug('Result (%s, %s, %d)', stdout, stderr, proc.returncode)
ret = (stdout, stderr, proc.returncode)
if expected_code is not None and expected_code != ret[2]:
raise Exception('Unable to execute command %s, result: %s', ret)
return ret
def update_pom_files(version):
base_dir = os.getcwd()
logger.info('Updating POM files to version %s...', version)
cmd = ['./mvnw', 'versions:set', '-am', '-pl', 'signalflow-client',
'-DnewVersion=%s' % version]
(stdout, _, code) = execute(cmd, expected_code=0)
os.chdir(base_dir)
def perform_file_replacements(version):
for file_name, repls in FILE_REPLACES.items():
logger.info('Updating %d version number location%s in %s...',
len(repls), 's' if len(repls) != 1 else '', file_name)
for repl in repls:
if not repl[0](version):
continue
logger.debug('%s -> %s', repl[1], repl[2])
file_name = os.path.join(os.getcwd(), file_name)
with open(file_name, 'r') as f:
contents = f.read()
contents = repl[1].sub(repl[2] % version, contents)
with open(file_name, 'w') as f:
f.write(contents)
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.stderr.write(f"usage: {sys.argv[0]} <version>\n")
sys.exit(1)
version = sys.argv[1]
version = re.sub(r'^v', '', version)
update_pom_files(version)
perform_file_replacements(version)