hgbook
changeset 78:a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Mon Sep 04 14:20:05 2006 -0700 (2006-09-04) |
parents | 773f4a9e7975 |
children | 53427f786a0f |
files | en/examples/run-example |
line diff
1.1 --- a/en/examples/run-example Mon Sep 04 11:57:31 2006 -0700 1.2 +++ b/en/examples/run-example Mon Sep 04 14:20:05 2006 -0700 1.3 @@ -6,6 +6,7 @@ 1.4 1.5 import cStringIO 1.6 import errno 1.7 +import getopt 1.8 import os 1.9 import pty 1.10 import re 1.11 @@ -40,8 +41,9 @@ 1.12 prompt = '__run_example_prompt__ ' 1.13 pi_re = re.compile(r'#\$\s*(name):\s*(.*)$') 1.14 1.15 - def __init__(self, name): 1.16 + def __init__(self, name, verbose): 1.17 self.name = name 1.18 + self.verbose = verbose 1.19 1.20 def parse(self): 1.21 '''yield each hunk of input from the file.''' 1.22 @@ -60,19 +62,33 @@ 1.23 sys.stdout.flush() 1.24 1.25 def send(self, s): 1.26 + if self.verbose: 1.27 + print >> sys.stderr, '>', self.debugrepr(s) 1.28 while s: 1.29 count = os.write(self.cfd, s) 1.30 s = s[count:] 1.31 1.32 + def debugrepr(self, s): 1.33 + rs = repr(s) 1.34 + limit = 60 1.35 + if len(rs) > limit: 1.36 + return ('%s%s ... [%d bytes]' % (rs[:limit], rs[0], len(s))) 1.37 + else: 1.38 + return rs 1.39 + 1.40 def receive(self): 1.41 out = cStringIO.StringIO() 1.42 while True: 1.43 try: 1.44 + if self.verbose: 1.45 + sys.stderr.write('< ') 1.46 s = os.read(self.cfd, 1024) 1.47 except OSError, err: 1.48 if err.errno == errno.EIO: 1.49 return '' 1.50 raise 1.51 + if self.verbose: 1.52 + print >> sys.stderr, self.debugrepr(s) 1.53 out.write(s) 1.54 s = out.getvalue() 1.55 if s.endswith(self.prompt): 1.56 @@ -90,6 +106,12 @@ 1.57 basename = os.path.basename(self.name) 1.58 self.status('running %s ' % basename) 1.59 tmpdir = tempfile.mkdtemp(prefix=basename) 1.60 + 1.61 + rcfile = os.path.join(tmpdir, '.hgrc') 1.62 + rcfp = open(rcfile, 'w') 1.63 + print >> rcfp, '[ui]' 1.64 + print >> rcfp, "username = Bryan O'Sullivan <bos@serpentine.com>" 1.65 + 1.66 rcfile = os.path.join(tmpdir, '.bashrc') 1.67 rcfp = open(rcfile, 'w') 1.68 print >> rcfp, 'PS1="%s"' % self.prompt 1.69 @@ -174,7 +196,11 @@ 1.70 shutil.rmtree(tmpdir) 1.71 1.72 def main(path='.'): 1.73 - args = sys.argv[1:] 1.74 + opts, args = getopt.getopt(sys.argv[1:], 'v', ['verbose']) 1.75 + verbose = False 1.76 + for o, a in opts: 1.77 + if o in ('-v', '--verbose'): 1.78 + verbose = True 1.79 errs = 0 1.80 if args: 1.81 for a in args: 1.82 @@ -185,7 +211,7 @@ 1.83 errs += 1 1.84 continue 1.85 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111: 1.86 - if example(a).run(): 1.87 + if example(a, verbose).run(): 1.88 errs += 1 1.89 else: 1.90 print >> sys.stderr, '%s: not a file, or not executable' % a 1.91 @@ -198,7 +224,7 @@ 1.92 pathname = os.path.join(path, name) 1.93 st = os.lstat(pathname) 1.94 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111: 1.95 - if example(pathname).run(): 1.96 + if example(pathname, verbose).run(): 1.97 errs += 1 1.98 print >> open(os.path.join(path, '.run'), 'w'), time.asctime() 1.99 return errs