blob: b760faec474f2447fa128c4712ca1dc5bddf6c08 [file] [log] [blame]
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +00001"""Tag the sandbox for release, make source and doc tarballs.
2
3Requires Python 2.6
4
5Example of invocation (use to test the script):
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +00006python makerelease.py --force --retag --platform=msvc6,msvc71,msvc80,mingw -ublep 0.5.0 0.6.0-dev
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +00007
8Example of invocation when doing a release:
9python makerelease.py 0.5.0 0.6.0-dev
10"""
11import os.path
12import subprocess
13import sys
14import doxybuild
15import subprocess
16import xml.etree.ElementTree as ElementTree
Baptiste Lepilleur7c171ee2010-02-23 08:44:52 +000017import shutil
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +000018import urllib2
19import tempfile
20import os
21import time
Baptiste Lepilleure94d2f42010-02-23 21:00:30 +000022from devtools import antglob, fixeol, tarball
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +000023
24SVN_ROOT = 'https://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/'
25SVN_TAG_ROOT = SVN_ROOT + 'tags/jsoncpp'
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +000026SCONS_LOCAL_URL = 'http://sourceforge.net/projects/scons/files/scons-local/1.2.0/scons-local-1.2.0.tar.gz/download'
27SOURCEFORGE_PROJECT = 'jsoncpp'
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +000028
29def set_version( version ):
30 with open('version','wb') as f:
31 f.write( version.strip() )
32
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +000033def rmdir_if_exist( dir_path ):
34 if os.path.isdir( dir_path ):
35 shutil.rmtree( dir_path )
36
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +000037class SVNError(Exception):
38 pass
39
40def svn_command( command, *args ):
41 cmd = ['svn', '--non-interactive', command] + list(args)
42 print 'Running:', ' '.join( cmd )
43 process = subprocess.Popen( cmd,
44 stdout=subprocess.PIPE,
45 stderr=subprocess.STDOUT )
46 stdout = process.communicate()[0]
47 if process.returncode:
48 error = SVNError( 'SVN command failed:\n' + stdout )
49 error.returncode = process.returncode
50 raise error
51 return stdout
52
53def check_no_pending_commit():
54 """Checks that there is no pending commit in the sandbox."""
55 stdout = svn_command( 'status', '--xml' )
56 etree = ElementTree.fromstring( stdout )
57 msg = []
58 for entry in etree.getiterator( 'entry' ):
59 path = entry.get('path')
60 status = entry.find('wc-status').get('item')
Baptiste Lepilleure6a77412010-03-11 21:02:26 +000061 if status != 'unversioned' and path != 'version':
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +000062 msg.append( 'File "%s" has pending change (status="%s")' % (path, status) )
63 if msg:
64 msg.insert(0, 'Pending change to commit found in sandbox. Commit them first!' )
65 return '\n'.join( msg )
66
67def svn_join_url( base_url, suffix ):
68 if not base_url.endswith('/'):
69 base_url += '/'
70 if suffix.startswith('/'):
71 suffix = suffix[1:]
72 return base_url + suffix
73
74def svn_check_if_tag_exist( tag_url ):
75 """Checks if a tag exist.
76 Returns: True if the tag exist, False otherwise.
77 """
78 try:
79 list_stdout = svn_command( 'list', tag_url )
80 except SVNError, e:
81 if e.returncode != 1 or not str(e).find('tag_url'):
82 raise e
83 # otherwise ignore error, meaning tag does not exist
84 return False
85 return True
86
Baptiste Lepilleure6a77412010-03-11 21:02:26 +000087def svn_commit( message ):
88 """Commit the sandbox, providing the specified comment.
89 """
90 svn_command( 'ci', '-m', message )
91
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +000092def svn_tag_sandbox( tag_url, message ):
93 """Makes a tag based on the sandbox revisions.
94 """
95 svn_command( 'copy', '-m', message, '.', tag_url )
96
97def svn_remove_tag( tag_url, message ):
98 """Removes an existing tag.
99 """
100 svn_command( 'delete', '-m', message, tag_url )
101
Baptiste Lepilleur7c171ee2010-02-23 08:44:52 +0000102def svn_export( tag_url, export_dir ):
103 """Exports the tag_url revision to export_dir.
104 Target directory, including its parent is created if it does not exist.
105 If the directory export_dir exist, it is deleted before export proceed.
106 """
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000107 rmdir_if_exist( export_dir )
Baptiste Lepilleur7c171ee2010-02-23 08:44:52 +0000108 svn_command( 'export', tag_url, export_dir )
109
Baptiste Lepilleure94d2f42010-02-23 21:00:30 +0000110def fix_sources_eol( dist_dir ):
111 """Set file EOL for tarball distribution.
112 """
113 print 'Preparing exported source file EOL for distribution...'
114 prune_dirs = antglob.prune_dirs + 'scons-local* ./build* ./libs ./dist'
115 win_sources = antglob.glob( dist_dir,
116 includes = '**/*.sln **/*.vcproj',
117 prune_dirs = prune_dirs )
118 unix_sources = antglob.glob( dist_dir,
119 includes = '''**/*.h **/*.cpp **/*.inl **/*.txt **/*.dox **/*.py **/*.html **/*.in
120 sconscript *.json *.expected AUTHORS LICENSE''',
121 excludes = antglob.default_excludes + 'scons.py sconsign.py scons-*',
122 prune_dirs = prune_dirs )
123 for path in win_sources:
124 fixeol.fix_source_eol( path, is_dry_run = False, verbose = True, eol = '\r\n' )
125 for path in unix_sources:
126 fixeol.fix_source_eol( path, is_dry_run = False, verbose = True, eol = '\n' )
127
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000128def download( url, target_path ):
129 """Download file represented by url to target_path.
130 """
131 f = urllib2.urlopen( url )
132 try:
133 data = f.read()
134 finally:
135 f.close()
136 fout = open( target_path, 'wb' )
137 try:
138 fout.write( data )
139 finally:
140 fout.close()
141
142def check_compile( distcheck_top_dir, platform ):
143 cmd = [sys.executable, 'scons.py', 'platform=%s' % platform, 'check']
144 print 'Running:', ' '.join( cmd )
145 log_path = os.path.join( distcheck_top_dir, 'build-%s.log' % platform )
146 flog = open( log_path, 'wb' )
147 try:
148 process = subprocess.Popen( cmd,
149 stdout=flog,
150 stderr=subprocess.STDOUT,
151 cwd=distcheck_top_dir )
152 stdout = process.communicate()[0]
153 status = (process.returncode == 0)
154 finally:
155 flog.close()
156 return (status, log_path)
157
158def write_tempfile( content, **kwargs ):
159 fd, path = tempfile.mkstemp( **kwargs )
160 f = os.fdopen( fd, 'wt' )
161 try:
162 f.write( content )
163 finally:
164 f.close()
165 return path
166
167class SFTPError(Exception):
168 pass
169
170def run_sftp_batch( userhost, sftp, batch, retry=0 ):
171 path = write_tempfile( batch, suffix='.sftp', text=True )
172 # psftp -agent -C blep,jsoncpp@web.sourceforge.net -batch -b batch.sftp -bc
173 cmd = [sftp, '-agent', '-C', '-batch', '-b', path, '-bc', userhost]
174 error = None
175 for retry_index in xrange(0, max(1,retry)):
176 heading = retry_index == 0 and 'Running:' or 'Retrying:'
177 print heading, ' '.join( cmd )
178 process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
179 stdout = process.communicate()[0]
180 if process.returncode != 0:
181 error = SFTPError( 'SFTP batch failed:\n' + stdout )
182 else:
183 break
184 if error:
185 raise error
186 return stdout
187
188def sourceforge_web_synchro( sourceforge_project, doc_dir,
189 user=None, sftp='sftp' ):
190 """Notes: does not synchronize sub-directory of doc-dir.
191 """
192 userhost = '%s,%s@web.sourceforge.net' % (user, sourceforge_project)
193 stdout = run_sftp_batch( userhost, sftp, """
194cd htdocs
195dir
196exit
197""" )
198 existing_paths = set()
199 collect = 0
200 for line in stdout.split('\n'):
201 line = line.strip()
202 if not collect and line.endswith('> dir'):
203 collect = True
204 elif collect and line.endswith('> exit'):
205 break
206 elif collect == 1:
207 collect = 2
208 elif collect == 2:
209 path = line.strip().split()[-1:]
210 if path and path[0] not in ('.', '..'):
211 existing_paths.add( path[0] )
212 upload_paths = set( [os.path.basename(p) for p in antglob.glob( doc_dir )] )
213 paths_to_remove = existing_paths - upload_paths
214 if paths_to_remove:
215 print 'Removing the following file from web:'
216 print '\n'.join( paths_to_remove )
217 stdout = run_sftp_batch( userhost, sftp, """cd htdocs
218rm %s
219exit""" % ' '.join(paths_to_remove) )
220 print 'Uploading %d files:' % len(upload_paths)
221 batch_size = 10
222 upload_paths = list(upload_paths)
223 start_time = time.time()
224 for index in xrange(0,len(upload_paths),batch_size):
225 paths = upload_paths[index:index+batch_size]
226 file_per_sec = (time.time() - start_time) / (index+1)
227 remaining_files = len(upload_paths) - index
228 remaining_sec = file_per_sec * remaining_files
229 print '%d/%d, ETA=%.1fs' % (index+1, len(upload_paths), remaining_sec)
230 run_sftp_batch( userhost, sftp, """cd htdocs
231lcd %s
232mput %s
233exit""" % (doc_dir, ' '.join(paths) ), retry=3 )
234
Baptiste Lepilleurd89d7962010-02-25 08:30:09 +0000235def sourceforge_release_tarball( sourceforge_project, paths, user=None, sftp='sftp' ):
236 userhost = '%s,%s@frs.sourceforge.net' % (user, sourceforge_project)
237 run_sftp_batch( userhost, sftp, """
238mput %s
239exit
240""" % (' '.join(paths),) )
241
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000242
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000243def main():
244 usage = """%prog release_version next_dev_version
245Update 'version' file to release_version and commit.
246Generates the document tarball.
247Tags the sandbox revision with release_version.
248Update 'version' file to next_dev_version and commit.
249
250Performs an svn export of tag release version, and build a source tarball.
251
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000252Must be started in the project top directory.
253
254Warning: --force should only be used when developping/testing the release script.
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000255"""
256 from optparse import OptionParser
257 parser = OptionParser(usage=usage)
258 parser.allow_interspersed_args = False
259 parser.add_option('--dot', dest="dot_path", action='store', default=doxybuild.find_program('dot'),
260 help="""Path to GraphViz dot tool. Must be full qualified path. [Default: %default]""")
261 parser.add_option('--doxygen', dest="doxygen_path", action='store', default=doxybuild.find_program('doxygen'),
262 help="""Path to Doxygen tool. [Default: %default]""")
263 parser.add_option('--force', dest="ignore_pending_commit", action='store_true', default=False,
264 help="""Ignore pending commit. [Default: %default]""")
265 parser.add_option('--retag', dest="retag_release", action='store_true', default=False,
266 help="""Overwrite release existing tag if it exist. [Default: %default]""")
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000267 parser.add_option('-p', '--platforms', dest="platforms", action='store', default='',
268 help="""Comma separated list of platform passed to scons for build check.""")
Baptiste Lepilleurd89d7962010-02-25 08:30:09 +0000269 parser.add_option('--no-test', dest="no_test", action='store_true', default=False,
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000270 help="""Skips build check.""")
Baptiste Lepilleurd89d7962010-02-25 08:30:09 +0000271 parser.add_option('--no-web', dest="no_web", action='store_true', default=False,
272 help="""Do not update web site.""")
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000273 parser.add_option('-u', '--upload-user', dest="user", action='store',
274 help="""Sourceforge user for SFTP documentation upload.""")
275 parser.add_option('--sftp', dest='sftp', action='store', default=doxybuild.find_program('psftp', 'sftp'),
276 help="""Path of the SFTP compatible binary used to upload the documentation.""")
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000277 parser.enable_interspersed_args()
278 options, args = parser.parse_args()
279
Baptiste Lepilleure6a77412010-03-11 21:02:26 +0000280 if len(args) != 2:
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000281 parser.error( 'release_version missing on command-line.' )
282 release_version = args[0]
Baptiste Lepilleure6a77412010-03-11 21:02:26 +0000283 next_version = args[1]
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000284
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000285 if not options.platforms and not options.no_test:
286 parser.error( 'You must specify either --platform or --no-test option.' )
287
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000288 if options.ignore_pending_commit:
289 msg = ''
290 else:
291 msg = check_no_pending_commit()
292 if not msg:
293 print 'Setting version to', release_version
294 set_version( release_version )
Baptiste Lepilleure6a77412010-03-11 21:02:26 +0000295 svn_commit( 'Release ' + release_version )
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000296 tag_url = svn_join_url( SVN_TAG_ROOT, release_version )
Baptiste Lepilleure94d2f42010-02-23 21:00:30 +0000297 if svn_check_if_tag_exist( tag_url ):
298 if options.retag_release:
299 svn_remove_tag( tag_url, 'Overwriting previous tag' )
300 else:
301 print 'Aborting, tag %s already exist. Use --retag to overwrite it!' % tag_url
302 sys.exit( 1 )
303 svn_tag_sandbox( tag_url, 'Release ' + release_version )
304
305 print 'Generated doxygen document...'
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000306## doc_dirname = r'jsoncpp-api-html-0.5.0'
307## doc_tarball_path = r'e:\prg\vc\Lib\jsoncpp-trunk\dist\jsoncpp-api-html-0.5.0.tar.gz'
308 doc_tarball_path, doc_dirname = doxybuild.build_doc( options, make_release=True )
309 doc_distcheck_dir = 'dist/doccheck'
310 tarball.decompress( doc_tarball_path, doc_distcheck_dir )
311 doc_distcheck_top_dir = os.path.join( doc_distcheck_dir, doc_dirname )
Baptiste Lepilleure94d2f42010-02-23 21:00:30 +0000312
313 export_dir = 'dist/export'
314 svn_export( tag_url, export_dir )
315 fix_sources_eol( export_dir )
316
317 source_dir = 'jsoncpp-src-' + release_version
318 source_tarball_path = 'dist/%s.tar.gz' % source_dir
319 print 'Generating source tarball to', source_tarball_path
320 tarball.make_tarball( source_tarball_path, [export_dir], export_dir, prefix_dir=source_dir )
Baptiste Lepilleur35bdc072010-02-24 08:05:41 +0000321
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000322 # Decompress source tarball, download and install scons-local
Baptiste Lepilleur35bdc072010-02-24 08:05:41 +0000323 distcheck_dir = 'dist/distcheck'
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000324 distcheck_top_dir = distcheck_dir + '/' + source_dir
Baptiste Lepilleur35bdc072010-02-24 08:05:41 +0000325 print 'Decompressing source tarball to', distcheck_dir
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000326 rmdir_if_exist( distcheck_dir )
Baptiste Lepilleur35bdc072010-02-24 08:05:41 +0000327 tarball.decompress( source_tarball_path, distcheck_dir )
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000328 scons_local_path = 'dist/scons-local.tar.gz'
329 print 'Downloading scons-local to', scons_local_path
330 download( SCONS_LOCAL_URL, scons_local_path )
331 print 'Decompressing scons-local to', distcheck_top_dir
332 tarball.decompress( scons_local_path, distcheck_top_dir )
333
334 # Run compilation
335 print 'Compiling decompressed tarball'
336 all_build_status = True
337 for platform in options.platforms.split(','):
338 print 'Testing platform:', platform
339 build_status, log_path = check_compile( distcheck_top_dir, platform )
340 print 'see build log:', log_path
341 print build_status and '=> ok' or '=> FAILED'
342 all_build_status = all_build_status and build_status
343 if not build_status:
344 print 'Testing failed on at least one platform, aborting...'
345 svn_remove_tag( tag_url, 'Removing tag due to failed testing' )
346 sys.exit(1)
347 if options.user:
Baptiste Lepilleurd89d7962010-02-25 08:30:09 +0000348 if not options.no_web:
349 print 'Uploading documentation using user', options.user
350 sourceforge_web_synchro( SOURCEFORGE_PROJECT, doc_distcheck_top_dir, user=options.user, sftp=options.sftp )
351 print 'Completed documentation upload'
352 print 'Uploading source and documentation tarballs for release using user', options.user
353 sourceforge_release_tarball( SOURCEFORGE_PROJECT,
354 [source_tarball_path, doc_tarball_path],
355 user=options.user, sftp=options.sftp )
356 print 'Source and doc release tarballs uploaded'
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000357 else:
Baptiste Lepilleurd89d7962010-02-25 08:30:09 +0000358 print 'No upload user specified. Web site and download tarbal were not uploaded.'
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000359 print 'Tarball can be found at:', doc_tarball_path
Baptiste Lepilleure6a77412010-03-11 21:02:26 +0000360
361 # Set next version number and commit
362 set_version( next_version )
363 svn_commit( 'Released ' + release_version )
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000364 else:
365 sys.stderr.write( msg + '\n' )
366
367if __name__ == '__main__':
368 main()