tst_LaTeX_docs.py 3.01 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import DHParser.dsl
from DHParser import toolkit

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')
    with open('LaTeX_ebnf_ERRORS.txt') as f:
        print(f.read())
    sys.exit(1)


from LaTeXCompiler import get_grammar, get_transformer

parser = get_grammar()
transformer = get_transformer()

def fail_on_error(src, result):
    if result.error_flag:
        print(result.as_sxpr())
48
49
        for e in result.collect_errors():
            print(str(e))
50
51
        sys.exit(1)

52

53
def tst_func():
54
    with toolkit.logging(False):
55
56
        if not os.path.exists('REPORT'):
            os.mkdir('REPORT')
57
58
59
        files = os.listdir('testdata')
        files.sort()
        for file in files:
60
            if fnmatch.fnmatch(file, '*.tex') and file.lower().find('error') < 0:
61
                with open(os.path.join('testdata', file), 'r', encoding='utf-8') as f:
62
63
64
                    doc = f.read()
                print('\n\nParsing document: "%s"\n' % file)
                result = parser(doc)
65
                if toolkit.is_logging():
66
67
68
69
70
                    with open('REPORT/' + file[:-4] + '.cst', 'w', encoding='utf-8') as f:
                        f.write(result.as_sxpr(compact=False))
                    transformer(result)
                    with open('REPORT/' + file[:-4] + '.ast', 'w', encoding='utf-8') as f:
                        f.write(result.as_sxpr(compact=False))
71
                    parser.log_parsing_history__()
72
73
74
75
76
77
78
                fail_on_error(doc, result)
                transformer(result)
                fail_on_error(doc, result)
                # print(result.as_sxpr())


def cpu_profile(func):
79
80
    pr = profile.Profile()
    pr.enable()
81
    func()
82
83
84
    pr.disable()
    st = pstats.Stats(pr)
    st.strip_dirs()
85
    st.sort_stats('time').print_stats(40)
86
87
88
89
90
91
92
93
94


def mem_profile(func):
    import tracemalloc
    tracemalloc.start()
    func()
    snapshot = tracemalloc.take_snapshot()
    top_stats = snapshot.statistics('lineno')
    print("[ Top 20 ]")
95
    for stat in top_stats[:40]:
96
97
98
        print(stat)

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

101