-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetversion.py
More file actions
executable file
·215 lines (168 loc) · 7.55 KB
/
Copy pathsetversion.py
File metadata and controls
executable file
·215 lines (168 loc) · 7.55 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python2
"""Set version.
This script updates the version number information
in the Properties.py files, as well as *.html and *.txt.
This should be used only by Webware developers whenever
a new Webware version is being cut. Please be very
careful and read also the ReleaseProcedures.html.
If setVersion is True, then the version information is updated
in various files as follows:
Properties.py files version information is set, replacing the
version setting and releaseDate setting.
*.html files version information is set by searching for a
comment tag surrounding both version and release date and replacing
the version and release date information respectively.
*.txt files version is set by matching
:Version:
:Released:
tags at the beginning of the line. This is designed for the
reStructured text documents. Note that reStructured text
HTML files will need to be re-generated after processing.
The version in ReleaseNotes-X.Y.phtml is not set (this will be
done by the installer), but they are renamed to the current version.
If possible, this is done with "git mv". Exception: If no new notes
have been written (i.e. ReleaseNotes-X.Y same as ReleaseNotesTemplate),
they will not be saved, but deleted, if possible, using "git rm".
If newRelease is True, then a new release is prepared as follows:
The version in ReleaseNotes-X.Y.phtml files is set, and they
are renamed to the current version if they are not empty.
New ReleaseNotes-X.y.phtml files are created from the
ReleaseNotesTemplates.phtml files instead.
If possible, "git mv" and "git add" will be used.
Note that this script will not automatically perform a "git commit"
so you can always revert when something goes wrong.
You should not use "setVersion" on the master branch, but after creating
a tag for the desired version which must not be a final release.
You should use "newRelease" on the trunk for the final version
when you want to freeze the release notes and switch to empty
release notes for the next release.
Written by Stuart Donaldson - stu at asyn.com.
Improved by Christoph Zwerschke - cito at online.de.
"""
# Version format is (Major, Minor, Sub, Alpha/Beta/etc)
# The Sub is optional, and if 0 is not returned.
# Examples: (0, 8, 1, 'b1'), (0, 8, 2) or (0, 9, 0, 'rc1')
# releaseDate format should be 'MM/DD/YY'.
# Update this to change the current version and release date:
version = ('X', 'Y', 0)
releaseDate = '@@/@@/@@'
# Set Version info in files (should not be done on the trunk):
setVersion = True
# Prepare a new release (this should be done on the trunk):
newRelease = False
# Verbose output (output unchanged files also):
verbose = False
import os
import sys
import re
from glob import glob
# We assume that this script is located in Webware/bin:
progPath = os.path.abspath(sys.argv[0])
webwarePath = os.path.dirname(os.path.dirname(progPath))
sys.path.append(webwarePath)
os.chdir(webwarePath)
from MiscUtils.PropertiesObject import PropertiesObject
class Replacer(object):
"""Class to handle substitutions in a file."""
def __init__(self, *args):
self._subs = list(args)
def add(self, search, replace):
self._subs.append((re.compile(search, re.M), replace))
def replaceInStr(self, data):
for search, replace in self._subs:
data = re.sub(search, replace, data)
return data
def replaceInFile(self, filename):
data = open(filename, 'rb').read()
newData = self.replaceInStr(data)
if data == newData:
if verbose:
print 'Unchanged ' + filename
else:
print 'Updating ' + filename
open(filename, 'wb').write(newData)
if os.system('git add ' + filename):
print "git add not possible."
def replaceGlob(self, pattern):
for filename in glob(pattern):
if os.path.exists(filename):
self.replaceInFile(filename)
def main():
po = PropertiesObject()
po.loadValues(version=version, releaseDate=releaseDate)
po.createVersionString()
if po['versionString'] == 'X.Y':
print "Please set the version."
sys.exit(1)
elif po['releaseDate'] == '@@/@@/@@':
print "Please set the release Date."
sys.exit(1)
propReplace = Replacer()
propReplace.add(r"(version\s*=)\s*.*", r"\g<1> %s" % repr(version))
propReplace.add(r"(releaseDate\s*=)\s*.*", r"\g<1> %s" % repr(releaseDate))
htmlReplace = Replacer()
htmlReplace.add(r"<!--\s*version\s*-->[^<]*<!--\s*/version\s*-->",
r"<!-- version --> %s <!-- /version -->" % po['versionString'])
htmlReplace.add(r"<!--\s*relDate\s*-->[^<]*<!--\s*/relDate\s*-->",
r"<!-- relDate --> %s <!-- /relDate -->" % po['releaseDate'])
rstReplace = Replacer()
rstReplace.add(r"^:Version:.*$", ":Version: %s" % po['versionString'])
rstReplace.add(r"^:Released:.*$", ":Released: %s" % po['releaseDate'])
phtmlReplace = Replacer()
phtmlReplace.add(r"(<%.*)' \+ versionString \+ '(.*%>)",
r"\g<1>%s\g<2>" % po['versionString'])
phtmlReplace.add(r"<% versionString %>", po['versionString'])
phtmlReplace.add(r"<% releaseDate %>", po['releaseDate'])
twillReplace = Replacer()
twillReplace.add(r"^setglobal version .*$",
r"setglobal version %s" % po['versionString'])
twillReplace.add(r"^setglobal date .*$",
r"setglobal date %s" % po['releaseDate'])
twillReplace.add(r"^# if release ", '')
if setVersion:
# Replace in Properties files:
propReplace.replaceGlob('Properties.py')
propReplace.replaceGlob('*/Properties.py')
# Replace in existing HTML:
htmlReplace.replaceGlob('*/Docs/*.html')
htmlReplace.replaceGlob('Docs/*.html')
# Replace in reStructuredText files:
rstReplace.replaceGlob('*/Docs/*.txt')
rstReplace.replaceGlob('Docs/*.txt')
# Replace in global README file:
rstReplace.replaceGlob('_README')
# Replace in twill test scripts:
twillReplace.replaceGlob('WebKit/Tests/twill/*.twill')
# Process release notes:
if setVersion or newRelease:
template = open('DocSupport/RelNotesTemplate.phtml', 'rb').read()
infile = 'RelNotes-X.Y.phtml'
outfile = infile.replace('X.Y', po['versionString'])
for filename in ['Docs/' + infile] + glob('*/Docs/' + infile):
if verbose:
print "Processing " + filename
current = open(filename, 'rb').read()
if current == template:
if newRelease:
print "Kept empty " + filename
continue
else:
print "Removing empty " + filename
if os.system('git rm ' + filename):
print "git rm not possible."
os.remove(filename)
else:
if newRelease:
phtmlReplace.replaceInFile(filename)
newName = os.path.join(os.path.split(filename)[0], outfile)
print "Renaming %s to %s" % (filename, outfile)
if os.system('git mv -f %s %s' % (filename, newName)):
print "git mv not possible."
os.rename(filename, newName)
if newRelease:
print "Creating empty " + filename
open(filename, 'wb').write(template)
if os.system('git add ' + filename):
print "git add not possible."
if __name__ == '__main__':
main()