Added target to push a collated and calculated BRAG status to diamonds.
authorSimon Howkins <simonh@symbian.org>
Mon, 05 Oct 2009 18:14:18 +0100
changeset 592 85da6704f82d
parent 591 0dd169f56564
child 596 3bb02a90f7e7
Added target to push a collated and calculated BRAG status to diamonds.
common/build.postbuild.xml
common/tools/brag/send_xml_to_diamonds.py
--- a/common/build.postbuild.xml	Mon Oct 05 18:22:28 2009 +0100
+++ b/common/build.postbuild.xml	Mon Oct 05 18:14:18 2009 +0100
@@ -283,7 +283,8 @@
             <then>
             <runtarget target="sf-run-analysis-diamonds"/>
             </then>
-        </if>    
+        </if>
+	<runtarget target="sf-brag-to-diamonds"/>
     </target>
 
     <target name="sf-run-analysis-ant">    
@@ -488,6 +489,55 @@
       </fmpp>
     </target>    
 
+    <target name="sf-brag-to-diamonds">
+        <!-- Merge all our bits of XML together -->
+        <exec executable="perl" output="${build.log.dir}/summary/_BRAG.xml">
+            <arg value="${sf.common.config.dir}/tools/mergeXML.pl"/>
+            <arg value="--xsl=brag.xsl"/>
+            <arg value="--merge=buildStatus,phase(name),step(name),failures(level)"/>
+            <arg value="${build.log.dir}/summary/*?_BRAG.xml"/>
+        </exec>
+        <!-- Run XSL transform to create file to send to diamonds -->
+        <java jar="${sf.common.config.dir}/sysdefdowngrade/xalan-j_2_7_1/xalan.jar" fork="true">
+            <arg value="-IN"/>
+            <arg value="${build.log.dir}/summary/_BRAG.xml"/>
+            <arg value="-XSL"/>
+            <arg value="${sf.common.config.dir}/tools/brag/bragForDiamonds.xsl"/>
+            <arg value="-OUT"/>
+            <arg value="${temp.build.dir}/bragForDiamonds.xml"/>
+        </java>
+        <if>
+            <istrue value="${sf.spec.publish.enable}"/>
+            <then>
+                <!-- Try to obtain the diamonds ID for this build -->
+                <if>
+                    <isset property="diamonds.build.id"/>
+                    <else>
+                        <property file="${build.log.dir}/diamonds.build.id.properties"/> <!-- This will fail silently -->
+                    </else>
+                </if>
+                <if>
+                    <isset property="diamonds.build.id"/>
+                    <then>
+                        <!-- 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/builds/${diamonds.build.id}"/>
+                            <arg value="-f"/>
+                            <arg value="${temp.build.dir}/bragForDiamonds.xml"/>
+                        </exec>
+                    </then>
+                    <else>
+                        <echo message="diamonds.build.id is not known - unable to push any build specific information to diamonds server"/>
+                    </else>
+                </if>
+            </then>
+        </if>
+    </target>
+
     <target name="sf-check-utilities-dir">
         <available property="sf-utilities-available" file="${build.drive}/utilities" type="dir"/>
     </target>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/tools/brag/send_xml_to_diamonds.py	Mon Oct 05 18:14:18 2009 +0100
@@ -0,0 +1,162 @@
+command_help = """
+Send XML data from file to Diamonds. v.1.23
+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
+    
+    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
+    
+    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
+    
+    Updating test results to existing build
+        send_xml_to_diamonds.py -s diamonds.nmp.nokia.com -u /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
+    
+    Sending data for Function Coverage
+        send_xml_to_diamonds.py -s diamonds.nmp.nokia.com -u /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
+"""
+
+from httplib import *
+import os, sys, time, re
+
+
+def send_email(subject, body, sender, receivers, encoding, mail_server):
+    """
+    Create an email message as MIMEText instance.
+    """
+    from email.Header import Header
+    from email.MIMEText import MIMEText
+    from email.Utils import parseaddr, formataddr
+    import smtplib
+    
+    msg = MIMEText(body, "plain", encoding)
+    msg["To"] = Header(u", ".join(receivers), encoding)
+    msg["Subject"] = Header(subject, encoding)
+    
+    smtp = smtplib.SMTP() 
+    smtp.connect(mail_server)
+    smtp.sendmail(sender, receivers, msg.as_string())
+    smtp.close()
+
+def get_username():
+    platform = sys.platform
+    if platform == "win32":
+        return os.getenv("USERNAME")
+    else:
+        return os.getlogin()
+
+def get_mail_subject(sender, server, url):
+    return "[DIAMONDS_DATA] %s>>>%s>>>%s" % (sender, server, url)
+
+def get_response_message(response):
+    return "Response status:%s \
+    \nResponse reason:%s\n" \
+           % (response.status, response.reason)
+
+def get_process_time(total_time):
+    if total_time<=60:
+        return  "%s seconds" % round(total_time, 1)
+    else:
+        return "%s minutes and %s seconds" % (int(total_time/60), round((total_time%60), 1))
+
+def main():
+    start_time          = time.time()
+    server_valid        = False
+    url_valid           = False
+    sfile_valid         = False
+    mail_address        = None
+    mail_server_address = "smtp.nokia.com"
+    _                   = sys.argv.pop(0)
+    
+    while sys.argv:
+        parameter = sys.argv.pop(0)
+        if re.search('^-', parameter):
+            if parameter == '-s':
+                server       = sys.argv.pop(0)
+                server_valid = True
+            elif parameter == '-u':
+                url          = sys.argv.pop(0)
+                url_valid    = True
+            elif parameter == '-f':
+                source_file  = sys.argv.pop(0)
+                sfile_valid  = True
+                try:
+                    xml = open(source_file).read()
+                except:
+                    sys.exit("Can not open the file %s" % source_file)
+            elif parameter == '-m':
+                mail_address = sys.argv.pop(0)
+            elif parameter == '-o':
+                mail_server_address = sys.argv.pop(0)
+            elif parameter == '-h':
+                sys.exit("HELP:\n %s" % (command_help))
+            else:
+                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")
+    
+    diamonds_mail_box      = "diamonds@diamonds.nmp.nokia.com"
+    import_failed_message  = "XML was not sent successfully to Diamonds via REST interface!\n"
+    import_succeed_message = "XML was sent successfully to Diamonds via REST interface.\n"
+    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"
+    
+    if not mail_address:
+        connection = HTTPConnection(server)
+        
+        try:
+            connection.request("POST", url, 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)
+            sys.exit(mail_sent_message)
+        
+        response = connection.getresponse()
+        
+        # More info about httplib
+        # http://docs.python.org/lib/module-httplib.html
+        if response.status == 200:
+            print import_succeed_message
+            print get_response_message(response)
+            print "Server response:%s\n" % response.read()
+        else:
+            print import_failed_message
+            print get_response_message(response)
+            sender = get_username()
+            send_email(get_mail_subject(sender, server, url), xml, sender, [diamonds_mail_box], "latin-1", mail_server_address)
+            print mail_sent_message
+        
+        connection.close()
+           
+    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)
+    
+    print "------------------------"
+    print "Processed in %s" % get_process_time(time.time()-start_time)
+    print "------------------------"
+
+if __name__ == "__main__":
+    main()