Debian Codegraph @

doc/sphinx/coverage.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2019, the cclib development team
4 #
5 # This file is part of cclib (http://cclib.github.io) and is distributed under
6 # the terms of the BSD 3-Clause License.
7
8 """Generate the coverage.rst and coverage.rst files from test results."""
9
10 from __future__ import print_function
11
12 import os
13 import sys
14
15 from docs_common import check_cclib
16
17 # Import cclib and check we are using the version from a subdirectory.
18 import cclib
19 check_cclib(cclib)
20
21
22 def generate_coverage():
23 """Generate a string containing a reStructuredTest table
24 representation of which parsers support which attributes, based on
25 test results.
26 """
27 lines = []
28
29 # Change directory to where tests are and add it to the path. Because there are
30 # separate directories for different branches/versions, and we use a symlink to
31 # point to the one we want, we need to test the real path this link resolves to.
32 if "cclib_prod" in os.path.realpath('cclib'):
33 testpath = "_build/cclib_prod"
34 else:
35 assert "cclib_dev" in os.path.realpath('cclib')
36 testpath = "_build/cclib_dev"
37
38 os.chdir(testpath)
39
40 thispath = os.path.dirname(os.path.realpath(__file__))
41 sys.path.insert(1, thispath)
42
43 from test.test_data import (all_modules, all_parsers, parser_names, DataSuite)
44 import inspect
45 ds_args = inspect.getargspec(DataSuite.__init__).args
46 logpath = thispath + "/coverage.tests.log"
47 try:
48 with open(logpath, "w") as flog:
49 stdout_backup = sys.stdout
50 sys.stdout = flog
51 alltests = {}
52 for p in parser_names:
53 assert 'parsers' in ds_args
54 suite = DataSuite(parsers={p: all_parsers[p]}, modules=all_modules, stream=flog)
55 suite.testall()
56 alltests[p] = [{'data': t.data} for t in suite.alltests]
57 sys.stdout = stdout_backup
58 except Exception as e:
59 print("Unit tests did not run correctly. Check log file for errors:")
60 with open(logpath) as fh:
61 print(fh.read())
62 print(e)
63 sys.exit(1)
64
65 ncols = len(parser_names) + 1
66 colwidth = 20
67 colfmt = "%%-%is" % colwidth
68 dashes = ("=" * (colwidth - 1) + " ") * ncols
69
70 lines.append(dashes)
71 lines.append(colfmt * ncols % tuple(["attributes"] + parser_names))
72 lines.append(dashes)
73
74 # Eventually we want to move this to cclib, too.
75 not_applicable = {
76 'ADF' : ['aonames', 'ccenergies', 'mpenergies'],
77 'DALTON' : ['fonames', 'fooverlaps', 'fragnames', 'frags'],
78 'GAMESS' : ['fonames', 'fooverlaps', 'fragnames', 'frags'],
79 'GAMESSUK' : ['fonames', 'fooverlaps', 'fragnames', 'frags'],
80 'Gaussian' : ['fonames', 'fooverlaps', 'fragnames', 'frags'],
81 'Jaguar' : ['fonames', 'fooverlaps', 'fragnames', 'frags'],
82 'Molpro' : ['fonames', 'fooverlaps', 'fragnames', 'frags'],
83 'NWChem' : ['fonames', 'fooverlaps', 'fragnames', 'frags'],
84 'ORCA' : ['fonames', 'fooverlaps', 'fragnames', 'frags'],
85 'Psi' : ['fonames', 'fooverlaps', 'fragnames', 'frags'],
86 'QChem' : ['fonames', 'fooverlaps', 'fragnames', 'frags'],
87 }
88 not_possible = {
89 'Psi' : ['aooverlaps', 'vibirs'],
90 'QChem' : ['aooverlaps', 'etrotats'],
91 }
92
93 # For each attribute, get a list of Boolean values for each parser that flags
94 # if it has been parsed by at least one unit test. Substitute an OK sign or
95 # T/D appropriately, with the exception of attributes that have been explicitely
96 # designated as N/A.
97 attributes = sorted(cclib.parser.data.ccData._attrlist)
98 for attr in attributes:
99 parsed = [any([attr in t['data'].__dict__ for t in alltests[p]]) for p in parser_names]
100 for ip, p in enumerate(parsed):
101 if p:
102 parsed[ip] = "√"
103 else:
104 if attr in not_applicable.get(parser_names[ip], []):
105 parsed[ip] = "N/A"
106 elif attr in not_possible.get(parser_names[ip], []):
107 parsed[ip] = "N/P"
108 else:
109 parsed[ip] = "T/D"
110 lines.append(colfmt*ncols % tuple(["`%s`_" % attr] + parsed))
111
112 lines.append(dashes)
113 lines.append("")
114
115 for attr in attributes:
116 lines.append(".. _`%s`: data_notes.html#%s" % (attr, attr))
117
118 return "\n".join(lines)
119
120 if __name__ == "__main__":
121 print(generate_coverage())