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

Chris McDonough chrism@zope.com
Sat, 5 Oct 2002 22:45:41 -0400


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

Added Files:
      Tag: chrism-install-branch
	ZctlLib.py 
Log Message:
Factoring out meat of zctl.


=== Added File Zope/lib/python/Controller/ZctlLib.py === (710/810 lines abridged)
##############################################################################
#
# 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
#
##############################################################################
"""
    Zope appserver controller.  This is akin to apache's apachectl,
    except with an interactive interpreter if no commands are specified
    on the command-line.
"""

__version__ = '$Revision$'[11:-2]

def start(config_location, zope_home, software_home):
    """ Called by stub zctl.py in an instance_home """
    global CONFIG_LOCATION
    global ZOPE_HOME
    global ZOPE_PY_LOCATION
    global SOFTWARE_HOME
    CONFIG_LOCATION = config_location
    ZOPE_HOME     = zope_home
    SOFTWARE_HOME = software_home
    ZOPE_PY_LOCATION = os.path.join(ZOPE_HOME, 'bin', 'zope.py')
    _ZopeCtlCmd().main()

import getopt, os, sys, re, signal, cmd, time
try:
    import readline
except:
    readline = None

_marker = []

from Directives import DirectiveRegistry
import TextBlockFormatter

USAGE = """\

zctl:  Zope appserver controller

Usage:
    zctl [-h | --help] [--config-location=filepath or url]


[-=- -=- -=- 710 lines omitted -=- -=- -=-]

    def main( self ):
        try:
            longopts = ['help', 'config-location=']
            o, a = getopt.getopt( sys.argv[1:], 'h', longopts)
        except getopt.GetoptError, v:
            print v
            self.usage()
            sys.exit(127)
        
        for k, v in o:
            if k in ('-h', '--help'):
                self.usage()
                sys.exit(0)
            elif k == '--config-location':
                config_location = v
        reg = DirectiveRegistry
        reg.reconfigure(CONFIG_LOCATION)
        self._engine._setConfig(reg)
        self._engine._setCommandLineOpts(o)
        if a:
            self.cmdqueue.append( ' '.join( a ) )
            self.cmdqueue.append( 'EOF' )

        self.cmdloop()

def get_pids(filename):
    for line in open(filename).readlines():
        pids = line.split()
        if pids:
            return [ int(x.strip()) for x in pids ]

def kill(pid, sig):
    if sys.platform == 'win32':
        # we ignore the signal on win32
        import win32api
        handle = win32api.OpenProcess(1, 0, pid)
        try:
            return (0 != win32api.TerminateProcess(handle, 0))
        except:
            return 1
    else:
        try:
            os.kill(pid, sig)
        except OSError, why:
            return 1
        else:
            return 0