aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xm-test/lib/XmTestReport/Report.py
blob: 92412b8770a2b3a5c73380c837e44e8fc622a0f9 (plain)
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
151
152
153
154
155
156
#!/usr/bin/python

"""
 Report.py - Handles the coordination of xm-test xml-reporting modules

 Copyright (C) International Business Machines Corp., 2005
 Author: Dan Smith <danms@us.ibm.com>

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; under version 2 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""

import OSReport
import ProgReport
import ResultReport
import utils

import sys
import os
import xml.dom.minidom
import httplib
import urllib
import re
from urlparse import urlparse

class XmTestReport:

    def __init__(self, files):
        self.files = files

    def __getContactInfo(self):
        if os.access("contact_info", os.R_OK):
            c = file("contact_info")
            line = c.readline()
            line = line.strip()
            return line
        else:
            return "nobody@nowhere.com"

    def __stringify(self, fileName):
        f = file(fileName)
        str = f.read()
        f.close()

        return str

    def __str__(self):
        string  = "<testname>xm-test</testname>\n"
        string += "<user>%s</user>\n" % self.__getContactInfo()

        for f in self.files:
            string += self.__stringify(f)

        return string

# Taken from example in Python Cookbook
def encodeForm(fieldList):
    body = []
    boundary = "-------XmTestReportingXML"

    for name in fieldList.keys():
        body.append('--' + boundary)
        body.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (name, "%s.txt" % name))
        body.append('Content-Type: text/plain')
        body.append('')
        body.append(fieldList[name])

    body.append('')
    body.append("--" + boundary + "--")
    body.append('')

    textBody = "\r\n".join(body)

    return 'multipart/form-data; boundary=%s' % boundary, textBody

def postResults(report_server, results):
    if not re.match('http://', report_server):
	report_server = 'http://'+report_server
    (report_host,report_url) = urlparse(report_server)[1:3]
    conn = httplib.HTTPConnection(report_host)

    type, body = encodeForm({"log" : results})

    headers = {"content-type" : type}

    # DEBUG OUTPUT
    # print "BODY\n%s\nBODY\n" % body
    # print "%s\n" % type
    # print headers
    
    conn.request("POST", report_url, body, headers)
    
    resp = conn.getresponse()
    data = resp.read()

    if resp.status == 200:
        print >>sys.stderr, "Your results have been submitted successfully!"
    else:
        print >>sys.stderr, "Unable to submit results:"
        print >>sys.stderr, "[http://%s%s] said %i: %s" % (report_host,
                                                           report_url,
                                                           resp.status,
                                                           resp.reason)
        print >>sys.stderr, data

if __name__ == "__main__":

    if len(sys.argv) <= 1:
        print "Usage: Report.py [opt] <outputfiles...>"
        print "Where:"
        print "-d    : don't submit, just dump XML"
        print "-D    : do submit and dump XML"
        sys.exit(1)

    submit = True
    dump = False
    files = []

    report_server = sys.argv[1]

    for a in sys.argv[2:]:
        if a == "-d":
            submit = False
            dump = True
        elif a == "-D":
            dump = True
            submit = True

        else:
            if not os.access(a, os.R_OK):
                print "Unable to access file: %s" % a
                sys.exit(1)
            else:
                files.append(a)

    report = XmTestReport(files)

    xmlout = "<xml>\n%s</xml>\n" % report

    if dump:
        print xmlout

    if submit:
        postResults(report_server, xmlout)