make_snapshot.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Copyright (c) MetaCommunications, Inc. 2003-2007
  2. #
  3. # Distributed under the Boost Software License, Version 1.0.
  4. # (See accompanying file LICENSE_1_0.txt or copy at
  5. # http://www.boost.org/LICENSE_1_0.txt)
  6. import tarfile
  7. import shutil
  8. import time
  9. import os.path
  10. import string
  11. import sys
  12. import traceback
  13. def retry( f, args, max_attempts=5, sleep_secs=10 ):
  14. for attempts in range( max_attempts, -1, -1 ):
  15. try:
  16. return f( *args )
  17. except Exception, msg:
  18. utils.log( '%s failed with message "%s"' % ( f.__name__, msg ) )
  19. if attempts == 0:
  20. utils.log( 'Giving up.' )
  21. raise
  22. utils.log( 'Retrying (%d more attempts).' % attempts )
  23. time.sleep( sleep_secs )
  24. def rmtree( path ):
  25. if os.path.exists( path ):
  26. if sys.platform == 'win32':
  27. os.system( 'del /f /s /q "%s" >nul 2>&1' % path )
  28. shutil.rmtree( path )
  29. else:
  30. os.system( 'rm -f -r "%s"' % path )
  31. def svn_command( command ):
  32. utils.log( 'Executing SVN command "%s"' % command )
  33. rc = os.system( command )
  34. if rc != 0:
  35. raise Exception( 'SVN command "%s" failed with code %d' % ( command, rc ) )
  36. def svn_export( sources_dir, user, tag ):
  37. if user is None or user == 'anonymous':
  38. command = 'svn export --force http://svn.boost.org/svn/boost/%s %s' % ( tag, sources_dir )
  39. else:
  40. command = 'svn export --force --non-interactive --username=%s https://svn.boost.org/svn/boost/%s %s' \
  41. % ( user, tag, sources_dir )
  42. os.chdir( os.path.basename( sources_dir ) )
  43. retry(
  44. svn_command
  45. , ( command, )
  46. )
  47. def make_tarball(
  48. working_dir
  49. , tag
  50. , user
  51. , site_dir
  52. ):
  53. timestamp = time.time()
  54. timestamp_suffix = time.strftime( '%y-%m-%d-%H%M', time.gmtime( timestamp ) )
  55. tag_suffix = tag.split( '/' )[-1]
  56. sources_dir = os.path.join(
  57. working_dir
  58. , 'boost-%s-%s' % ( tag_suffix, timestamp_suffix )
  59. )
  60. if os.path.exists( sources_dir ):
  61. utils.log( 'Directory "%s" already exists, cleaning it up...' % sources_dir )
  62. rmtree( sources_dir )
  63. try:
  64. os.mkdir( sources_dir )
  65. utils.log( 'Exporting files from SVN...' )
  66. svn_export( sources_dir, user, tag )
  67. except:
  68. utils.log( 'Cleaning up...' )
  69. rmtree( sources_dir )
  70. raise
  71. tarball_name = 'boost-%s.tar.bz2' % tag_suffix
  72. tarball_path = os.path.join( working_dir, tarball_name )
  73. utils.log( 'Archiving "%s" to "%s"...' % ( sources_dir, tarball_path ) )
  74. tar = tarfile.open( tarball_path, 'w|bz2' )
  75. tar.posix = False # see http://tinyurl.com/4ebd8
  76. tar.add( sources_dir, os.path.basename( sources_dir ) )
  77. tar.close()
  78. tarball_timestamp_path = os.path.join( working_dir, 'boost-%s.timestamp' % tag_suffix )
  79. utils.log( 'Writing timestamp into "%s"...' % tarball_timestamp_path )
  80. timestamp_file = open( tarball_timestamp_path, 'w' )
  81. timestamp_file.write( '%f' % timestamp )
  82. timestamp_file.close()
  83. md5sum_path = os.path.join( working_dir, 'boost-%s.md5' % tag_suffix )
  84. utils.log( 'Writing md5 checksum into "%s"...' % md5sum_path )
  85. old_dir = os.getcwd()
  86. os.chdir( os.path.dirname( tarball_path ) )
  87. os.system( 'md5sum -b "%s" >"%s"' % ( os.path.basename( tarball_path ), md5sum_path ) )
  88. os.chdir( old_dir )
  89. if site_dir is not None:
  90. utils.log( 'Moving "%s" to the site location "%s"...' % ( tarball_name, site_dir ) )
  91. temp_site_dir = os.path.join( site_dir, 'temp' )
  92. if not os.path.exists( temp_site_dir ):
  93. os.mkdir( temp_site_dir )
  94. shutil.move( tarball_path, temp_site_dir )
  95. shutil.move( os.path.join( temp_site_dir, tarball_name ), site_dir )
  96. shutil.move( tarball_timestamp_path, site_dir )
  97. shutil.move( md5sum_path, site_dir )
  98. utils.log( 'Removing "%s"...' % sources_dir )
  99. rmtree( sources_dir )
  100. def accept_args( args ):
  101. args_spec = [
  102. 'working-dir='
  103. , 'tag='
  104. , 'user='
  105. , 'site-dir='
  106. , 'mail='
  107. , 'help'
  108. ]
  109. options = {
  110. '--tag': 'trunk'
  111. , '--user': None
  112. , '--site-dir': None
  113. }
  114. utils.accept_args( args_spec, args, options, usage )
  115. return (
  116. options[ '--working-dir' ]
  117. , options[ '--tag' ]
  118. , options[ '--user' ]
  119. , options[ '--site-dir' ]
  120. )
  121. def usage():
  122. print 'Usage: %s [options]' % os.path.basename( sys.argv[0] )
  123. print '''
  124. \t--working-dir working directory
  125. \t--tag snapshot tag (i.e. 'trunk')
  126. \t--user Boost SVN user ID (optional)
  127. \t--site-dir site directory to copy the snapshot to (optional)
  128. '''
  129. def main():
  130. make_tarball( *accept_args( sys.argv[ 1: ] ) )
  131. if __name__ != '__main__': import utils
  132. else:
  133. # in absense of relative import...
  134. xsl_path = os.path.abspath( os.path.dirname( sys.argv[ 0 ] ) )
  135. while os.path.basename( xsl_path ) != 'xsl_reports': xsl_path = os.path.dirname( xsl_path )
  136. sys.path.append( xsl_path )
  137. import utils
  138. main()
粤ICP备19079148号