changed util to a module and added /_sysinfo as a possible interceptor

This commit is contained in:
Steve 'Ashcrow' Milner 2010-09-17 22:42:24 -04:00
parent 1cc76a1be2
commit c0f54fb49a
4 changed files with 162 additions and 0 deletions

View File

@ -46,6 +46,46 @@ try:
except ImportError:
import simplejson as json
class URLInterceptor(object):
"""
Intercepts one or more paths.
"""
paths = []
def __init__(self, app, paths=[]):
"""
Creates an instance.
:Parameters:
- `app`: Application to fall through to
"""
self.app = app
def _intercept(self, env, start_response):
"""
Executes business logic.
:Parameters:
- `env`: environment information
- `start_response`: wsgi response function
"""
raise NotImplementedError('_intercept must be overridden')
def __call__(self, env, start_response):
"""
Dispatches input to the proper method.
:Parameters:
- `env`: environment information
- `start_response`: wsgi response function
"""
if env['PATH_INFO'] in self.paths:
return self._intercept(env, start_response)
return self.app(env, start_response)
class FigleafCoverage(object):
def __init__(self, app):
import figleaf
@ -68,6 +108,26 @@ class FigleafCoverage(object):
return self.app(env, start_response)
class SystemInfo(URLInterceptor):
"""
Intercepts /_sysinfo path and returns json data.
"""
paths = ['/_sysinfo']
def _intercept(self, env, start_response):
"""
Executes business logic.
:Parameters:
- `env`: environment information
- `start_response`: wsgi response function
"""
import spawning.util.system
start_response("200 OK", [('Content-type', 'application/json')])
return [json.dumps(spawning.util.system.System())]
class ExitChild(Exception):
pass
@ -110,6 +170,8 @@ def serve_from_child(sock, config, controller_pid):
if config.get('coverage'):
wsgi_application = FigleafCoverage(wsgi_application)
elif config.get('sysinfo'):
wsgi_application = SystemInfo(wsgi_application)
if threads > 1:
# proxy calls of the application through tpool

View File

@ -310,6 +310,9 @@ def main():
help='If given, gather coverage data from the running program and make the '
'coverage report available from the /_coverage url. See the figleaf docs '
'for more info: http://darcs.idyll.org/~t/projects/figleaf/doc/')
parser.add_option('--sysinfo', dest='sysinfo', action='store_true',
help='If given, gather system information data and make the '
'report available from the /_sysinfo url.')
parser.add_option('-m', '--max-memory', dest='max_memory', type='int', default=0,
help='If given, the maximum amount of memory this instance of Spawning '
'is allowed to use. If all of the processes started by this Spawning controller '
@ -453,6 +456,7 @@ def main():
'access_log_file': options.access_log_file,
'pidfile': options.pidfile,
'coverage': options.coverage,
'sysinfo': options.sysinfo,
'no_keepalive' : options.no_keepalive,
'max_age' : options.max_age,
'argv_str': " ".join(sys.argv[1:]),

View File

@ -0,0 +1,96 @@
# Copyright (c) 2010, Steve 'Ashcrow' MIlner
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
Platform related items.
"""
import os
import platform
import sys
import tempfile
class System(dict):
"""
Class to make finding out system information all in one place.
**Note**: You can not add attributes to an instance of this class.
"""
def __init__(self):
dict.__init__(self, {
'architecture': platform.architecture(),
'max_int': sys.maxint,
'max_size': sys.maxsize,
'max_unicode': sys.maxunicode,
'name': platform.node(),
'path_seperator': os.path.sep,
'processor': platform.processor(),
'python_version': platform.python_version(),
'python_branch': platform.python_branch(),
'python_build': platform.python_build(),
'python_compiler': platform.python_compiler(),
'python_implementation': platform.python_implementation(),
'python_revision': platform.python_revision(),
'python_version_tuple': platform.python_version_tuple(),
'python_path': sys.path,
'login': os.getlogin(),
'system': platform.system(),
'temp_directory': tempfile.gettempdir(),
'uname': platform.uname(),
})
def __getattr__(self, name):
"""
Looks in the dictionary for items **only**.
:Parameters:
- 'name': name of the attribute to get.
"""
data = dict(self).get(name)
if data == None:
raise AttributeError("'%s' has no attribute '%s'" % (
self.__class__.__name__, name))
return data
def __setattr__(self, key, value):
"""
Setting attributes is **not** allowed.
:Parameters:
- `key`: attribute name to set.
- `value`: value to set attribute to.
"""
raise AttributeError("can't set attribute")
def __repr__(self):
"""
Nice object representation.
"""
return unicode(
"<Platform: system='%s', name='%s', arch=%s, processor='%s'>" % (
self.system, self.name, self.architecture, self.processor))
# Method aliases
__str__ = __repr__
__unicode__ = __repr__
__setitem__ = __setattr__