File: session_email_parser.py

package info (click to toggle)
babel 0.10.2-1
  • links: PTS
  • area: contrib
  • in suites: sarge
  • size: 43,932 kB
  • ctags: 29,707
  • sloc: java: 74,695; ansic: 73,142; cpp: 40,649; sh: 18,411; f90: 10,062; fortran: 6,727; python: 6,406; makefile: 3,866; xml: 118; perl: 48
file content (150 lines) | stat: -rw-r--r-- 6,464 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#
# Copyright(c) 2001-2003, The Regents of the University of California.
# Produced at the Lawrence Livermore National Laboratory.
# Written by Gary Kumfert <kumfert@llnl.gov>.
# UCRL-CODE-2002-043.
# All rights reserved.
#
# This file is part of Gantlet.  For details, see 
# http://www.llnl.gov/CASC/components/software.html or contact the author.
#
# Gantlet is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License (as published by 
# the Free Software Foundation) version 2.1 dated February 1999.
#
# Gantlet is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even IMPLIED WARRANTY OF MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the terms and conditions of
# the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this software; if not, write to the Free Software Foundation, 
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ADDITIONAL NOTICE:
# 
# A. This notice is required to be provided under our contract with the
#    U.S. Department of Energy (DOE).  This work was produced at the 
#    University of California, Lawrence Livermore National Laboratory
#    under Contract No. W-7405-ENG-48 with the DOE.
#
# B. Neither the United States Government nor the University of California
#    nor any of their employees make any warranty, express or implied, or
#    assumes any liability or responsibility for the accuracy, completeness, 
#    or usefulness of any information, apparatus, product, or process 
#    disclosed, or represents that its use would not infringe on 
#    privately-owned rights.
# 
# C. Also, reference herein to any specific commercial products, process, or
#    services by trade name, trademark, manufacturer or otherwise does not
#    necessarily constitute or imply its endoresement, recommendation, or
#    favoring by the United States Government or the University of California.
#    The views and opinions of authors expressed herein do not necessarily 
#    state or reflect those of the United States Government or the University
#    of California, and shall not be used for advertising or product 
#    endorsement purposes.

import re
import rfc822
import string
import StringIO
import xml.sax
import xml.sax.handler

class SAXShortCircuitException( xml.sax.SAXException ):
    """Exception used to keep from parsing the entire document(only need header)"""
    pass

class session_email_parser ( xml.sax.handler.ContentHandler ) :
    """A XML-encoded Gantlet report recieved via email"""

    def __init__( self, f, id=0, content='' ):
        """Initialize from an email message encoded as a single long string
        
        f - the entire email file as one big string, hopefully containing xml
        id  - a unique id number (email message id)
        """
        #        self.msg = msg
        if content:
            self.msg = content
        else:
            self.msg = string.join(f.readlines(),"")
            f = StringIO.StringIO(self.msg)
        self.id = id
        hdr = rfc822.Message(f)  # parse mail headers into dict
        if not hdr.has_key('Subject'):
            self.is_gantlet = None
            self.subject='(no subject)'
            return
        self.subject = hdr['Subject']
        if (self.subject[:13] != "<gantlet-xml " and
	   self.subject[:14] != "<gauntlet-xml "):  #backward compatable
            self.is_gantlet = None
            return
        self.fromwhom = hdr['From']
        self.is_gantlet = 1
        self.date = hdr['Date']
        self.date_tuple = hdr.getdate('Date')
        self.fp = hdr.fp
        self.attr = {}
        self.attr['package'] = ''
        self.attr['profile'] = ''
        self.attr['session'] = ''
        self.attr['elapsedtime'] = '0'
        self.attr['total_tests'] = '0'
        self.attr['passed_tests'] = '0'
        self.attr['xfailed_tests'] = '0'
        self.attr['failed_tests' ] = '0'
        self.attr['broken_tests' ] = '0'
        self.attr['total_parts' ] = '0'
        self.attr['passed_parts' ] = '0'
        self.attr['xfailed_parts' ] = '0'
        self.attr['failed_parts' ] = '0'
        self.PVT_parse_attr()
        self.PVT_clean_date()

    def startElement( self, name, attrs ):
        """Overrides xml.sax.handler.ContentHandler.startElement()"""
        if name == 'attribute':
            self.attr[ str( attrs['key'] )  ] = str( attrs['value'] )

    def endElement( self, name ):
        """Overrides xml.sax.handler.ContentHandler.startElement()"""
        if name == 'head':
            raise SAXShortCircuitException('no more attributes here')

    def PVT_parse_attr( self ):
        """PRIVATE: Load attributes at front of XML file using SAX"""
        if re.search('result="BROKEN"', self.subject):
            self.PVT_infer_broken_attrs()
        else:
            self.attr['result'] = re.search('result="([^"]*)"',
                                            self.subject ).group(1)
            tparser = xml.sax.make_parser()
            tparser.setContentHandler( self )
            try:
                tparser.parse( self.fp )
            except SAXShortCircuitException:
                # exception raised to stop before parsing whole document
                pass

    def PVT_clean_date(self):
        """PRIVATE: convert 'Thu, 18 Apr 2002' to '2002-04-18'"""
        date = str(self.attr['date']) + ' 00:00:00'
        s = rfc822.parsedate( date )
        if s:
            self.attr['date_text'] = self.attr['date']
            self.attr['date'] = '%04d-%02d-%02d' % s[0:3]
            
    def PVT_infer_broken_attrs( self ):
        """PRIVATE: Infer needed attributes from email header"""
        self.attr['package'] = re.search('package="([^"]*)"', self.subject ).group(1)
        self.attr['profile'] = re.search('profile="([^"]*)"', self.subject ).group(1)
        self.attr['session'] = re.search('session="([^"]*)"', self.subject ).group(1)
        self.attr['date_text'] = self.date
        self.attr['date'] = "%04d-%02d-%02d" % self.date_tuple[0:3]
        self.attr['time'] = "%d:%d:%d" % self.date_tuple[3:6]
        email = rfc822.parseaddr( self.fromwhom )[1]
        self.attr['whoami'] = re.search('^([^@]*)@', email ).group(1)
        self.attr['hostname'] = re.search('@(.*)', email ).group(1)        
        self.attr['result'] = 'BROKEN'