1
2import os
3import sys
4import runtest_v1 as runtest
5
6
7def write_stderr(log_file, s):
8    """
9    Writes s to stderr and to file log_file
10    unless log_file is None.
11    """
12    if log_file:
13        with open(log_file, 'w') as f:
14            f.write(s)
15    sys.stderr.write(s)
16
17
18class Filter(runtest.Filter):
19
20    def __init__(self):
21        runtest.Filter.__init__(self)
22
23    def add(self, *args, **kwargs):
24        try:
25            runtest.Filter.add(self, *args, **kwargs)
26        except runtest.FilterKeywordError as e:
27            sys.stderr.write(str(e))  # FIXME currently not written to any log file
28            sys.exit(-1)
29
30
31class TestRun(runtest.TestRun):
32
33    def __init__(self, _file, argv):
34        runtest.TestRun.__init__(self, _file, argv)
35        self.return_code = 0
36
37    def run(self, inp_files, mol_files=[], other_files=[], f=None, args='', accepted_errors=[], noarch=True):
38
39        launch_script = os.path.normpath(os.path.join(self.binary_dir, 'dalton'))
40        if self.skip_run:
41            sys.stdout.write('\nskipping actual run\n')
42        else:
43            if not os.path.exists(launch_script):
44                sys.stderr.write('ERROR: launch script %s not found\n' % launch_script)
45                sys.stderr.write('       have you set the correct --binary-dir (or -b)?\n')
46                sys.stderr.write('       try also --help\n')
47                sys.exit(-1)
48
49        if noarch:
50            launcher = '%s -ow -noarch -nobackup %s' % (launch_script, args)
51        else:
52            launcher = '%s -ow -nobackup %s' % (launch_script, args)
53
54        commands = []
55        messages = []
56        outputs_no_suffix = []
57        for inp in inp_files:
58            inp_no_suffix = os.path.splitext(inp)[0]
59            if mol_files:
60                for mol in mol_files:
61                    mol_no_suffix = os.path.splitext(mol)[0]
62                    if other_files:
63                        for other in other_files:
64                            other_no_suffix = os.path.splitext(other)[0]
65                            if inp_no_suffix == mol_no_suffix and mol_no_suffix == other_no_suffix:
66                                output_no_suffix = '%s' % (inp_no_suffix,)
67                            else:
68                                output_no_suffix = '%s_%s_%s' % (inp_no_suffix, mol_no_suffix, other_no_suffix)
69                            outputs_no_suffix.append(output_no_suffix)
70                            messages.append('\nrunning test: %s %s %s\n' % (inp_no_suffix, mol_no_suffix, other_no_suffix))
71                            commands.append(launcher + ' %s %s %s' % (inp_no_suffix, mol_no_suffix, other_no_suffix))
72                    else:
73                        if inp_no_suffix == mol_no_suffix:
74                            output_no_suffix = '%s' % (inp_no_suffix,)
75                        else:
76                            output_no_suffix = '%s_%s' % (inp_no_suffix, mol_no_suffix)
77                        outputs_no_suffix.append(output_no_suffix)
78                        messages.append('\nrunning test: %s %s\n' % (inp_no_suffix, mol_no_suffix))
79                        commands.append(launcher + ' %s %s' % (inp_no_suffix, mol_no_suffix))
80            else:
81                outputs_no_suffix.append('%s' % (inp_no_suffix,))
82                messages.append('\nrunning test: %s\n' % (inp_no_suffix,))
83                commands.append(launcher + ' %s' % (inp_no_suffix,))
84        for output_no_suffix, message, command in zip(outputs_no_suffix, messages, commands):
85            try:
86                sys.stdout.write(message)
87                runtest.TestRun.execute(self,
88                                        command=command,
89                                        stdout_file_name = '%s.stdout' % output_no_suffix,
90                                        accepted_errors=accepted_errors)
91                if f is None:
92                    sys.stdout.write('finished (no reference)\n')
93                else:
94                    try:
95                        # f is a suffix-filter dictionary
96                        for suffix in f:
97                            out = '%s.%s' % (output_no_suffix, suffix)
98                            f[suffix].check(self.work_dir, '%s' % out, 'result/%s' % out, self.verbose)
99                        sys.stdout.write('passed\n')
100                    except IOError as e:
101                        write_stderr(self.log, 'ERROR: could not open file %s\n' % e.filename)
102                        sys.exit(-1)
103                    except runtest.TestFailedError as e:
104                        write_stderr(self.log, str(e))
105                        self.return_code += 1
106                    except runtest.BadFilterError as e:
107                        write_stderr(self.log, str(e))
108                        sys.exit(-1)
109                    except runtest.FilterKeywordError as e:
110                        write_stderr(self.log, str(e))
111                        sys.exit(-1)
112            except runtest.AcceptedError as e:
113                sys.stdout.write(str(e))
114            except runtest.SubprocessError as e:
115                write_stderr(self.log, str(e))
116                sys.exit(-1)
117