buildframework/helium/tools/relnotes/readHTML.py
changeset 217 0f5e3a7fb6af
parent 181 59bb7c4d6172
child 307 22ecbfc20eb4
child 584 56dd7656a965
child 587 85df38eb4012
equal deleted inserted replaced
181:59bb7c4d6172 217:0f5e3a7fb6af
     1 #============================================================================ 
       
     2 #Name        : readHTML.py 
       
     3 #Part of     : Helium 
       
     4 
       
     5 #Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     6 #All rights reserved.
       
     7 #This component and the accompanying materials are made available
       
     8 #under the terms of the License "Eclipse Public License v1.0"
       
     9 #which accompanies this distribution, and is available
       
    10 #at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    11 #
       
    12 #Initial Contributors:
       
    13 #Nokia Corporation - initial contribution.
       
    14 #
       
    15 #Contributors:
       
    16 #
       
    17 #Description:
       
    18 #===============================================================================
       
    19 
       
    20 # Name: readHTML.py
       
    21 # Synopsis: This script creates a CSV file from the Log File Summary (.html)
       
    22 
       
    23 import htmllib
       
    24 import sys
       
    25 import formatter
       
    26 import re
       
    27 
       
    28 class HTMLComponent(object):
       
    29     """
       
    30     Represents a component in the log file summary
       
    31     """
       
    32     def __init__(self):
       
    33         self._name = ''
       
    34         self._errorCount = 0
       
    35         self._warningCount = 0
       
    36         
       
    37     def getCSV(self):
       
    38         return self._name + ',' + str(self._errorCount) + ',' + str(self._warningCount)
       
    39         
       
    40     def __setName(self, n):
       
    41         self._name = n
       
    42     def __setErrorCount(self, n):
       
    43         self._errorCount = n
       
    44     def __setWarningCount(self, n):
       
    45         self._warningCount = n
       
    46         
       
    47     name = property(None, __setName)    
       
    48     errorCount = property(None, __setErrorCount)
       
    49     warningCount = property(None, __setWarningCount)
       
    50 
       
    51 class LogHTMLParser(htmllib.HTMLParser):
       
    52     """
       
    53     Parse the scan2log HTML file into CSV
       
    54     """
       
    55     def __init__(self, verbose=0):
       
    56         self.anchors = {}
       
    57         f = formatter.NullFormatter()
       
    58         htmllib.HTMLParser.__init__(self, f, verbose)
       
    59         
       
    60         self.state = -1 #represents column, 0 is first
       
    61         self.printFlag = False
       
    62         self.errorCount = 0
       
    63         self.warningCount = 0
       
    64         
       
    65         self._components = []
       
    66         self.component = None
       
    67         
       
    68     def __getComponents(self):
       
    69         return self._components
       
    70         
       
    71     components = property(__getComponents)
       
    72         
       
    73     def handle_data(self, text):
       
    74         text = text.strip()
       
    75         
       
    76         #ignore plain text links that appear eg. [9]
       
    77         p = re.compile('\[[0-9]*\]')
       
    78         if not text or p.match(text):
       
    79             return
       
    80       
       
    81         #start of area to parse
       
    82         if (text == 'Component'):
       
    83             self.state = 0
       
    84   
       
    85         #end of area to parse
       
    86         if (text == 'By Command'):
       
    87             self.state = -1
       
    88           
       
    89         #reset column if we get lost
       
    90         #if (self.state > 0 and not text.isdigit()):
       
    91         #    self.state = 0
       
    92         
       
    93         if (self.state == 0):
       
    94             self.component = HTMLComponent()
       
    95             self.component.name = text
       
    96         if (text.isdigit() and self.state == 2):
       
    97             self.errorCount += int(text)
       
    98             self.component.errorCount = text
       
    99         if (text.isdigit() and self.state == 3):
       
   100             self.warningCount += int(text)
       
   101             self.component.warningCount = text
       
   102             
       
   103         #if there are more than/ equal 5 errors or 50 warnings we print this row
       
   104         if (text.isdigit() and ((self.state == 2 and (int(text) >= 5)) or 
       
   105           (self.state == 3 and (int(text) >= 50)))):
       
   106             self.printFlag = True
       
   107 
       
   108         if (self.state == 5):
       
   109             if (self.printFlag):                
       
   110                 self.components.append(self.component)
       
   111                 
       
   112             self.printFlag = False
       
   113           
       
   114         if (self.state >= 0):
       
   115             self.state += 1
       
   116             self.state %= 6
       
   117         
       
   118 def main():    
       
   119     if len(sys.argv) != 3:
       
   120         print "Usage: readHTML.pl LogFile.html errors.csv"
       
   121         sys.exit(1)
       
   122     
       
   123     parser = LogHTMLParser()
       
   124     
       
   125     inputFile = file( sys.argv[1], 'rb' )
       
   126     outFile = file( sys.argv[2], 'w' )
       
   127     
       
   128     outFile.write("Component,Errors (more than 5),Warnings (more than 50)\n")
       
   129 
       
   130     parser.feed(inputFile.read())
       
   131 
       
   132     for c in parser.components:
       
   133         outFile.write(c.getCSV() + "\n")
       
   134     
       
   135     outFile.write("Total," + str(parser.errorCount) + "," + str(parser.warningCount) + "\n")
       
   136     
       
   137     inputFile.close()
       
   138     outFile.close()
       
   139     parser.close()
       
   140         
       
   141 if __name__ == '__main__' :
       
   142     main()