2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

tst_LaTeX_docs.py 4.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/python3

"""tst_LaTeX_doc.py - tests with full documents in subdir 'testdata'

Author: Eckhart Arnold <arnold@badw.de>

Copyright 2017 Bavarian Academy of Sciences and Humanities

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

22
import cProfile as profile
23
import fnmatch
24
import os
25
import pstats
26
27
import sys

28
29
sys.path.extend(['../../', '../', './'])

30
import DHParser.dsl
31
import DHParser.log
32
33
from DHParser.log import log_parsing_history

34

di68kap's avatar
di68kap committed
35
LOGGING = False
eckhart's avatar
eckhart committed
36

37
38
if not DHParser.dsl.recompile_grammar('LaTeX.ebnf', force=False):  # recompiles Grammar only if it has changed
    print('\nErrors while recompiling "LaTeX.ebnf":\n--------------------------------------\n\n')
39
    with open('LaTeX_ebnf_ERRORS.txt', encoding="utf-8") as f:
40
41
42
43
        print(f.read())
    sys.exit(1)


44
from LaTeXCompiler import get_grammar, get_transformer, get_compiler
45
46
47

parser = get_grammar()
transformer = get_transformer()
48
compiler = get_compiler()
49
50
51
52

def fail_on_error(src, result):
    if result.error_flag:
        print(result.as_sxpr())
53
54
        for e in result.collect_errors():
            print(str(e))
55
56
        sys.exit(1)

57

58
59
60
61
62
63
64
def count_nodes(tree, condition=lambda n: True):
    N = 0
    for nd in tree.select(condition, include_root=True):
        N += 1
    return N


65
def tst_func():
eckhart's avatar
eckhart committed
66
    with DHParser.log.logging(LOGGING):
67
68
        if not os.path.exists('REPORT'):
            os.mkdir('REPORT')
69
70
71
        files = os.listdir('testdata')
        files.sort()
        for file in files:
72
            if fnmatch.fnmatch(file, '*.tex') and file.lower().find('error') < 0:
73
                with open(os.path.join('testdata', file), 'r', encoding='utf-8') as f:
74
                    doc = f.read()
75
76

                print('\n\nParsing document: "%s"' % file)
77
                result = parser(doc)
78
79
80
                print("Number of CST-nodes: " + str(count_nodes(result)))
                # print("Number of empty nodes: " + str(count_nodes(result,
                #                                                 lambda n: not bool(n.result))))
81
                if DHParser.log.is_logging():
82
                    print('Saving CST')
83
                    with open('REPORT/' + file[:-4] + '.cst', 'w', encoding='utf-8') as f:
84
                        f.write(result.as_sxpr(compact=True))
85
                    print('Saving parsing history')
86
                    log_parsing_history(parser, os.path.basename(file), html=True)
87
88

                print('\nTransforming document: "%s"' % file)
89
90
91
                fail_on_error(doc, result)
                transformer(result)
                fail_on_error(doc, result)
92
                print("Number of AST-nodes: " + str(count_nodes(result)))
93
94
95
                if DHParser.log.is_logging():
                    print('Saving AST')
                    with open('LOGS/' + file[:-4] + '.ast', 'w', encoding='utf-8') as f:
96
                        f.write(result.as_sxpr(compact=True))
97
98
                    with open('LOGS/' + file[:-4] + '.tex', 'w', encoding='utf-8') as f:
                        f.write(str(result))
99
100
101

                print('\nCompiling document: "%s"' % file)
                output = compiler(result)
eckhart's avatar
eckhart committed
102
                # print(output)
103
104
105


def cpu_profile(func):
106
107
    pr = profile.Profile()
    pr.enable()
108
    func()
109
110
111
    pr.disable()
    st = pstats.Stats(pr)
    st.strip_dirs()
112
    st.sort_stats('time').print_stats(40)
113
114
115
116
117
118
119
120
121


def mem_profile(func):
    import tracemalloc
    tracemalloc.start()
    func()
    snapshot = tracemalloc.take_snapshot()
    top_stats = snapshot.statistics('lineno')
    print("[ Top 20 ]")
122
    for stat in top_stats[:40]:
123
124
125
        print(stat)

if __name__ == "__main__":
Eckhart Arnold's avatar
Eckhart Arnold committed
126
    cpu_profile(tst_func)
127

128