annotate clwapp/clwapp.py @ 3:672addc0c4be

more fixup
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 20 Dec 2011 12:44:40 -0800
parents 79a43cbdf584
children dfcd4dbe4b1c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
1 """
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
2 clwapp: Command Line Web APP
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
3
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
4 clwapp is configured with clwapp.command in paste's .ini file
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
5 arguments are passed in the query string. That is, if clwapp.command = ls,
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
6 one could do http://localhost:9999/?-l&-a to pass the '-l' and '-a' arguments.
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
7
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
8 DO NOT USE THIS (WITHOUT AUTH) WITH PROGRAMS THAT CAN ADVERSELY AFFECT THE
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
9 SYSTEM OR EXPOSE SENSITIVE DATA! subprocess.Popen is used, so that limit to
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
10 the shell is restricted, but programs that can alter your system can be
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
11 exploited. clwapp works well with programs that display information (provided
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
12 their access is restricted from secure information), and less well with
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
13 programs that change the state of the server.
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
14 *NEVER* use with Popen(shell=True) unless you are sure of your security.
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
15
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
16 """
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
17
2
79a43cbdf584 fix unicode stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
18 import os
0
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
19 import subprocess
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
20 import utils
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
21
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
22 from webob import Request, Response, exc
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
23
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
24 class View(object):
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
25
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
26 ### class level variables
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
27 defaults = {}
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
28
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
29 def __init__(self, command, **kw):
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
30 self.command = utils.shargs(command)
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
31 print self.command
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
32 for key in self.defaults:
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
33 setattr(self, key, kw.get(key, self.defaults[key]))
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
34 self.response_functions = { 'GET': self.get }
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
35
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
36 ### methods dealing with HTTP
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
37 def __call__(self, environ, start_response):
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
38 self.request = Request(environ)
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
39 res = self.make_response(self.request.method)
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
40 return res(environ, start_response)
2
79a43cbdf584 fix unicode stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
41
0
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
42 def make_response(self, method):
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
43 return self.response_functions.get(method, self.error)()
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
44
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
45 def get_response(self, text, content_type='text/html'):
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
46 res = Response(content_type=content_type, body=text)
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
47 res.content_length = len(res.body)
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
48 return res
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
49
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
50 def get(self):
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
51 """
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
52 return response to a GET requst
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
53 """
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
54 args = self.command + self.request.GET.keys()
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
55 process = subprocess.Popen(args, stdout=subprocess.PIPE)
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
56 output = process.communicate()[0]
3
672addc0c4be more fixup
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
57 output = output.decode('utf-8', 'ignore')
2
79a43cbdf584 fix unicode stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
58 title = ' '.join([os.path.basename(args[0])] + args[1:])
0
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
59 return self.get_response("""<html><head><title>%s</title></head><body><pre>
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
60 %s
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
61 </pre></body></html>
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
62 """ % (title, output) )
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
63
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
64 def error(self):
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
65 """deal with non-supported methods"""
196f241286f7 initial import of clwapp, from https://svn.openplans.org/svn/standalone/clwapp/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
66 return exc.HTTPMethodNotAllowed("Only %r operations are allowed" % self.response_functions.keys())
2
79a43cbdf584 fix unicode stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
67