testsuite_run 4.84 KB
Newer Older
1
2
3
4
5
#!/usr/bin/env python
import os
import sys
import re
import subprocess
6
import io
7
import argparse
8
9
10
11
12
import logging


LOGGER=logging.getLogger(__name__)

13
14
15
16
17

baseDir = os.path.dirname(os.path.abspath(__file__))
qeDir = os.path.normpath(os.path.join(baseDir,"../"))

if not qeDir in sys.path:
18
    sys.path.insert(0, qeDir)
19
20
21
22
23

import parser_quantum_espresso

RE_IGNORE_FILE = re.compile(r".*\.(?:annotate|json)$")
RE_NO = re.compile(r"[^\|]+      no\|")
24
25
RE_GREPC = re.compile(r"(.*):(\d+)\S*$")
RE_WCL = re.compile(r"\s*(\d+)\s+(.*)$")
26

27
28
ANNOTATE = True

29
30
CALL_PYTHON = ['python']
CALL_QEPARSER = [ qeDir + '/parser_quantum_espresso.py', '--stream']
31

32
def run_tests(testsuite_dir, testsuite_base, profile=False):
33
34
    annotated = []
    input_files = [] 
35
36
37
38
39
    testsuite_list_file = testsuite_base + ".input.json"
    testsuite_out = testsuite_base + ".out"
    testsuite_log = testsuite_base + ".log"
    testsuite_parsed_missing = testsuite_base + ".parsed_missing"
    with open(testsuite_list_file, "w") as input_stream:
40
        for filename in os.listdir(testsuite_dir):
41
42
            if RE_IGNORE_FILE.match(filename):
                continue
43
            src_file = testsuite_dir + "/" + filename
44
45
46
            input_stream.write(
                '{\n  "mainFileUri": "file://%s",\n  "mainFile": "%s",\n  "outFile": "%s"\n},\n' % (
                    src_file, src_file, src_file + ".stream.json"))
47
48
            annotated.append(src_file + ".annotate")
            input_files.append(src_file)
49
50
51
52
    CALL_CMD = CALL_PYTHON
    if profile:
        CALL_CMD += ['-m', 'cProfile', '-o',  testsuite_base + '.profile']
    CALL_CMD += CALL_QEPARSER
53
    with open(testsuite_list_file, "r") as input_stream, open(testsuite_out, "w") as input_stdout, open(testsuite_log, "w") as input_stderr:
54
        subprocess.call(
55
            CALL_CMD,
56
            stdin=input_stream, stdout=input_stdout, stderr=input_stderr
57
        )
58
59
60
61
62
63
64
65
66
67
68
69
    input_nlines = {}
    with subprocess.Popen([
            'wc', '-l' ] + input_files, stdout=subprocess.PIPE) as wcl:
        for line in io.TextIOWrapper(wcl.stdout, encoding="utf-8"):
            if line == '':
                break
            line = line.rstrip()
            m = RE_WCL.match(line)
            if m is None:
                LOGGER.warning("skipping wc -l output: %s", line)
                continue
            input_nlines[m.group(2)] = int(m.group(1))
70
71
    if ANNOTATE:
        annotated_nlines = {}
72
        with subprocess.Popen([
73
74
                'wc', '-l' ] + annotated, stdout=subprocess.PIPE) as wcl:
            for line in io.TextIOWrapper(wcl.stdout, encoding="utf-8"):
75
76
                if line == '':
                    break
77
78
79
80
                line = line.rstrip()
                m = RE_WCL.match(line)
                if m is None:
                    LOGGER.warning("skipping wc -l output: %s", line)
81
                    continue
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
                annotated_nlines[m.group(2)] = int(m.group(1))
        with open(testsuite_parsed_missing, "w") as parsed_missing:
            pm = []
            with subprocess.Popen([
                    'grep', '-c', '     no|'] + annotated, stdout=subprocess.PIPE) as grepc:
                for line in io.TextIOWrapper(grepc.stdout, encoding="utf-8"):
                    if line == '':
                        break
                    m = RE_GREPC.match(line)
                    if not m:
                        LOGGER.error("unrecognized grep output: %s",line.rstrip())
                        continue
                    fname = m.group(1)
                    input_fname = re.sub(r"\.annotate",r"", fname)
                    aborted_parse = (input_nlines[input_fname] > (annotated_nlines[fname]-1))
                    if aborted_parse:
                        count_missing = -1
                    else:
                        count_missing =int(m.group(2))
                    pm.append( (count_missing, fname) )
            for num, fname in sorted(pm):
                parsed_missing.write("%5d %s\n" % (num, fname))
104
105

if __name__ == '__main__':
106
107
108
109
    CMDLINE_PARSER = argparse.ArgumentParser(
        description='run QE parser over testsuite')
    CMDLINE_PARSER.add_argument(
        '--no_annotate', action='store_true', default=False)
110
111
    CMDLINE_PARSER.add_argument(
        '--matchTelemetry', action='store_true', default=False)
112
113
    CMDLINE_PARSER.add_argument(
        '--profile', action='store_true', default=False)
114
115
116
117
118
119
120
121
    CMDLINE_PARSER.add_argument(
        'file_base', default='testsuite', nargs='?')
    CMDLINE_PARSER.add_argument(
        'testsuite_dir', default='/srv/work/tmp/espresso/test-link', nargs='?')
    CMDLINE_ARGS = CMDLINE_PARSER.parse_args()

    if CMDLINE_ARGS.no_annotate:
        ANNOTATE = False
122
    else:
123
124
        CALL_QEPARSER.append('--annotate')

125
126
127
    if CMDLINE_ARGS.matchTelemetry:
        CALL_QEPARSER.append('--matchTelemetry')

128
129
    run_tests(CMDLINE_ARGS.testsuite_dir, CMDLINE_ARGS.file_base,
              CMDLINE_ARGS.profile)