Add --pidfile option to the devserver for easy daemon maintenance.

BUG=chromium:251309
TEST=Ran the unittests + manual with/without option.

Change-Id: Iaf08cc342119082582e791d37905009fc8843ba6
Reviewed-on: https://gerrit.chromium.org/gerrit/66548
Commit-Queue: Chris Sosa <sosa@chromium.org>
Reviewed-by: Chris Sosa <sosa@chromium.org>
Tested-by: Chris Sosa <sosa@chromium.org>
diff --git a/devserver.py b/devserver.py
index 9c34f28..dd02430 100755
--- a/devserver.py
+++ b/devserver.py
@@ -54,7 +54,8 @@
 from logging import handlers
 
 import cherrypy
-import cherrypy._cplogging
+from cherrypy import _cplogging as cplogging
+from cherrypy.process import plugins
 
 import autoupdate
 import common_util
@@ -698,7 +699,7 @@
 
     build_id, file_name = self._xbuddy.Get(args)
     if return_dir:
-      directory = os.path.join(cherrypy.request.base, 'static',  build_id)
+      directory = os.path.join(cherrypy.request.base, 'static', build_id)
       _Log("Directory requested, returning: %s", directory)
       return directory
     else:
@@ -946,6 +947,9 @@
   group.add_option('--logfile',
                    metavar='PATH',
                    help='log output to this file instead of stdout')
+  group.add_option('--pidfile',
+                   metavar='PATH',
+                   help='path to output a pid file for the server.')
   group.add_option('--production',
                    action='store_true', default=False,
                    help='have the devserver use production values when '
@@ -959,10 +963,7 @@
   hdlr_cls = handlers.TimedRotatingFileHandler
   hdlr = hdlr_cls(logfile, when=_LOG_ROTATION_TIME,
                   backupCount=_LOG_ROTATION_BACKUP)
-  # The cherrypy documentation says to use the _cplogging module for
-  # this, even though it's named as a private module.
-  # pylint: disable=W0212
-  hdlr.setFormatter(cherrypy._cplogging.logfmt)
+  hdlr.setFormatter(cplogging.logfmt)
   return hdlr
 
 
@@ -1063,6 +1064,9 @@
 
   dev_server = DevServerRoot(_xbuddy)
 
+  if options.pidfile:
+    plugins.PIDFile(cherrypy.engine, options.pidfile).subscribe()
+
   cherrypy.quickstart(dev_server, config=_GetConfig(options))