[Zodb-checkins] CVS: ZODB3/ZEO - util.py:1.1 start.py:1.38

Jeremy Hylton jeremy@zope.com
Thu, 1 Aug 2002 14:50:29 -0400


Update of /cvs-repository/ZODB3/ZEO
In directory cvs.zope.org:/tmp/cvs-serv10481

Modified Files:
	start.py 
Added Files:
	util.py 
Log Message:
Factor out some startup code to util module.

Add a couple of simple tests that make sure that start.py script
runs.  The tests are meager -- most of the possible arguments are
untested -- and only run on Unix.


=== Added File ZODB3/ZEO/util.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
# 
##############################################################################
"""Utilities for setting up the server environment."""

import os

def parentdir(p, n=1):
    """Return the parent of p, n levels up."""
    d = p
    while n:
        d = os.path.split(d)[0]
        if not d or d == '.':
            d = os.getcwd()
        n -= 1
    return d

class Environment:

    def __init__(self, argv0):
        v = os.environ.get("INSTANCE_HOME")
        if v is None:
            # looking for a Zope/var directory assuming that this code
            # is installed in Zope/lib/python/ZEO
            p = parentdir(argv0, 4)
            if os.path.isdir(os.path.join(p, "var")):
                v = p
            else:
                v = os.getcwd()
        self.home = v
        self.var = os.path.join(v, "var")
        if not os.path.isdir(self.var):
            self.var = self.home

        pid = os.environ.get("ZEO_SERVER_PID")
        if pid is None:
            pid = os.path.join(v, "ZEO_SERVER.pid")
        self.zeo_pid = pid    
        
        self.fs = os.path.join(self.var, "Data.fs")


=== ZODB3/ZEO/start.py 1.37 => 1.38 ===
     return d
 
 def get_storage(m, n, cache={}):
-    p=sys.path
+    p = sys.path
     d, m = os.path.split(m)
     if m.endswith('.py'):
         m = m[:-3]
@@ -102,6 +102,8 @@
 
     global LOG, INFO, ERROR
     from zLOG import LOG, INFO, ERROR, PANIC
+    from ZEO.util import Environment
+    env = Environment(me)
 
     # XXX hack for profiling support
     global unix, storages, zeo_pid, asyncore
@@ -116,23 +118,6 @@
         args.append(a)
         last = a
 
-    if os.environ.has_key('INSTANCE_HOME'):
-        INSTANCE_HOME = os.environ['INSTANCE_HOME']
-    elif os.path.isdir(os.path.join(directory(me, 4),'var')):
-        INSTANCE_HOME = directory(me, 4)
-    else:
-        INSTANCE_HOME = os.getcwd()
-
-    if os.path.isdir(os.path.join(INSTANCE_HOME, 'var')):
-        var = os.path.join(INSTANCE_HOME, 'var')
-    else:
-        var = INSTANCE_HOME
-
-    zeo_pid = os.environ.get('ZEO_SERVER_PID',
-                             os.path.join(var, 'ZEO_SERVER.pid'))
-
-    fs = os.path.join(var, 'Data.fs')
-
     usage="""%s [options] [filename]
 
     where options are:
@@ -175,7 +160,7 @@
           -s flag.
 
     if no file name is specified, then %s is used.
-    """ % (me, fs)
+    """ % (me, env.fs)
 
     try:
         opts, args = getopt.getopt(args, 'p:Dh:U:sS:u:P:d')
@@ -192,6 +177,7 @@
     UID = 'nobody'
     prof = None
     detailed = 0
+    fs = None
     for o, v in opts:
         if o =='-p':
             port = int(v)
@@ -225,7 +211,6 @@
             sys.exit(1)
         fs = args[0]
 
-##    __builtins__.__debug__ = debug
     if debug:
         os.environ['Z_DEBUG_MODE'] = '1'
     if detailed:
@@ -259,8 +244,8 @@
                 storages[n]=get_storage(m,a)
 
         if not storages:
-            import ZODB.FileStorage
-            storages['1']=ZODB.FileStorage.FileStorage(fs)
+            from ZODB.FileStorage import FileStorage
+            storages['1'] = FileStorage(fs or env.fs)
 
         # Try to set up a signal handler
         setup_signals(storages)
@@ -280,7 +265,7 @@
         except:
             pass # getpid not supported
         else:
-            open(zeo_pid,'w').write("%s %s" % (ppid, pid))
+            open(env.zeo_pid,'w').write("%s %s" % (ppid, pid))
             
     except:
         # Log startup exception and tell zdaemon not to restart us.
@@ -324,6 +309,7 @@
     signal.signal(signal.SIGHUP, rotate_logs_handler)
 
 def shutdown(storages, die=1):
+    LOG("ZEO Server", INFO, "Received signal")
     import asyncore
 
     # Do this twice, in case we got some more connections
@@ -343,7 +329,6 @@
             pass
 
     try:
-        from zLOG import LOG, INFO
         s = die and "shutdown" or "restart"
         LOG('ZEO Server', INFO, "Shutting down (%s)" % s)
     except: