03c66fa571f75a8fd50dd92b31dd41159e983669
[scons.git] / bin / scons-doc.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2010 The SCons Foundation
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 #
25 # scons-doc.py -    an SGML preprocessor for capturing SCons output
26 #                   and inserting it into examples in our DocBook
27 #                   documentation
28 #
29 # This script looks for some SGML tags that describe SCons example
30 # configurations and commands to execute in those configurations, and
31 # uses TestCmd.py to execute the commands and insert the output from
32 # those commands into the SGML that we output.  This way, we can run a
33 # script and update all of our example documentation output without
34 # a lot of laborious by-hand checking.
35 #
36 # An "SCons example" looks like this, and essentially describes a set of
37 # input files (program source files as well as SConscript files):
38 #
39 #       <scons_example name="ex1">
40 #         <file name="SConstruct" printme="1">
41 #           env = Environment()
42 #           env.Program('foo')
43 #         </file>
44 #         <file name="foo.c">
45 #           int main() { printf("foo.c\n"); }
46 #         </file>
47 #       </scons_example>
48 #
49 # The <file> contents within the <scons_example> tag will get written
50 # into a temporary directory whenever example output needs to be
51 # generated.  By default, the <file> contents are not inserted into text
52 # directly, unless you set the "printme" attribute on one or more files,
53 # in which case they will get inserted within a <programlisting> tag.
54 # This makes it easy to define the example at the appropriate
55 # point in the text where you intend to show the SConstruct file.
56 #
57 # Note that you should usually give the <scons_example> a "name"
58 # attribute so that you can refer to the example configuration later to
59 # run SCons and generate output.
60 #
61 # If you just want to show a file's contents without worry about running
62 # SCons, there's a shorter <sconstruct> tag:
63 #
64 #       <sconstruct>
65 #         env = Environment()
66 #         env.Program('foo')
67 #       </sconstruct>
68 #
69 # This is essentially equivalent to <scons_example><file printme="1">,
70 # but it's more straightforward.
71 #
72 # SCons output is generated from the following sort of tag:
73 #
74 #       <scons_output example="ex1" os="posix">
75 #         <scons_output_command>scons -Q foo</scons_output_command>
76 #         <scons_output_command>scons -Q foo</scons_output_command>
77 #       </scons_output>
78 #
79 # You tell it which example to use with the "example" attribute, and then
80 # give it a list of <scons_output_command> tags to execute.  You can also
81 # supply an "os" tag, which specifies the type of operating system this
82 # example is intended to show; if you omit this, default value is "posix".
83 #
84 # The generated SGML will show the command line (with the appropriate
85 # command-line prompt for the operating system), execute the command in
86 # a temporary directory with the example files, capture the standard
87 # output from SCons, and insert it into the text as appropriate.
88 # Error output gets passed through to your error output so you
89 # can see if there are any problems executing the command.
90 #
91 from __future__ import generators  ### KEEP FOR COMPATIBILITY FIXERS
92
93 import optparse
94 import os
95 import re
96 import sgmllib
97 import sys
98 import time
99
100 sys.path.append(os.path.join(os.getcwd(), 'QMTest'))
101 sys.path.append(os.path.join(os.getcwd(), 'build', 'QMTest'))
102
103 scons_py = os.path.join('bootstrap', 'src', 'script', 'scons.py')
104 if not os.path.exists(scons_py):
105     scons_py = os.path.join('src', 'script', 'scons.py')
106
107 scons_lib_dir = os.path.join(os.getcwd(), 'bootstrap', 'src', 'engine')
108 if not os.path.exists(scons_lib_dir):
109     scons_lib_dir = os.path.join(os.getcwd(), 'src', 'engine')
110
111 os.environ['SCONS_LIB_DIR'] = scons_lib_dir
112
113 import TestCmd
114
115 # The regular expression that identifies entity references in the
116 # standard sgmllib omits the underscore from the legal characters.
117 # Override it with our own regular expression that adds underscore.
118 sgmllib.entityref = re.compile('&([a-zA-Z][-_.a-zA-Z0-9]*)[^-_a-zA-Z0-9]')
119
120 # Classes for collecting different types of data we're interested in.
121 class DataCollector:
122     """Generic class for collecting data between a start tag and end
123     tag.  We subclass for various types of tags we care about."""
124     def __init__(self):
125         self.data = ""
126     def afunc(self, data):
127         self.data = self.data + data
128
129 class Example(DataCollector):
130     """An SCons example.  This is essentially a list of files that
131     will get written to a temporary directory to collect output
132     from one or more SCons runs."""
133     def __init__(self):
134         DataCollector.__init__(self)
135         self.files = []
136         self.dirs = []
137
138 class File(DataCollector):
139     """A file, that will get written out to a temporary directory
140     for one or more SCons runs."""
141     def __init__(self, name):
142         DataCollector.__init__(self)
143         self.name = name
144
145 class Directory(DataCollector):
146     """A directory, that will get created in a temporary directory
147     for one or more SCons runs."""
148     def __init__(self, name):
149         DataCollector.__init__(self)
150         self.name = name
151
152 class Output(DataCollector):
153     """Where the command output goes.  This is essentially
154     a list of commands that will get executed."""
155     def __init__(self):
156         DataCollector.__init__(self)
157         self.commandlist = []
158
159 class Command(DataCollector):
160     """A tag for where the command output goes.  This is essentially
161     a list of commands that will get executed."""
162     def __init__(self):
163         DataCollector.__init__(self)
164         self.output = None
165
166 Prompt = {
167     'posix' : '% ',
168     'win32' : 'C:\\>'
169 }
170
171 # The magick SCons hackery that makes this work.
172 #
173 # So that our examples can still use the default SConstruct file, we
174 # actually feed the following into SCons via stdin and then have it
175 # SConscript() the SConstruct file.  This stdin wrapper creates a set
176 # of ToolSurrogates for the tools for the appropriate platform.  These
177 # Surrogates print output like the real tools and behave like them
178 # without actually having to be on the right platform or have the right
179 # tool installed.
180 #
181 # The upshot:  The wrapper transparently changes the world out from
182 # under the top-level SConstruct file in an example just so we can get
183 # the command output.
184
185 Stdin = """\
186 import os
187 import re
188 import SCons.Action
189 import SCons.Defaults
190 import SCons.Node.FS
191
192 platform = '%(osname)s'
193
194 Sep = {
195     'posix' : '/',
196     'win32' : '\\\\',
197 }[platform]
198
199
200 #  Slip our own __str__() method into the EntryProxy class used to expand
201 #  $TARGET{S} and $SOURCE{S} to translate the path-name separators from
202 #  what's appropriate for the system we're running on to what's appropriate
203 #  for the example system.
204 orig = SCons.Node.FS.EntryProxy
205 class MyEntryProxy(orig):
206     def __str__(self):
207         return string.replace(str(self._Proxy__subject), os.sep, Sep)
208 SCons.Node.FS.EntryProxy = MyEntryProxy
209
210 # Slip our own RDirs() method into the Node.FS.File class so that the
211 # expansions of $_{CPPINC,F77INC,LIBDIR}FLAGS will have the path-name
212 # separators translated from what's appropriate for the system we're
213 # running on to what's appropriate for the example system.
214 orig_RDirs = SCons.Node.FS.File.RDirs
215 def my_RDirs(self, pathlist, orig_RDirs=orig_RDirs):
216     return map(lambda x: string.replace(str(x), os.sep, Sep),
217                orig_RDirs(self, pathlist))
218 SCons.Node.FS.File.RDirs = my_RDirs
219
220 class Curry:
221     def __init__(self, fun, *args, **kwargs):
222         self.fun = fun
223         self.pending = args[:]
224         self.kwargs = kwargs.copy()
225
226     def __call__(self, *args, **kwargs):
227         if kwargs and self.kwargs:
228             kw = self.kwargs.copy()
229             kw.update(kwargs)
230         else:
231             kw = kwargs or self.kwargs
232
233         return self.fun(*self.pending + args, **kw)
234
235 def Str(target, source, env, cmd=""):
236     result = []
237     for cmd in env.subst_list(cmd, target=target, source=source):
238         result.append(string.join(map(str, cmd)))
239     return string.join(result, '\\n')
240
241 class ToolSurrogate:
242     def __init__(self, tool, variable, func, varlist):
243         self.tool = tool
244         if not isinstance(variable, list):
245             variable = [variable]
246         self.variable = variable
247         self.func = func
248         self.varlist = varlist
249     def __call__(self, env):
250         t = Tool(self.tool)
251         t.generate(env)
252         for v in self.variable:
253             orig = env[v]
254             try:
255                 strfunction = orig.strfunction
256             except AttributeError:
257                 strfunction = Curry(Str, cmd=orig)
258             # Don't call Action() through its global function name, because
259             # that leads to infinite recursion in trying to initialize the
260             # Default Environment.
261             env[v] = SCons.Action.Action(self.func,
262                                          strfunction=strfunction,
263                                          varlist=self.varlist)
264     def __repr__(self):
265         # This is for the benefit of printing the 'TOOLS'
266         # variable through env.Dump().
267         return repr(self.tool)
268
269 def Null(target, source, env):
270     pass
271
272 def Cat(target, source, env):
273     target = str(target[0])
274     f = open(target, "wb")
275     for src in map(str, source):
276         f.write(open(src, "rb").read())
277     f.close()
278
279 def CCCom(target, source, env):
280     target = str(target[0])
281     fp = open(target, "wb")
282     def process(source_file, fp=fp):
283         for line in open(source_file, "rb").readlines():
284             m = re.match(r'#include\s[<"]([^<"]+)[>"]', line)
285             if m:
286                 include = m.group(1)
287                 for d in [str(env.Dir('$CPPPATH')), '.']:
288                     f = os.path.join(d, include)
289                     if os.path.exists(f):
290                         process(f)
291                         break
292             elif line[:11] != "STRIP CCCOM":
293                 fp.write(line)
294     for src in map(str, source):
295         process(src)
296         fp.write('debug = ' + ARGUMENTS.get('debug', '0') + '\\n')
297     fp.close()
298
299 public_class_re = re.compile('^public class (\S+)', re.MULTILINE)
300
301 def JavaCCom(target, source, env):
302     # This is a fake Java compiler that just looks for
303     #   public class FooBar
304     # lines in the source file(s) and spits those out
305     # to .class files named after the class.
306     tlist = list(map(str, target))
307     not_copied = {}
308     for t in tlist:
309        not_copied[t] = 1
310     for src in map(str, source):
311         contents = open(src, "rb").read()
312         classes = public_class_re.findall(contents)
313         for c in classes:
314             for t in [x for x in tlist if x.find(c) != -1]:
315                 open(t, "wb").write(contents)
316                 del not_copied[t]
317     for t in not_copied.keys():
318         open(t, "wb").write("\\n")
319
320 def JavaHCom(target, source, env):
321     tlist = map(str, target)
322     slist = map(str, source)
323     for t, s in zip(tlist, slist):
324         open(t, "wb").write(open(s, "rb").read())
325
326 def find_class_files(arg, dirname, names):
327     class_files = filter(lambda n: n[-6:] == '.class', names)
328     paths = map(lambda n: os.path.join(dirname, n), class_files)
329     arg.extend(paths)
330
331 def JarCom(target, source, env):
332     target = str(target[0])
333     class_files = []
334     for src in map(str, source):
335         os.path.walk(src, find_class_files, class_files)
336     f = open(target, "wb")
337     for cf in class_files:
338         f.write(open(cf, "rb").read())
339     f.close()
340
341 # XXX Adding COLOR, COLORS and PACKAGE to the 'cc' varlist(s) by hand
342 # here is bogus.  It's for the benefit of doc/user/command-line.in, which
343 # uses examples that want  to rebuild based on changes to these variables.
344 # It would be better to figure out a way to do it based on the content of
345 # the generated command-line, or else find a way to let the example markup
346 # language in doc/user/command-line.in tell this script what variables to
347 # add, but that's more difficult than I want to figure out how to do right
348 # now, so let's just use the simple brute force approach for the moment.
349
350 ToolList = {
351     'posix' :   [('cc', ['CCCOM', 'SHCCCOM'], CCCom, ['CCFLAGS', 'CPPDEFINES', 'COLOR', 'COLORS', 'PACKAGE']),
352                  ('link', ['LINKCOM', 'SHLINKCOM'], Cat, []),
353                  ('ar', ['ARCOM', 'RANLIBCOM'], Cat, []),
354                  ('tar', 'TARCOM', Null, []),
355                  ('zip', 'ZIPCOM', Null, []),
356                  ('BitKeeper', 'BITKEEPERCOM', Cat, []),
357                  ('CVS', 'CVSCOM', Cat, []),
358                  ('RCS', 'RCS_COCOM', Cat, []),
359                  ('SCCS', 'SCCSCOM', Cat, []),
360                  ('javac', 'JAVACCOM', JavaCCom, []),
361                  ('javah', 'JAVAHCOM', JavaHCom, []),
362                  ('jar', 'JARCOM', JarCom, []),
363                  ('rmic', 'RMICCOM', Cat, []),
364                 ],
365     'win32' :   [('msvc', ['CCCOM', 'SHCCCOM', 'RCCOM'], CCCom, ['CCFLAGS', 'CPPDEFINES', 'COLOR', 'COLORS', 'PACKAGE']),
366                  ('mslink', ['LINKCOM', 'SHLINKCOM'], Cat, []),
367                  ('mslib', 'ARCOM', Cat, []),
368                  ('tar', 'TARCOM', Null, []),
369                  ('zip', 'ZIPCOM', Null, []),
370                  ('BitKeeper', 'BITKEEPERCOM', Cat, []),
371                  ('CVS', 'CVSCOM', Cat, []),
372                  ('RCS', 'RCS_COCOM', Cat, []),
373                  ('SCCS', 'SCCSCOM', Cat, []),
374                  ('javac', 'JAVACCOM', JavaCCom, []),
375                  ('javah', 'JAVAHCOM', JavaHCom, []),
376                  ('jar', 'JARCOM', JarCom, []),
377                  ('rmic', 'RMICCOM', Cat, []),
378                 ],
379 }
380
381 toollist = ToolList[platform]
382 filter_tools = string.split('%(tools)s')
383 if filter_tools:
384     toollist = [x for x in toollist if x[0] in filter_tools]
385
386 toollist = [ToolSurrogate(*t) for t in toollist]
387
388 toollist.append('install')
389
390 def surrogate_spawn(sh, escape, cmd, args, env):
391     pass
392
393 def surrogate_pspawn(sh, escape, cmd, args, env, stdout, stderr):
394     pass
395
396 SCons.Defaults.ConstructionEnvironment.update({
397     'PLATFORM' : platform,
398     'TOOLS'    : toollist,
399     'SPAWN'    : surrogate_spawn,
400     'PSPAWN'   : surrogate_pspawn,
401 })
402
403 SConscript('SConstruct')
404 """
405
406 # "Commands" that we will execute in our examples.
407 def command_scons(args, c, test, dict):
408     save_vals = {}
409     delete_keys = []
410     try:
411         ce = c.environment
412     except AttributeError:
413         pass
414     else:
415         for arg in c.environment.split():
416             key, val = arg.split('=')
417             try:
418                 save_vals[key] = os.environ[key]
419             except KeyError:
420                 delete_keys.append(key)
421             os.environ[key] = val
422     test.run(interpreter = sys.executable,
423              program = scons_py,
424              # We use ToolSurrogates to capture win32 output by "building"
425              # examples using a fake win32 tool chain.  Suppress the
426              # warnings that come from the new revamped VS support so
427              # we can build doc on (Linux) systems that don't have
428              # Visual C installed.
429              arguments = '--warn=no-visual-c-missing -f - ' + ' '.join(args),
430              chdir = test.workpath('WORK'),
431              stdin = Stdin % dict)
432     os.environ.update(save_vals)
433     for key in delete_keys:
434         del(os.environ[key])
435     out = test.stdout()
436     out = out.replace(test.workpath('ROOT'), '')
437     out = out.replace(test.workpath('WORK/SConstruct'),
438                               '/home/my/project/SConstruct')
439     lines = out.split('\n')
440     if lines:
441         while lines[-1] == '':
442             lines = lines[:-1]
443     #err = test.stderr()
444     #if err:
445     #    sys.stderr.write(err)
446     return lines
447
448 def command_touch(args, c, test, dict):
449     if args[0] == '-t':
450         t = int(time.mktime(time.strptime(args[1], '%Y%m%d%H%M')))
451         times = (t, t)
452         args = args[2:]
453     else:
454         time.sleep(1)
455         times = None
456     for file in args:
457         if not os.path.isabs(file):
458             file = os.path.join(test.workpath('WORK'), file)
459         if not os.path.exists(file):
460             open(file, 'wb')
461         os.utime(file, times)
462     return []
463
464 def command_edit(args, c, test, dict):
465     try:
466         add_string = c.edit[:]
467     except AttributeError:
468         add_string = 'void edit(void) { ; }\n'
469     if add_string[-1] != '\n':
470         add_string = add_string + '\n'
471     for file in args:
472         if not os.path.isabs(file):
473             file = os.path.join(test.workpath('WORK'), file)
474         contents = open(file, 'rb').read()
475         open(file, 'wb').write(contents + add_string)
476     return []
477
478 def command_ls(args, c, test, dict):
479     def ls(a):
480         return ['  '.join(sorted([x for x in os.listdir(a) if x[0] != '.']))]
481     if args:
482         l = []
483         for a in args:
484             l.extend(ls(test.workpath('WORK', a)))
485         return l
486     else:
487         return ls(test.workpath('WORK'))
488
489 def command_sleep(args, c, test, dict):
490     time.sleep(int(args[0]))
491
492 CommandDict = {
493     'scons' : command_scons,
494     'touch' : command_touch,
495     'edit'  : command_edit,
496     'ls'    : command_ls,
497     'sleep' : command_sleep,
498 }
499
500 def ExecuteCommand(args, c, t, dict):
501     try:
502         func = CommandDict[args[0]]
503     except KeyError:
504         func = lambda args, c, t, dict: []
505     return func(args[1:], c, t, dict)
506
507 class MySGML(sgmllib.SGMLParser):
508     """A subclass of the standard Python 2.2 sgmllib SGML parser.
509
510     This extends the standard sgmllib parser to recognize, and do cool
511     stuff with, the added tags that describe our SCons examples,
512     commands, and other stuff.
513
514     Note that this doesn't work with the 1.5.2 sgmllib module, because
515     that didn't have the ability to work with ENTITY declarations.
516     """
517     def __init__(self, outfp):
518         sgmllib.SGMLParser.__init__(self)
519         self.examples = {}
520         self.afunclist = []
521         self.outfp = outfp
522
523     # The first set of methods here essentially implement pass-through
524     # handling of most of the stuff in an SGML file.  We're really
525     # only concerned with the tags specific to SCons example processing,
526     # the methods for which get defined below.
527
528     def handle_data(self, data):
529         try:
530             f = self.afunclist[-1]
531         except IndexError:
532             self.outfp.write(data)
533         else:
534             f(data)
535
536     def handle_comment(self, data):
537         self.outfp.write('<!--' + data + '-->')
538
539     def handle_decl(self, data):
540         self.outfp.write('<!' + data + '>')
541
542     def unknown_starttag(self, tag, attrs):
543         try:
544             f = self.example.afunc
545         except AttributeError:
546             f = self.outfp.write
547         if not attrs:
548             f('<' + tag + '>')
549         else:
550             f('<' + tag)
551             for name, value in attrs:
552                 f(' ' + name + '=' + '"' + value + '"')
553             f('>')
554
555     def unknown_endtag(self, tag):
556         self.outfp.write('</' + tag + '>')
557
558     def unknown_entityref(self, ref):
559         self.outfp.write('&' + ref + ';')
560
561     def unknown_charref(self, ref):
562         self.outfp.write('&#' + ref + ';')
563
564     # Here is where the heavy lifting begins.  The following methods
565     # handle the begin-end tags of our SCons examples.
566
567     def start_scons_example(self, attrs):
568         t = [t for t in attrs if t[0] == 'name']
569         if t:
570             name = t[0][1]
571             try:
572                e = self.examples[name]
573             except KeyError:
574                e = self.examples[name] = Example()
575         else:
576             e = Example()
577         for name, value in attrs:
578             setattr(e, name, value)
579         self.e = e
580         self.afunclist.append(e.afunc)
581
582     def end_scons_example(self):
583         e = self.e
584         files = [f for f in e.files if f.printme]
585         if files:
586             self.outfp.write('<programlisting>')
587             for f in files:
588                 if f.printme:
589                     i = len(f.data) - 1
590                     while f.data[i] == ' ':
591                         i = i - 1
592                     output = f.data[:i+1].replace('__ROOT__', '')
593                     output = output.replace('<', '&lt;')
594                     output = output.replace('>', '&gt;')
595                     self.outfp.write(output)
596             if e.data and e.data[0] == '\n':
597                 e.data = e.data[1:]
598             self.outfp.write(e.data + '</programlisting>')
599         delattr(self, 'e')
600         self.afunclist = self.afunclist[:-1]
601
602     def start_file(self, attrs):
603         try:
604             e = self.e
605         except AttributeError:
606             self.error("<file> tag outside of <scons_example>")
607         t = [t for t in attrs if t[0] == 'name']
608         if not t:
609             self.error("no <file> name attribute found")
610         try:
611             e.prefix
612         except AttributeError:
613             e.prefix = e.data
614             e.data = ""
615         f = File(t[0][1])
616         f.printme = None
617         for name, value in attrs:
618             setattr(f, name, value)
619         e.files.append(f)
620         self.afunclist.append(f.afunc)
621
622     def end_file(self):
623         self.e.data = ""
624         self.afunclist = self.afunclist[:-1]
625
626     def start_directory(self, attrs):
627         try:
628             e = self.e
629         except AttributeError:
630             self.error("<directory> tag outside of <scons_example>")
631         t = [t for t in attrs if t[0] == 'name']
632         if not t:
633             self.error("no <directory> name attribute found")
634         try:
635             e.prefix
636         except AttributeError:
637             e.prefix = e.data
638             e.data = ""
639         d = Directory(t[0][1])
640         for name, value in attrs:
641             setattr(d, name, value)
642         e.dirs.append(d)
643         self.afunclist.append(d.afunc)
644
645     def end_directory(self):
646         self.e.data = ""
647         self.afunclist = self.afunclist[:-1]
648
649     def start_scons_example_file(self, attrs):
650         t = [t for t in attrs if t[0] == 'example']
651         if not t:
652             self.error("no <scons_example_file> example attribute found")
653         exname = t[0][1]
654         try:
655             e = self.examples[exname]
656         except KeyError:
657             self.error("unknown example name '%s'" % exname)
658         fattrs = [t for t in attrs if t[0] == 'name']
659         if not fattrs:
660             self.error("no <scons_example_file> name attribute found")
661         fname = fattrs[0][1]
662         f = [f for f in e.files if f.name == fname]
663         if not f:
664             self.error("example '%s' does not have a file named '%s'" % (exname, fname))
665         self.f = f[0]
666
667     def end_scons_example_file(self):
668         f = self.f
669         self.outfp.write('<programlisting>')
670         self.outfp.write(f.data + '</programlisting>')
671         delattr(self, 'f')
672
673     def start_scons_output(self, attrs):
674         t = [t for t in attrs if t[0] == 'example']
675         if not t:
676             self.error("no <scons_output> example attribute found")
677         exname = t[0][1]
678         try:
679             e = self.examples[exname]
680         except KeyError:
681             self.error("unknown example name '%s'" % exname)
682         # Default values for an example.
683         o = Output()
684         o.preserve = None
685         o.os = 'posix'
686         o.tools = ''
687         o.e = e
688         # Locally-set.
689         for name, value in attrs:
690             setattr(o, name, value)
691         self.o = o
692         self.afunclist.append(o.afunc)
693
694     def end_scons_output(self):
695         # The real raison d'etre for this script, this is where we
696         # actually execute SCons to fetch the output.
697         o = self.o
698         e = o.e
699         t = TestCmd.TestCmd(workdir='', combine=1)
700         if o.preserve:
701             t.preserve()
702         t.subdir('ROOT', 'WORK')
703         t.rootpath = t.workpath('ROOT').replace('\\', '\\\\')
704
705         for d in e.dirs:
706             dir = t.workpath('WORK', d.name)
707             if not os.path.exists(dir):
708                 os.makedirs(dir)
709
710         for f in e.files:
711             i = 0
712             while f.data[i] == '\n':
713                 i = i + 1
714             lines = f.data[i:].split('\n')
715             i = 0
716             while lines[0][i] == ' ':
717                 i = i + 1
718             lines = [l[i:] for l in lines]
719             path = f.name.replace('__ROOT__', t.rootpath)
720             if not os.path.isabs(path):
721                 path = t.workpath('WORK', path)
722             dir, name = os.path.split(path)
723             if dir and not os.path.exists(dir):
724                 os.makedirs(dir)
725             content = '\n'.join(lines)
726             content = content.replace('__ROOT__', t.rootpath)
727             path = t.workpath('WORK', path)
728             t.write(path, content)
729             if hasattr(f, 'chmod'):
730                 os.chmod(path, int(f.chmod, 0))
731
732         i = len(o.prefix)
733         while o.prefix[i-1] != '\n':
734             i = i - 1
735
736         self.outfp.write('<screen>' + o.prefix[:i])
737         p = o.prefix[i:]
738
739         # Regular expressions for making the doc output consistent,
740         # regardless of reported addresses or Python version.
741
742         # Massage addresses in object repr strings to a constant.
743         address_re = re.compile(r' at 0x[0-9a-fA-F]*\>')
744
745         # Massage file names in stack traces (sometimes reported as absolute
746         # paths) to a consistent relative path.
747         engine_re = re.compile(r' File ".*/src/engine/SCons/')
748
749         # Python 2.5 changed the stack trace when the module is read
750         # from standard input from read "... line 7, in ?" to
751         # "... line 7, in <module>".
752         file_re = re.compile(r'^( *File ".*", line \d+, in) \?$', re.M)
753
754         # Python 2.6 made UserList a new-style class, which changes the
755         # AttributeError message generated by our NodeList subclass.
756         nodelist_re = re.compile(r'(AttributeError:) NodeList instance (has no attribute \S+)')
757
758         for c in o.commandlist:
759             self.outfp.write(p + Prompt[o.os])
760             d = c.data.replace('__ROOT__', '')
761             self.outfp.write('<userinput>' + d + '</userinput>\n')
762
763             e = c.data.replace('__ROOT__', t.workpath('ROOT'))
764             args = e.split()
765             lines = ExecuteCommand(args, c, t, {'osname':o.os, 'tools':o.tools})
766             content = None
767             if c.output:
768                 content = c.output
769             elif lines:
770                 content = ( '\n' + p).join(lines)
771             if content:
772                 content = address_re.sub(r' at 0x700000&gt;', content)
773                 content = engine_re.sub(r' File "bootstrap/src/engine/SCons/', content)
774                 content = file_re.sub(r'\1 <module>', content)
775                 content = nodelist_re.sub(r"\1 'NodeList' object \2", content)
776                 content = content.replace('<', '&lt;')
777                 content = content.replace('>', '&gt;')
778                 self.outfp.write(p + content + '\n')
779
780         if o.data[0] == '\n':
781             o.data = o.data[1:]
782         self.outfp.write(o.data + '</screen>')
783         delattr(self, 'o')
784         self.afunclist = self.afunclist[:-1]
785
786     def start_scons_output_command(self, attrs):
787         try:
788             o = self.o
789         except AttributeError:
790             self.error("<scons_output_command> tag outside of <scons_output>")
791         try:
792             o.prefix
793         except AttributeError:
794             o.prefix = o.data
795             o.data = ""
796         c = Command()
797         for name, value in attrs:
798             setattr(c, name, value)
799         o.commandlist.append(c)
800         self.afunclist.append(c.afunc)
801
802     def end_scons_output_command(self):
803         self.o.data = ""
804         self.afunclist = self.afunclist[:-1]
805
806     def start_sconstruct(self, attrs):
807         f = File('')
808         self.f = f
809         self.afunclist.append(f.afunc)
810
811     def end_sconstruct(self):
812         f = self.f
813         self.outfp.write('<programlisting>')
814         output = f.data.replace('__ROOT__', '')
815         self.outfp.write(output + '</programlisting>')
816         delattr(self, 'f')
817         self.afunclist = self.afunclist[:-1]
818
819 def process(filename):
820     if filename == '-':
821         f = sys.stdin
822     else:
823         try:
824             f = open(filename, 'r')
825         except EnvironmentError, e:
826             sys.stderr.write('%s: %s\n' % (filename, msg))
827             return 1
828
829     data = f.read()
830     if f is not sys.stdin:
831         f.close()
832
833     if data.startswith('<?xml '):
834         first_line, data = data.split('\n', 1)
835         sys.stdout.write(first_line + '\n')
836
837     x = MySGML(sys.stdout)
838     for c in data:
839         x.feed(c)
840     x.close()
841
842     return 0
843
844 def main(argv=None):
845     if argv is None:
846         argv = sys.argv
847
848     parser = optparse.OptionParser()
849     opts, args = parser.parse_args(argv[1:])
850
851     if not args:
852         args = ['-']
853
854     for arg in args:
855         process(arg)
856
857 if __name__ == "__main__":
858     sys.exit(main())
859
860 # Local Variables:
861 # tab-width:4
862 # indent-tabs-mode:nil
863 # End:
864 # vim: set expandtab tabstop=4 shiftwidth=4: