]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding pp_lib/pidfile.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Aug 2017 10:06:30 +0000 (12:06 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Aug 2017 10:06:30 +0000 (12:06 +0200)
pp_lib/pidfile.py [new file with mode: 0644]

diff --git a/pp_lib/pidfile.py b/pp_lib/pidfile.py
new file mode 100644 (file)
index 0000000..466ba14
--- /dev/null
@@ -0,0 +1,118 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+@author: Frank Brehm
+@contact: frank.brehm@pixelpark.com
+@copyright: © 2017 by Frank Brehm, Berlin
+@summary: A module for a pidfile object.
+          It provides methods to define, check, create
+          and remove a pidfile.
+"""
+from __future__ import absolute_import
+
+# Standard modules
+import os
+import sys
+import logging
+
+import re
+import signal
+import errno
+
+# Third party modules
+import six
+from six import reraise
+
+# Own modules
+
+from .errors import ReadTimeoutError
+
+from .obj import PpBaseObjectError
+from .obj import PpBaseObject
+
+from .common import to_utf8
+
+__version__ = '0.1.1'
+
+LOG = logging.getLogger(__name__)
+
+# =============================================================================
+class PidFileError(PpBaseObjectError):
+    """Base error class for all exceptions happened during
+    handling a pidfile."""
+
+    pass
+
+
+# =============================================================================
+class InvalidPidFileError(PidFileError):
+    """An error class indicating, that the given pidfile is unusable"""
+
+    def __init__(self, pidfile, reason=None):
+        """
+        Constructor.
+
+        @param pidfile: the filename of the invalid pidfile.
+        @type pidfile: str
+        @param reason: the reason, why the pidfile is invalid.
+        @type reason: str
+
+        """
+
+        self.pidfile = pidfile
+        self.reason = reason
+
+    # -------------------------------------------------------------------------
+    def __str__(self):
+        """Typecasting into a string for error output."""
+
+        msg = None
+        if self.reason:
+            msg = "Invalid pidfile {!r} given: {}".format(self.pidfile, self.reason)
+        else:
+            msg = "Invalid pidfile {!r} given.".format(self.pidfile)
+
+        return msg
+
+# =============================================================================
+class PidFileInUseError(PidFileError):
+    """
+    An error class indicating, that the given pidfile is in use
+    by another application.
+    """
+
+    def __init__(self, pidfile, pid):
+        """
+        Constructor.
+
+        @param pidfile: the filename of the pidfile.
+        @type pidfile: str
+        @param pid: the PID of the process owning the pidfile
+        @type pid: int
+
+        """
+
+        self.pidfile = pidfile
+        self.pid = pid
+
+    # -------------------------------------------------------------------------
+    def __str__(self):
+        """Typecasting into a string for error output."""
+
+        msg = "The pidfile {!r} is currently in use by the application with the PID {}.".format(
+            self.pidfile, self.pid)
+
+        return msg
+
+
+
+
+# =============================================================================
+
+if __name__ == "__main__":
+
+    pass
+
+# =============================================================================
+
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list