1 # Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 # All rights reserved. |
|
3 # This component and the accompanying materials are made available |
|
4 # under the terms of "Eclipse Public License v1.0" |
|
5 # which accompanies this distribution, and is available |
|
6 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 # |
|
8 # Initial Contributors: |
|
9 # Nokia Corporation - initial contribution. |
|
10 # |
|
11 # Contributors: |
|
12 # |
|
13 # Description: |
|
14 # Script to send xml data to diamonds |
|
15 # |
|
16 # |
|
17 command_help = """ |
|
18 Send XML data from file to Diamonds. v.1.23 |
|
19 Use: |
|
20 send_xml_to_diamonds.py options |
|
21 |
|
22 Mandatory options: |
|
23 -s Server address |
|
24 -u Url |
|
25 -f path of XML file |
|
26 |
|
27 Optional options: |
|
28 -m Send only mail, without POST connection. Recommend only, |
|
29 when direct POST connection is not available. |
|
30 -o mail server. Not needed inside Nokia intranet. |
|
31 -h help |
|
32 |
|
33 Examples: |
|
34 Sending only by mail, without POST. (not recommended) |
|
35 send_xml_to_diamonds.py -s diamonds.nmp.nokia.com -u /diamonds/builds/ -f c:\\build.xml -m buildtoolsautomation@nokia.com |
|
36 |
|
37 Sending a new build to release instance of Diamonds |
|
38 send_xml_to_diamonds.py -s diamonds.nmp.nokia.com -u /diamonds/builds/ -f c:\\build.xml |
|
39 |
|
40 Updating test results to existing build |
|
41 send_xml_to_diamonds.py -s diamonds.nmp.nokia.com -u /diamonds/builds/123/ -f c:\\test.xml |
|
42 |
|
43 Sending data for Relative Change in SW Asset metrics |
|
44 send_xml_to_diamonds.py -s diamonds.nmp.nokia.com -u /diamonds/metrics/ -f c:\\relative.xml |
|
45 |
|
46 Sending data for Function Coverage |
|
47 send_xml_to_diamonds.py -s diamonds.nmp.nokia.com -u /diamonds/tests/coverage/ -f c:\\coverage.xml |
|
48 |
|
49 Note: If you want to send XML to development version of Diamonds in testing purposes, use |
|
50 address: trdeli02.nmp.nokia.com:9001 in the server address: |
|
51 send_xml_to_diamonds.py -s trdeli02.nmp.nokia.com:9001 -u /diamonds/builds/ -f c:\\build.xml |
|
52 """ |
|
53 |
|
54 from httplib import * |
|
55 import os, sys, time, re |
|
56 |
|
57 |
|
58 def send_email(subject, body, sender, receivers, encoding, mail_server): |
|
59 """ |
|
60 Create an email message as MIMEText instance. |
|
61 """ |
|
62 from email.Header import Header |
|
63 from email.MIMEText import MIMEText |
|
64 from email.Utils import parseaddr, formataddr |
|
65 import smtplib |
|
66 |
|
67 msg = MIMEText(body, "plain", encoding) |
|
68 msg["To"] = Header(u", ".join(receivers), encoding) |
|
69 msg["Subject"] = Header(subject, encoding) |
|
70 |
|
71 smtp = smtplib.SMTP() |
|
72 smtp.connect(mail_server) |
|
73 smtp.sendmail(sender, receivers, msg.as_string()) |
|
74 smtp.close() |
|
75 |
|
76 def get_username(): |
|
77 platform = sys.platform |
|
78 if platform == "win32": |
|
79 return os.getenv("USERNAME") |
|
80 else: |
|
81 return os.getlogin() |
|
82 |
|
83 def get_mail_subject(sender, server, url): |
|
84 return "[DIAMONDS_DATA] %s>>>%s>>>%s" % (sender, server, url) |
|
85 |
|
86 def get_response_message(response): |
|
87 return "Response status:%s \ |
|
88 \nResponse reason:%s\n" \ |
|
89 % (response.status, response.reason) |
|
90 |
|
91 def get_process_time(total_time): |
|
92 if total_time<=60: |
|
93 return "%s seconds" % round(total_time, 1) |
|
94 else: |
|
95 return "%s minutes and %s seconds" % (int(total_time/60), round((total_time%60), 1)) |
|
96 |
|
97 def main(): |
|
98 start_time = time.time() |
|
99 server_valid = False |
|
100 url_valid = False |
|
101 sfile_valid = False |
|
102 mail_address = None |
|
103 mail_server_address = "smtp.nokia.com" |
|
104 _ = sys.argv.pop(0) |
|
105 |
|
106 while sys.argv: |
|
107 parameter = sys.argv.pop(0) |
|
108 if re.search('^-', parameter): |
|
109 if parameter == '-s': |
|
110 server = sys.argv.pop(0) |
|
111 server_valid = True |
|
112 elif parameter == '-u': |
|
113 url = sys.argv.pop(0) |
|
114 url_valid = True |
|
115 elif parameter == '-f': |
|
116 source_file = sys.argv.pop(0) |
|
117 sfile_valid = True |
|
118 try: |
|
119 xml = open(source_file).read() |
|
120 except: |
|
121 sys.exit("Can not open the file %s" % source_file) |
|
122 elif parameter == '-m': |
|
123 mail_address = sys.argv.pop(0) |
|
124 elif parameter == '-o': |
|
125 mail_server_address = sys.argv.pop(0) |
|
126 elif parameter == '-h': |
|
127 sys.exit("HELP:\n %s" % (command_help)) |
|
128 else: |
|
129 sys.exit("Incorrect parameter! %s" % (parameter) + command_help ) |
|
130 else: |
|
131 sys.exit("Incorrect parameter! %s" % (parameter) + command_help) |
|
132 if not server_valid or not url_valid or not sfile_valid: |
|
133 sys.exit("Too few parameters: Use -h for help") |
|
134 |
|
135 diamonds_mail_box = "diamonds@diamonds.nmp.nokia.com" |
|
136 import_failed_message = "XML was not sent successfully to Diamonds via REST interface!\n" |
|
137 import_succeed_message = "XML was sent successfully to Diamonds via REST interface.\n" |
|
138 mail_sent_message = "XML was sent to Diamonds by mail. Scheduled script will try to import it to Diamonds. If you can not see data soon in Diamonds, please contact to Diamonds developers.\n" |
|
139 |
|
140 if not mail_address: |
|
141 connection = HTTPConnection(server) |
|
142 |
|
143 try: |
|
144 connection.request("POST", url, xml) |
|
145 except: |
|
146 print "Can not connect to the server %s\n" % server |
|
147 sender = get_username() |
|
148 #send_email(get_mail_subject(sender, server, url), xml, sender, [diamonds_mail_box], "latin-1", mail_server_address) |
|
149 sys.exit(mail_sent_message) |
|
150 |
|
151 response = connection.getresponse() |
|
152 |
|
153 # More info about httplib |
|
154 # http://docs.python.org/lib/module-httplib.html |
|
155 if response.status == 200: |
|
156 print import_succeed_message |
|
157 print get_response_message(response) |
|
158 print "Server response:%s\n" % response.read() |
|
159 else: |
|
160 print import_failed_message |
|
161 print get_response_message(response) |
|
162 sender = get_username() |
|
163 #send_email(get_mail_subject(sender, server, url), xml, sender, [diamonds_mail_box], "latin-1", mail_server_address) |
|
164 print mail_sent_message |
|
165 |
|
166 connection.close() |
|
167 |
|
168 else: |
|
169 print 'Sending only mail' |
|
170 sender = get_username() |
|
171 #send_email(get_mail_subject(sender, server, url), xml, sender, [mail_address], "latin-1", mail_server_address) |
|
172 |
|
173 print "------------------------" |
|
174 print "Processed in %s" % get_process_time(time.time()-start_time) |
|
175 print "------------------------" |
|
176 |
|
177 if __name__ == "__main__": |
|
178 main() |
|