Update send_xml_to_diamonds.py and update invoking code to match new i/f
authorDario Sestito <darios@symbian.org>
Wed, 18 Nov 2009 19:14:23 +0000
changeset 778 33511d5b1edb
parent 777 77f4b6d2d3e0
child 779 625b08d1da2b
Update send_xml_to_diamonds.py and update invoking code to match new i/f
common/build.postbuild.xml
common/tools/brag/send_xml_to_diamonds.py
--- a/common/build.postbuild.xml	Wed Nov 18 11:52:39 2009 +0000
+++ b/common/build.postbuild.xml	Wed Nov 18 19:14:23 2009 +0000
@@ -508,12 +508,8 @@
       <!-- Send the data to the server -->
 	  <exec executable="python">
 		    <arg value="${sf.common.config.dir}/tools/brag/send_xml_to_diamonds.py"/>
-		    <arg value="-s"/>
-		    <arg value="${diamonds.host}"/>
-		    <arg value="-u"/>
-		    <arg value="${diamonds.build.id}"/>
-		    <arg value="-f"/>
-		    <arg value="${build.log.dir}/linksForDiamonds.xml"/>
+		    <arg value="-u http://${diamonds.host}${diamonds.build.id}"/>
+		    <arg value="-f ${build.log.dir}/linksForDiamonds.xml"/>
 	  </exec>
     </target>
 
@@ -556,12 +552,8 @@
                         <!-- Send the data to the server -->
                         <exec executable="python">
                             <arg value="${sf.common.config.dir}/tools/brag/send_xml_to_diamonds.py"/>
-                            <arg value="-s"/>
-                            <arg value="${diamonds.host}"/>
-                            <arg value="-u"/>
-                            <arg value="${diamonds.build.id}"/>
-                            <arg value="-f"/>
-                            <arg value="${temp.build.dir}/bragForDiamonds.xml"/>
+                            <arg value="-u http://${diamonds.host}${diamonds.build.id}"/>
+                            <arg value="-f ${temp.build.dir}/bragForDiamonds.xml"/>
                         </exec>
                     </then>
                     <else>
--- a/common/tools/brag/send_xml_to_diamonds.py	Wed Nov 18 11:52:39 2009 +0000
+++ b/common/tools/brag/send_xml_to_diamonds.py	Wed Nov 18 19:14:23 2009 +0000
@@ -1,38 +1,40 @@
 command_help = """
-Send XML data from file to Diamonds. v.1.23
+Send XML data from file to Diamonds. v.2.0
 Use:
     send_xml_to_diamonds.py options
     
     Mandatory options:
-    -s    Server address
     -u    Url
     -f    path of XML file
     
     Optional options:
     -m    Send only mail, without POST connection. Recommend only,
           when direct POST connection is not available.
-    -o    mail server. Not needed inside Nokia intranet.
-    -h    help
+    -o    Mail server, not needed inside Nokia intranet.
+    -h    Help, prints this help text.
     
     Examples:
     Sending only by mail, without POST. (not recommended)
-        send_xml_to_diamonds.py -s diamonds.nmp.nokia.com -u /diamonds/builds/ -f c:\\build.xml -m buildtoolsautomation@nokia.com
+        send_xml_to_diamonds.py -u http://diamonds.nmp.nokia.com/diamonds/builds/ -f c:\\build.xml -m buildtoolsautomation@nokia.com
     
     Sending a new build to release instance of Diamonds
-        send_xml_to_diamonds.py -s diamonds.nmp.nokia.com -u /diamonds/builds/ -f c:\\build.xml
+        send_xml_to_diamonds.py -u http://diamonds.nmp.nokia.com/diamonds/builds/ -f c:\\build.xml
     
     Updating test results to existing build
-        send_xml_to_diamonds.py -s diamonds.nmp.nokia.com -u /diamonds/builds/123/ -f c:\\test.xml
+        send_xml_to_diamonds.py -u http://diamonds.nmp.nokia.com/diamonds/builds/123/ -f c:\\test.xml
     
     Sending data for Relative Change in SW Asset metrics
-        send_xml_to_diamonds.py -s diamonds.nmp.nokia.com -u /diamonds/metrics/ -f c:\\relative.xml
+        send_xml_to_diamonds.py -u http://diamonds.nmp.nokia.com/diamonds/metrics/ -f c:\\relative.xml
     
-    Sending data for Function Coverage
-        send_xml_to_diamonds.py -s diamonds.nmp.nokia.com -u /diamonds/tests/coverage/ -f c:\\coverage.xml
+    Sending data for Function Coverage of Releases
+        send_xml_to_diamonds.py -u http://diamonds.nmp.nokia.com/diamonds/tests/coverage/ -f c:\\coverage.xml
     
     Note: If you want to send XML to development version of Diamonds in testing purposes, use
     address: trdeli02.nmp.nokia.com:9001 in the server address:
-        send_xml_to_diamonds.py -s trdeli02.nmp.nokia.com:9001 -u /diamonds/builds/ -f c:\\build.xml
+        send_xml_to_diamonds.py -u http://trdeli02.nmp.nokia.com:9001/diamonds/builds/ -f c:\\build.xml
+    
+    For local Diamonds development:
+        send_xml_to_diamonds.py -u http://127.0.0.1:8000/diamonds/builds/ -f c:\\build.xml
 """
 
 from httplib import *
