make_snapshot.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # Copyright (c) MetaCommunications, Inc. 2003-2004
  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 socket
  7. import tarfile
  8. import shutil
  9. import time
  10. import os.path
  11. import string
  12. import sys
  13. import traceback
  14. def retry( f, args, max_attempts=2, sleep_secs=10 ):
  15. for attempts in range( max_attempts, -1, -1 ):
  16. try:
  17. return f( *args )
  18. except Exception, msg:
  19. utils.log( '%s failed with message "%s"' % ( f.__name__, msg ) )
  20. if attempts == 0:
  21. utils.log( 'Giving up.' )
  22. raise
  23. utils.log( 'Retrying (%d more attempts).' % attempts )
  24. time.sleep( sleep_secs )
  25. def rmtree( path ):
  26. if os.path.exists( path ):
  27. if sys.platform == 'win32':
  28. os.system( 'del /f /s /q "%s" >nul 2>&1' % path )
  29. shutil.rmtree( path )
  30. else:
  31. os.system( 'rm -f -r "%s"' % path )
  32. def cvs_command( user, command ):
  33. cmd = 'cvs -d:ext:%(user)s@cvs.sourceforge.net:/cvsroot/boost -z9 %(command)s' \
  34. % { 'user': user, 'command': command }
  35. utils.log( 'Executing CVS command "%s"' % cmd )
  36. rc = os.system( cmd )
  37. if rc != 0:
  38. raise Exception( 'CVS command "%s" failed with code %d' % ( cmd, rc ) )
  39. def cvs_export( working_dir, user, tag ):
  40. if tag != 'CVS-HEAD':
  41. command = 'export -r %s boost' % tag
  42. else:
  43. command = 'export -r HEAD boost'
  44. os.chdir( working_dir )
  45. retry(
  46. cvs_command
  47. , ( user, command )
  48. , max_attempts=5
  49. )
  50. def make_tarball(
  51. working_dir
  52. , tag
  53. , user
  54. , site_dir
  55. ):
  56. sources_dir = os.path.join( working_dir, 'boost' )
  57. if os.path.exists( sources_dir ):
  58. utils.log( 'Already running, exiting this one...' )
  59. return False
  60. try:
  61. os.mkdir( sources_dir )
  62. utils.log( 'Exporting files from CVS...' )
  63. cvs_export( working_dir, user, tag )
  64. except:
  65. utils.log( 'Cleaning up...' )
  66. rmtree( sources_dir )
  67. raise
  68. timestamped_dir_name = 'boost-%s-%s' % ( tag, time.strftime( '%y-%m-%d-%H%M', time.gmtime() ) )
  69. timestamped_dir = os.path.join( working_dir, timestamped_dir_name )
  70. utils.log( 'Renaming "%s" to "%s"...' % ( sources_dir, timestamped_dir ) )
  71. os.rename( sources_dir, timestamped_dir )
  72. tarball_name = 'boost-%s.tar.bz2' % tag
  73. tarball_path = os.path.join( working_dir, tarball_name )
  74. utils.log( 'Archiving "%s" to "%s"...' % ( timestamped_dir, tarball_path ) )
  75. tar = tarfile.open( tarball_path, 'w|bz2' )
  76. tar.posix = False # see http://tinyurl.com/4ebd8
  77. tar.add( timestamped_dir, timestamped_dir_name )
  78. tar.close()
  79. if site_dir is not None:
  80. utils.log( 'Moving "%s" to the site location "%s"...' % ( tarball_name, site_dir ) )
  81. shutil.move( tarball_path, site_dir )
  82. utils.log( 'Removing "%s"...' % timestamped_dir )
  83. rmtree( timestamped_dir )
  84. return True
  85. def format_time( t ):
  86. return time.strftime(
  87. '%a, %d %b %Y %H:%M:%S +0000'
  88. , t
  89. )
  90. def make_tarball_send_mail(
  91. working_dir
  92. , tag
  93. , user
  94. , site_dir
  95. , mail
  96. ):
  97. try:
  98. mail_subject = '[Boost CVS tarball] Build for %s on %s' % ( tag, string.split(socket.gethostname(), '.')[0] )
  99. send_mail = make_tarball(
  100. working_dir
  101. , tag
  102. , user
  103. , site_dir
  104. )
  105. if mail and send_mail:
  106. utils.log( 'Sending report to "%s"' % mail )
  107. utils.send_mail(
  108. mail
  109. , '%s completed successfully at %s.' % ( mail_subject, format_time( time.localtime() ) )
  110. )
  111. except:
  112. if mail:
  113. utils.log( 'Sending report to "%s"' % mail )
  114. msg = apply( traceback.format_exception, sys.exc_info() )
  115. utils.send_mail(
  116. mail
  117. , '%s failed at %s.' % ( mail_subject, format_time( time.localtime() ) )
  118. , '\n'.join( msg )
  119. )
  120. raise
  121. def accept_args( args ):
  122. args_spec = [
  123. 'working-dir='
  124. , 'tag='
  125. , 'user='
  126. , 'site-dir='
  127. , 'mail='
  128. , 'help'
  129. ]
  130. options = {
  131. '--tag': 'CVS-HEAD'
  132. , '--site-dir': None
  133. , '--mail': None
  134. }
  135. utils.accept_args( args_spec, args, options, usage )
  136. return (
  137. options[ '--working-dir' ]
  138. , options[ '--tag' ]
  139. , options[ '--user' ]
  140. , options[ '--site-dir' ]
  141. , options[ '--mail' ]
  142. )
  143. def usage():
  144. print 'Usage: %s [options]' % os.path.basename( sys.argv[0] )
  145. print '''
  146. \t--working-dir working directory
  147. \t--tag snapshot tag (i.e. 'CVS-HEAD')
  148. \t--user SourceForge user name for a CVS account
  149. \t--site-dir site directory to copy the snapshot to (optional)
  150. \t--mail email address to send run notification to (optional)
  151. '''
  152. def main():
  153. make_tarball_send_mail( *accept_args( sys.argv[ 1: ] ) )
  154. if __name__ != '__main__': import utils
  155. else:
  156. # in absense of relative import...
  157. xsl_path = os.path.abspath( os.path.dirname( sys.argv[ 0 ] ) )
  158. while os.path.basename( xsl_path ) != 'xsl_reports': xsl_path = os.path.dirname( xsl_path )
  159. sys.path.append( xsl_path )
  160. import utils
  161. main()
粤ICP备19079148号