[PATCH 3/3] Refactor CheetahWrapper tests to locate my local cheetah/cheetah-compile in my PATH

"R. Tyler Ballance" <[email protected]> Mon, 9 Nov 2009 09:13:18 -0800
Newsgroups gmane.comp.python.cheetah
Message-ID <[email protected]>
Adding the "buildandrun" shortcut script so to easily rerun the
full test suite locally; usage: ./buildandrun cheetah/Tests/Test.py
---
 buildandrun                     |   66 +++++++++++++++++++++++++++++++++++++++
 cheetah/Tests/CheetahWrapper.py |   27 +++++++++++++---
 2 files changed, 88 insertions(+), 5 deletions(-)
 create mode 100755 buildandrun

diff --git a/buildandrun b/buildandrun
new file mode 100755
index 0000000..d3b4e42
--- /dev/null
+++ b/buildandrun
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+
+import logging
+import os
+import os.path
+import subprocess
+import sys
+
+from optparse import OptionParser
+
+if os.getenv('DEBUG'):
+    logging.basicConfig(level=logging.DEBUG)
+
+def recursiverm(d):
+    for root, dirs, files in os.walk(d):
+        for f in files:
+            f = root + os.path.sep + f
+            logging.debug('Removing file: %s' % f)
+            os.unlink(f)
+
+def main():
+    op = OptionParser()
+    op.add_option('-c', '--clean', dest='clean', action='store_true',
+            help='Remove the contents of the build directory')
+    opts, args = op.parse_args()
+    if not args:
+        print('Specify a test script')
+        return
+
+
+    if opts.clean:
+        logging.debug('Removing build/')
+        recursiverm('build')
+
+    logging.debug('Building Cheetah')
+    rc = subprocess.call(('python', 'setup.py', 'build'))
+
+    if rc:
+        logging.debug('Build failed!')
+        return
+
+    logging.debug('Adjusting PATH and PYTHONPATH')
+    cwd = os.getcwd()
+
+    libdir = None
+    scriptdir = None
+
+    for sub in os.listdir('build'):
+        if sub.startswith('scripts'):
+            scriptdir = os.path.sep.join((cwd, 'build', sub))
+        if sub.startswith('lib'):
+            libdir = os.path.sep.join((cwd, 'build', sub))
+
+    newpath = '%s:%s' % (scriptdir, os.getenv('PATH'))
+    logging.debug('Setting PATH to: %s' % newpath)
+    os.putenv('PATH', newpath)
+    logging.debug('Setting PYTHONPATH to: %s' % libdir)
+    os.putenv('PYTHONPATH', libdir)
+
+    rc = subprocess.call( ['python',] + args )
+
+
+
+if __name__ == '__main__':
+    main()
+
diff --git a/cheetah/Tests/CheetahWrapper.py b/cheetah/Tests/CheetahWrapper.py
index 809be40..e44edef 100644
--- a/cheetah/Tests/CheetahWrapper.py
+++ b/cheetah/Tests/CheetahWrapper.py
@@ -12,6 +12,8 @@ Besides unittest usage, recognizes the following command-line options:
         Show the output of each subcommand.  (Normally suppressed.)
 '''
 import os
+import os.path
+import pdb
 import re                                     # Used by listTests.
 import shutil
 import sys
@@ -24,10 +26,13 @@ from Cheetah.CheetahWrapper import CheetahWrapper  # Used by NoBackup.
 try:
     from subprocess import Popen, PIPE, STDOUT
     class Popen4(Popen):
-        def __init__(self, cmd, bufsize=-1):
-            super(Popen4, self).__init__(cmd, bufsize=bufsize,
-                                         shell=True, close_fds=True,
-                                         stdin=PIPE, stdout=PIPE, stderr=STDOUT)
+        def __init__(self, cmd, bufsize=-1, shell=True, close_fds=True,
+                        stdin=PIPE, stdout=PIPE, stderr=STDOUT, **kwargs):
+
+            super(Popen4, self).__init__(cmd, bufsize=bufsize, shell=shell,
+                            close_fds=close_fds, stdin=stdin, stdout=stdout,
+                            stderr=stderr, **kwargs)
+
             self.tochild = self.stdin
             self.fromchild = self.stdout
             self.childerr = self.stderr
@@ -152,6 +157,17 @@ Found %(result)r"""
         msg = "backup file exists in spite of --nobackup: %s" % path
         self.failIf(exists, msg)
 
+    def locate_command(self, cmd):
+        paths = os.getenv('PATH')
+        if not paths:
+            return cmd
+        parts = cmd.split(' ')
+        paths = paths.split(':')
+        for p in paths:
+            p = p + os.path.sep + parts[0]
+            if os.path.isfile(p):
+                return ' '.join([p] + parts[1:])
+        return ' '.join(parts)
 
     def assertWin32Subprocess(self, cmd):
         _in, _out = os.popen4(cmd)
@@ -163,7 +179,8 @@ Found %(result)r"""
         return rc, output
 
     def assertPosixSubprocess(self, cmd):
-        process = Popen4(cmd)
+        cmd = self.locate_command(cmd)
+        process = Popen4(cmd, env=os.environ)
         process.tochild.close()
         output = process.fromchild.read()
         status = process.wait()
-- 
1.6.4.2


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july