-
Notifications
You must be signed in to change notification settings - Fork 0
/
version-bump.py
94 lines (83 loc) · 3.53 KB
/
version-bump.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
84
85
86
87
88
89
90
91
92
93
94
#this is the script
import os
import re
import sys
from optparse import OptionParser
#TODO What happens if writing fails half way through?
#TODO Compare old and new version.txt
p = re.compile('^(\d+\.)?(\d+\.)?(\d+\-SNAPSHOT)$')
np = re.compile('^(\d+\.)?(\d+\.)?(\d+)?(\-SNAPSHOT)?$')
# find filename
filename = 'VERSION.txt'
def updateFile(filename, version):
foundit = False
try:
with open(filename, 'r', encoding='utf-8') as v, open('NEW'+filename, 'w', encoding='utf-8') as nv:
print('write data to '+'NEW'+filename)
for read_data in v:
#Check for string matching vesion pattern
m = p.match(read_data)
if foundit:
#Found a match so now just ignore the rest of the file
nv.write(read_data)
else:
if m:
print('Found version in file! '+read_data[:-1])
newversion=read_data[:-10]
#remove '-SNAPSHOT', don't forget the newline character
print('Changed to '+newversion)
if version:
print('New version in file: '+version)
nv.write(version+'\n')
else:
print('New version in file: '+newversion)
nv.write(newversion+'\n')
print('now just ignore the rest of the file')
foundit = True
else:
#not found a match yet so just ignore line
nv.write(read_data)
#delete old file and rename new one
v.close()
nv.close()
try:
os.remove('OLD'+filename)
print('deleted '+'OLD'+filename)
except FileNotFoundError:
print('OLD'+filename+' not found')
os.rename(filename, 'OLD'+filename)
print('renamed '+filename+' to '+'OLD'+filename)
os.rename('NEW'+filename, filename)
print('renamed '+'NEW'+filename+' to '+filename)
except FileNotFoundError:
print(filename+' not found, no action taken')
#argv = sys.argv
#print('Argument List:', str(argv))
parser = OptionParser()
parser.add_option("-f", "--file", dest='fname', help='name of the version text file')
parser.add_option("-v", "--version", dest='newversion', help='new version number to be added to the file')
(opts, args) = parser.parse_args()
if opts.fname:
filename = opts.fname
print('Updating version file: '+filename)
specifiednewversion = None
if opts.newversion:
specifiednewversion = opts.newversion
if np.match(specifiednewversion):
print('Updating version file with version '+specifiednewversion)
updateFile(filename, specifiednewversion)
else:
print(specifiednewversion+' is not a valid version, no action taken')
#HOW DO I GET OUT FROM HERE
else:
#new version not supplied, prompt for it
invalidVersion = True
while invalidVersion:
inputversion = input('Please provide a new version, format <major>.<minor>.<patch>-<qualifier>')
if np.match(inputversion):
print('Updating version file with version '+inputversion)
invalidVersion = False
specifiednewversion = inputversion
else:
print('new version is not in the correct format')
updateFile(filename, specifiednewversion)