# Copyright 2001, Andy McKay, andym@activestate.com import sys, os, string, time, imp, getopt, urllib def usage(): print ''' Usage \bin\python -p [-h] is where Zope is installed. This is necessary so that python uses the correct PythonService. It also means you dont need a seperate version of python. -p the port you want this to run on eg: 8080 (required) -h print this help For more info contact andym@activestate.com ''' sys.exit() home=os.path.split(os.path.split(sys.executable)[0])[0] opts, args = getopt.getopt(sys.argv[1:], 'p:ih') if '-p' not in opts[0]: usage() for o, v in opts: if o=='-p': port=int(v) if o=='-h': usage() script = os.path.join(home, 'ZServer', 'ZService.py') python = os.path.join(home, 'bin', 'python.exe') # delete pyc, pyo def deleter(arg, dirname, filenames): files_to_delete = ['.pyc', '.pyo'] for file in filenames: if file[-4:] in files_to_delete: os.chmod(dirname + os.sep + file, 0666) os.unlink(dirname + os.sep + file) def alterstartbat(home, python): _file = os.path.join(home, 'start.bat') os.chmod(_file, 0666) data = '"%s" "%s" -D ' % (python, os.path.join(home, 'z2.py')) data = data + '%1 %2 %3 %4 %5 %6 %7 %8 %9' f = open(_file, 'w') f.write(data) f.close def isvar(home): dir = os.path.join(home, 'var') lock = os.path.join(home, 'var', 'Data.fs.lock') try: os.chdir(dir) print "no, already exists" except: os.mkdir(dir) f = open(lock, 'w') f.close print "yes, made and lock file written" # fiddle z2 def alterz2(home): _file = os.path.join(home, 'z2.py') os.chmod(_file, 0666) data = open(_file, 'r').read() txt = "swhome=r'" start = string.find(data, txt) if start < 0: raise "Error", "Couldnt locate swhome in z2 to edit" end = string.find(data[start+len(txt):], "'") file = data[start+len(txt):start+len(txt)+end] data = string.replace(data, file, home) f = open(_file, 'w') f.write(data) f.close # fiddle with svcname def fiddlesvc(home): _file = os.path.join(home, 'ZServer', 'svcname.txt') os.chmod(_file, 0666) f = open(_file, 'w') f.write('%s:%s' % (string.split(home, os.sep)[-1],port)) f.close # install service def installsvc(home, port, _scr, _py): _file = os.path.join(home, 'bin', 'lib', 'win32', 'PythonService.exe') _cmd = '%s /register' % _file os.system(_cmd) _cmd = '%s %s install' % (_py, _scr) os.system(_cmd) def removesvc(_scr, _py): _cmd = '%s %s remove' % (_py, _scr) os.system(_cmd) def stopsvc(_scr, _py): _cmd = '%s %s stop' % (_py, _scr) os.system(_cmd) #start def startsvc(_scr, _py): _cmd = '%s %s start' % (_py, _scr) os.system(_cmd) print "Deleting compiled files...." os.path.walk(home, deleter, None) print "Need to make var?...," isvar(home) print "Altering z2.py and start.bat..." alterz2(home) alterstartbat(home, python) print "Setting service name..." fiddlesvc(home) print "\nRemoving service..." removesvc(script, python) print "\nInstalling service..." installsvc(home, port, script, python) print "\n(Re)starting service..." stopsvc(script, python) time.sleep(3) startsvc(script, python) print "Installation finished"