boostbook_report.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import ftplib
  2. import optparse
  3. import os
  4. import time
  5. import urlparse
  6. import utils
  7. import shutil
  8. import sys
  9. import zipfile
  10. import xml.sax.saxutils
  11. import utils.libxslt
  12. def get_date( words ):
  13. date = words[ 5: -1 ]
  14. t = time.localtime()
  15. month_names = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]
  16. year = time.localtime()[0] # If year is not secified is it the current year
  17. month = month_names.index( date[0] ) + 1
  18. day = int( date[1] )
  19. hours = 0
  20. minutes = 0
  21. if date[2].find( ":" ) != -1:
  22. ( hours, minutes ) = [ int(x) for x in date[2].split( ":" ) ]
  23. else:
  24. # there is no way to get seconds for not current year dates
  25. year = int( date[2] )
  26. return ( year, month, day, hours, minutes, 0, 0, 0, 0 )
  27. #def check_for_new_upload( target_dir, boostbook_info ):
  28. def accept_args( args ):
  29. parser = optparse.OptionParser()
  30. parser.add_option( '-t', '--tag', dest='tag', help="the tag for the results (i.e. 'RC_1_34_0')" )
  31. parser.add_option( '-d', '--destination', dest='destination', help='destination directory' )
  32. if len(args) == 0:
  33. parser.print_help()
  34. sys.exit( 1 )
  35. (options, args) = parser.parse_args()
  36. if not options.destination:
  37. print '-d is required'
  38. parser.print_help()
  39. sys.exit( 1 )
  40. return options
  41. def unzip( archive_path, result_dir ):
  42. utils.log( 'Unpacking %s into %s' % ( archive_path, result_dir ) )
  43. z = zipfile.ZipFile( archive_path, 'r', zipfile.ZIP_DEFLATED )
  44. for f in z.infolist():
  45. dir = os.path.join( result_dir, os.path.dirname( f.filename ) )
  46. if not os.path.exists( dir ):
  47. os.makedirs( dir )
  48. result = open( os.path.join( result_dir, f.filename ), 'wb' )
  49. result.write( z.read( f.filename ) )
  50. result.close()
  51. z.close()
  52. def boostbook_report( options ):
  53. site = 'fx.meta-comm.com'
  54. site_path = '/boost-regression/%s' % options.tag
  55. utils.log( 'Opening %s ...' % site )
  56. f = ftplib.FTP( site )
  57. f.login()
  58. utils.log( ' cd %s ...' % site_path )
  59. f.cwd( site_path )
  60. utils.log( ' dir' )
  61. lines = []
  62. f.dir( lambda x: lines.append( x ) )
  63. word_lines = [ x.split( None, 8 ) for x in lines ]
  64. boostbook_info = [ ( l[-1], get_date( l ) ) for l in word_lines if l[-1] == "BoostBook.zip" ]
  65. if len( boostbook_info ) > 0:
  66. boostbook_info = boostbook_info[0]
  67. utils.log( 'BoostBook found! (%s)' % ( boostbook_info, ) )
  68. local_copy = os.path.join( options.destination,'BoostBook-%s.zip' % options.tag )
  69. if 1:
  70. if os.path.exists( local_copy ):
  71. utils.log( 'Local copy exists. Checking if it is older than uploaded one...' )
  72. uploaded_mtime = time.mktime( boostbook_info[1] )
  73. local_mtime = os.path.getmtime( local_copy )
  74. utils.log( ' uploaded: %s %s, local: %s %s' %
  75. ( uploaded_mtime
  76. , boostbook_info[1]
  77. , local_mtime
  78. , time.localtime( local_mtime )) )
  79. modtime = time.localtime( os.path.getmtime( local_copy ) )
  80. if uploaded_mtime <= local_mtime:
  81. utils.log( 'Local copy is newer: exiting' )
  82. sys.exit()
  83. if 1:
  84. temp = os.path.join( options.destination,'BoostBook.zip' )
  85. result = open( temp, 'wb' )
  86. f.retrbinary( 'RETR %s' % boostbook_info[0], result.write )
  87. result.close()
  88. if os.path.exists( local_copy ):
  89. os.unlink( local_copy )
  90. os.rename( temp, local_copy )
  91. m = time.mktime( boostbook_info[1] )
  92. os.utime( local_copy, ( m, m ) )
  93. docs_name = os.path.splitext( os.path.basename( local_copy ) )[0]
  94. if 1:
  95. unpacked_docs_dir = os.path.join( options.destination, docs_name )
  96. utils.log( 'Dir %s ' % unpacked_docs_dir )
  97. if os.path.exists( unpacked_docs_dir ):
  98. utils.log( 'Cleaning up...' )
  99. shutil.rmtree( unpacked_docs_dir )
  100. os.makedirs( unpacked_docs_dir )
  101. unzip( local_copy, unpacked_docs_dir )
  102. utils.system( [ 'cd %s' % unpacked_docs_dir
  103. , 'tar -c -f ../%s.tar.gz -z --exclude=tarball *' % docs_name ] )
  104. process_boostbook_build_log( os.path.join( unpacked_docs_dir, 'boostbook.log' ), read_timestamp( unpacked_docs_dir ) )
  105. utils.libxslt( log
  106. , os.path.abspath( os.path.join( unpacked_docs_dir, 'boostbook.log.xml' ) )
  107. , os.path.abspath( os.path.join( os.path.dirname( __file__ ), 'xsl', 'v2', 'boostbook_log.xsl' ) )
  108. , os.path.abspath( os.path.join( unpacked_docs_dir, 'boostbook.log.html' ) ) )
  109. def log( msg ):
  110. print msg
  111. def process_boostbook_build_log( path, timestamp ):
  112. f = open( path + '.xml', 'w' )
  113. g = xml.sax.saxutils.XMLGenerator( f )
  114. lines = open( path ).read().splitlines()
  115. output_lines = []
  116. result = 'success'
  117. for line in lines:
  118. type = 'output'
  119. if line.startswith( '...failed' ):
  120. type = 'failure'
  121. result='failure'
  122. if line.startswith( 'runtime error:' ):
  123. type = 'failure'
  124. if line.startswith( '...skipped' ):
  125. type = 'skipped'
  126. output_lines.append( ( type, line ) )
  127. g.startDocument()
  128. g.startElement( 'build', { 'result': result, 'timestamp': timestamp } )
  129. for line in output_lines:
  130. g.startElement( 'line', { 'type': line[0]} )
  131. g.characters( line[1] )
  132. g.endElement( 'line' )
  133. g.endElement( 'build' )
  134. g.endDocument()
  135. def read_timestamp( docs_directory ):
  136. f = open( os.path.join( docs_directory, 'timestamp' ) )
  137. try:
  138. return f.readline()
  139. finally:
  140. f.close()
  141. def main():
  142. options = accept_args( sys.argv[1:])
  143. boostbook_report( options )
  144. if __name__ == '__main__':
  145. main()
粤ICP备19079148号