[Zope-Checkins] CVS: ZODB3/zdaemon - zdaemon.py:1.16

Guido van Rossum guido@python.org
Wed, 13 Nov 2002 17:39:38 -0500


Update of /cvs-repository/ZODB3/zdaemon
In directory cvs.zope.org:/tmp/cvs-serv17286

Modified Files:
	zdaemon.py 
Log Message:
More refactoring:
- create a subprocess management class
- turned logging methods into functions a la PEP 282
- turned a bunch of stateless helper functions into functions


=== ZODB3/zdaemon/zdaemon.py 1.15 => 1.16 === (548/648 lines abridged)
--- ZODB3/zdaemon/zdaemon.py:1.15	Wed Nov 13 12:51:14 2002
+++ ZODB3/zdaemon/zdaemon.py	Wed Nov 13 17:39:37 2002
@@ -83,8 +83,11 @@
 
     """A class to parse and hold the command line options.
 
-    Options are represented by instance attributes.
+    Options are represented by various attributes (backofflimit etc.).
     Positional arguments are represented by the args attribute.
+
+    This also has a public usage() method that can be used to report
+    errors related to the command line.
     """
 
     progname = "zdaemon.py"             # Program name for usage message
@@ -129,7 +132,7 @@
         When -h is detected, print the module docstring to stdout and exit(0).
         """
         for o, a in self.opts:
-            # Alphabetical order please!
+            # Keep these in alphabetical order please!
             if o == "-b":
                 try:
                     self.backofflimit = float(a)
@@ -163,6 +166,109 @@
         sys.stderr.write("For help, use %s -h\n" % self.progname)
         sys.exit(2)
 
+class Subprocess:
+
+    """A class to manage a subprocess."""
+
+    # Initial state; overridden by instance variables
+    pid = 0 # Subprocess pid; 0 when not running
+    lasttime = 0 # Last time the subprocess was started; 0 if never
+
+    def __init__(self, opts, args=None):
+        """Constructor.
+
+        Arguments are an Options instance and a list of program
+        arguments; the latter's first item must be the program name.
+        """
+        if args is None:
+            args = opts.args
+        if not args:
+            opts.usage("missing 'program' argument")
+        self.opts = opts
+        self.args = args
+        self._set_filename(args[0])
+

[-=- -=- -=- 548 lines omitted -=- -=- -=-]

+        msg = "unknown termination cause 0x%04x" % sts
+        return -1, msg
 
-    def problem(self, msg):
-        self.log(msg, zLOG.PROBLEM)
+_signames = None
 
-    def error(self, msg, error=None):
-        self.log(msg, zLOG.ERROR, error)
+def signame(sig):
+    """Return a symbolic name for a signal.
 
-    def panic(self, msg, error=None):
-        self.log(msg, zLOG.PANIC, error)
+    Return "signal NNN" if there is no corresponding SIG name in the
+    signal module.
+    """
 
-    def getsubsystem(self):
-        return "ZD:%d" % os.getpid()
+    if _signames is None:
+        _init_signames()
+    return _signames.get(sig) or "signal %d" % sig
+
+def _init_signames():
+    global _signames
+    d = {}
+    for k, v in signal.__dict__.items():
+        k_startswith = getattr(k, "startswith", None)
+        if k_startswith is None:
+            continue
+        if k_startswith("SIG") and not k_startswith("SIG_"):
+            d[v] = k
+    _signames = d
+
+def get_path():
+    """Return a list corresponding to $PATH, or a default."""
+    path = ["/bin", "/usr/bin", "/usr/local/bin"]
+    if os.environ.has_key("PATH"):
+        p = os.environ["PATH"]
+        if p:
+            path = p.split(os.pathsep)
+    return path
 
-    def log(self, msg, severity=zLOG.INFO, error=None):
-        zLOG.LOG(self.getsubsystem(), severity, msg, "", error)
+# Main program
 
 def main(args=None):
     d = Daemonizer()