def handler(req): ### if you don't need indirect modules reloaded, comment out ### code unitl ### end # if there are any new modules since the import of this module, # delete them. for m in sys.modules.keys(): if m not in original: # unless they are part of standard library mod = sys.modules[m] if hasattr(mod, "__file__"): path, x = os.path.split(mod.__file__) if path != stdlib: del sys.modules[m] ### end # get the filename of the script if req.subprocess_env.has_key("script_filename"): dir, file = os.path.split(req.subprocess_env["script_filename"]) else: dir, file = os.path.split(req.filename) module_name, ext = os.path.splitext(file) _lock.acquire() try: try: # The CGI spec requires us to set current working # directory to that of the script. This is not # thread safe, this is why we must obtain the lock. cwd = os.getcwd() os.chdir(dir) # simulate cgi environment env, si, so = apache.setup_cgi(req) try: # we do not search the pythonpath (security reasons) fd, path, desc = imp.find_module(module_name, [dir]) except ImportError: raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND # this executes the module imp.load_module(module_name, fd, path, desc) return apache.OK finally: # unsimulate the cgi environment apache.restore_nocgi(env, si, so) try: fd.close() except: pass os.chdir(cwd) finally: _lock.release()
def handler(req): conf = req.get_config() # get module name to be published dir, file = os.path.split(req.filename) module_name, ext = os.path.splitext(file) # if autoreload is on, we will check dates # and reload the module if the source is newer apache.import_module(module_name, req) # setup CGI environment env, si, so = apache.setup_cgi(req) try: ZPublisher.publish_module(module_name, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr, environ=os.environ) finally: apache.restore_nocgi(env, si, so) return apache.OK