@@ -78,27 +80,55 @@
     else:
         return "%s minutes and %s seconds" % (int(total_time/60), round((total_time%60), 1))
 
+def safe_pop():
+    try:
+        return sys.argv.pop(0)
+    except IndexError:
+        sys.exit("Syntax Error!\n%s" % command_help)
+
+def get_server_from_url(url):
+    # Remove 'http://' if any
+    try:
+        return url.replace("http://", "").split("/")[0]
+    except IndexError:
+        sys.exit("Invalid URL: %s" % url)
+
+def get_path_from_url(url):
+    try:
+        path = url.replace("http://", "").split("/")
+        # Discard server address
+        _ = path.pop(0)
+        path = "/" + "/".join(path)
+        # Append slash if needed
+        if path[-1] != "/":
+            path = path + "/"
+        return path
+    except IndexError:
+        sys.exit("Invalid URL: %s" % url)
+
 def main():
-    start_time          = time.time()
-    server_valid        = False
-    url_valid           = False
-    sfile_valid         = False
-    mail_address        = None
+    if len(sys.argv) < 2:
+        sys.exit(command_help)
+    start_time = time.time()
+    server_valid = False
+    path_valid = False
+    sfile_valid = False
+    mail_address = None
     mail_server_address = "smtp.nokia.com"
-    _                   = sys.argv.pop(0)
+    _ = sys.argv.pop(0)
     
     while sys.argv:
         parameter = sys.argv.pop(0)
         if re.search('^-', parameter):
-            if parameter == '-s':
-                server       = sys.argv.pop(0)
+            if parameter == '-u':
+                url = safe_pop()
+                server = get_server_from_url(url)
                 server_valid = True
-            elif parameter == '-u':
-                url          = sys.argv.pop(0)
-                url_valid    = True
+                path = get_path_from_url(url)
+                path_valid = True
             elif parameter == '-f':
-                source_file  = sys.argv.pop(0)
-                sfile_valid  = True
+                source_file = safe_pop()
+                sfile_valid = True
                 try:
                     xml = open(source_file).read()
                 except:
@@ -113,8 +143,8 @@
                 sys.exit("Incorrect parameter! %s" % (parameter) + command_help )
         else:
             sys.exit("Incorrect parameter! %s" % (parameter) + command_help)
-    if not server_valid or not url_valid or not sfile_valid:
-        sys.exit("Too few parameters: Use -h for help")
+    if not server_valid or not path_valid or not sfile_valid:
+        sys.exit("Too few parameters. \n%s" % command_help)
     
     diamonds_mail_box      = "diamonds@diamonds.nmp.nokia.com"
     import_failed_message  = "XML was not sent successfully to Diamonds via REST interface!\n"
@@ -125,11 +155,11 @@
         connection = HTTPConnection(server)
         
         try:
-            connection.request("POST", url, xml)
+            connection.request("POST", path, xml)
         except:
             print "Can not connect to the server %s\n" % server
             sender = get_username()
-            send_email(get_mail_subject(sender, server, url), xml, sender, [diamonds_mail_box], "latin-1", mail_server_address)
+            send_email(get_mail_subject(sender, server, path), xml, sender, [diamonds_mail_box], "latin-1", mail_server_address)
             sys.exit(mail_sent_message)
         
         response = connection.getresponse()
@@ -143,8 +173,9 @@
         else:
             print import_failed_message
             print get_response_message(response)
+            #print "Server response:%s\n" % response.read()
             sender = get_username()
-            send_email(get_mail_subject(sender, server, url), xml, sender, [diamonds_mail_box], "latin-1", mail_server_address)
+            send_email(get_mail_subject(sender, server, path), xml, sender, [diamonds_mail_box], "latin-1", mail_server_address)
             print mail_sent_message
         
         connection.close()
@@ -152,7 +183,7 @@
     else:
         print 'Sending only mail'
         sender = get_username()
-        send_email(get_mail_subject(sender, server, url), xml, sender, [mail_address], "latin-1", mail_server_address)
+        send_email(get_mail_subject(sender, server, path), xml, sender, [mail_address], "latin-1", mail_server_address)
     
     print "------------------------"
     print "Processed in %s" % get_process_time(time.time()-start_time)