[Zope-Checkins] CVS: Zope/lib/python/Controller - Main.py:1.1.2.6 ZctlLib.py:1.1.2.2

Chris McDonough chrism@zope.com
Sun, 6 Oct 2002 00:32:38 -0400


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

Modified Files:
      Tag: chrism-install-branch
	Main.py ZctlLib.py 
Log Message:
Work on zctl:

 - zctl no longer uses os.system to spawn Zope.  Instead we use
   os.spawnv.  Hopefully this will make it easier on Win32 to 
   start Zope from zctl.

 - created a lock_file module that is shared by Zope and zctl.



=== Zope/lib/python/Controller/Main.py 1.1.2.5 => 1.1.2.6 ===
--- Zope/lib/python/Controller/Main.py:1.1.2.5	Wed Sep 18 16:46:47 2002
+++ Zope/lib/python/Controller/Main.py	Sun Oct  6 00:32:38 2002
@@ -15,6 +15,7 @@
 import os, sys, codecs, string, socket, re, warnings
 from types import StringType, IntType
 import Directives
+from lock_file import lock_file
 _marker = []
 
 def start_zope(config_location):
@@ -523,20 +524,4 @@
 
     return out
 
-try:
-    import fcntl
-    def lock_file(file):
-        un=file.fileno()
-        fcntl.flock(un, fcntl.LOCK_EX | fcntl.LOCK_NB)
-except:
-    # Try windows-specific code:
-    try:
-        from winlock import LockFile
-        def lock_file(file):
-            un=file.fileno()
-            LockFile(un,0,0,1,0) # just lock the first byte, who cares
-    except:
-        # we don't understand any kind of locking, forget it
-        def lock_file(file):
-            pass
     


=== Zope/lib/python/Controller/ZctlLib.py 1.1.2.1 => 1.1.2.2 ===
--- Zope/lib/python/Controller/ZctlLib.py:1.1.2.1	Sat Oct  5 22:45:40 2002
+++ Zope/lib/python/Controller/ZctlLib.py	Sun Oct  6 00:32:38 2002
@@ -40,6 +40,7 @@
 
 from Directives import DirectiveRegistry
 import TextBlockFormatter
+from lock_file import lock_file
 
 USAGE = """\
 
@@ -65,8 +66,6 @@
     help [<command>]
     start
     stop
-    start_zeo
-    stop_zeo
     restart
     logopenclose
     status
@@ -101,131 +100,32 @@
         """
         if self._getDirective('use_zeo_server'):
             
-            # Is the ZEO server up? start it if not
+            # Is the ZEO server up?
             zeo_host, zeo_port, zeo_socket = self._getZEOConfig()
             if not self._checkService(zeo_host, zeo_port, zeo_socket):
-                self.start_zeo(arg)
+                self._report('Error: the ZEO server at %s, port %s, socket %s '
+                             'is not responding.  You may need to start it.'
+                             % (zeo_host, zeo_port, zeo_socket))
+                return
 
         else:
             lock_status = self.lockFile()
             if lock_status:
-                self._report('Cannot start Zope.  Another Zope instance has locked'
-                             ' the "%s" file.  Use "stop" to stop it.' % 
-                             self._getPidfileName())
-                self._report()
-                return
-
-        cmdline = '"%s" "%s" %s %s' % (
-            sys.executable,
-	    ZOPE_PY_LOCATION,
-            self._buildCommandLine(),
-            arg
-            )
-        self._doSystem('Starting Zope', cmdline)
-
-    def start_zeo(self, arg):
-        """
-            Start the ZEO server.
-
-            Syntax: start_zeo [options]
-
-            All options are passed to the ZEO start.py script.
-        """
-        zeo_host, zeo_port, zeo_socket = self._getZEOConfig()
-        my_args = []
-
-        pp = self._getDirective('python_path')
-        if pp:
-            paths = pp.split(':')
-
-            for path in paths:
-                sys.path.append(path)
-
-        try:
-            import ZEO
-
-            if self._checkService(zeo_host, zeo_port, zeo_socket):
-                self._report('ZEO server already running.')
-                self._report()
-                return
-
-            # ZEO not running. gotta start.
-            initpy_path = os.path.abspath(ZEO.__file__)
-            zeo_path = os.path.split(initpy_path)[0]
-            start_py = os.path.join(zeo_path, 'start.py')
-
-            if zeo_port and not zeo_socket:
-                my_args.append('-h %s' % zeo_host)
-                my_args.append('-p %s' % str(zeo_port))
-                msg = 'Starting ZEO on %s port %s' % (zeo_host, str(zeo_port))
-            elif zeo_socket and not zeo_port:
-                my_args.append('-U %s' % zeo_socket)
-                msg = 'Starting ZEO on socket %s' % zeo_socket
-            else:
-                self._report('ZEO misconfiguration. Please check your'
-                             ' zope.conf configuration file.')
+                self._report('Error:  cannot start Zope.  Another Zope '
+                             'instance has locked the "%s" file.  Use "stop" '
+                             'to stop it.' %  self._getPidfileName())
                 self._report()
                 return
 
