bos@67: #!/usr/bin/env 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@6: import signal
bos@36: import stat
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@70:     shell = '/usr/bin/env bash'
bos@6:     prompt = '__run_example_prompt__\n'
bos@71:     pi_re = re.compile(r'#\$\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@6:     def send(self, s):
bos@6:         self.cfp.write(s)
bos@6:         self.cfp.flush()
bos@6: 
bos@6:     def receive(self):
bos@6:         out = cStringIO.StringIO()
bos@4:         while True:
bos@6:             s = self.cfp.readline().replace('\r\n', '\n')
bos@6:             if not s or s == self.prompt:
bos@6:                 break
bos@6:             out.write(s)
bos@6:         return out.getvalue()
bos@4:         
bos@6:     def sendreceive(self, s):
bos@6:         self.send(s)
bos@6:         r = self.receive()
bos@6:         if r.startswith(s):
bos@6:             r = r[len(s):]
bos@6:         return r
bos@6:     
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@6:         rcfile = os.path.join(tmpdir, '.bashrc')
bos@6:         rcfp = open(rcfile, 'w')
bos@6:         print >> rcfp, 'PS1="%s"' % self.prompt
bos@6:         print >> rcfp, 'unset HISTFILE'
bos@19:         print >> rcfp, 'export EXAMPLE_DIR="%s"' % os.getcwd()
bos@6:         print >> rcfp, 'export LANG=C'
bos@6:         print >> rcfp, 'export LC_ALL=C'
bos@6:         print >> rcfp, 'export TZ=GMT'
bos@6:         print >> rcfp, 'export HGRC="%s/.hgrc"' % tmpdir
bos@6:         print >> rcfp, 'export HGRCPATH=$HGRC'
bos@6:         print >> rcfp, 'cd %s' % tmpdir
bos@6:         rcfp.close()
bos@68:         sys.stdout.flush()
bos@68:         sys.stderr.flush()
bos@6:         pid, fd = pty.fork()
bos@6:         if pid == 0:
bos@70:             cmdline = ['/usr/bin/env', 'bash', '--noediting', '--noprofile',
bos@70:                        '--norc']
bos@68:             try:
bos@68:                 os.execv(cmdline[0], cmdline)
bos@68:             except OSError, err:
bos@68:                 print >> sys.stderr, '%s: %s' % (cmdline[0], err.strerror)
bos@68:                 sys.stderr.flush()
bos@68:                 os._exit(0)
bos@6:         self.cfp = os.fdopen(fd, 'w+')
bos@4:         try:
bos@71:             clean_exit = True
bos@71:             try:
bos@71:                 # setup env and prompt
bos@71:                 self.sendreceive('source %s\n\n' % rcfile)
bos@71:                 for hunk in self.parse():
bos@71:                     # is this line a processing instruction?
bos@71:                     m = self.pi_re.match(hunk)
bos@71:                     if m:
bos@71:                         pi, rest = m.groups()
bos@71:                         if pi == 'name':
bos@71:                             self.status('.')
bos@71:                             out = rest
bos@71:                             assert os.sep not in out
bos@71:                             if out:
bos@71:                                 ofp = open('%s.%s.out' % (self.name, out), 'w')
bos@71:                             else:
bos@71:                                 ofp = None
bos@71:                     elif hunk.strip():
bos@71:                         # it's something we should execute
bos@71:                         output = self.sendreceive(hunk)
bos@71:                         if not ofp:
bos@71:                             continue
bos@71:                         # first, print the command we ran
bos@71:                         if not hunk.startswith('#'):
bos@71:                             nl = hunk.endswith('\n')
bos@71:                             hunk = ('$ \\textbf{%s}' %
bos@71:                                     tex_escape(hunk.rstrip('\n')))
bos@71:                             if nl: hunk += '\n'
bos@71:                         ofp.write(hunk)
bos@71:                         # then its output
bos@71:                         ofp.write(tex_escape(output))
bos@71:                 self.status('\n')
bos@71:                 open(self.name + '.run', 'w')
bos@71:             except:
bos@71:                 clean_exit = False
bos@71:                 raise
bos@4:         finally:
bos@71:             if clean_exit:
bos@71:                 try:
bos@71:                     output = self.sendreceive('exit\n')
bos@71:                     if ofp:
bos@71:                         ofp.write(output)
bos@71:                     self.cfp.close()
bos@71:                 except IOError:
bos@71:                     pass
bos@6:             os.kill(pid, signal.SIGTERM)
bos@71:             time.sleep(0.1)
bos@71:             os.kill(pid, signal.SIGKILL)
bos@71:             pid, rc = os.wait()
bos@71:             if rc:
bos@71:                 if os.WIFEXITED(rc):
bos@71:                     print >> sys.stderr, '(exit %s)' % os.WEXITSTATUS(rc)
bos@71:                 elif os.WIFSIGNALED(rc):
bos@71:                     print >> sys.stderr, '(signal %s)' % os.WTERMSIG(rc)
bos@4:             shutil.rmtree(tmpdir)
bos@71:             return rc
bos@3: 
bos@3: def main(path='.'):
bos@3:     args = sys.argv[1:]
bos@71:     errs = 0
bos@3:     if args:
bos@3:         for a in args:
bos@71:             if example(a).run():
bos@71:                 errs += 1
bos@71:         return errs
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@45:         if name.endswith('.run'): continue
bos@19:         pathname = os.path.join(path, name)
bos@36:         st = os.lstat(pathname)
bos@36:         if stat.S_ISREG(st.st_mode) and st.st_mode & 0111:
bos@71:             if example(pathname).run():
bos@71:                 errs += 1
bos@4:     print >> open(os.path.join(path, '.run'), 'w'), time.asctime()
bos@71:     return errs
bos@3: 
bos@3: if __name__ == '__main__':
bos@71:     sys.exit(main())