summaryrefslogtreecommitdiffstats
path: root/common/latex.py
blob: 9704486c69a707b6794a53f5e3e1ace70b0d0330 (plain) (blame)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#
# Support for Latex used to build the PDF output format.
#

import os
import platform
import re

package_test_preamble = ['\\newif\\ifsphinxKeepOldNames \\sphinxKeepOldNamestrue',
                         '\documentclass[a4paper,11pt,english]{report}']
package_test_postamble = ['\\begin{document} test \\end{document}']
package_tests = {
    'Bjarne'         : ['\\usepackage[Bjarne]{fncychap}'],
    'alltt'          : ['\\usepackage{alltt}'],
    'amsmath'        : ['\\usepackage{amsmath}'],
    'amssymb'        : ['\\usepackage{amssymb}'],
    'amstext'        : ['\\usepackage{amstext}'],
    'anyfontsize'    : ['\\usepackage{anyfontsize}'],
    'array'          : ['\\usepackage{array}'],
    'atbegshi'       : ['\\usepackage{atbegshi}'],
    'babel'          : ['\\usepackage{babel}'],
    'babel'          : ['\\usepackage{babel}'],
    'calc'           : ['\\usepackage{calc}'],
    'capt-of'        : ['\\usepackage{capt-of}'],
    'charter'        : ['\\usepackage{charter}'],
    'cmap'           : ['\\usepackage{cmap}'],
    'color'          : ['\\usepackage{color}'],
    'eqparbox'       : ['\\usepackage{eqparbox}'],
    'enumitem'       : ['\\usepackage{enumitem}'],
    'etoolbox'       : ['\\usepackage{etoolbox}'],
    'fancybox'       : ['\\usepackage{fancybox}'],
    'fancyhdr'       : ['\\usepackage{fancyhdr}'],
    'fancyvrb'       : ['\\usepackage{fancyvrb}'],
    'float'          : ['\\usepackage{float}'],
    'fncychap'       : ['\\usepackage{fncychap}'],
    'fontenc'        : ['\\usepackage[T1]{fontenc}'],
    'footnote'       : ['\\usepackage{footnote}'],
    'framed'         : ['\\usepackage{framed}'],
    'graphicx'       : ['\\usepackage{graphicx}'],
    'hypcap'         : ['\\usepackage{hyperref}',
                        '\\usepackage{hypcap}'],
    'hyperref'       : ['\\usepackage{hyperref}'],
    'inconsolata'    : ['\\usepackage{inconsolata}'],
    'ifplatform'     : ['\\usepackage{ifplatform}'],
    'ifthen'         : ['\\usepackage{ifthen}'],
    'inputenc'       : ['\\usepackage{inputenc}'],
    'keyval'         : ['\\usepackage{keyval}'],
    'kvoptions'      : ['\\usepackage{kvoptions}'],
    'lato'           : ['\\usepackage{lato}'],
    'lineno'         : ['\\usepackage{lineno}'],
    'longtable'      : ['\\usepackage{longtable}'],
    'makeidx'        : ['\\usepackage{makeidx}'],
    'multirow'       : ['\\usepackage{multirow}'],
    'parskip'        : ['\\usepackage{parskip}'],
    'pdftexcmds'     : ['\\usepackage{pdftexcmds}'],
    'textcomp'       : ['\\usepackage{textcomp}'],
    'threeparttable' : ['\\usepackage{threeparttable}'],
    'times'          : ['\\usepackage{times}'],
    'titlesec'       : ['\\usepackage{titlesec}'],
    'upquote'        : ['\\usepackage{upquote}'],
    'utf8'           : ['\\usepackage[utf8]{inputenc}'],
    'wrapfig'        : ['\\usepackage{wrapfig}'],
    'xcolor'         : ['\\usepackage{xcolor}'],
    'xstring'        : ['\\usepackage{xstring}'],
}
package_optional = ['inconsolata',
                    'lato']

#
# Add per host support. If there is a version clash for the same texlive
# package create a directory, add to that directory and use the path in this
# name here.
#
hosts = {
    # All versions of CentOS until told otherwise
    'Linux/centos' : { '.*' : ['capt-of.sty',
                               'eqparbox.sty',
                               'environ.sty',
                               'ifplatform.sty',
                               'trimspaces.sty',
                               'slantsc.sty',
                               'upquote.sty'] }
}

def tex_test(test):
    return os.linesep.join(package_test_preamble +
                           package_tests[test] +
                           package_test_postamble)

def host_name():
    uname = os.uname()
    if uname[0] == 'Linux':
        distro = platform.dist()
        name = '%s/%s' % (uname[0], distro[0])
        version = distro[1]
    else:
        name = uname[0]
        version = uname[2]
    return name, version

def local_packages():
    host, version = host_name()
    packages = None
    if host in hosts:
        for hv in list(hosts[host].keys()):
            if re.compile(hv).match(version) is not None:
                packages = hosts[host][hv]
    return packages

def configure_tests(conf):
    #
    # Using a hint from ita (thank you) :
    #  https://github.com/waf-project/waf/blob/master/demos/tex/wscript
    #
    def build_latex_test(bld):
        def write_tex_test(tsk):
            tsk.outputs[0].write(tex_test(tsk.env.TEST))

        test = bld.kw['tex_test']
        bld.env.TEST = test

        bld.to_log('%s.tex %s' % (test, '=' * (40 - len(test) + 5)))
        bld.to_log(tex_test(test))
        bld.to_log('=' * 40)

        bld(rule = write_tex_test, target = 'main.tex')
        bld(features = 'tex', type = 'pdflatex', source = 'main.tex', prompt = 0)

    tests = sorted(package_tests.keys())
    local_packs = local_packages()
    excludes = [p for p in package_optional]
    if local_packs is not None:
        excludes += [p[:p.rfind('.')] for p in local_packs]
    for e in excludes:
        if e in tests:
            tests.remove(e)

    fails = 0
    r = conf.find_program("pygmentize", mandatory = False)
    if r is None:
        fails += 1
    for t in tests:
        r = conf.test(build_fun = build_latex_test,
                      msg = "Checking for Tex package '%s'" % (t),
                      tex_test = t,
                      okmsg = 'ok',
                      errmsg = 'not found (please install)',
                      mandatory = False)
        if r is None:
            fails += 1
    if fails > 0:
        conf.fatal('There are %d Tex package failures. Please fix.' % (fails))

    fails = 0
    for t in package_optional:
        r = conf.test(build_fun = build_latex_test,
                      msg = "Checking for Tex package '%s'" % (t),
                      tex_test = t,
                      okmsg = 'ok',
                      errmsg = 'not found (degraded fonts)',
                      mandatory = False)
        if r is None:
            fails += 1
    if fails == 0:
        conf.env.RTEMSEXTRAFONTS = 'rtemsextrafonts.sty'
    else:
        if not conf.options.disable_extra_fonts:
            conf.fatal('Extra fonts not found, install or use --disable-extra-fonts')
        conf.env.RTEMSEXTRAFONTS = 'rtemsextrafonts-null.sty'