summaryrefslogtreecommitdiff
path: root/bin/update-release-info.py
blob: a4096d1521f59c24fdb114178654d2a71c02487d (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python
"""
This program takes information about a release in the ReleaseConfig file
and inserts the information in various files.  It helps to keep the files
in sync because it never forgets which files need to be updated, nor what
information should be inserted in each file.

It takes one parameter that says what the mode of update should be, which
may be one of 'develop', 'release', or 'post'.

In 'develop' mode, which is what someone would use as part of their own
development practices, the release type is forced to be 'alpha' and the
patch level is the string 'yyyymmdd'.  Otherwise, it's the same as the
'release' mode.

In 'release' mode, the release type is taken from ReleaseConfig and the
patch level is calculated from the release date for non-final runs and
taken from the version tuple for final runs.  It then inserts information
in various files:
  - The RELEASE header line in src/CHANGES.txt and src/Announce.txt.
  - The version string at the top of src/RELEASE.txt.
  - The version string in the 'default_version' variable in SConstruct
    and QMTest/TestSCons.py.
  - The copyright years in SConstruct and QMTest/TestSCons.py.
  - The month and year (used for documentation) in SConstruct.
  - The unsupported and deprecated Python floors in QMTest/TestSCons.py
    and src/engine/SCons/Script/Main.py
  - The version string in the filenames in README.

In 'post' mode, files are prepared for the next release cycle:
  - In ReleaseConfig, the version tuple is updated for the next release
    by incrementing the release number (either minor or micro, depending
    on the branch) and resetting release type to 'alpha'.
  - A blank template replaces src/RELEASE.txt.
  - A new section to accumulate changes is added to src/CHANGES.txt and
    src/Announce.txt.
"""
#
# Copyright (c) 2001 - 2017 The SCons Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import print_function

__revision__ = "bin/update-release-info.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"

import os
import sys
import time
import re

DEBUG = os.environ.get('DEBUG', 0)

# Evaluate parameter

if len(sys.argv) < 2:
    mode = 'develop'
else:
    mode = sys.argv[1]
    if mode not in ['develop', 'release', 'post']:
        print(("""ERROR: `%s' as a parameter is invalid; it must be one of
\tdevelop, release, or post.  The default is develop.""" % mode))
        sys.exit(1)

# Get configuration information

config = dict()
exec(open('ReleaseConfig').read(), globals(), config)

try:
    version_tuple = config['version_tuple']
    unsupported_version = config['unsupported_python_version']
    deprecated_version = config['deprecated_python_version']
except KeyError:
    print('''ERROR: Config file must contain at least version_tuple,
\tunsupported_python_version, and deprecated_python_version.''')
    sys.exit(1)
if DEBUG: print('version tuple', version_tuple)
if DEBUG: print('unsupported Python version', unsupported_version)
if DEBUG: print('deprecated Python version', deprecated_version)

try:
    release_date = config['release_date']
except KeyError:
    release_date = time.localtime()[:6]
else:
    if len(release_date) == 3:
        release_date = release_date + time.localtime()[3:6]
    if len(release_date) != 6:
        print('''ERROR: Invalid release date''', release_date)
        sys.exit(1)
if DEBUG: print('release date', release_date)

if mode == 'develop' and version_tuple[3] != 'alpha':
    version_tuple ==  version_tuple[:3] + ('alpha', 0)
if len(version_tuple) > 3 and version_tuple[3] != 'final':
    if mode == 'develop':
        version_tuple = version_tuple[:4] + ('yyyymmdd',)
    else:
        yyyy,mm,dd,_,_,_ = release_date
        version_tuple = version_tuple[:4] + ((yyyy*100 + mm)*100 + dd,)
version_string = '.'.join(map(str, version_tuple))
if len(version_tuple) > 3:
    version_type = version_tuple[3]
else:
    version_type = 'final'
if DEBUG: print('version string', version_string)

if version_type not in ['alpha', 'beta', 'candidate', 'final']:
    print(("""ERROR: `%s' is not a valid release type in version tuple;
\tit must be one of alpha, beta, candidate, or final""" % version_type))
    sys.exit(1)

try:
    month_year = config['month_year']
except KeyError:
    if version_type == 'alpha':
        month_year = 'MONTH YEAR'
    else:
        month_year =  time.strftime('%B %Y', release_date + (0,0,0))
if DEBUG: print('month year', month_year)

try:
    copyright_years = config['copyright_years']
except KeyError:
    copyright_years = '2001 - %d'%(release_date[0] + 1)
if DEBUG: print('copyright years', copyright_years)

class UpdateFile(object):
    """
    XXX
    """

    def __init__(self, file, orig = None):
        '''
        '''
        if orig is None: orig = file
        try:
            self.content = open(orig, 'r').read()
        except IOError:
            # Couldn't open file; don't try to write anything in __del__
            self.file = None
            raise
        else:
            self.file = file
            if file == orig:
                # so we can see if it changed
                self.orig = self.content
            else:
                # pretend file changed
                self.orig = ''

    def sub(self, pattern, replacement, count = 1):
        '''
        XXX
        '''
        self.content = re.sub(pattern, replacement, self.content, count)

    def replace_assign(self, name, replacement, count = 1):
        '''
        XXX
        '''
        self.sub('\n' + name + ' = .*', '\n' + name + ' = ' + replacement)

    # Determine the pattern to match a version

    _rel_types = '(alpha|beta|candidate|final)'
    match_pat = '\d+\.\d+\.\d+\.' + _rel_types + '\.(\d+|yyyymmdd)'
    match_rel = re.compile(match_pat)

    def replace_version(self, replacement = version_string, count = 1):
        '''
        XXX
        '''
        self.content = self.match_rel.sub(replacement, self.content, count)

    # Determine the release date and the pattern to match a date
    # Mon, 05 Jun 2010 21:17:15 -0700
    # NEW DATE WILL BE INSERTED HERE

    if mode == 'develop':
        new_date = 'NEW DATE WILL BE INSERTED HERE'
    else:
        min = (time.daylight and time.altzone or time.timezone)//60
        hr = min//60
        min = -(min%60 + hr*100)
        new_date =  (time.strftime('%a, %d %b %Y %X', release_date + (0,0,0))
                         + ' %+.4d' % min)

    _days = '(Sun|Mon|Tue|Wed|Thu|Fri|Sat)'
    _months = '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oce|Nov|Dec)'
    match_date = _days+', \d\d '+_months+' \d\d\d\d \d\d:\d\d:\d\d [+-]\d\d\d\d'
    match_date = re.compile(match_date)

    def replace_date(self, replacement = new_date, count = 1):
        '''
        XXX
        '''
        self.content = self.match_date.sub(replacement, self.content, count)

    def __del__(self):
        '''
        XXX
        '''
        if self.file is not None and self.content != self.orig:
            print('Updating ' + self.file + '...')
            open(self.file, 'w').write(self.content)

if mode == 'post':
    # Set up for the next release series.

    if version_tuple[2]:
        # micro release, increment micro value
        minor = version_tuple[1]
        micro = version_tuple[2] + 1
    else:
        # minor release, increment minor value
        minor = version_tuple[1] + 1
        micro = 0
    new_tuple = (version_tuple[0], minor, micro, 'alpha', 0)
    new_version = '.'.join(map(str, new_tuple[:4])) + '.yyyymmdd'

    # Update ReleaseConfig

    t = UpdateFile('ReleaseConfig')
    if DEBUG: t.file = '/tmp/ReleaseConfig'
    t.replace_assign('version_tuple', str(new_tuple))

    # Update src/CHANGES.txt

    t = UpdateFile(os.path.join('src', 'CHANGES.txt'))
    if DEBUG: t.file = '/tmp/CHANGES.txt'
    t.sub('(\nRELEASE .*)', r"""\nRELEASE  VERSION/DATE TO BE FILLED IN LATER\n
  From John Doe:\n
    - Whatever John Doe did.\n
\1""")

    # Update src/RELEASE.txt

    t = UpdateFile(os.path.join('src', 'RELEASE.txt'),
                   os.path.join('template', 'RELEASE.txt'))
    if DEBUG: t.file = '/tmp/RELEASE.txt'
    t.replace_version(new_version)

    # Update src/Announce.txt

    t = UpdateFile(os.path.join('src', 'Announce.txt'))
    if DEBUG: t.file = '/tmp/Announce.txt'
    t.sub('\nRELEASE .*', '\nRELEASE VERSION/DATE TO BE FILLED IN LATER')
    announce_pattern = """(
  Please note the following important changes scheduled for the next
  release:
)"""
    announce_replace = (r"""\1
    --  FEATURE THAT WILL CHANGE\n
  Please note the following important changes since release """
            + '.'.join(map(str, version_tuple[:3])) + ':\n')
    t.sub(announce_pattern, announce_replace)

    # Write out the last update and exit

    t = None
    sys.exit()

# Update src/CHANGES.txt

t = UpdateFile(os.path.join('src', 'CHANGES.txt'))
if DEBUG: t.file = '/tmp/CHANGES.txt'
t.sub('\nRELEASE .*', '\nRELEASE ' + version_string + ' - ' + t.new_date)

# Update src/RELEASE.txt

t = UpdateFile(os.path.join('src', 'RELEASE.txt'))
if DEBUG: t.file = '/tmp/RELEASE.txt'
t.replace_version()

# Update src/Announce.txt

t = UpdateFile(os.path.join('src', 'Announce.txt'))
if DEBUG: t.file = '/tmp/Announce.txt'
t.sub('\nRELEASE .*', '\nRELEASE ' + version_string + ' - ' + t.new_date)


# Update SConstruct

t = UpdateFile('SConstruct')
if DEBUG: t.file = '/tmp/SConstruct'
t.replace_assign('month_year', repr(month_year))
t.replace_assign('copyright_years', repr(copyright_years))
t.replace_assign('default_version', repr(version_string))

# Update README

t = UpdateFile('README.rst')
if DEBUG: t.file = '/tmp/README.rst'
t.sub('-' + t.match_pat + '\.', '-' + version_string + '.', count = 0)
for suf in ['tar', 'win32', 'zip', 'rpm', 'exe', 'deb']:
    t.sub('-(\d+\.\d+\.\d+)\.%s' % suf,
          '-%s.%s' % (version_string, suf),
          count = 0)

# Update QMTest/TestSCons.py

t = UpdateFile(os.path.join('QMTest', 'TestSCons.py'))
if DEBUG: t.file = '/tmp/TestSCons.py'
t.replace_assign('copyright_years', repr(copyright_years))
t.replace_assign('default_version', repr(version_string))
#??? t.replace_assign('SConsVersion', repr(version_string))
t.replace_assign('python_version_unsupported', str(unsupported_version))
t.replace_assign('python_version_deprecated', str(deprecated_version))

# Update Script/Main.py

t = UpdateFile(os.path.join('src', 'engine', 'SCons', 'Script', 'Main.py'))
if DEBUG: t.file = '/tmp/Main.py'
t.replace_assign('unsupported_python_version', str(unsupported_version))
t.replace_assign('deprecated_python_version', str(deprecated_version))

# Update doc/user/main.{in,xml}

docyears = '2004 - %d' % release_date[0]
if os.path.exists(os.path.join('doc', 'user', 'main.in')):
    # this is no longer used as of Dec 2013
    t = UpdateFile(os.path.join('doc', 'user', 'main.in'))
    if DEBUG: t.file = '/tmp/main.in'
    ## TODO debug these
    #t.sub('<pubdate>[^<]*</pubdate>', '<pubdate>' + docyears + '</pubdate>')
    #t.sub('<year>[^<]*</year>', '<year>' + docyears + '</year>')

t = UpdateFile(os.path.join('doc', 'user', 'main.xml'))
if DEBUG: t.file = '/tmp/main.xml'
t.sub('<pubdate>[^<]*</pubdate>', '<pubdate>' + docyears + '</pubdate>')
t.sub('<year>[^<]*</year>', '<year>' + docyears + '</year>')

# Write out the last update

t = None

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: