#!/usr/bin/python '''zopesh: Zope Shell. Usage: zopesh [-u username] [-p password] [-h host] [-p port]. Then type 'help' for a list of commands. Copyright 2000 Stéfane Fermigier, www.fermigier.com. Use it at your own risk under the Python (www.python.org) license.. ''' __version__ = '$Revision: 1.1 $'[11:-2] import sys, getopt, cmd, os, tempfile, string, re import ZPublisher.Client EDITOR = 'vi' class Shell(cmd.Cmd): def __init__(self, base_url, username, password): self.base_url = base_url self.username = username self.password = password self.curr_folder = '/' self.client = ZPublisher.Client.Object( self.base_url, username=self.username, password=self.password) def emptyline(self): pass def do_ls(self, arg): l = eval(self.client.objectIds()[1]) l.sort() for id in l: print id def do_cd(self, arg): if arg == '..': new_folder = '/' + string.join( string.split(self.curr_folder, '/')[0:-1], '/') else: new_folder = self.curr_folder + '/' + arg new_folder = re.sub('//', '/', new_folder) try: client = ZPublisher.Client.Object( self.base_url + new_folder, username=self.username, password=self.password) self.client = client self.curr_folder = new_folder except: traceback.print_exc() def do_pwd(self, arg): print self.curr_folder def do_vi(self, arg): try: client = ZPublisher.Client.Object( self.base_url + self.curr_folder + '/' + arg, username=self.username, password=self.password) old_data = client.document_src()[1] create_document = 0 except: create_document = 1 old_data = '' tempname = tempfile.mktemp() open(tempname, 'w').write(old_data) os.system('%s %s' % (EDITOR, tempname)) new_data = open(tempname).read() os.unlink(tempname) if create_document: try: self.client.manage_addDTMLDocument(id=arg) except: # There's an exception even in the regular case. (FIXME) pass client = ZPublisher.Client.Object( self.base_url + self.curr_folder + '/' + arg, username=self.username, password=self.password) client.manage_upload(file=new_data) do_emacs = do_joe = do_nedit = do_vi def do_rm(self, arg): self.client.manage_delObjects(ids=[arg]) def do_mkdir(self, arg): self.client.manage_addFolder(id=arg) def do_quit(self, arg): print sys.exit() do_EOF = do_quit def main(): host = 'localhost' user = 'superuser' port = 80 password = '' opts, args = getopt.getopt(sys.argv[1:], 'P:p:u:h:') for name, value in opts: if name == '-p': password = value elif name == '-P': port = int(value) elif name == '-u': user = value elif name == '-h': host = value else: print __doc__ sys.exit(1) shell = Shell('http://%s:%d' % (host, port), user, password) shell.cmdloop() if __name__ == '__main__': main()