summaryrefslogtreecommitdiff
path: root/site_scons/soe_utils.py
blob: 451c7de46c79fe5c5ca1d02cd6884dd4315c9470 (plain)
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
import os.path
import re

from SCons.Script import Builder, Action, Scanner

def soelim(target, source, env):
    """
    Interpolate files included in [gnt]roff source files using the
    .so directive.

    This behaves somewhat like the soelim(1) wrapper around groff, but
    makes us independent of whether the actual underlying implementation
    includes an soelim() command or the corresponding command-line option
    to groff(1).  The key behavioral difference is that this doesn't
    recursively include .so files from the include file.  Not yet, anyway.
    """
    t = str(target[0])
    s = str(source[0])
    dir, f = os.path.split(s)
    tfp = open(t, 'w')
    sfp = open(s, 'r')
    for line in sfp.readlines():
        if line[:4] in ['.so ', "'so "]:
            sofile = os.path.join(dir, line[4:-1])
            tfp.write(open(sofile, 'r').read())
        else:
            tfp.write(line)
    sfp.close()
    tfp.close()

def soscan(node, env, path):
    c = node.get_text_contents()
    return re.compile(r"^[\.']so\s+(\S+)", re.M).findall(c)

soelimbuilder = Builder(action = Action(soelim),
                        source_scanner = Scanner(soscan))