#!/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 = os.environ.get('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 _edit(self, data): # Edit using external editor. tempname = tempfile.mktemp() open(tempname, 'w').write(data) os.system('%s %s' % (EDITOR, tempname)) new_data = open(tempname).read() os.unlink(tempname) return new_data def _getClient(self, id): client = ZPublisher.Client.Object( self.base_url + self.curr_folder + '/' + id, username=self.username, password=self.password) return client # # Basic commands. # def do_ls(self, arg): l = eval(self.client.objectIds()[1]) l.sort() for id in l: print id def do_cd(self, id): if id == '..': new_folder = '/' + string.join( string.split(self.curr_folder, '/')[0:-1], '/') else: new_folder = self.curr_folder + '/' + id 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, id): # Check if document exists already id_list = eval(self.client.objectIds()[1]) if id in id_list: client = self._getClient(id) old_data = client.document_src()[1] create_document = 0 else: old_data = '' create_document = 1 new_data = self._edit(old_data) if create_document: try: self.client.manage_addDTMLDocument(id=id) except: # There's an exception even in the regular case. (FIXME) pass client = self._getClient(id) while 1: ok = 0 try: client.manage_upload(file=new_data) ok = 1 except: traceback.print_exc() if ok: break new_data = self._edit(new_data) do_emacs = do_joe = do_nedit = do_vi def do_more(self, id): client = self._getClient(id) data = client.document_src()[1] tempname = tempfile.mktemp() open(tempname, 'w').write(data) os.system('more %s' % tempname) os.unlink(tempname) def do_rm(self, id): self.client.manage_delObjects(ids=[id]) def do_mkdir(self, id): self.client.manage_addFolder(id=id) def do_quit(self, arg): print sys.exit() do_EOF = do_quit # # Toing with properties. # def do_lsprop(self, id): client = self._getClient(id) l = eval(client.propertyItems()[1]) for k, v in l: print "%s: %s" % (k, str(v)) def do_editprop(self, arg): id, property_id = string.split(arg, ' ') client = self._getClient(id) old_data = client.getProperty(id=property_id)[1] new_data = self._edit(old_data) apply(client.manage_changeProperties, (), {property_id: new_data}) 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()