sbsv2/raptor/bin/sbs_index.py
changeset 625 a1925fb7753a
child 641 8dd670a9f34f
equal deleted inserted replaced
624:f70b728ea30c 625:a1925fb7753a
       
     1 #!/usr/bin/python
       
     2 
       
     3 # Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 # All rights reserved.
       
     5 # This component and the accompanying materials are made available
       
     6 # under the terms of the License "Symbian Foundation License v1.0"
       
     7 # which accompanies this distribution, and is available
       
     8 # at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     9 #
       
    10 # Initial Contributors:
       
    11 # Nokia Corporation - initial contribution.
       
    12 #
       
    13 # Contributors:
       
    14 #
       
    15 # Description:
       
    16 #
       
    17 # Tie together a set of HTML build summaries by creating a single index page
       
    18 # which shows the total number of Errors, Warnings etc. across all the parts
       
    19 # of the build and links to the individual summaries.
       
    20 
       
    21 import os
       
    22 import sys
       
    23 
       
    24 # get the absolute path to this script
       
    25 script = os.path.abspath(sys.argv[0])
       
    26 bindir = os.path.dirname(script)
       
    27 # add the Raptor python and plugins directories to the PYTHONPATH
       
    28 sys.path.append(os.path.join(bindir, "..", "python"))
       
    29 sys.path.append(os.path.join(bindir, "..", "python", "plugins"))
       
    30 
       
    31 if len(sys.argv) < 3:
       
    32 	sys.stderr.write("""usage: %s input_dir1 [input_dir2...] output_index_file
       
    33 	
       
    34 The input directories are scanned recursively for totals.txt files and all
       
    35 those found are added to the generated index.
       
    36 """ % os.path.basename(script))
       
    37 	sys.exit(1)
       
    38 
       
    39 roots = []
       
    40 for a in sys.argv[1:-1]:
       
    41 	if os.path.isdir(a):
       
    42 		roots.append(a)
       
    43 	else:
       
    44 		sys.stderr.write("warning: %s is not a directory\n" % a)
       
    45 
       
    46 indexfile = sys.argv[-1]
       
    47 indexdir = os.path.dirname(indexfile)
       
    48 	
       
    49 def findtotals(dirs, files):
       
    50 	"recurse directories until we find a totals.txt file."
       
    51 	sub = []
       
    52 	for d in dirs:
       
    53 		name = os.path.join(d, "totals.txt")
       
    54 		if os.path.isfile(name):
       
    55 			files.append(name)
       
    56 		else:
       
    57 			for s in os.listdir(d):
       
    58 				dir = os.path.join(d,s)
       
    59 				if os.path.isdir(dir):
       
    60 					sub.append(dir)
       
    61 	if sub:
       
    62 		findtotals(sub, files)
       
    63 
       
    64 totals = []
       
    65 findtotals(roots, totals)
       
    66 totals.sort()
       
    67 
       
    68 # look for a style file we can link to
       
    69 css = "style.css"
       
    70 for t in totals:
       
    71 	c = os.path.join(os.path.dirname(t),"style.css")
       
    72 	if os.path.isfile(c):
       
    73 		css = os.path.relpath(c, indexdir)
       
    74 		break
       
    75 	
       
    76 # write the header of the index
       
    77 import filter_html
       
    78 try:
       
    79 	index = open(indexfile, "w")
       
    80 	index.write("""<html>
       
    81 <head>
       
    82 <title>Raptor Build Index</title>
       
    83 <link type="text/css" rel="stylesheet" href="%s">
       
    84 </head>
       
    85 <body>
       
    86 <h1>Raptor Build Index</h1>
       
    87 <table>
       
    88 <tr><th>build</th>""" % css)
       
    89 
       
    90 	for i in filter_html.Records.TITLES:
       
    91 		index.write('<th class="numbers">%s</th>' % i)
       
    92 	index.write("</tr>")
       
    93 except:
       
    94 	sys.stderr.write("error: cannot write index file %s\n" % indexfile)
       
    95 	sys.exit(1)
       
    96 	
       
    97 import csv
       
    98 grandtotal = [0 for i in filter_html.Records.TITLES]
       
    99 
       
   100 for t in totals:
       
   101 	columns = []
       
   102 	try:
       
   103 		reader = csv.reader(open(t, "rb"))
       
   104 		for row in reader:
       
   105 			type = int(row[0])
       
   106 			style = row[1]
       
   107 			count = int(row[2])
       
   108 			if count == 0 or filter_html.Records.SUBDIRS[type] == style:
       
   109 				grandtotal[type] += count
       
   110 				columns.append((style,count))
       
   111 			else:
       
   112 				sys.stderr.write("warning: %s appears to be corrupt or out of date\n" % t)	
       
   113 	except:
       
   114 		sys.stderr.write("warning: %s could not be read\n" % t)
       
   115 
       
   116 	if len(columns) == len(filter_html.Records.TITLES):
       
   117 		try:
       
   118 			linktext = os.path.dirname(t)
       
   119 			linkname = os.path.relpath(os.path.join(linktext, "index.html"), indexdir)
       
   120 			index.write('<tr><td class="name"><a href="%s">%s</a></td>' % (linkname, linktext))
       
   121 			for (style, count) in columns:
       
   122 				index.write('<td class="%s">%d</td>' % (style, count))
       
   123 			index.write("</tr>")
       
   124 		except:
       
   125 			sys.stderr.write("error: cannot write index file %s\n" % indexfile)
       
   126 			sys.exit(1)
       
   127 	
       
   128 # finish off
       
   129 try:
       
   130 	index.write('<tr><td>&nbsp;</td></tr><tr><td class="name">total</td>')
       
   131 	for i, count in enumerate(grandtotal):
       
   132 		if count == 0:
       
   133 			index.write('<td class="zero">0</td>')
       
   134 		else:
       
   135 			index.write('<td class="%s">%d</td>' % (filter_html.Records.SUBDIRS[i], count))
       
   136 	index.write("</tr></table>")
       
   137 	index.write("</body></html>\n")
       
   138 	index.close()
       
   139 
       
   140 except:
       
   141 	sys.stderr.write("error: cannot close index file %s\n" % indexfile)
       
   142 	sys.exit(1)
       
   143 			
       
   144 sys.exit(0)