This repository has been archived by the owner on Oct 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
updateversion.py
executable file
·140 lines (114 loc) · 3.19 KB
/
updateversion.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
"""
Usage:
python updateversion.py [options]
Options:
-h, --help Display this help message.
-v x.xx, --version=x.xx
New version. Required.
--verbose Print extra info.
Example:
python updateversion.py -v 1.17c
"""
#
# Imports
from __future__ import print_function
import sys
import getopt
import re
import shutil
#
# Globals and constants
VERBOSE = False
REPL_SPEC = [
{
'filename': 'setup.py',
'pattern': r'(##VERSION##\s*version=")([\w\.\-]*)(",\s*##VERSION##)',
},
{
'filename': 'generateDS.py',
'pattern': r"(##VERSION##\s*VERSION = ')([\w\.\-]*)('\s*##VERSION##)",
},
{
'filename': 'process_includes.py',
'pattern': r"(##VERSION##\s*VERSION = ')([\w\.\-]*)('\s*##VERSION##)",
},
{
'filename': 'generateDS.txt',
'pattern': r"(.. version\s*:revision: )([\w\.\-]*)(\s*.. version)",
},
{
'filename': 'generateds_gui_notes.txt',
'pattern': r"(.. version\s*:revision: )([\w\.\-]*)(\s*.. version)",
},
{
'filename': 'librarytemplate_howto.txt',
'pattern': r"(.. version\s*:revision: )([\w\.\-]*)(\s*.. version)",
},
{
'filename': 'tutorial/generateds_tutorial.txt',
'pattern': r"(.. version\s*:revision: )([\w\.\-]*)(\s*.. version)",
},
{
'filename': 'gui/generateds_gui.py',
'pattern': r"(##VERSION##\s*VERSION = ')([\w\.\-]*)('\s*##VERSION##)",
},
]
#
# Functions for external use
def updateversion(version):
replfunc = replfuncmaker(version)
for spec in REPL_SPEC:
targetfilename = spec['filename']
if VERBOSE:
print('updating: "%s"' % (targetfilename, ))
targetfile = open(targetfilename, 'r')
content = targetfile.read()
targetfile.close()
content1 = re.sub(
spec['pattern'],
replfunc,
content)
update1file(targetfilename, content1)
# Classes
#
# Functions for internal use and testing
def update1file(targetfilename, content):
backupfilename = targetfilename + '.bak'
shutil.copy2(targetfilename, backupfilename)
targetfile = open(targetfilename, 'w')
targetfile.write(content)
targetfile.close()
#shutil.copymode(backupfilename, targetfilename)
def replfuncmaker(version):
def replfunc(matchobj):
if VERBOSE:
print('(replfunc) matchobj.groups()', matchobj.groups())
return matchobj.group(1) + version + matchobj.group(3)
return replfunc
USAGE_TEXT = __doc__
def usage():
print(USAGE_TEXT)
sys.exit(1)
def main():
global VERBOSE
args = sys.argv[1:]
try:
opts, args = getopt.getopt(args, 'hv:', [
'help', 'version=', 'verbose', ])
except:
usage()
version = None
for opt, val in opts:
if opt in ('-h', '--help'):
usage()
elif opt in ('-v', '--version'):
version = val
elif opt in ('--verbose', ):
VERBOSE = True
if len(args) != 0 or version is None:
usage()
updateversion(version)
if __name__ == '__main__':
#import pdb; pdb.set_trace()
main()