[CalendarServer-changes] [15642] twext/trunk/twext/python/usage.py

source_changes at macosforge.org source_changes at macosforge.org
Fri May 27 16:22:52 PDT 2016


Revision: 15642
          http://trac.calendarserver.org//changeset/15642
Author:   wsanchez at apple.com
Date:     2016-05-27 16:22:51 -0700 (Fri, 27 May 2016)
Log Message:
-----------
API cleanup

Modified Paths:
--------------
    twext/trunk/twext/python/usage.py

Modified: twext/trunk/twext/python/usage.py
===================================================================
--- twext/trunk/twext/python/usage.py	2016-05-27 22:49:34 UTC (rev 15641)
+++ twext/trunk/twext/python/usage.py	2016-05-27 23:22:51 UTC (rev 15642)
@@ -64,10 +64,33 @@
             options = None
 
         instance = cls(options)
+        try:
+            instance.run()
+        except Exception as e:
+            exit(ExitStatus.EX_SOFTWARE, "Error: {}\n".format(e))
 
-        pidFile = options.get("pidFile")
 
-        if options.get("kill"):
+    def __init__(self, options):
+        self.options = options
+
+
+    def run(self):
+        self.postOptions()
+        self.optionallyKill()
+        self.writePIDFile()
+        self.startLogging()
+        self.runReactor()
+        self.shutDown()
+
+
+    def postOptions(self):
+        pass
+
+
+    def optionallyKill(self):
+        pidFile = self.options.get("pidFile")
+
+        if self.options.get("kill"):
             if pidFile is None:
                 exit(ExitStatus.EX_USAGE, "--pid-file required to use --kill")
             else:
@@ -82,28 +105,21 @@
                 except ValueError:
                     exit(ExitStatus.EX_DATAERR, "Invalid pid file.")
 
-            instance.startLogging()
-            cls.log.info("Terminating process: {pid}", pid=pid)
+            self.startLogging()
+            self.log.info("Terminating process: {pid}", pid=pid)
 
             kill(pid, signal.SIGTERM)
 
             exit(ExitStatus.EX_OK)
 
+
+    def writePIDFile(self):
+        pidFile = self.options.get("pidFile")
         if pidFile is not None:
             pid = getpid()
             pidFile.setContent(u"{}\n".format(pid).encode("utf-8"))
 
-        instance.startLogging()
-        from twisted.internet import reactor
-        reactor.callWhenRunning(instance.whenRunning)
-        instance.run()
-        instance.tearDown()
 
-
-    def __init__(self, options):
-        self.options = options
-
-
     def startLogging(self):
         logFile = self.options.get("logFile", sys.stderr)
 
@@ -124,17 +140,18 @@
         globalLogBeginner.beginLoggingTo([filteringObserver])
 
 
-    def whenRunning(self):
-        pass
-
-
-    def run(self):
+    def runReactor(self):
         from twisted.internet import reactor
+        reactor.callWhenRunning(self.whenRunning)
         self.log.info("Starting reactor...")
         reactor.run()
 
 
-    def tearDown(self):
+    def whenRunning(self):
+        pass
+
+
+    def shutDown(self):
         pidFile = self.options.get("pidFile")
         if pidFile is not None:
             pidFile.remove()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20160527/17e954a4/attachment.html>


More information about the calendarserver-changes mailing list