import sys
import json
import subprocess
import re
import statistics

def get_complexity():

    # Load the cyclomatic complexity info
    cyclostats = subprocess.check_output(['./gocyclo', 'repo']).decode("utf-8") 
    results = re.findall('([0-9]+)\s([^\s]+)\s([^\s]+)\s([^:]+):([0-9]+):([0-9]+)', cyclostats)

    # Setup a dictionary in which to keep track of the complixities
    # for each file
    files = {}

    # Build an array of complexities for each file
    for result in results:
        if result[3] in files:
            files[result[3]].append(int(result[0]))
        else:
            files[result[3]] = [int(result[0])]

    # Pick out the median value (picking the highest of the two
    # middle entries if needed) for each file
    for name, values in files.items():
        files[name] = statistics.median_high(values)

    return files

def get_duplicate_const_strings():

    # Load the const string duplication info
    cyclostats = subprocess.check_output(['./goconst', './repo/...']).decode("utf-8") 
    results = re.findall('([^:]+).+ other occurrence\(s\) of \"(.+)\" found in: ([^:]+).+\n?', cyclostats)

    files = {}

    # Build an array containing the number of potentially duplicated
    # constants by file
    for result in results:
        if result[0] in files:
            files[result[0]] = files[result[0]]+1
        else:
            files[result[0]] = 1

    return files

# Main service body
if __name__ == "__main__":  

    complexity = get_complexity()
    duplicate_const_strings = get_duplicate_const_strings()

    files = set()
    files.update(complexity.keys())
    files.update(duplicate_const_strings.keys())

    result = []

    for f in files:
        result.append({
            'filename': f,
            'cyclomaticComplexity': complexity[f] if f in complexity else 0,
            'duplicateConstStrings': duplicate_const_strings[f] if f in duplicate_const_strings else 0
        })

    print(json.dumps(result))
[((11, 14, 11, 95), 're.findall', 're.findall', ({(11, 25, 11, 82): '"""([0-9]+)\\\\s([^\\\\s]+)\\\\s([^\\\\s]+)\\\\s([^:]+):([0-9]+):([0-9]+)"""', (11, 84, 11, 94): 'cyclostats'}, {}), "('([0-9]+)\\\\s([^\\\\s]+)\\\\s([^\\\\s]+)\\\\s([^:]+):([0-9]+):([0-9]+)',\n    cyclostats)", False, 'import re\n'), ((35, 14, 35, 106), 're.findall', 're.findall', ({(35, 25, 35, 93): '"""([^:]+).+ other occurrence\\\\(s\\\\) of "(.+)" found in: ([^:]+).+\n?"""', (35, 95, 35, 105): 'cyclostats'}, {}), '(\n    """([^:]+).+ other occurrence\\\\(s\\\\) of "(.+)" found in: ([^:]+).+\n?""",\n    cyclostats)', False, 'import re\n'), ((27, 22, 27, 52), 'statistics.median_high', 'statistics.median_high', ({(27, 45, 27, 51): 'values'}, {}), '(values)', False, 'import statistics\n'), ((68, 10, 68, 28), 'json.dumps', 'json.dumps', ({(68, 21, 68, 27): 'result'}, {}), '(result)', False, 'import json\n'), ((10, 17, 10, 63), 'subprocess.check_output', 'subprocess.check_output', ({(10, 41, 10, 62): "['./gocyclo', 'repo']"}, {}), "(['./gocyclo', 'repo'])", False, 'import subprocess\n'), ((34, 17, 34, 69), 'subprocess.check_output', 'subprocess.check_output', ({(34, 41, 34, 68): "['./goconst', './repo/...']"}, {}), "(['./goconst', './repo/...'])", False, 'import subprocess\n')]