#!/usr/bin/env python
# vim: set expandtab sw=4 ts=4:
# Reads timestamps.csv, calculates stage duration and saves the result
# to an xml file
# usage :
#  analyse.py -h
#    --build=<buildID>
#    --job=<jobID>
#    --branch=<branchname>
#    --repo=<repo_slug>
#    --ci=<ci_platform> : fe. travis, jenkins, shippable, local, ...
#
# Copyright (C) 2014 Dieter Adriaenssens <ruleant@users.sourceforge.net>
#
# This file is part of buildtime-trend
# <https://github.com/ruleant/buildtime-trend/>
#
# 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, either version 3 of the License, or
# any later version.
#
# 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, see <http://www.gnu.org/licenses/>.

import os
import sys
import getopt
from lxml import etree
import keen
from buildtimetrend.build import Build
from buildtimetrend.keenio import keen_io_writable

# use parameter for timestamps file and check if file exists
TIMESTAMP_FILE = os.getenv('BUILD_TREND_LOGFILE', 'timestamps.csv')
RESULT_FILE = os.getenv('BUILD_TREND_OUTPUTFILE', 'buildtimes.xml')
if not os.path.isfile(TIMESTAMP_FILE):
    quit()


def analyse(argv):
    # read build data from timestamp CSV file
    build = Build(TIMESTAMP_FILE)

    # process arguments
    usage_string = 'analyse.py -h --build=<buildID>' \
        ' --job=<jobID> --branch=<branchname> --repo=<repo_slug>' \
        ' --ci=<ci_platform>'
    try:
        opts, args = getopt.getopt(
            argv, "h",
            ["build=", "job=", "branch=", "repo=", "ci=", "help"])
    except getopt.GetoptError:
        print usage_string
        sys.exit(2)
    for opt, arg in opts:
        if opt in ('-h', "--help"):
            print usage_string
            sys.exit()
        elif opt == "--build":
            build.add_property("build", arg)
        elif opt == "--job":
            build.add_property("job", arg)
        elif opt == "--branch":
            build.add_property("branch", arg)
        elif opt == "--repo":
            build.add_property("repo", arg)
        elif opt == "--ci":
            build.add_property("ci_platform", arg)

    # load previous buildtimes file, or create a new xml root
    if os.path.isfile(RESULT_FILE):
        try:
            root_xml = etree.parse(RESULT_FILE).getroot()
        except etree.XMLSyntaxError:
            print 'ERROR : XML format invalid : a new file is created,' \
                ' corrupt file is discarded'
            root_xml = etree.Element("builds")
    else:
        root_xml = etree.Element("builds")

    # add build data to xml
    root_xml.append(build.to_xml())

    # write xml to file
    with open(RESULT_FILE, 'wb') as xmlfile:
        xmlfile.write(etree.tostring(
            root_xml, xml_declaration=True,
            encoding='utf-8', pretty_print=True))

    # send build data to keen.io
    if keen_io_writable():
        print "Sending data to Keen.io"
        keen.add_events({"build_stages": build.stages_to_list()})

if __name__ == "__main__":
    analyse(sys.argv[1:])

@@ -324,16 +324,81 @@
 al, ...%0A
+#    --result=%3Cbuild_result%3E : fe. success, failed, errored, ...%0A
 #%0A# Copy
@@ -1856,16 +1856,40 @@
 latform%3E
+ --result=%3Cbuild_result%3E
 '%0A    tr
@@ -2007,16 +2007,27 @@
 , %22ci=%22,
+ %22result=%22,
  %22help%22%5D
@@ -2600,16 +2600,94 @@
 m%22, arg)
+%0A        elif opt == %22--result%22:%0A            build.add_property(%22result%22, arg)
 %0A%0A    # 

add build result property, close #11