[Checkins] SVN: gocept.selenium/trunk/ use python2.4 features for starting and stopping the simplehttpserver for serving the static test files

Jan-Wijbrand Kolman janwijbrand at gmail.com
Wed Sep 15 02:02:01 EDT 2010


Log message for revision 116425:
  use python2.4 features for starting and stopping the simplehttpserver for serving the static test files

Changed:
  U   gocept.selenium/trunk/src/gocept/selenium/static/__init__.py
  U   gocept.selenium/trunk/ztk.cfg

-=-
Modified: gocept.selenium/trunk/src/gocept/selenium/static/__init__.py
===================================================================
--- gocept.selenium/trunk/src/gocept/selenium/static/__init__.py	2010-09-15 04:11:02 UTC (rev 116424)
+++ gocept.selenium/trunk/src/gocept/selenium/static/__init__.py	2010-09-15 06:02:00 UTC (rev 116425)
@@ -12,19 +12,52 @@
 #
 ##############################################################################
 
+import BaseHTTPServer
 import os
+import os.path
+import posixpath
 import shutil
+import SimpleHTTPServer
 import subprocess
 import sys
 import tempfile
+import threading
 import time
 import unittest
+import urllib
 
 import gocept.selenium.base
 
 _suffix = 'gocept.selenium.static'
 
 
+class StaticFileRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
+
+    # The documentroot is set on the class just before passing the class on
+    # to the BaseHTTPServer.HTTPServer.
+    documentroot = None
+
+    def translate_path(self, path):
+        # We subclass SimpleHTTPRequestHandler as it is dependent on
+        # the cwd. We however want to inject a different path as the
+        # "documentroot".
+        # The rest of the method's implementation is copied verbatim from
+        # SimpleHTTPServer.SimpleHTTPRequestHandler.
+        path = path.split('?',1)[0]
+        path = path.split('#',1)[0]
+        path = posixpath.normpath(urllib.unquote(path))
+        words = path.split('/')
+        words = filter(None, words)
+
+        path = self.documentroot
+        for word in words:
+            drive, word = os.path.splitdrive(word)
+            head, word = os.path.split(word)
+            if word in (os.curdir, os.pardir): continue
+            path = os.path.join(path, word)
+        return path
+
+
 class StaticFilesLayer(gocept.selenium.base.Layer):
 
     host = 'localhost'
@@ -37,8 +70,10 @@
         self.start_server()
 
     def start_server(self):
-        cmd = [sys.executable, '-m', 'SimpleHTTPServer', str(self.port)]
-        self.server = subprocess.Popen(cmd, cwd=self.documentroot)
+        StaticFileRequestHandler.documentroot = self.documentroot
+        self.server = BaseHTTPServer.HTTPServer(
+            (self.host, self.port), StaticFileRequestHandler)
+        self._server_thread = threading.Thread(target=server.serve_forever)
         # Wait a little as it sometimes takes a while to get the server
         # started.
         time.sleep(0.25)
@@ -46,7 +81,8 @@
     def stop_server(self):
         if self.server is None:
             return
-        self.server.kill()
+        self.server.shutdown()
+        self._server_thread.join()
         self.server = None
 
     def tearDown(self):

Modified: gocept.selenium/trunk/ztk.cfg
===================================================================
--- gocept.selenium/trunk/ztk.cfg	2010-09-15 04:11:02 UTC (rev 116424)
+++ gocept.selenium/trunk/ztk.cfg	2010-09-15 06:02:00 UTC (rev 116425)
@@ -1,6 +1,6 @@
 [buildout]
-extends = http://svn.zope.org/repos/main/zopetoolkit/trunk/ztk.cfg
-          http://svn.zope.org/repos/main/zopetoolkit/trunk/zopeapp.cfg
+extends = http://download.zope.org/zopetoolkit/index/1.0a3/ztk-versions.cfg
+          http://download.zope.org/zopetoolkit/index/1.0a3/zopeapp-versions.cfg
 develop = .
 parts = test seleniumrc instance
 package = gocept.selenium



More information about the checkins mailing list