"""
Copyright (C) 2016 by Strong Leaf Consultant Ltd.

GNU AGPL v3+

Bob-Bench xUnit file detection
"""

from benchupload import xunit
from benchupload import cidetect
import argparse
import os
import sys
import requests


xunit_dest_url = "https://api.bob-bench.org/v1/xunit_upload.pl"

def post(url, ci, fnames):
    files = list(map(lambda x: (os.path.basename(x), open(x, 'rb')), fnames))
    data = {
        'x-is-github':  ci.is_github,
        'x-commit':     ci.commit,
        'x-tag':        ci.tag,
        'x-branch':     ci.branch,
        'x-buildnr':    ci.build_nr,
        'x-os-name':    ci.os_name,
        'x-repo-url':   ci.repo_url,
        'x-ci':         ci.system_name()
    }
    res = requests.post(xunit_dest_url, data=data, files=files)
    if res.status_code != 200:
        sys.stderr.write("Failed to POST resources\n")
        sys.exit(43)

def parsed_args():
    parser = argparse.ArgumentParser(description="Upload handling")
    parser.add_argument('--dir', dest='start_dir', default=os.curdir, help="Start directory")
    return parser.parse_args()

def main():
    args = parsed_args()
    ci = cidetect.detect()
    if not ci:
        sys.stderr.write("No CI system reported. Please report it to cidetect@bob-bench.org\n")
        sys.exit(42)

    files = xunit.detect_xunit_files(args.start_dir)
    if len(files) == 0:
        sys.stdout.write("No XML test results detected\n")
        sys.exit(0)

    post(xunit_dest_url, ci, files)

if __name__ == "__main__":
    main()


@@ -220,73 +220,8 @@
 s%0A%0A%0A
-xunit_dest_url = %22https://api.bob-bench.org/v1/xunit_upload.pl%22%0A%0A
 def 
@@ -651,35 +651,24 @@
 quests.post(
-xunit_dest_
 url, data=da
@@ -973,16 +973,136 @@
 ctory%22)%0A
+    parser.add_argument('--url', dest='url', default='https://api.bob-bench.org/v1/xunit_upload.pl', help=%22Uplpad URL%22)%0A
     retu
@@ -1492,19 +1492,13 @@
 ost(
-xunit_dest_
+args.
 url,

Make upload URL configurable, it eases testing