CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/Utilities/ReleaseScripts/python/cmsCodeRules/showPage.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 __author__="Aurelija"
00004 __date__ ="$2010-08-12 10.50.40$"
00005 
00006 from optparse import OptionParser
00007 from shutil import rmtree
00008 from os.path import join
00009 import os.path
00010 
00011 from Utilities.ReleaseScripts.cmsCodeRules.Formatter import SimpleHTMLFormatter
00012 from Utilities.ReleaseScripts.cmsCodeRules.pickleFileParser import readPicFiles
00013 from Utilities.ReleaseScripts.cmsCodeRules.config import rulesNames, ordering, Configuration, htmlPath, checkPath
00014 
00015 # helper function:
00016 
00017 def getIB(pathIn):
00018 
00019     ib = None
00020     wkDay = None
00021     dirs = os.path.abspath(pathIn).split('/')
00022     for item in dirs:
00023         if item.startswith('CMSSW_'):
00024             ib = item
00025             break
00026 
00027     if ib:
00028         import datetime
00029         fooYr, m, d, hr = ib.split('-')
00030         wkDay = datetime.date( int(fooYr[-4:]), int(m), int(d) ).strftime("%a")
00031 
00032     return ib, wkDay
00033 
00034 class BuildViewer(object):
00035 
00036     def __init__(self, formatter, pickleDir, logsDir, htmlDir):
00037 
00038         self.formatter = formatter
00039 
00040         self.configuration = Configuration
00041 
00042         self.logsDir = logsDir
00043         self.pickleDir = pickleDir
00044         self.htmlDir = htmlDir
00045         return
00046 
00047     # --------------------------------------------------------------------------------
00048 
00049     def showResults(self):
00050 
00051         ib, wkDay = getIB(checkPath)
00052 
00053         rulesResults = readPicFiles(self.pickleDir, True)
00054         createLogFiles(rulesResults, self.logsDir, wkDay)
00055 
00056         self.formatter.writeAnchor(ref='top')
00057         self.formatter.writeH2("CMSSW code rules violation for "+ib)
00058             
00059         self.formatter.startTable([20,20,20,20,50], 
00060 ['Rule','Packages', 'Files','Sum of violations','Description'], id =
00061 'descriptionTable', tableAttr='border="0" cellspacing="5" cellpadding="5"')
00062 
00063         for ruleName in rulesNames:
00064             try:
00065                 ruleRes = rulesResults[ruleName]
00066                 totalViolations = 0
00067                 totalFiles = 0
00068                 for package, packageResult in ruleRes:
00069                     totalFiles += len(packageResult)
00070                     for file, lines in packageResult:
00071                         totalViolations += len(lines)
00072                 self.formatter.writeRow([ruleName,str(len(ruleRes)), 
00073 str(totalFiles), str(totalViolations), 
00074 self.configuration[ruleName]['description']])
00075             except KeyError:
00076                 self.formatter.writeRow([ruleName,'-', '-', '-',
00077 self.configuration[ruleName]['description']])
00078         self.formatter.endTable()
00079 
00080         msg = """
00081 <p>
00082 Click on the package links to get list of files
00083 </p>
00084 
00085 """
00086         self.formatter.write(msg)
00087 
00088         colFmt = [   50    ]
00089         colLab = ['Package']
00090 
00091         rules = ordering
00092         for rule in rules:
00093             colFmt.append(20)
00094             colLab.append('Rule %s' %rule)
00095 
00096         self.formatter.startTable(colFmt, colLab, id = 'mainTable', cls='display', tableAttr='border="0" cellspacing="5" cellpadding="5"')
00097 
00098         packages = []
00099         table = []
00100         tableRow = len(colLab)*tuple('')
00101         ruleNr = 0
00102         for ruleName in rules:
00103             try:
00104                 ruleResult = rulesResults[ruleName]
00105                 for package, packageResult in ruleResult:
00106                     try:
00107                         index = packages.index(package)
00108                         tableRow = table[index] +(str(len(packageResult)),)
00109                         table[index] = tableRow
00110                     except ValueError:
00111                         packages.append(package) 
00112                         tableRow = ('<a href="logs/'+package+'/log.html"/>'+package,) + tuple('-' for i in range(ruleNr)) + (str(len(packageResult)),) #
00113                         table.append(tableRow)
00114                 addDash(table, ruleNr)
00115             except KeyError:
00116                 addDash(table, ruleNr)
00117             ruleNr += 1
00118 
00119         for row in table:
00120             self.formatter.writeRow(row)
00121 
00122         self.formatter.endTable()
00123 
00124         return
00125 
00126 def addDash(table, ruleNr):
00127     for index, tableRow in enumerate(table):
00128         if len(tableRow)-1 != ruleNr + 1:
00129             table[index] = table[index] + ('-',)
00130 
00131 def numberConverter(number):
00132     number = str(number)
00133     length = len(number)
00134     if  length < 3:
00135        number = (3-length)*str(0) + number
00136     return number
00137 
00138 def createLogFiles(rulesResult, logsDir, wkDay):
00139     logDir = join(logsDir,"logs")
00140     if os.path.exists(logDir):
00141         rmtree(logDir)
00142     for ruleName in rulesNames:
00143         try:
00144             ruleResult = rulesResult[ruleName]
00145             for package, packageResult in ruleResult:
00146                 logsDir = join(logDir, package)
00147                 if not os.path.exists(logsDir): os.makedirs(logsDir, 0755)
00148                 file = open(join(logsDir, "log.html"), 'a')
00149                 file.write('Rule %s'%ruleName)
00150                 file.write("<br/>")
00151                 for path, lineNumbers in packageResult:
00152                     for line in lineNumbers:
00153                         directory = join(package, path)
00154                         file.write('<a href="https://cmssdt.cern.ch/SDT/lxr/source/%s?v=%s#%s">%s:%s</a>\n'%(directory, wkDay, numberConverter(line), directory, line))
00155                         file.write("<br/>")
00156                 file.write('\n')
00157                 file.close()
00158         except KeyError:
00159             pass
00160 
00161 def run(pickleDir, logsDir, htmlDir):
00162     aoSorting = ""
00163     for i in range(len(ordering)):
00164         aoSorting += "["+ str(i+1) +",'desc'],"
00165     aoSorting += "[0, 'asc']"
00166 
00167     style = """
00168 
00169     <style type="text/css" title="currentStyle">
00170        @import "/SDT/html/jsExt/dataTables/media/css/demo_table.css";
00171     </style>
00172 
00173     <script type="text/javascript" src="/SDT/html/jsExt/dataTables/media/js/jquery.js"></script>
00174     <script type="text/javascript" src="/SDT/html/jsExt/dataTables/media/js/jquery.dataTables.js"></script>
00175                         
00176     <script type="text/javascript" charset="utf-8">
00177         /* Initialise the table with the required column sorting data types */
00178         $(document).ready(function() {
00179                 $('#mainTable').dataTable( {
00180                         "oLanguage": {
00181                                 "sLengthMenu": "Display _MENU_ records per page",
00182                                 "sInfoEmpty": "Showing 0 to 0 of 0 records"
00183                                 },
00184                         "aaSorting": [%s]
00185                         } );
00186                 $('#descriptionTable').dataTable( {
00187                         "aaSorting": [[0, 'asc']],
00188                         "bPaginate": false,
00189                         "bLengthChange": false,
00190                         "bFilter": false,
00191                         "bSort": false,
00192                         "bInfo": false,
00193                         "bAutoWidth": false
00194                         });
00195                 $('#descriptionTable thead th').css({'border-bottom': '1px solid black'});
00196         } );
00197     </script>
00198     """%(aoSorting)
00199 
00200     fmtr = SimpleHTMLFormatter(title="CMSSW integration builds", style=style, outFile = open(join(htmlDir,"cmsCRPage.html"), "w"))
00201 
00202     bv = BuildViewer(fmtr, pickleDir, logsDir, htmlDir)
00203     bv.showResults()
00204 
00205 def main():
00206 
00207     parser = OptionParser()
00208     parser.add_option("-l", "-L", dest="logDir", help = "creates log files to DIRECTORY", metavar = "DIRECTORY", default = os.getcwd())
00209     parser.add_option("-p", "-P", dest="pickleDir", help = "reads pickle files from DIRECTORY", metavar = "DIRECTORY", default = os.getcwd())
00210     parser.add_option("-c", "-C", dest="htmlDir", help = "creates cmsCRPage.html file to DIRECTORY", metavar = "DIRECTORY", default = os.getcwd())
00211     (options, args) = parser.parse_args()
00212 
00213     logsDir = options.logDir
00214     pickleDir = options.pickleDir
00215     htmlDir = options.htmlDir
00216 
00217     if not os.path.exists(logsDir):
00218         print "Error: wrong directory %s"%logsDir
00219         return
00220 
00221     if not os.path.exists(pickleDir):
00222         print "Error: wrong directory %s"%pickleDir
00223         return
00224 
00225     if not os.path.exists(htmlDir):
00226         print "Error: wrong directory %s"%htmlDir
00227         return
00228 
00229     run(pickleDir, logsDir, htmlDir)
00230 
00231     return
00232 
00233 if __name__ == "__main__":
00234     main()