-            cmdline = 'PYTHONPATH="%s"; "%s" "%s" %s %s &' % (
-                                           ':'.join(sys.path)
-                                          , sys.executable
-                                          , start_py
-                                          , ' '.join(my_args)
-                                          , arg
-                                          )
-
-            self._doSystem(msg, cmdline)
-
-            count = 0
-            while not self._checkService(zeo_host, zeo_port, zeo_socket):
-                count = count + 1
-                if count > 180:
-                    self._report('ZEO connection failure')
-                    self._report()
-                    return
-
-                self._report('Waiting for ZEO server to start... %d' % count)
-                time.sleep(1)
-
-            self._report('ZEO server ready')
-
-        except ImportError:
-            self._report('Cannot find the ZEO installation. Please make sure'
-                         ' that ZEO is installed and in your PYTHONPATH.')
-            return
-
-    def stop_zeo(self, arg):
-        """
-            Stop the ZEO server
-
-            Syntax: zeo_stop
-        """
-        client_home = self._getClientHome()
-        zeo_pidfile = os.path.join(client_home, 'ZEO_SERVER.pid')
+        self._report('Starting Zope')
+        args = self._getCommandLineArgs()
+        args = [ZOPE_PY_LOCATION] + args + [arg]
+        wait = self._getDirective('debug_mode')
         try:
-            pids = get_pids(zeo_pidfile)
-            pid = pids[-1]
-            status = kill(pid, signal.SIGTERM)
-
-            if status:
-                self._report('Could not stop ZEO (maybe already stopped or '
-                             'pending start?)')
-            else:
-                self._report('ZEO stopped successfully')
-
-            zeo_host, zeo_port, zeo_socket = self._getZEOConfig()
-            if zeo_socket:
-                try:
-                    os.remove(zeo_socket)
-                except OSError:
-                    pass
-
-            return status
-
-        except IOError:
-            self._report('Pid file %s could not be found' % zeo_pidfile)
-
+            apply(self._spawnPython, args, {'wait':wait})
+        except KeyboardInterrupt:
+            if not wait:
+                raise
 
     def restart( self, arg ):
         """
@@ -302,7 +202,14 @@
                 pstat = []
 
                 for port in ports:
-                    ok = self._checkService( 'localhost', port )
+                    try:
+                        portnum = int(port)
+                    except ValueError:
+                        portnum = None
+                    if portnum:
+                        ok = self._checkService( 'localhost', portnum )
+                    else:
+                        ok = 0
                     status = ok and 'OK' or 'dead'
                     pstat.append( '%5s (%s)' % ( port, status ) )
 
@@ -426,14 +333,16 @@
         """
         swhome = self._getSoftwareHome()
         zopehome = self._getZopeHome()
-        cmdline = (
-            '"%s" -c "import sys; sys.path.insert(0, \'%s\');'
-            'sys.path.insert(0, \'%s\'); import Zope; app=Zope.app();'
-            'execfile( \'%s\' )"'
-                  % ( sys.executable, zopehome, swhome, arg )
+        cmd = (
+            'import os, sys; os.environ["EVENT_LOG_SEVERITY"]="300"; '
+            'sys.path.insert(0, "%s"); sys.path.insert(0, "%s"); '
+            'import Controller.Directives; '
+            'Controller.Directives.DirectiveRegistry.reconfigure("%s");'
+            'import Zope; app=Zope.app(); execfile( "%s" )'
+                  % ( zopehome, swhome, CONFIG_LOCATION, arg )
                   )
- 
-        status = self._doSystem( 'Running script: %s' %arg, cmdline )
+        self._report( 'Running script: %s' % arg )
+        status = self._spawnPython('-c', cmd)
 
     def debug( self, arg ):
         """
@@ -443,15 +352,18 @@
         """
         swhome = self._getSoftwareHome()
         zopehome = self._getZopeHome()
-        cmdline = (
-            '"%s" -i -c "import sys; sys.path.insert(0, \'%s\');'
-            'sys.path.insert(0, \'%s\'); import Zope; app=Zope.app()"'
-                  % ( sys.executable, zopehome, swhome)
+        cmd = (
+            'import os, sys; os.environ["EVENT_LOG_SEVERITY"]="300"; '
+            'sys.path.insert(0, "%s"); sys.path.insert(0, "%s"); '
+            'import Controller.Directives;'
+            'Controller.Directives.DirectiveRegistry.reconfigure("%s");'
+            'import Zope; app=Zope.app()'
+                  % ( zopehome, swhome, CONFIG_LOCATION)
                   )
-
-        msg = ('Starting debugger.  The name "app" will be bound to the Zope'
+        msg = ('Starting debugger.  The name "app" will be bound to the Zope '
                '"root object" when you enter interactive mode.')
-        status = self._doSystem( msg, cmdline )
+        self._report( msg )
+        status = self._spawnPython('-i', '-c', cmd)
 
     #
     #   Helper functions
@@ -496,10 +408,22 @@
             l.append('%s=%s' % (k,v))
         return l
 
-    def _doSystem( self, message, cmdline ):
-        self._report( message )
-        self._report( '   ' + cmdline )
-        status = os.system( cmdline )
+##     def _doSystem( self, message, cmdline ):
+##         self._report( message )
+##         self._report( '   ' + cmdline )
+##         status = os.system( cmdline )
+##         return status
+
+    def _spawnPython(self, *args, **kw):
+        if not kw.has_key('wait'):
+            startup = os.P_WAIT
+        elif kw.has_key('wait') and kw['wait']:
+            startup = os.P_WAIT
+        else:
+            startup = os.P_NOWAIT
+        args = list(args)
+        args = [sys.executable] + args
+        status = os.spawnv(startup, sys.executable, args)
         return status
 
     def _checkService( self, host, port, socket_path=None ):
@@ -740,8 +664,6 @@
         return [a for a in meta if a.startswith(text) ]
     
     do_start         = _MAKEDO( 'start' )
-    do_start_zeo     = _MAKEDO( 'start_zeo' )
-    do_stop_zeo      = _MAKEDO( 'stop_zeo' )
     do_restart       = _MAKEDO( 'restart' )
     do_logopenclose  = _MAKEDO( 'logopenclose' )
     do_stop          = _MAKEDO( 'stop' )