[Twisted-Python] Re: [Twisted-commits] r16495 - test that scripts wrapped in a CGIDirectory resource actually execute
On Tue, 28 Mar 2006 22:29:13 -0700, David Reid <dreid@wolfwood.twistedmatrix.com> wrote:
Author: dreid Date: Tue Mar 28 22:29:11 2006 New Revision: 16495
Modified: branches/childNotFound-1596/twisted/web2/test/test_cgi.py Log: test that scripts wrapped in a CGIDirectory resource actually execute
Modified: branches/childNotFound-1596/twisted/web2/test/test_cgi.py ============================================================================== --- branches/childNotFound-1596/twisted/web2/test/test_cgi.py (original) +++ branches/childNotFound-1596/twisted/web2/test/test_cgi.py Tue Mar 28 22:29:11 2006 @@ -161,7 +161,7 @@
self.failUnless(isinstance(resource, (twcgi.CGIScript,)))
- def testAnotherDirectory(self): + def testSubDirectory(self): resource, segments = self.root.locateChild(None, ('directory', 'paths', 'that', @@ -170,4 +170,46 @@
self.failUnless(isinstance(resource, twcgi.CGIDirectory))
- + def createScript(self, filename): + cgiFile = open(filename, 'wt') + cgiFile.write("#!%s\n\n%s" % (sys.executable, + DUMMY_CGI)) + cgiFile.close() + os.chmod(filename, 0700) + + def testScriptsExecute(self): + cgiBinDir = os.path.abspath(self.mktemp()) + os.mkdir(cgiBinDir) + root = twcgi.CGIDirectory(cgiBinDir) + + self.createScript(os.path.join(cgiBinDir, 'dummy')) + + cgiSubDir = os.path.join(cgiBinDir, 'sub') + os.mkdir(cgiSubDir) + + self.createScript(os.path.join(cgiSubDir, 'dummy')) + + self.p = reactor.listenTCP(0, channel.HTTPFactory(server.Site(root))) + portnum = self.p.getHost().port + + def _firstResponse(res): + self.failUnlessEqual(res, "cgi output%s" % os.linesep) + + return client.getPage('http://localhost:%d/sub/dummy' % portnum) + + def _secondResponse(res): + self.failUnlessEqual(res, "cgi output%s" % os.linesep) + + def _cleanup(res): + d = self.p.stopListening() + d.addCallback(lambda ign: res) + return d + + d = client.getPage('http://localhost:%d/dummy' % portnum) + + d.addCallback(_firstResponse + ).addCallback(_secondResponse + ).addBoth(_cleanup) + + return d + testScriptsExecute.timeout=10
Tabs? Jean-Paul
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 For general discussion, I have attached a pre-commit hook that rejects commits of .py files containing leading tabs on any line. It does not do anything to files not ending in .py. It does check for tabs mixed with spaces. It ignores tabs occurring anywhere but the leading whitespace. Here's the regex: _tabs = re.compile(r'^\s*\t\s*.*$') I have used this, albeit lightly. C Jean-Paul Calderone wrote:
Tabs?
==== #!/usr/bin/env python """This is a pre-commit hook that checks whether py files being committed contain tabs in their significant whitespace. TODO?: check for a file property that says to ignore the significant tabs, in cases where they're actually needed (although I can't think of any). """ import sys import re from svn import core, fs, delta, repos _tabs = re.compile(r'^\s*\t\s*.*$') class ChangeReceiver(delta.Editor): def __init__(self, txn_root, base_root, pool): self.txn_root = txn_root self.base_root = base_root self.pool = pool def add_file(self, path, parent_baton, copyfrom_path, copyfrom_revision, file_pool): return [0, path] def open_file(self, path, parent_baton, base_revision, file_pool): return [0, path] def apply_textdelta(self, file_baton, base_checksum): file_baton[0] = 1 # no handler return None def close_file(self, file_baton, text_checksum): changed, path = file_baton if len(path) < 3 or path.lower()[-3:] != '.py' or not changed: # This is not a .py file, don't care about tabs # TODO - only look inside trunk return # Read the file contents through a tab-finder subpool = core.svn_pool_create(self.pool) stream = core.Stream(fs.file_contents(self.txn_root, path, subpool)) data = stream.read() # core.SVN_STREAM_CHUNK_SIZE) for line in data.splitlines(): if _tabs.match(line): core.svn_pool_destroy(subpool) msg = ("Python file contains lines that begin with tabs: '%s'\n" "There may be others as well." % (path,)) sys.stderr.write(msg) sys.exit(1) core.svn_pool_destroy(subpool) def check_tabs(pool, repos_path, txn): def authz_cb(root, path, pool): return 1 fs_ptr = repos.svn_repos_fs(repos.svn_repos_open(repos_path, pool)) txn_ptr = fs.open_txn(fs_ptr, txn, pool) txn_root = fs.txn_root(txn_ptr, pool) base_root = fs.revision_root(fs_ptr, fs.txn_base_revision(txn_ptr), pool) editor = ChangeReceiver(txn_root, base_root, pool) e_ptr, e_baton = delta.make_editor(editor, pool) repos.svn_repos_dir_delta(base_root, '', '', txn_root, '', e_ptr, e_baton, authz_cb, 0, 1, 0, 0, pool) if __name__ == '__main__': assert len(sys.argv) == 3 core.run_app(check_tabs, sys.argv[1], sys.argv[2]) -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFEKsF93A5SrXAiHQcRAtG2AJ9FJHBr8T7MD6MUpxx85y3T12QBKACeJqXB CxzYXpGospuBh7PUqQ1pYLk= =36Ya -----END PGP SIGNATURE-----
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 In case you've never encountered a pre-commit hook, here's a sample of the effects: cory ~/wc/vellum% echo ' ' > foo.py cory ~/wc/vellum% svn add foo.py A foo.py cory ~/wc/vellum% svn ci -m "i did not read the coding standard" foo.py Adding foo.py Transmitting file data .svn: Commit failed (details follow): svn: 'pre-commit' hook failed with error output: Python file contains lines that begin with tabs: 'trunk/foo.py' There may be others as well. If Twisted wanted to adopt this, I would suggest adding a link to the coding standard in this message, or something. C Cory Dodt wrote:
For general discussion, I have attached a pre-commit hook that rejects commits of .py files containing leading tabs on any line. It does not do anything to files not ending in .py. It does check for tabs mixed with spaces. It ignores tabs occurring anywhere but the leading whitespace. Here's the regex:
_tabs = re.compile(r'^\s*\t\s*.*$')
I have used this, albeit lightly.
C
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFEKsVH3A5SrXAiHQcRAgwkAJoDMNtUGzCt7An+vkv9JjhwEC88bACbB0Ys X7LTPtokm3F/GYZsgi5+2Es= =vuCz -----END PGP SIGNATURE-----
See also tabnanny.py and reindent.py, included with Python. Regards, Zooko
Jean-Paul Calderone wrote:
Tabs?
Yes, it's a new Emacs install, and Carbon Emacs still seems to ignore indent-tabs-mode unless it's set by customize, it's been fixed, I'll untabify and commit a little later today. - David
participants (4)
-
Cory Dodt
-
David Reid
-
Jean-Paul Calderone
-
zooko@zooko.com