File: runtest.py

package info (click to toggle)
psi4 1%3A1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 229,808 kB
  • sloc: cpp: 416,201; python: 207,680; perl: 4,328; makefile: 384; sh: 158; csh: 6
file content (154 lines) | stat: -rw-r--r-- 5,016 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#
# @BEGIN LICENSE
#
# Psi4: an open-source quantum chemistry software package
#
# Copyright (c) 2007-2018 The Psi4 Developers.
#
# The copyrights for code used from other parties are included in
# the corresponding files.
#
# This file is part of Psi4.
#
# Psi4 is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, version 3.
#
# Psi4 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with Psi4; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# @END LICENSE
#

from __future__ import print_function
import os
import sys
import time
import subprocess

if len(sys.argv) not in [5, 6, 7, 8, 9, 10]:
    print("""Usage: %s input_file logfile doperltest top_srcdir doreaptest alt_output_file alt_psi4_exe alt_psi4datadir""" % (sys.argv[0]))
    sys.exit(1)

# extract run condition from arguments
python_exec = sys.argv[0]
infile = sys.argv[1]
logfile = sys.argv[2]
psiautotest = sys.argv[3]
top_srcdir = sys.argv[4]
sowreap = sys.argv[5]

if len(sys.argv) >= 7:
    outfile = sys.argv[6]
else:
    outfile = 'output.dat'

if len(sys.argv) >= 8:
    psi = sys.argv[7]
else:
    psi = '../../bin/psi4'

if len(sys.argv) >= 9:
    psidatadir = sys.argv[8]
else:
    psidatadir = os.path.dirname(os.path.realpath(psi)) + '/../share/psi4'

if len(sys.argv) >= 10:
    psilibdir = sys.argv[9] + os.path.sep
else:
    psilibdir = os.path.abspath('/../')

# open logfile and print test case header
try:
    loghandle = open(logfile, 'a')
except IOError as e:
    print("""I can't write to %s: %s""" % (logfile, e))
loghandle.write("""\n%s\n%s\n""" % (os.path.dirname(infile).split(os.sep)[-1], time.strftime("%Y-%m-%d %H:%M")))


def backtick(exelist):
    """Executes the command-argument list in *exelist*, directing the
    standard output to screen and file logfile and string p4out. Returns
    the system status of the call.

    """
    try:
        retcode = subprocess.Popen(exelist, bufsize=0, stdout=subprocess.PIPE, universal_newlines=True)
    except OSError as e:
        sys.stderr.write('Command %s execution failed: %s\n' % (exelist, e.strerror))
        sys.exit(1)

    p4out = ''
    while True:
        data = retcode.stdout.readline()
        if not data:
            break
        sys.stdout.write(data)  # screen
        loghandle.write(data)  # file
        loghandle.flush()
        p4out += data  # string
    while True:
        retcode.poll()
        exstat = retcode.returncode
        if exstat is not None:
            return exstat
        time.sleep(0.1)
    loghandle.close()
    # not sure why 2nd while loop needed, as 1st while loop has always
    #   been adequate for driver interfaces. nevertheless, to collect
    #   the proper exit code, 2nd while loop very necessary.

# run psi4 and collect testing status from any compare_* in input file
if os.path.isfile(infile):
    pyexitcode = backtick([psi, infile, outfile, '-l', psidatadir])
elif os.path.isfile(infile.replace(".dat", ".py")):
    infile = infile.replace(".dat", ".py")
    os.environ["PYTHONPATH"] = psilibdir
    outfile = os.path.dirname(infile) + os.path.sep + outfile
    pyexitcode = backtick(["python3", infile, " > ", outfile])
else:
    raise Exception("\n\nError: Input file %s not found\n" % infile)

if sowreap == 'true':
    try:
        retcode = subprocess.Popen([sys.executable, '%s/tests/reap.py' %
                  (top_srcdir), infile, outfile, logfile, psi, psidatadir])
    except OSError as e:
        print("""Can't find reap script: %s """ % (e))
    while True:
        retcode.poll()
        exstat = retcode.returncode
        if exstat is not None:
            reapexitcode = exstat
            break
        time.sleep(0.1)
else:
    reapexitcode = None

# additionally invoke autotest script comparing output.dat to output.ref
if psiautotest == 'true':
    os.environ['SRCDIR'] = os.path.dirname(infile)
    try:
        retcode = subprocess.Popen(['perl', '%s/tests/psitest.pl' % (top_srcdir), infile, logfile])
    except IOError as e:
        print("""Can't find psitest script: %s""" % (e))
    while True:
        retcode.poll()
        exstat = retcode.returncode
        if exstat is not None:
            plexitcode = exstat
            break
        time.sleep(0.1)
else:
    plexitcode = None

# combine, print, and return (0/1) testing status
exitcode = 0 if (pyexitcode == 0 and (plexitcode is None or plexitcode == 0) and (reapexitcode is None or reapexitcode == 0)) else 1
print('Exit Status: infile (', pyexitcode, '); autotest (', plexitcode, '); sowreap (', reapexitcode, '); overall (', exitcode, ')')
sys.exit(exitcode)