Merge pull request #4611 from jcbrill/jbrill-mscommon-debug
[scons.git] / site_scons / soe_utils.py
blobbbe4247e922069407a49f05c0f7ab3ae5e1e1228
1 # SPDX-License-Identifier: MIT
3 # Copyright The SCons Foundation
5 import os.path
6 import re
8 from SCons.Script import Builder, Action, Scanner
11 def soelim(target, source, env):
12 """
13 Interpolate files included in [gnt]roff source files using the
14 .so directive.
16 This behaves somewhat like the soelim(1) wrapper around groff, but
17 makes us independent of whether the actual underlying implementation
18 includes an soelim() command or the corresponding command-line option
19 to groff(1). The key behavioral difference is that this doesn't
20 recursively include .so files from the include file. Not yet, anyway.
21 """
22 t = str(target[0])
23 s = str(source[0])
24 dir, f = os.path.split(s)
25 with open(t, 'w') as tfp, open(s) as sfp:
26 for line in sfp.readlines():
27 if line[:4] in ['.so ', "'so "]:
28 sofile = os.path.join(dir, line[4:-1])
29 with open(sofile) as f:
30 tfp.write(f.read())
31 else:
32 tfp.write(line)
35 def soscan(node, env, path):
36 c = node.get_text_contents()
37 return re.compile(r"^[.']so\s+(\S+)", re.M).findall(c)
40 soelimbuilder = Builder(action=Action(soelim), source_scanner=Scanner(soscan))