# HG changeset patch # User k0s # Date 1257117875 18000 # Node ID 196f241286f7c3a9ec21e6323d923eb721761fbd initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/ diff -r 000000000000 -r 196f241286f7 clwapp.ini --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/clwapp.ini Sun Nov 01 18:24:35 2009 -0500 @@ -0,0 +1,22 @@ +#!/usr/bin/env paster + +[DEFAULT] +debug = true +email_to = jhammel@openplans.org +smtp_server = localhost +error_email_from = paste@localhost + +[server:main] +use = egg:Paste#http +host = 0.0.0.0 +port = 9999 + +[composite:main] +use = egg:Paste#urlmap +/ = clwapp + +set debug = false + +[app:clwapp] +paste.app_factory = clwapp.factory:factory +clwapp.command = echo \ No newline at end of file diff -r 000000000000 -r 196f241286f7 clwapp/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/clwapp/__init__.py Sun Nov 01 18:24:35 2009 -0500 @@ -0,0 +1,1 @@ +# diff -r 000000000000 -r 196f241286f7 clwapp/clwapp.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/clwapp/clwapp.py Sun Nov 01 18:24:35 2009 -0500 @@ -0,0 +1,65 @@ +""" +clwapp: Command Line Web APP + +clwapp is configured with clwapp.command in paste's .ini file +arguments are passed in the query string. That is, if clwapp.command = ls, +one could do http://localhost:9999/?-l&-a to pass the '-l' and '-a' arguments. + +DO NOT USE THIS (WITHOUT AUTH) WITH PROGRAMS THAT CAN ADVERSELY AFFECT THE +SYSTEM OR EXPOSE SENSITIVE DATA! subprocess.Popen is used, so that limit to +the shell is restricted, but programs that can alter your system can be +exploited. clwapp works well with programs that display information (provided +their access is restricted from secure information), and less well with +programs that change the state of the server. +*NEVER* use with Popen(shell=True) unless you are sure of your security. + +""" + +import subprocess +import utils + +from webob import Request, Response, exc + +class View(object): + + ### class level variables + defaults = {} + + def __init__(self, command, **kw): + self.command = utils.shargs(command) + print self.command + for key in self.defaults: + setattr(self, key, kw.get(key, self.defaults[key])) + self.response_functions = { 'GET': self.get } + + ### methods dealing with HTTP + def __call__(self, environ, start_response): + self.request = Request(environ) + res = self.make_response(self.request.method) + return res(environ, start_response) + + def make_response(self, method): + return self.response_functions.get(method, self.error)() + + def get_response(self, text, content_type='text/html'): + res = Response(content_type=content_type, body=text) + res.content_length = len(res.body) + return res + + def get(self): + """ + return response to a GET requst + """ + args = self.command + self.request.GET.keys() + process = subprocess.Popen(args, stdout=subprocess.PIPE) + output = process.communicate()[0] + title = ' '.join(args) + return self.get_response("""%s
+%s
+
+ """ % (title, output) ) + + def error(self): + """deal with non-supported methods""" + return exc.HTTPMethodNotAllowed("Only %r operations are allowed" % self.response_functions.keys()) + diff -r 000000000000 -r 196f241286f7 clwapp/factory.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/clwapp/factory.py Sun Nov 01 18:24:35 2009 -0500 @@ -0,0 +1,13 @@ +from clwapp import View +from paste.httpexceptions import HTTPExceptionHandler + +def factory(global_conf, **app_conf): + """create a webob view and wrap it in middleware""" + + config = [ 'command' ] + key_str = 'clwapp.%s' + args = dict([(key, app_conf[ key_str % key]) for key in config + if app_conf.has_key(key_str % key) ]) + app = View(**args) + return HTTPExceptionHandler(app) + diff -r 000000000000 -r 196f241286f7 clwapp/utils.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/clwapp/utils.py Sun Nov 01 18:24:35 2009 -0500 @@ -0,0 +1,8 @@ +import subprocess + +def shargs(args): + process = subprocess.Popen('for arg in %s; do echo $arg; done' % args, + shell=True, stdout=subprocess.PIPE) + output = process.communicate()[0] + output = output[:-1] # last line will be blank + return output.split('\n') diff -r 000000000000 -r 196f241286f7 setup.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.py Sun Nov 01 18:24:35 2009 -0500 @@ -0,0 +1,31 @@ +from setuptools import setup, find_packages +import sys, os + +version = "0.0" + +setup(name='clwapp', + version=version, + description="Command Line Web APP", + long_description=""" +""", + classifiers=[], # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers + author='Jeff Hammel', + author_email='jhammel@openplans.org', + url='', + license="", + packages=find_packages(exclude=['ez_setup', 'examples', 'tests']), + include_package_data=True, + zip_safe=False, + install_requires=[ + # -*- Extra requirements: -*- + 'WebOb', + 'Paste', + 'PasteScript', + ], + entry_points=""" + # -*- Entry points: -*- + [paste.app_factory] + main = clwapp.factory:factory + """, + ) +