#!/usr/bin/env python"""This program takes information about a release in the ReleaseConfig fileand inserts the information in various files. It helps to keep the filesin sync because it never forgets which files need to be updated, nor whatinformation should be inserted in each file.It takes one parameter that says what the mode of update should be, whichmay be one of 'develop', 'release', or 'post'.In 'develop' mode, which is what someone would use as part of their owndevelopment practices, the release type is forced to be 'alpha' and thepatch 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 thepatch level is calculated from the release date for non-final runs andtaken from the version tuple for final runs. It then inserts informationin 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__## 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.__revision__="__FILE__ __REVISION__ __DATE__ __DEVELOPER__"importosimportsysimporttimeimportreDEBUG=os.environ.get('DEBUG',0)# Evaluate parameteriflen(sys.argv)<2:mode='develop'else:mode=sys.argv[1]ifmodenotin['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 informationconfig=dict()execopen('ReleaseConfig').read()inglobals(),configtry:version_tuple=config['version_tuple']unsupported_version=config['unsupported_python_version']deprecated_version=config['deprecated_python_version']exceptKeyError:print('''ERROR: Config file must contain at least version_tuple,\tunsupported_python_version, and deprecated_python_version.''')sys.exit(1)ifDEBUG:print'version tuple',version_tupleifDEBUG:print'unsupported Python version',unsupported_versionifDEBUG:print'deprecated Python version',deprecated_versiontry:release_date=config['release_date']exceptKeyError:release_date=time.localtime()[:6]else:iflen(release_date)==3:release_date=release_date+time.localtime()[3:6]iflen(release_date)!=6:print'''ERROR: Invalid release date''',release_datesys.exit(1)ifDEBUG:print'release date',release_dateifmode=='develop'andversion_tuple[3]!='alpha':version_tuple==version_tuple[:3]+('alpha',0)iflen(version_tuple)>3andversion_tuple[3]!='final':ifmode=='develop':version_tuple=version_tuple[:4]+('yyyymmdd',)else:yyyy,mm,dd,_,_,_=release_dateversion_tuple=version_tuple[:4]+((yyyy*100+mm)*100+dd,)version_string='.'.join(map(str,version_tuple))iflen(version_tuple)>3:version_type=version_tuple[3]else:version_type='final'ifDEBUG:print'version string',version_stringifversion_typenotin['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']exceptKeyError:ifversion_type=='alpha':month_year='MONTH YEAR'else:month_year=time.strftime('%B %Y',release_date+(0,0,0))ifDEBUG:print'month year',month_yeartry:copyright_years=config['copyright_years']exceptKeyError:copyright_years=', '.join(map(str,list(range(2001,release_date[0]+1))))ifDEBUG:print'copyright years',copyright_yearsclassUpdateFile(object):""" XXX """def__init__(self,file,orig=None):''' '''iforigisNone:orig=filetry:self.content=open(orig,'rU').read()exceptIOError:# Couldn't open file; don't try to write anything in __del__self.file=Noneraiseelse:self.file=fileiffile==orig:# so we can see if it changedself.orig=self.contentelse:# pretend file changedself.orig=''defsub(self,pattern,replacement,count=1):''' XXX '''self.content=re.sub(pattern,replacement,self.content,count)defreplace_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)defreplace_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 HEREifmode=='develop':new_date='NEW DATE WILL BE INSERTED HERE'else:min=(time.daylightandtime.altzoneortime.timezone)//60hr=min//60min=-(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)defreplace_date(self,replacement=new_date,count=1):''' XXX '''self.content=self.match_date.sub(replacement,self.content,count)def__del__(self):''' XXX '''ifself.fileisnotNoneandself.content!=self.orig:print'Updating '+self.file+'...'open(self.file,'w').write(self.content)ifmode=='post':# Set up for the next release series.ifversion_tuple[2]:# micro release, increment micro valueminor=version_tuple[1]micro=version_tuple[2]+1else:# minor release, increment minor valueminor=version_tuple[1]+1micro=0new_tuple=(version_tuple[0],minor,micro,'alpha',0)new_version='.'.join(map(str,new_tuple[:4]))+'.yyyymmdd'# Update ReleaseConfigt=UpdateFile('ReleaseConfig')ifDEBUG:t.file='/tmp/ReleaseConfig't.replace_assign('version_tuple',str(new_tuple))# Update src/CHANGES.txtt=UpdateFile(os.path.join('src','CHANGES.txt'))ifDEBUG: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.txtt=UpdateFile(os.path.join('src','RELEASE.txt'),os.path.join('template','RELEASE.txt'))ifDEBUG:t.file='/tmp/RELEASE.txt't.replace_version(new_version)# Update src/Announce.txtt=UpdateFile(os.path.join('src','Announce.txt'))ifDEBUG: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 exitt=Nonesys.exit()# Update src/CHANGES.txtt=UpdateFile(os.path.join('src','CHANGES.txt'))ifDEBUG:t.file='/tmp/CHANGES.txt't.sub('\nRELEASE .*','\nRELEASE '+version_string+' - '+t.new_date)# Update src/RELEASE.txtt=UpdateFile(os.path.join('src','RELEASE.txt'))ifDEBUG:t.file='/tmp/RELEASE.txt't.replace_version()# Update src/Announce.txtt=UpdateFile(os.path.join('src','Announce.txt'))ifDEBUG:t.file='/tmp/Announce.txt't.sub('\nRELEASE .*','\nRELEASE '+version_string+' - '+t.new_date)# Update SConstructt=UpdateFile('SConstruct')ifDEBUG: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 READMEt=UpdateFile('README.rst')ifDEBUG:t.file='/tmp/README.rst't.sub('-'+t.match_pat+'\.','-'+version_string+'.',count=0)forsufin['tar','win32','zip','rpm','exe','deb']:t.sub('-(\d+\.\d+\.\d+)\.%s'%suf,'-%s.%s'%(version_string,suf),count=0)# Update QMTest/TestSCons.pyt=UpdateFile(os.path.join('QMTest','TestSCons.py'))ifDEBUG: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.pyt=UpdateFile(os.path.join('src','engine','SCons','Script','Main.py'))ifDEBUG: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=', '.join(map(str,iter(range(2004,release_date[0]+1))))t=UpdateFile(os.path.join('doc','user','main.in'))ifDEBUG: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'))ifDEBUG:t.file='/tmp/main.xml'## TODO debug these#t.sub('<pubdate>[^<]*</pubdate>', '<pubdate>' + docyears + '</pubdate>')#t.sub('<year>[^<]*</year>', '<year>' + docyears + '</year>')# Write out the last updatet=None# Local Variables:# tab-width:4# indent-tabs-mode:nil# End:# vim: set expandtab tabstop=4 shiftwidth=4: