bos@3: #!/usr/bin/python
bos@4: #
bos@4: # This program takes something that resembles a shell script and runs
bos@4: # it, spitting input (commands from the script) and output into text
bos@4: # files, for use in examples.
bos@3: 
bos@3: import cStringIO
bos@3: import os
bos@3: import pty
bos@3: import re
bos@4: import shutil
bos@3: import sys
bos@4: import tempfile
bos@4: import time
bos@3: 
bos@4: def tex_escape(s):
bos@4:     if '\\' in s:
bos@4:         s = s.replace('\\', '\\\\')
bos@4:     if '{' in s:
bos@4:         s = s.replace('{', '\\{')
bos@4:     if '}' in s:
bos@4:         s = s.replace('}', '\\}')
bos@4:     return s
bos@4:         
bos@3: class example:
bos@4:     shell = '/bin/bash'
bos@4:     pi_re = re.compile('#\$\s*(name):\s*(.*)$')
bos@4:     
bos@3:     def __init__(self, name):
bos@3:         self.name = name
bos@3: 
bos@3:     def parse(self):
bos@4:         '''yield each hunk of input from the file.'''
bos@3:         fp = open(self.name)
bos@3:         cfp = cStringIO.StringIO()
bos@3:         for line in fp:
bos@3:             cfp.write(line)
bos@3:             if not line.rstrip().endswith('\\'):
bos@3:                 yield cfp.getvalue()
bos@3:                 cfp.seek(0)
bos@3:                 cfp.truncate()
bos@3:         
bos@3:     def status(self, s):
bos@3:         sys.stdout.write(s)
bos@3:         if not s.endswith('\n'):
bos@3:             sys.stdout.flush()
bos@3: 
bos@4:     def drain(self, ifp, ofp):
bos@4:         while True:
bos@4:             s = ifp.read(4096)
bos@4:             if not s: break
bos@4:             if ofp: ofp.write(tex_escape(s))
bos@4:         
bos@3:     def run(self):
bos@3:         ofp = None
bos@4:         basename = os.path.basename(self.name)
bos@4:         self.status('running %s ' % basename)
bos@4:         tmpdir = tempfile.mkdtemp(prefix=basename)
bos@4:         try:
bos@4:             for hunk in self.parse():
bos@4:                 # is this line a processing instruction?
bos@4:                 m = self.pi_re.match(hunk)
bos@4:                 if m:
bos@4:                     pi, rest = m.groups()
bos@4:                     if pi == 'name':
bos@4:                         self.status('.')
bos@4:                         out = rest
bos@4:                         assert os.sep not in out
bos@4:                         if out:
bos@4:                             ofp = open('%s.%s.out' % (self.name, out), 'w')
bos@4:                         else:
bos@4:                             ofp = None
bos@3:                 else:
bos@4:                     # it's something we should execute
bos@4:                     cin, cout = os.popen4('cd %s; %s' % (tmpdir, hunk))
bos@4:                     cin.close()
bos@4:                     if ofp:
bos@4:                         # first, print the command we ran
bos@4:                         if not hunk.startswith('#'):
bos@4:                             nl = hunk.endswith('\n')
bos@4:                             hunk = ('$ \\textbf{%s}' %
bos@4:                                     tex_escape(hunk.rstrip('\n')))
bos@4:                             if nl: hunk += '\n'
bos@4:                         ofp.write(hunk)
bos@4:                     # then its output
bos@4:                     self.drain(cout, ofp)
bos@4:             self.status('\n')
bos@4:         finally:
bos@4:             os.wait()
bos@4:             shutil.rmtree(tmpdir)
bos@3: 
bos@3: def main(path='.'):
bos@3:     args = sys.argv[1:]
bos@3:     if args:
bos@3:         for a in args:
bos@3:             example(a).run()
bos@3:         return
bos@3:     for name in os.listdir(path):
bos@3:         if name == 'run-example' or name.startswith('.'): continue
bos@3:         if name.endswith('.out') or name.endswith('~'): continue
bos@3:         example(os.path.join(path, name)).run()
bos@4:     print >> open(os.path.join(path, '.run'), 'w'), time.asctime()
bos@3: 
bos@3: if __name__ == '__main__':
bos@3:     main()