common/tools/brag/send_xml_to_diamonds.py
author Simon Howkins <simonh@symbian.org>
Wed, 27 Oct 2010 16:22:14 +0100
changeset 1316 0b4a09013baf
parent 778 33511d5b1edb
permissions -rw-r--r--
Added copyright messages
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     1
command_help = """
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
     2
Send XML data from file to Diamonds. v.2.0
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     3
Use:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     4
    send_xml_to_diamonds.py options
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     5
    
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     6
    Mandatory options:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     7
    -u    Url
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     8
    -f    path of XML file
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     9
    
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    10
    Optional options:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    11
    -m    Send only mail, without POST connection. Recommend only,
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    12
          when direct POST connection is not available.
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    13
    -o    Mail server, not needed inside Nokia intranet.
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    14
    -h    Help, prints this help text.
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    15
    
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    16
    Examples:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    17
    Sending only by mail, without POST. (not recommended)
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    18
        send_xml_to_diamonds.py -u http://diamonds.nmp.nokia.com/diamonds/builds/ -f c:\\build.xml -m buildtoolsautomation@nokia.com
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    19
    
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    20
    Sending a new build to release instance of Diamonds
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    21
        send_xml_to_diamonds.py -u http://diamonds.nmp.nokia.com/diamonds/builds/ -f c:\\build.xml
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    22
    
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    23
    Updating test results to existing build
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    24
        send_xml_to_diamonds.py -u http://diamonds.nmp.nokia.com/diamonds/builds/123/ -f c:\\test.xml
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    25
    
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    26
    Sending data for Relative Change in SW Asset metrics
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    27
        send_xml_to_diamonds.py -u http://diamonds.nmp.nokia.com/diamonds/metrics/ -f c:\\relative.xml
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    28
    
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    29
    Sending data for Function Coverage of Releases
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    30
        send_xml_to_diamonds.py -u http://diamonds.nmp.nokia.com/diamonds/tests/coverage/ -f c:\\coverage.xml
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    31
    
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    32
    Note: If you want to send XML to development version of Diamonds in testing purposes, use
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    33
    address: trdeli02.nmp.nokia.com:9001 in the server address:
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    34
        send_xml_to_diamonds.py -u http://trdeli02.nmp.nokia.com:9001/diamonds/builds/ -f c:\\build.xml
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    35
    
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    36
    For local Diamonds development:
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    37
        send_xml_to_diamonds.py -u http://127.0.0.1:8000/diamonds/builds/ -f c:\\build.xml
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    38
"""
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    39
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    40
from httplib import *
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    41
import os, sys, time, re
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    42
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    43
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    44
def send_email(subject, body, sender, receivers, encoding, mail_server):
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    45
    """
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    46
    Create an email message as MIMEText instance.
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    47
    """
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    48
    from email.Header import Header
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    49
    from email.MIMEText import MIMEText
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    50
    from email.Utils import parseaddr, formataddr
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    51
    import smtplib
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    52
    
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    53
    msg = MIMEText(body, "plain", encoding)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    54
    msg["To"] = Header(u", ".join(receivers), encoding)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    55
    msg["Subject"] = Header(subject, encoding)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    56
    
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    57
    smtp = smtplib.SMTP() 
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    58
    smtp.connect(mail_server)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    59
    smtp.sendmail(sender, receivers, msg.as_string())
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    60
    smtp.close()
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    61
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    62
def get_username():
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    63
    platform = sys.platform
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    64
    if platform == "win32":
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    65
        return os.getenv("USERNAME")
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    66
    else:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    67
        return os.getlogin()
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    68
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    69
def get_mail_subject(sender, server, url):
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    70
    return "[DIAMONDS_DATA] %s>>>%s>>>%s" % (sender, server, url)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    71
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    72
def get_response_message(response):
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    73
    return "Response status:%s \
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    74
    \nResponse reason:%s\n" \
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    75
           % (response.status, response.reason)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    76
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    77
def get_process_time(total_time):
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    78
    if total_time<=60:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    79
        return  "%s seconds" % round(total_time, 1)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    80
    else:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    81
        return "%s minutes and %s seconds" % (int(total_time/60), round((total_time%60), 1))
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    82
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    83
def safe_pop():
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    84
    try:
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    85
        return sys.argv.pop(0)
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    86
    except IndexError:
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    87
        sys.exit("Syntax Error!\n%s" % command_help)
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    88
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    89
def get_server_from_url(url):
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    90
    # Remove 'http://' if any
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    91
    try:
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    92
        return url.replace("http://", "").split("/")[0]
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    93
    except IndexError:
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    94
        sys.exit("Invalid URL: %s" % url)
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    95
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    96
def get_path_from_url(url):
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    97
    try:
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    98
        path = url.replace("http://", "").split("/")
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
    99
        # Discard server address
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   100
        _ = path.pop(0)
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   101
        path = "/" + "/".join(path)
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   102
        # Append slash if needed
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   103
        if path[-1] != "/":
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   104
            path = path + "/"
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   105
        return path
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   106
    except IndexError:
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   107
        sys.exit("Invalid URL: %s" % url)
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   108
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   109
def main():
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   110
    if len(sys.argv) < 2:
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   111
        sys.exit(command_help)
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   112
    start_time = time.time()
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   113
    server_valid = False
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   114
    path_valid = False
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   115
    sfile_valid = False
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   116
    mail_address = None
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   117
    mail_server_address = "smtp.nokia.com"
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   118
    _ = sys.argv.pop(0)
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   119
    
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   120
    while sys.argv:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   121
        parameter = sys.argv.pop(0)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   122
        if re.search('^-', parameter):
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   123
            if parameter == '-u':
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   124
                url = safe_pop()
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   125
                server = get_server_from_url(url)
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   126
                server_valid = True
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   127
                path = get_path_from_url(url)
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   128
                path_valid = True
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   129
            elif parameter == '-f':
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   130
                source_file = safe_pop()
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   131
                sfile_valid = True
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   132
                try:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   133
                    xml = open(source_file).read()
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   134
                except:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   135
                    sys.exit("Can not open the file %s" % source_file)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   136
            elif parameter == '-m':
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   137
                mail_address = sys.argv.pop(0)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   138
            elif parameter == '-o':
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   139
                mail_server_address = sys.argv.pop(0)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   140
            elif parameter == '-h':
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   141
                sys.exit("HELP:\n %s" % (command_help))
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   142
            else:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   143
                sys.exit("Incorrect parameter! %s" % (parameter) + command_help )
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   144
        else:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   145
            sys.exit("Incorrect parameter! %s" % (parameter) + command_help)
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   146
    if not server_valid or not path_valid or not sfile_valid:
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   147
        sys.exit("Too few parameters. \n%s" % command_help)
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   148
    
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   149
    diamonds_mail_box      = "diamonds@diamonds.nmp.nokia.com"
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   150
    import_failed_message  = "XML was not sent successfully to Diamonds via REST interface!\n"
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   151
    import_succeed_message = "XML was sent successfully to Diamonds via REST interface.\n"
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   152
    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"
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   153
    
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   154
    if not mail_address:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   155
        connection = HTTPConnection(server)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   156
        
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   157
        try:
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   158
            connection.request("POST", path, xml)
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   159
        except:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   160
            print "Can not connect to the server %s\n" % server
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   161
            sender = get_username()
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   162
            send_email(get_mail_subject(sender, server, path), xml, sender, [diamonds_mail_box], "latin-1", mail_server_address)
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   163
            sys.exit(mail_sent_message)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   164
        
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   165
        response = connection.getresponse()
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   166
        
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   167
        # More info about httplib
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   168
        # http://docs.python.org/lib/module-httplib.html
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   169
        if response.status == 200:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   170
            print import_succeed_message
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   171
            print get_response_message(response)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   172
            print "Server response:%s\n" % response.read()
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   173
        else:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   174
            print import_failed_message
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   175
            print get_response_message(response)
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   176
            #print "Server response:%s\n" % response.read()
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   177
            sender = get_username()
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   178
            send_email(get_mail_subject(sender, server, path), xml, sender, [diamonds_mail_box], "latin-1", mail_server_address)
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   179
            print mail_sent_message
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   180
        
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   181
        connection.close()
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   182
           
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   183
    else:
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   184
        print 'Sending only mail'
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   185
        sender = get_username()
778
33511d5b1edb Update send_xml_to_diamonds.py and update invoking code to match new i/f
Dario Sestito <darios@symbian.org>
parents: 592
diff changeset
   186
        send_email(get_mail_subject(sender, server, path), xml, sender, [mail_address], "latin-1", mail_server_address)
592
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   187
    
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   188
    print "------------------------"
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   189
    print "Processed in %s" % get_process_time(time.time()-start_time)
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   190
    print "------------------------"
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   191
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   192
if __name__ == "__main__":
85da6704f82d Added target to push a collated and calculated BRAG status to diamonds.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
   193
    main()