diff options
Diffstat (limited to 'paste')
| -rwxr-xr-x | paste/server.py | 106 | ||||
| -rw-r--r-- | paste/servers/cgi/__init__.py | 1 | ||||
| -rw-r--r-- | paste/servers/cgi_server.py | 30 | ||||
| -rw-r--r-- | paste/servers/cgi_wsgi.py (renamed from paste/servers/cgi/cgiserver.py) | 0 | ||||
| -rw-r--r-- | paste/servers/console_server.py | 32 | ||||
| -rw-r--r-- | paste/servers/scgi/__init__.py | 1 | ||||
| -rw-r--r-- | paste/servers/scgi_server/__init__.py | 7 | ||||
| -rw-r--r-- | paste/servers/scgi_server/scgiserver.py (renamed from paste/servers/scgi/scgiserver.py) | 0 | ||||
| -rw-r--r-- | paste/servers/server_script_template.py (renamed from paste/server_script_template.py) | 0 | ||||
| -rw-r--r-- | paste/servers/wsgiutils_server.py (renamed from paste/servers/wsgiutils.py) | 6 | ||||
| -rw-r--r-- | paste/util/plugin.py | 47 |
11 files changed, 123 insertions, 107 deletions
diff --git a/paste/server.py b/paste/server.py index 3d9f479..f80cb6e 100755 --- a/paste/server.py +++ b/paste/server.py @@ -32,6 +32,7 @@ import os from paste import reloader from paste import wsgilib from paste import CONFIG +from paste.util import plugin # This way you can run this out of a checkout, and we'll fix up # the path... @@ -57,8 +58,6 @@ from paste.configmiddleware import config_middleware from paste.webkit import wsgiwebkit from paste.util import thirdparty -servers = {} - default_ops = { 'port': 8080, 'host': 'localhost', @@ -98,7 +97,7 @@ def load_commandline(args, allow_reload=True): conf['verbose'] = False server = conf.get('server') if not server: - server_ops = servers.keys() + server_ops = plugin.find_plugins('servers', '_server') server_ops.sort() print "Missing --server=name, one of: %s" % ', '.join(server_ops) return None, 0 @@ -124,16 +123,27 @@ def run_commandline(args): return run_server(conf, app) def run_server(conf, app): - server = servers[conf['server']] + try: + server_mod = plugin.load_plugin_module( + dir='servers', + dir_package='paste.servers', + name=conf['server'], + name_extension='_server') + except plugin.PluginNotFound, e: + print "Error loading server: %s" % e + print "Available servers:" + server_ops = plugin.find_plugins('servers', '_server') + server_ops.sort() + print ', '.join(server_ops) + return 1 if conf['verbose']: print "Starting server." try: - server(conf, app) + server_mod.serve(conf, app) except KeyboardInterrupt: # This is an okay error pass return 0 - def load_conf(conf, filename, default=False): if isinstance(filename, (list, tuple)): @@ -159,90 +169,6 @@ def help(): program = sys.argv[0] return help_message % {'program': program} -def twisted_serve(conf, app): - print 'Twisted support has been temporarily removed from Paste.' - -servers['twisted'] = twisted_serve - -def scgi_serve(conf, app): - thirdparty.add_package('scgi') - from paste.scgiserver import serve_application - prefix = conf.get('scgi_prefix', '/') - serve_application(app, prefix, port=int(conf.get('port', 4000))) - -servers['scgi'] = scgi_serve - -def wsgiutils_serve(conf, app): - thirdparty.add_package('wsgiutils') - from wsgiutils import wsgiServer - server = wsgiServer.WSGIServer( - (conf.get('host', 'localhost'), - int(conf.get('port', 8080))), {'': app}) - server.serve_forever() - -servers['wsgiutils'] = wsgiutils_serve - -def cgi_serve(conf, app): - replacements = {} - replacements['default_config_fn'] = os.path.abspath(default_config_fn) - - # Ideally, other_conf should be any options that came from the - # command-line. - # @@: This assumes too much about the ordering of namespaces. - other_conf = dict(conf.namespaces[-2]) - # Not a good idea to let 'verbose' through, but this doesn't really - # stop any sourced configs from setting it either... - if other_conf.has_key('verbose'): - del other_conf['verbose'] - replacements['other_conf'] = other_conf - - template_fn = os.path.join(os.path.dirname(__file__), - 'server_script_template.py') - template = open(template_fn).read() - for name, value in replacements.items(): - template = template.replace('@@' + name + '@@', repr(value)) - - print "#!%s" % sys.executable - print template - print "if __name__ == '__main__':" - print " from paste.cgiserver import run_with_cgi" - print " run_with_cgi(app)" - -servers['cgi'] = cgi_serve - -def console_server(conf, app): - url = conf.get('url', '/') - query_string = '' - if '?' in url: - url, query_string = url.split('?', 1) - quiet = conf.get('quiet', False) - status, headers, content, errors = wsgilib.raw_interactive( - app, url, QUERY_STRING=query_string) - any_header = False - if not quiet or int(status.split()[0]) != 200: - print 'Status:', status - any_header = True - for header, value in headers: - if quiet and ( - header.lower() in ('content-type', 'content-length') - or (header.lower() == 'set-cookie' - and value.startswith('_SID_'))): - continue - print '%s: %s' % (header, value) - any_header = True - if any_header: - print - if conf.get('compact', False): - # Remove empty lines - content = '\n'.join([l for l in content.splitlines() - if l.strip()]) - print content - if errors: - sys.stderr.write('-'*25 + ' Errors ' + '-'*25 + '\n') - sys.stderr.write(errors + '\n') - -servers['console'] = console_server - def make_app(conf): if conf.get('publish_dir'): app = wsgiwebkit.webkit(conf['publish_dir'], use_lint=conf.get('lint')) diff --git a/paste/servers/cgi/__init__.py b/paste/servers/cgi/__init__.py deleted file mode 100644 index 792d600..0000000 --- a/paste/servers/cgi/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# diff --git a/paste/servers/cgi_server.py b/paste/servers/cgi_server.py new file mode 100644 index 0000000..7bf8ce3 --- /dev/null +++ b/paste/servers/cgi_server.py @@ -0,0 +1,30 @@ +import os +import sys +from paste import server + +def serve(conf, app): + replacements = {} + replacements['default_config_fn'] = os.path.abspath( + server.default_config_fn) + + # Ideally, other_conf should be any options that came from the + # command-line. + # @@: This assumes too much about the ordering of namespaces. + other_conf = dict(conf.namespaces[-2]) + # Not a good idea to let 'verbose' through, but this doesn't really + # stop any sourced configs from setting it either... + if other_conf.has_key('verbose'): + del other_conf['verbose'] + replacements['other_conf'] = other_conf + + template_fn = os.path.join(os.path.dirname(__file__), + 'server_script_template.py') + template = open(template_fn).read() + for name, value in replacements.items(): + template = template.replace('@@' + name + '@@', repr(value)) + + print "#!%s" % sys.executable + print template + print "if __name__ == '__main__':" + print " from paste.cgiserver import run_with_cgi" + print " run_with_cgi(app)" diff --git a/paste/servers/cgi/cgiserver.py b/paste/servers/cgi_wsgi.py index 83d257c..83d257c 100644 --- a/paste/servers/cgi/cgiserver.py +++ b/paste/servers/cgi_wsgi.py diff --git a/paste/servers/console_server.py b/paste/servers/console_server.py new file mode 100644 index 0000000..92f4456 --- /dev/null +++ b/paste/servers/console_server.py @@ -0,0 +1,32 @@ +from paste import wsgilib + +def serve(conf, app): + url = conf.get('url', '/') + query_string = '' + if '?' in url: + url, query_string = url.split('?', 1) + quiet = conf.get('quiet', False) + status, headers, content, errors = wsgilib.raw_interactive( + app, url, QUERY_STRING=query_string) + any_header = False + if not quiet or int(status.split()[0]) != 200: + print 'Status:', status + any_header = True + for header, value in headers: + if quiet and ( + header.lower() in ('content-type', 'content-length') + or (header.lower() == 'set-cookie' + and value.startswith('_SID_'))): + continue + print '%s: %s' % (header, value) + any_header = True + if any_header: + print + if conf.get('compact', False): + # Remove empty lines + content = '\n'.join([l for l in content.splitlines() + if l.strip()]) + print content + if errors: + sys.stderr.write('-'*25 + ' Errors ' + '-'*25 + '\n') + sys.stderr.write(errors + '\n') diff --git a/paste/servers/scgi/__init__.py b/paste/servers/scgi/__init__.py deleted file mode 100644 index 792d600..0000000 --- a/paste/servers/scgi/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# diff --git a/paste/servers/scgi_server/__init__.py b/paste/servers/scgi_server/__init__.py new file mode 100644 index 0000000..0ef4e56 --- /dev/null +++ b/paste/servers/scgi_server/__init__.py @@ -0,0 +1,7 @@ +from paste.util import thirdparty +thirdparty.add_package('scgi') +from scgiserver import serve_application + +def serve(conf, app): + prefix = conf.get('scgi_prefix', '/') + serve_application(app, prefix, port=int(conf.get('port', 4000))) diff --git a/paste/servers/scgi/scgiserver.py b/paste/servers/scgi_server/scgiserver.py index 15fbef2..15fbef2 100644 --- a/paste/servers/scgi/scgiserver.py +++ b/paste/servers/scgi_server/scgiserver.py diff --git a/paste/server_script_template.py b/paste/servers/server_script_template.py index aa5c1cb..aa5c1cb 100644 --- a/paste/server_script_template.py +++ b/paste/servers/server_script_template.py diff --git a/paste/servers/wsgiutils.py b/paste/servers/wsgiutils_server.py index 2c63854..00ff182 100644 --- a/paste/servers/wsgiutils.py +++ b/paste/servers/wsgiutils_server.py @@ -1,6 +1,8 @@ +from paste.util import thirdparty +thirdparty.add_package('wsgiutils') +from wsgiutils import wsgiServer + def serve(conf, app): - thirdparty.add_package('wsgiutils') - from wsgiutils import wsgiServer server = wsgiServer.WSGIServer( (conf.get('host', 'localhost'), int(conf.get('port', 8080))), {'': app}) diff --git a/paste/util/plugin.py b/paste/util/plugin.py index 2e61294..442a7a6 100644 --- a/paste/util/plugin.py +++ b/paste/util/plugin.py @@ -1,24 +1,41 @@ import os import import_string +class PluginNotFound(ImportError): + pass + paste_dir = os.path.dirname(os.path.dirname(__file__)) -def find_plugins(dir): +def find_plugins(dir, name_extension=''): """ Looks in directory for plugins, and returns them by name. A plugin can be a .txt file, .py file, or directory. + + If name_extension is given, only files ending with that string + will be found (and that string will be removed). """ plugins = [] - dir = os.path.join(paste_dir, dir)) + dir = os.path.join(paste_dir, dir) for name in os.listdir(dir): + if name.startswith('.'): + continue + if not os.path.splitext(name)[0].endswith(name_extension): + continue if os.path.isdir(os.path.join(dir, name)): - plugins.append(name) - ext = os.path.splitext(name)[1].lower() - if name in ('.py', '.txt'): - plugins.append(name) + if name_extension: + plugins.append(name[:-len(name_extension)]) + else: + plugins.append(name) + continue + base, ext = os.path.splitext(name) + if ext.lower() in ('.py', '.txt'): + if name_extension: + plugins.append(base[:-len(name_extension)]) + else: + plugins.append(base) return plugins -def load_plugin_module(dir, dir_package, name): +def load_plugin_module(dir, dir_package, name, name_extension=''): """ Loads a plugin module. Either the module is imported, or if it is a .txt file then the file is loaded and must contain the name @@ -28,11 +45,15 @@ def load_plugin_module(dir, dir_package, name): are not supported. """ dir = os.path.join(paste_dir, dir) - if os.path.exists(os.path.join(dir, name + '.txt')): - module_name = parse_txt_plugin(os.path.join(dir, name + '.txt')) + txt_name = name + name_extension + '.txt' + if os.path.exists(os.path.join(dir, txt_name)): + module_name = parse_txt_plugin(os.path.join(dir, txt_name)) else: - module_name = dir_package + '.' + name - return import_string.import_module(module_name) + module_name = dir_package + '.' + name + name_extension + try: + return import_string.import_module(module_name) + except ImportError, e: + raise PluginNotFound(str(e)) def parse_txt_plugin(fn): f = open(fn) @@ -43,11 +64,11 @@ def parse_txt_plugin(fn): if not line or line.startswith('#'): continue if module_name is not None: - raise ValueError( + raise PluginNotFound( "Plugin file %s contains more than one line that " "could be interpreted as a module name" % fn) if module_name is None: - raise ValueError( + raise PluginNotFound( "Plugin file %s does not contain a module name" % fn) return module_name finally: |
