blob: 2b6c564b8ab0a9af855d6d1b20ce975ed700e8b3 [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 Lepilleure1b26452010-03-13 10:59:50 +00006python makerelease.py --platform=msvc6,msvc71,msvc80,msvc90,mingw -ublep 0.6.0 0.7.0-dev
Baptiste Lepilleurcd6cb5d2010-03-13 07:59:07 +00007
8When testing this script:
Baptiste Lepilleurbafb43c2011-05-01 20:36:55 +00009python makerelease.py --force --retag --platform=msvc6,msvc71,msvc80,mingw -ublep test-0.6.0 test-0.6.1-dev
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +000010
11Example of invocation when doing a release:
12python makerelease.py 0.5.0 0.6.0-dev
Christopher Dunnb7894972014-09-10 18:01:10 -070013
14Note: This was for Subversion. Now that we are in GitHub, we do not
15need to build versioned tarballs anymore, so makerelease.py is defunct.
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +000016"""
17import os.path
18import subprocess
19import sys
20import doxybuild
21import subprocess
22import xml.etree.ElementTree as ElementTree
Baptiste Lepilleur7c171ee2010-02-23 08:44:52 +000023import shutil
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +000024import urllib2
25import tempfile
26import os
27import time
Baptiste Lepilleure94d2f42010-02-23 21:00:30 +000028from devtools import antglob, fixeol, tarball
Baptiste Lepilleureadc4782011-05-02 21:09:30 +000029import amalgamate
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +000030
31SVN_ROOT = 'https://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/'
32SVN_TAG_ROOT = SVN_ROOT + 'tags/jsoncpp'
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +000033SCONS_LOCAL_URL = 'http://sourceforge.net/projects/scons/files/scons-local/1.2.0/scons-local-1.2.0.tar.gz/download'
34SOURCEFORGE_PROJECT = 'jsoncpp'
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +000035
36def set_version( version ):
37 with open('version','wb') as f:
38 f.write( version.strip() )
39
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +000040def rmdir_if_exist( dir_path ):
41 if os.path.isdir( dir_path ):
42 shutil.rmtree( dir_path )
43
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +000044class SVNError(Exception):
45 pass
46
47def svn_command( command, *args ):
48 cmd = ['svn', '--non-interactive', command] + list(args)
49 print 'Running:', ' '.join( cmd )
50 process = subprocess.Popen( cmd,
51 stdout=subprocess.PIPE,
52 stderr=subprocess.STDOUT )
53 stdout = process.communicate()[0]
54 if process.returncode:
55 error = SVNError( 'SVN command failed:\n' + stdout )
56 error.returncode = process.returncode
57 raise error
58 return stdout
59
60def check_no_pending_commit():
61 """Checks that there is no pending commit in the sandbox."""
62 stdout = svn_command( 'status', '--xml' )
63 etree = ElementTree.fromstring( stdout )
64 msg = []
65 for entry in etree.getiterator( 'entry' ):
66 path = entry.get('path')
67 status = entry.find('wc-status').get('item')
Baptiste Lepilleure6a77412010-03-11 21:02:26 +000068 if status != 'unversioned' and path != 'version':
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +000069 msg.append( 'File "%s" has pending change (status="%s")' % (path, status) )
70 if msg:
71 msg.insert(0, 'Pending change to commit found in sandbox. Commit them first!' )
72 return '\n'.join( msg )
73
74def svn_join_url( base_url, suffix ):
75 if not base_url.endswith('/'):
76 base_url += '/'
77 if suffix.startswith('/'):
78 suffix = suffix[1:]
79 return base_url + suffix
80
81def svn_check_if_tag_exist( tag_url ):
82 """Checks if a tag exist.
83 Returns: True if the tag exist, False otherwise.
84 """
85 try:
86 list_stdout = svn_command( 'list', tag_url )
Christopher Dunn9aa61442014-11-19 23:10:02 -060087 except SVNError as e:
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +000088 if e.returncode != 1 or not str(e).find('tag_url'):
89 raise e
90 # otherwise ignore error, meaning tag does not exist
91 return False
92 return True
93
Baptiste Lepilleure6a77412010-03-11 21:02:26 +000094def svn_commit( message ):
95 """Commit the sandbox, providing the specified comment.
96 """
97 svn_command( 'ci', '-m', message )
98
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +000099def svn_tag_sandbox( tag_url, message ):
100 """Makes a tag based on the sandbox revisions.
101 """
102 svn_command( 'copy', '-m', message, '.', tag_url )
103
104def svn_remove_tag( tag_url, message ):
105 """Removes an existing tag.
106 """
107 svn_command( 'delete', '-m', message, tag_url )
108
Baptiste Lepilleur7c171ee2010-02-23 08:44:52 +0000109def svn_export( tag_url, export_dir ):
110 """Exports the tag_url revision to export_dir.
111 Target directory, including its parent is created if it does not exist.
112 If the directory export_dir exist, it is deleted before export proceed.
113 """
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000114 rmdir_if_exist( export_dir )
Baptiste Lepilleur7c171ee2010-02-23 08:44:52 +0000115 svn_command( 'export', tag_url, export_dir )
116
Baptiste Lepilleure94d2f42010-02-23 21:00:30 +0000117def fix_sources_eol( dist_dir ):
118 """Set file EOL for tarball distribution.
119 """
120 print 'Preparing exported source file EOL for distribution...'
121 prune_dirs = antglob.prune_dirs + 'scons-local* ./build* ./libs ./dist'
122 win_sources = antglob.glob( dist_dir,
123 includes = '**/*.sln **/*.vcproj',
124 prune_dirs = prune_dirs )
125 unix_sources = antglob.glob( dist_dir,
126 includes = '''**/*.h **/*.cpp **/*.inl **/*.txt **/*.dox **/*.py **/*.html **/*.in
127 sconscript *.json *.expected AUTHORS LICENSE''',
128 excludes = antglob.default_excludes + 'scons.py sconsign.py scons-*',
129 prune_dirs = prune_dirs )
130 for path in win_sources:
131 fixeol.fix_source_eol( path, is_dry_run = False, verbose = True, eol = '\r\n' )
132 for path in unix_sources:
133 fixeol.fix_source_eol( path, is_dry_run = False, verbose = True, eol = '\n' )
134
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000135def download( url, target_path ):
136 """Download file represented by url to target_path.
137 """
138 f = urllib2.urlopen( url )
139 try:
140 data = f.read()
141 finally:
142 f.close()
143 fout = open( target_path, 'wb' )
144 try:
145 fout.write( data )
146 finally:
147 fout.close()
148
149def check_compile( distcheck_top_dir, platform ):
150 cmd = [sys.executable, 'scons.py', 'platform=%s' % platform, 'check']
151 print 'Running:', ' '.join( cmd )
152 log_path = os.path.join( distcheck_top_dir, 'build-%s.log' % platform )
153 flog = open( log_path, 'wb' )
154 try:
155 process = subprocess.Popen( cmd,
156 stdout=flog,
157 stderr=subprocess.STDOUT,
158 cwd=distcheck_top_dir )
159 stdout = process.communicate()[0]
160 status = (process.returncode == 0)
161 finally:
162 flog.close()
163 return (status, log_path)
164
165def write_tempfile( content, **kwargs ):
166 fd, path = tempfile.mkstemp( **kwargs )
167 f = os.fdopen( fd, 'wt' )
168 try:
169 f.write( content )
170 finally:
171 f.close()
172 return path
173
174class SFTPError(Exception):
175 pass
176
177def run_sftp_batch( userhost, sftp, batch, retry=0 ):
178 path = write_tempfile( batch, suffix='.sftp', text=True )
179 # psftp -agent -C blep,jsoncpp@web.sourceforge.net -batch -b batch.sftp -bc
180 cmd = [sftp, '-agent', '-C', '-batch', '-b', path, '-bc', userhost]
181 error = None
182 for retry_index in xrange(0, max(1,retry)):
183 heading = retry_index == 0 and 'Running:' or 'Retrying:'
184 print heading, ' '.join( cmd )
185 process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
186 stdout = process.communicate()[0]
187 if process.returncode != 0:
188 error = SFTPError( 'SFTP batch failed:\n' + stdout )
189 else:
190 break
191 if error:
192 raise error
193 return stdout
194
195def sourceforge_web_synchro( sourceforge_project, doc_dir,
196 user=None, sftp='sftp' ):
197 """Notes: does not synchronize sub-directory of doc-dir.
198 """
199 userhost = '%s,%s@web.sourceforge.net' % (user, sourceforge_project)
200 stdout = run_sftp_batch( userhost, sftp, """
201cd htdocs
202dir
203exit
204""" )
205 existing_paths = set()
206 collect = 0
207 for line in stdout.split('\n'):
208 line = line.strip()
209 if not collect and line.endswith('> dir'):
210 collect = True
211 elif collect and line.endswith('> exit'):
212 break
213 elif collect == 1:
214 collect = 2
215 elif collect == 2:
216 path = line.strip().split()[-1:]
217 if path and path[0] not in ('.', '..'):
218 existing_paths.add( path[0] )
219 upload_paths = set( [os.path.basename(p) for p in antglob.glob( doc_dir )] )
220 paths_to_remove = existing_paths - upload_paths
221 if paths_to_remove:
222 print 'Removing the following file from web:'
223 print '\n'.join( paths_to_remove )
224 stdout = run_sftp_batch( userhost, sftp, """cd htdocs
225rm %s
226exit""" % ' '.join(paths_to_remove) )
227 print 'Uploading %d files:' % len(upload_paths)
228 batch_size = 10
229 upload_paths = list(upload_paths)
230 start_time = time.time()
231 for index in xrange(0,len(upload_paths),batch_size):
232 paths = upload_paths[index:index+batch_size]
233 file_per_sec = (time.time() - start_time) / (index+1)
234 remaining_files = len(upload_paths) - index
235 remaining_sec = file_per_sec * remaining_files
236 print '%d/%d, ETA=%.1fs' % (index+1, len(upload_paths), remaining_sec)
237 run_sftp_batch( userhost, sftp, """cd htdocs
238lcd %s
239mput %s
240exit""" % (doc_dir, ' '.join(paths) ), retry=3 )
241
Baptiste Lepilleurd89d7962010-02-25 08:30:09 +0000242def sourceforge_release_tarball( sourceforge_project, paths, user=None, sftp='sftp' ):
243 userhost = '%s,%s@frs.sourceforge.net' % (user, sourceforge_project)
244 run_sftp_batch( userhost, sftp, """
245mput %s
246exit
247""" % (' '.join(paths),) )
248
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000249
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000250def main():
251 usage = """%prog release_version next_dev_version
252Update 'version' file to release_version and commit.
253Generates the document tarball.
254Tags the sandbox revision with release_version.
255Update 'version' file to next_dev_version and commit.
256
257Performs an svn export of tag release version, and build a source tarball.
258
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000259Must be started in the project top directory.
260
261Warning: --force should only be used when developping/testing the release script.
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000262"""
263 from optparse import OptionParser
264 parser = OptionParser(usage=usage)
265 parser.allow_interspersed_args = False
266 parser.add_option('--dot', dest="dot_path", action='store', default=doxybuild.find_program('dot'),
267 help="""Path to GraphViz dot tool. Must be full qualified path. [Default: %default]""")
268 parser.add_option('--doxygen', dest="doxygen_path", action='store', default=doxybuild.find_program('doxygen'),
269 help="""Path to Doxygen tool. [Default: %default]""")
270 parser.add_option('--force', dest="ignore_pending_commit", action='store_true', default=False,
271 help="""Ignore pending commit. [Default: %default]""")
272 parser.add_option('--retag', dest="retag_release", action='store_true', default=False,
273 help="""Overwrite release existing tag if it exist. [Default: %default]""")
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000274 parser.add_option('-p', '--platforms', dest="platforms", action='store', default='',
275 help="""Comma separated list of platform passed to scons for build check.""")
Baptiste Lepilleurd89d7962010-02-25 08:30:09 +0000276 parser.add_option('--no-test', dest="no_test", action='store_true', default=False,
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000277 help="""Skips build check.""")
Baptiste Lepilleurd89d7962010-02-25 08:30:09 +0000278 parser.add_option('--no-web', dest="no_web", action='store_true', default=False,
279 help="""Do not update web site.""")
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000280 parser.add_option('-u', '--upload-user', dest="user", action='store',
281 help="""Sourceforge user for SFTP documentation upload.""")
282 parser.add_option('--sftp', dest='sftp', action='store', default=doxybuild.find_program('psftp', 'sftp'),
283 help="""Path of the SFTP compatible binary used to upload the documentation.""")
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000284 parser.enable_interspersed_args()
285 options, args = parser.parse_args()
286
Baptiste Lepilleure6a77412010-03-11 21:02:26 +0000287 if len(args) != 2:
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000288 parser.error( 'release_version missing on command-line.' )
289 release_version = args[0]
Baptiste Lepilleure6a77412010-03-11 21:02:26 +0000290 next_version = args[1]
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000291
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000292 if not options.platforms and not options.no_test:
293 parser.error( 'You must specify either --platform or --no-test option.' )
294
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000295 if options.ignore_pending_commit:
296 msg = ''
297 else:
298 msg = check_no_pending_commit()
299 if not msg:
300 print 'Setting version to', release_version
301 set_version( release_version )
Baptiste Lepilleure6a77412010-03-11 21:02:26 +0000302 svn_commit( 'Release ' + release_version )
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000303 tag_url = svn_join_url( SVN_TAG_ROOT, release_version )
Baptiste Lepilleure94d2f42010-02-23 21:00:30 +0000304 if svn_check_if_tag_exist( tag_url ):
305 if options.retag_release:
306 svn_remove_tag( tag_url, 'Overwriting previous tag' )
307 else:
308 print 'Aborting, tag %s already exist. Use --retag to overwrite it!' % tag_url
309 sys.exit( 1 )
310 svn_tag_sandbox( tag_url, 'Release ' + release_version )
311
312 print 'Generated doxygen document...'
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000313## doc_dirname = r'jsoncpp-api-html-0.5.0'
314## doc_tarball_path = r'e:\prg\vc\Lib\jsoncpp-trunk\dist\jsoncpp-api-html-0.5.0.tar.gz'
315 doc_tarball_path, doc_dirname = doxybuild.build_doc( options, make_release=True )
316 doc_distcheck_dir = 'dist/doccheck'
317 tarball.decompress( doc_tarball_path, doc_distcheck_dir )
318 doc_distcheck_top_dir = os.path.join( doc_distcheck_dir, doc_dirname )
Baptiste Lepilleure94d2f42010-02-23 21:00:30 +0000319
320 export_dir = 'dist/export'
321 svn_export( tag_url, export_dir )
322 fix_sources_eol( export_dir )
323
324 source_dir = 'jsoncpp-src-' + release_version
325 source_tarball_path = 'dist/%s.tar.gz' % source_dir
326 print 'Generating source tarball to', source_tarball_path
327 tarball.make_tarball( source_tarball_path, [export_dir], export_dir, prefix_dir=source_dir )
Baptiste Lepilleur35bdc072010-02-24 08:05:41 +0000328
Baptiste Lepilleureadc4782011-05-02 21:09:30 +0000329 amalgamation_tarball_path = 'dist/%s-amalgamation.tar.gz' % source_dir
330 print 'Generating amalgamation source tarball to', amalgamation_tarball_path
331 amalgamation_dir = 'dist/amalgamation'
332 amalgamate.amalgamate_source( export_dir, '%s/jsoncpp.cpp' % amalgamation_dir, 'json/json.h' )
333 amalgamation_source_dir = 'jsoncpp-src-amalgamation' + release_version
334 tarball.make_tarball( amalgamation_tarball_path, [amalgamation_dir],
335 amalgamation_dir, prefix_dir=amalgamation_source_dir )
Baptiste Lepilleurbafb43c2011-05-01 20:36:55 +0000336
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000337 # Decompress source tarball, download and install scons-local
Baptiste Lepilleur35bdc072010-02-24 08:05:41 +0000338 distcheck_dir = 'dist/distcheck'
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000339 distcheck_top_dir = distcheck_dir + '/' + source_dir
Baptiste Lepilleur35bdc072010-02-24 08:05:41 +0000340 print 'Decompressing source tarball to', distcheck_dir
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000341 rmdir_if_exist( distcheck_dir )
Baptiste Lepilleur35bdc072010-02-24 08:05:41 +0000342 tarball.decompress( source_tarball_path, distcheck_dir )
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000343 scons_local_path = 'dist/scons-local.tar.gz'
344 print 'Downloading scons-local to', scons_local_path
345 download( SCONS_LOCAL_URL, scons_local_path )
346 print 'Decompressing scons-local to', distcheck_top_dir
347 tarball.decompress( scons_local_path, distcheck_top_dir )
348
349 # Run compilation
350 print 'Compiling decompressed tarball'
351 all_build_status = True
352 for platform in options.platforms.split(','):
353 print 'Testing platform:', platform
354 build_status, log_path = check_compile( distcheck_top_dir, platform )
355 print 'see build log:', log_path
356 print build_status and '=> ok' or '=> FAILED'
357 all_build_status = all_build_status and build_status
358 if not build_status:
359 print 'Testing failed on at least one platform, aborting...'
360 svn_remove_tag( tag_url, 'Removing tag due to failed testing' )
361 sys.exit(1)
362 if options.user:
Baptiste Lepilleurd89d7962010-02-25 08:30:09 +0000363 if not options.no_web:
364 print 'Uploading documentation using user', options.user
365 sourceforge_web_synchro( SOURCEFORGE_PROJECT, doc_distcheck_top_dir, user=options.user, sftp=options.sftp )
366 print 'Completed documentation upload'
367 print 'Uploading source and documentation tarballs for release using user', options.user
368 sourceforge_release_tarball( SOURCEFORGE_PROJECT,
369 [source_tarball_path, doc_tarball_path],
370 user=options.user, sftp=options.sftp )
371 print 'Source and doc release tarballs uploaded'
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000372 else:
Baptiste Lepilleurd89d7962010-02-25 08:30:09 +0000373 print 'No upload user specified. Web site and download tarbal were not uploaded.'
Baptiste Lepilleur64ba0622010-02-24 23:08:47 +0000374 print 'Tarball can be found at:', doc_tarball_path
Baptiste Lepilleure6a77412010-03-11 21:02:26 +0000375
376 # Set next version number and commit
377 set_version( next_version )
378 svn_commit( 'Released ' + release_version )
Baptiste Lepilleur1f4847c2010-02-23 07:57:38 +0000379 else:
380 sys.stderr.write( msg + '\n' )
381
382if __name__ == '__main__':
383 main()