[z3-checkins] r39645 - z3/deliverance/DeliveranceVHoster/trunk/dvhoster
ltucker at codespeak.net
ltucker at codespeak.net
Thu Mar 1 23:05:06 CET 2007
Author: ltucker
Date: Thu Mar 1 23:05:03 2007
New Revision: 39645
Added:
z3/deliverance/DeliveranceVHoster/trunk/dvhoster/api_wrapper.py
z3/deliverance/DeliveranceVHoster/trunk/dvhoster/cli.py
Log:
adding a crumby cli interface that may be of some value temporarily
Added: z3/deliverance/DeliveranceVHoster/trunk/dvhoster/api_wrapper.py
==============================================================================
--- (empty file)
+++ z3/deliverance/DeliveranceVHoster/trunk/dvhoster/api_wrapper.py Thu Mar 1 23:05:03 2007
@@ -0,0 +1,159 @@
+#
+# exposes the restful deliverance API
+# as python calls
+#
+
+from StringIO import StringIO
+from paste.wsgilib import intercept_output
+from paste.proxy import TransparentProxy
+from paste.request import construct_url
+from deliverance.utils import DeliveranceError
+import simplejson
+
+class DeliveranceAdmin:
+
+ def __init__(self, domain, force_host=None):
+ self.domain = domain
+ self.force_host = force_host
+
+ def set_rules(self, rule_data):
+ return self.put_data('/.deliverance/rules/rule.xml', rule_data)
+
+ def get_rules(self):
+ return self.get_data('/.deliverance/rules/rule.xml')
+
+ def set_theme_uri(self, theme_uri):
+ return self.put_data('/.deliverance/theme_uri', theme_uri)
+
+ def get_theme_uri(self):
+ return self.get_data('/.deliverance/theme_uri')
+
+ def set_domain(self, new_domain):
+ rc = self.put_data('/.deliverance/domain', new_domain)
+ self.domain = new_domain
+ return rc
+
+ def get_domain(self):
+ return self.get_data('/.deliverance/domain')
+
+ def set_static_file(self, path, data):
+ return self.put_data('/.deliverance/static/%s' % path, data)
+
+ def static_mkdir(self, subdir):
+ return self.mkdir('/.deliverance/static/%s' % subdir)
+
+ #-----------------------------
+
+ def get_redirects(self):
+ return simplejson.loads(self.get_data('/.deliverance/redirects'))
+
+ def set_redirects(self, python_map):
+ return self.put_data('/.deliverance/redirectss', simplejson.dumps(python_map))
+
+ def set_redirects_json(self, json_str):
+ return self.put_data('/.deliverance/redirects', json_str, content_type="application/json; charset=utf8")
+
+ def append_redirect(self, path, redirect, comment, prefix=False):
+ cur_redirects = self.get_redirects()
+ rmap = {}
+ if prefix:
+ rmap['prefix'] = path
+ else:
+ rmap['path'] = path
+ rmap['rewrite'] = remote_uri
+ rmap['comment'] = comment
+
+ new_redirects = [m for m in cur_redirects if 'path' in m and m['path'] != path]
+ new_redirects.append(rmap)
+
+ return self.set_redirects(new_redirects)
+
+
+ #--------------------------------
+
+
+ def get_remote_uris(self):
+ return simplejson.loads(self.get_data('/.deliverance/remote_uris'))
+
+ def set_remote_uris(self, python_map):
+ return self.put_data('/.deliverance/remote_uris', simplejson.dumps(python_map))
+
+ def set_remote_uris_json(self, json_str):
+ return self.put_data('/.deliverance/remote_uris', json_str, content_type="application/json; charset=utf8")
+
+ def append_remote_uri(self, path, remote_uri, comment):
+ cur_uris = self.get_remote_uris()
+ rmap = {}
+ rmap['path'] = path
+ rmap['remote_uri'] = remote_uri
+ rmap['comment'] = comment
+
+ new_remote_uris = [m for m in cur_uris if 'path' in m and m['path'] != path]
+ new_remote_uris.append(rmap)
+
+ return self.set_remote_uris(new_remote_uris)
+
+ #-------------------------------------
+
+ def mkdir(self, subdir):
+ env = {}
+
+ env['wsgi.input'] = StringIO('')
+ env['wsgi.url_scheme'] = 'http'
+ env['REQUEST_METHOD'] = 'MKCOL'
+ env['HTTP_HOST'] = self.domain
+ env['SCRIPT_INFO'] = ''
+ env['PATH_INFO'] = subdir
+ env['CONTENT_LENGTH'] = '0'
+
+ app = TransparentProxy(force_host=self.force_host)
+ status, headers, body = intercept_output(env, app)
+
+ if not status.startswith('201'):
+ raise DeliveranceError('Error creating directory %s got %s [%s]' % (subdir, status, body))
+
+ return True
+
+ def put_data(self, put_path, data, content_type=None):
+ env = {}
+ env['wsgi.version'] = (1,0)
+ env['wsgi.input'] = StringIO(data)
+ env['wsgi.url_scheme'] = 'http'
+ env['HTTP_HOST'] = self.domain
+ env['SCRIPT_INFO'] = ''
+ env['REQUEST_METHOD'] = 'PUT'
+ env['PATH_INFO'] = put_path
+ if content_type:
+ env['CONTENT_TYPE'] = content_type
+
+ app = TransparentProxy(force_host=self.force_host)
+ status, headers, body = intercept_output(env, app)
+
+ if not (status.startswith('200') or status.startswith('204')):
+ raise DeliveranceError('Error setting %s got %s [%s]' % (put_path, status, body))
+
+ return True
+
+
+ def get_data(self, get_path):
+ env = {}
+
+ env['wsgi.version'] = (1,0)
+ env['wsgi.input'] = StringIO('')
+ env['wsgi.url_scheme'] = 'http'
+ env['REQUEST_METHOD'] = 'GET'
+ env['HTTP_HOST'] = self.domain
+ env['SCRIPT_INFO'] = ''
+ env['PATH_INFO'] = get_path
+ env['CONTENT_LENGTH'] = '0'
+
+
+ app = TransparentProxy(force_host=self.force_host)
+ status, headers, body = intercept_output(env, app)
+
+ if not status.startswith('200'):
+ raise DeliveranceError('Error getting %s got %s [%s]' % (get_path, status, body))
+
+ return body
+
+
Added: z3/deliverance/DeliveranceVHoster/trunk/dvhoster/cli.py
==============================================================================
--- (empty file)
+++ z3/deliverance/DeliveranceVHoster/trunk/dvhoster/cli.py Thu Mar 1 23:05:03 2007
@@ -0,0 +1,157 @@
+#
+# command line interface to deliverance vhoster
+#
+
+import sys
+import optparse
+import traceback
+from api_wrapper import DeliveranceAdmin
+
+
+def do_get_domain(adm, *args):
+ print adm.get_domain()
+
+def do_set_domain(adm, name):
+ if adm.set_domain(name):
+ print "[-] OK"
+
+def do_get_rules(adm, *args):
+ print adm.get_rules()
+
+def do_set_rules(adm, rules):
+ if rules.startswith("\'") or rules.startswith('\"'):
+ if not rules.endswith(rules[0]):
+ print "[X] mismatched quotes"
+ return
+ data = rules[1:-1]
+ else:
+ data = open(rules).read()
+
+ if adm.set_rules(data):
+ print "[-] OK"
+
+
+def do_get_theme_uri(adm, *args):
+ print adm.get_theme_uri()
+
+def do_set_theme_uri(adm, uri):
+ if adm.set_theme_uri(uri):
+ print "[-] OK"
+
+def do_get_redirects(adm, *args):
+ print adm.get_redirects()
+
+def do_set_redirects(adm, json):
+ if adm.set_redirects_json(json):
+ print "[-] OK"
+
+def do_append_redirect(adm, path, redirect):
+ if adm.append_redirect(path, redirect, '?'):
+ print "[-] OK"
+
+def do_append_redirect_prefix(adm, path, redirect):
+ if adm.append_redirect(path, redirect, '?', prefix=True):
+ print "[-] OK"
+
+def do_get_remote_uris(adm, *args):
+ print adm.get_remote_uris()
+
+def do_set_remote_uris(adm, json):
+ if adm.set_remote_uris_json(json):
+ print "[-] OK"
+
+def do_append_remote_uri(adm, path, remote_uri):
+ if adm.append_remote_uri(path, remote_uri, '?'):
+ print "[-] OK"
+
+
+def do_help(adm, command=None):
+ if command is None:
+ print "available commands:"
+ print "-------------------"
+ for cmd in COMMANDS:
+ print cmd
+ return
+ if command in COMMANDS_HELP:
+ print COMMANDS_HELP[command]
+ return
+ else:
+ print "sorry, no help for ", command
+
+COMMANDS = {
+ 'get_domain': do_get_domain,
+ 'set_domain': do_set_domain,
+ 'get_rules': do_get_rules,
+ 'set_rules': do_set_rules,
+ 'get_theme_uri': do_get_theme_uri,
+ 'set_theme_uri': do_set_theme_uri,
+ 'get_redirects': do_get_redirects,
+ 'set_redirects': do_set_redirects,
+ 'append_redirect': do_append_redirect,
+ 'append_redirect_prefix': do_append_redirect_prefix,
+ 'get_remote_uris': do_get_remote_uris,
+ 'set_remote_uris': do_set_remote_uris,
+ 'append_remote_uri': do_append_remote_uri,
+ 'help': do_help
+}
+
+COMMANDS_HELP = {
+ 'get_domain': "retrieves domain name",
+ 'set_domain': "eg: set_domain foo.wooley.bar",
+ 'get_rules': "retrieves rules",
+ 'set_rules': "eg: set_rules '<some xml>' or set_rules some_file.xml",
+ 'get_theme_uri': "retrieve theme uri",
+ 'set_theme_uri': "eg: set_theme_uri http://foo.org/theme.html",
+ 'get_redirects': "retrieve current list of redirects",
+ 'set_redirects': "eg set_redirects [{'path': '/', 'rewrite': 'http://www.quux.org/blah', 'comment': 'go to quux'}, {...}]",
+ 'append_redirect': "eg append_redirect / http://www.quux.org/blah" ,
+ 'append_redirect_prefix': "eg append_redirect / http://www.quux.org/blah" ,
+ 'get_remote_uris': "retrieve current list of remote_uris",
+ 'set_remote_uris': "eg set_remote_uris [{'path': '/', 'remote_uri': 'http://www.quux.org/blah', 'comment': 'go to quux'}, {...}]",
+ 'append_remote_uri': "eg append_remote_uri / http://www.quux.org/blah" ,
+}
+
+def repl(adm):
+ done = False
+
+ while not done:
+ sys.stdout.write("> ")
+ cmd_buf = raw_input().split(' ')
+ if cmd_buf[0] in COMMANDS:
+ try:
+ COMMANDS[cmd_buf[0]](adm, *cmd_buf[1:])
+ except:
+ traceback.print_exc()
+ else:
+ print "[X] unknown command"
+
+def main(argv=None):
+ if argv is None:
+ argv = sys.argv
+
+ usage = "usage: %prog domain [options]"
+ parser = optparse.OptionParser(usage=usage)
+ parser.add_option('-F','--force-host',
+ help="specify the administration server to contact",
+ dest='force_host',
+ default=None)
+ options, args = parser.parse_args(argv)
+
+ if len(args) < 2:
+ parser.print_usage()
+ sys.exit(0)
+
+ domain = args[1]
+ if options.force_host:
+ print "configuring domain [%s] at %s" % (domain, options.force_host)
+ else:
+ print "configuring domain [%s]" % domain
+
+
+ adm = DeliveranceAdmin(domain, force_host=options.force_host)
+
+
+ repl(adm)
+
+if __name__ == '__main__':
+ main(sys.argv)
More information about the z3-checkins
mailing list