Skip to content
Snippets Groups Projects
gather_results.py 1.24 KiB
"""
gather_results: gather OAR results from one dir to one file called gathered_results in the same directory.

The result files should consist of lines and should have "stdout" in their name.

Usage:
    gather_results -i IPATH [-p regex]

Options:
    -h --help                   Show this screen.
    -i --input-dir=<IPATH>      Input directory wher to find results
    -p --patern=regex           Specify the pattern of the files to be looked at [default: .+\.stdout].
"""

from os import listdir
import os
from os.path import isfile, join
import docopt
import re


if __name__ == '__main__':
    arguments = docopt.docopt(__doc__)
    pattern_to_recognize = arguments["--patern"]
    mypath = os.path.abspath(arguments["--input-dir"])
    onlyfiles = [os.path.join(mypath, f) for f in listdir(mypath) if isfile(join(mypath, f))]
    count = 0
    results = []
    compiled_re = re.compile(pattern_to_recognize)
    for f_name in onlyfiles:
        if compiled_re.match(f_name) is None:
            continue
        with open(f_name, "r") as f:
            str_f = f.read().strip()
            results.append(str_f)

    with open(os.path.join(mypath, "gathered_results.csv"), 'w') as f_w:
        for s in results:
            f_w.write(s)
            f_w.write("\n")