From 781bce6ce7e8f657aaf7bce3a56ef107fb2d6a81 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 4 Aug 2017 12:06:30 +0200 Subject: [PATCH] Adding pp_lib/pidfile.py --- pp_lib/pidfile.py | 118 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 pp_lib/pidfile.py diff --git a/pp_lib/pidfile.py b/pp_lib/pidfile.py new file mode 100644 index 0000000..466ba14 --- /dev/null +++ b/pp_lib/pidfile.py @@ -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 -- 2.39.5