[Zope] export product outside zope

John Hunter jdhunter@ace.bsd.uchicago.edu
Tue, 14 May 2002 11:26:08 -0500


I have just finished a python script that solve the problem of
exporting a remote zope folder to the local filesystem.  The script
takes a number of command line arguments to specify the remote
server/folder/port and the local output dir.  You can call this script
from cron if you trust hiding your password in the /etc/cron.daily
root folder.  Otherwise, you can supply it to a getpass call

Hope someone else finds it useful...

---- begin zope_export.p ----
#!/usr/local/bin/python
# Command line args:
#  -d              optional -- debug mode.  If supplied, then just print the wget command
#  -s SERVER       optional -- default 'localhost' 
#  -f FOLDER       required -- remote folder to backup
#  -o LOCALDIR     optional --  local output directory, default '.'
#  -p PORT         optional --  default 8080 
#  -u USER:PASS    optional --  default prompt; username and password in format 'user:pass'
#
# Example usage:
#   ./zope_export.py -f 'SRP2002/Calendar' -s myhost.com -o /backup/

import os, time, sys, getopt, getpass

optlist, args = getopt.getopt(sys.argv[1:], 'df:o:p:u:s:')

# opts is a map from opts to default values.  You can overrise the
# default values, with, for example, myscript.py -s myhost.com.  The
# options with default value None must be supplied at the command line
# or an error will be generated.
opts = {} 
opts['d'] = 0                       # debug mode
opts['s'] = 'localhost'             # remote zope server   
opts['f'] = None                    # remote folder to backup
opts['o'] = '.'                     # output directory
opts['p'] = 8080                    # zope port
opts['u'] = 0                    # username:passwd

for (opt, val) in optlist:
    #for binary opts, convert to 1
    if val == '':
        val = 1
    opts[opt[1]] = val

for (key, val) in opts.items():
    if val==None:
        print 'Error: You must supply the -%s option' % key
        sys.exit()

if opts['u']==0:
    print 'Supply a username and password for %s in format user:pass' % opts['s']
    opts['u'] = getpass.getpass('User:Pass ')

    
folder = opts['f']
server = opts['s']
port = opts['p']
userpass = opts['u']
outDir = opts['o']
debug = opts['d']
script = 'manage_exportObject'
cgi = """download%3Aint=1&submit=Export"""

ymd = time.strftime('%Y-%m-%d', time.gmtime())
outFile = '%s/%s.%s.zexp' % ( outDir, folder.replace('/', '.'),  ymd )
url = 'http://%s@%s:%d/%s/%s' % \
      ( userpass, server, port, folder, script)

if len(cgi)>0:
    url = url + '?' + cgi
    
command = 'wget -q -O %s %s' % ( outFile, url)
if debug:
    print command
else:
    os.popen(command)

---- end zope_export.p ----



Here is an example file from /etc/cron.daily which calls this script.
If you hardcode your user:pass, make sure you make the file 'chmod
600'

--- begin /etc/cron.daily/zope_course_export ---
#!/bin/sh
# Export the course folder to /backup/
/usr/local/bin/python2.1 /usr/local/bin/zope_export.py -u myuser:mypass -o /backup -f SRP2002 -s myhost.com
--- end /etc/cron.daily/zope_course_export ---