[Zope-Checkins] CVS: Zope/lib/python - zope.py:1.1.2.1

Chris McDonough chrism@zope.com
Mon, 14 Oct 2002 02:33:03 -0400


Update of /cvs-repository/Zope/lib/python
In directory cvs.zope.org:/tmp/cvs-serv16813/lib/python

Added Files:
      Tag: chrism-install-branch
	zope.py 
Log Message:
Move zope.py to SOFTWARE_HOME.

Make it possible to pass all directives on the command line to
zope.py as well as zctl.py.



=== Added File Zope/lib/python/zope.py ===
##############################################################################
#
# Copyright (c) 2001 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
#
##############################################################################
"""
This is a replacement for z2.py which can make use of a config file to obtain
configuration values (instead of environment variables).  It is
meant to be run from a shell or exec'ed by another program.

Do not change this file unless you know what you're doing. ;-)
"""

import os, sys, getopt, traceback

# assume that this file is sys.argv[0], and that it lives in
# SOFTWARE_HOME

SOFTWARE_HOME=os.path.split(os.path.abspath(os.path.normpath(sys.argv[0])))[0]
if SOFTWARE_HOME not in sys.path:
    sys.path.insert(0, SOFTWARE_HOME)

def usage():
    print """
zope.py [--config-location=filename-or-url] [--directive=value ...]

zope.py starts a Zope instance.

If the config-location option is not passed, an attempt is made to obtain
configuration info from a file in the current working directory named
'zope.conf;.  An alternate absolute or relative filename may be provided, or
a URL may be provided.   The URL should return a file in the same format as the
filesystem config file.

Directives passed in the options list override those found in the config file.
An example of a directive is "--client_home=/var/client_home".  A
complete list of directives can be found in any 'zope.conf' config file.
"""

if __name__ == '__main__':
    try:
        import Controller
    except ImportError:
        print ("Could not import the Zope 'Controller' package.  This "
               "may indicate that the zope.py file has been relocated "
               "from its typical location inside the Zope 'SOFTWARE_HOME'. ")
        usage()
        sys.exit(1)

    config_location = 'zope.conf'
    overrides = {}

    # preregister default directives
    from Controller.Directives import DirectiveRegistry
    DirectiveRegistry.registerDefaults()

    # add directives to options list
    directives = DirectiveRegistry.namesByPriority()
    longopts = [ "help", "config-location="]
    for name in directives:
        longopts.append('%s=' % name)

    try:
        opts, args = getopt.getopt(sys.argv[1:], "h", longopts)
    except getopt.GetoptError, v:
        print v
        usage()
        sys.exit(127)
    for k, v in opts:
        if k in ('-h', '--help'):
            usage()
            sys.exit(0)
        elif k == '--config-location':
            config_location = v
        else:
            if k.startswith('--'):
                k = k[2:]
                overrides[k] = v

    # reconfigure registry with config filename and overrides
    DirectiveRegistry.reconfigure(config_location, overrides)

    # start Zope
    try:
        from Controller import Main
        Main.start_zope()
    except SystemExit:
        pass
    except:
        try:
            import zLOG
            zLOG.LOG("Zope Initialization", zLOG.PANIC, "Startup exception",
                     error=sys.exc_info())
        except:
            pass
        traceback.print_exc()
        # tell zdaemon not to restart us by setting 255 exit code
        sys.exit(255)