"""
@author: Frank Brehm
@contact: frank.brehm@pixelpark.com
-@copyright: © 2018 by Frank Brehm, Berlin
+@copyright: © 2021 by Frank Brehm, Berlin
@summary: A module for a pidfile object.
It provides methods to define, check, create
and remove a pidfile.
from fb_tools.common import to_utf8
from fb_tools.obj import FbBaseObjectError, FbBaseObject
-__version__ = '0.3.0'
+from .xlate import XLATOR
+
+__version__ = '0.3.1'
LOG = logging.getLogger(__name__)
+_ = XLATOR.gettext
+
+
# =============================================================================
class PidFileError(FbBaseObjectError):
"""Base error class for all exceptions happened during
msg = None
if self.reason:
- msg = "Invalid pidfile {!r} given: {}".format(self.pidfile, self.reason)
+ msg = _("Invalid pidfile {f!r} given: {r}").format(
+ f=self.pidfile, r=self.reason)
else:
- msg = "Invalid pidfile {!r} given.".format(self.pidfile)
+ msg = _("Invalid pidfile {!r} given.").format(self.pidfile)
return msg
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)
+ msg = _(
+ "The pidfile {f!r} is currently in use by the application with the PID {p}.").format(
+ f=self.pidfile, p=self.pid)
return msg
)
if not filename:
- raise ValueError('No filename given on initializing PidFile object.')
+ raise ValueError(_(
+ 'No filename given on initializing {} object.').format('PidFile'))
self._filename = os.path.abspath(str(filename))
"""
if not os.path.exists(self.filename):
if self.verbose > 3:
- LOG.debug("Pidfile {!r} doesn't exists, not removing.".format(self.filename))
+ LOG.debug(_("Pidfile {!r} doesn't exists, not removing.").format(self.filename))
return
if not self.auto_remove:
if self.verbose > 3:
- LOG.debug("Auto removing disabled, don't deleting {!r}.".format(self.filename))
+ LOG.debug(_("Auto removing disabled, don't deleting {!r}.").format(self.filename))
return
if self.verbose > 1:
- LOG.debug("Removing pidfile {!r} ...".format(self.filename))
+ LOG.debug(_("Removing pidfile {!r} ...").format(self.filename))
if self.simulate:
if self.verbose > 1:
- LOG.debug("Just kidding ..")
+ LOG.debug(_("Just kidding .."))
return
try:
os.remove(self.filename)
except OSError as e:
- LOG.err("Could not delete pidfile {!r}: {}".format(self.filename, e))
+ LOG.err(_("Could not delete pidfile {f!r}: {e}").format(f=self.filename, e=e))
except Exception as e:
self.handle_error(str(e), e.__class__.__name__, True)
if pid:
pid = int(pid)
if pid <= 0:
- msg = "Invalid PID {} for creating pidfile {!r} given.".format(pid, self.filename)
+ msg = _("Invalid PID {p} for creating pidfile {f!r} given.").format(
+ p=pid, f=self.filename)
raise PidFileError(msg)
else:
pid = os.getpid()
if self.check():
- LOG.info("Deleting pidfile {!r} ...".format(self.filename))
+ LOG.info(_("Deleting pidfile {!r} ...").format(self.filename))
if self.simulate:
- LOG.debug("Just kidding ..")
+ LOG.debug(_("Just kidding .."))
else:
try:
os.remove(self.filename)
raise InvalidPidFileError(self.filename, str(e))
if self.verbose > 1:
- LOG.debug("Trying opening {!r} exclusive ...".format(self.filename))
+ LOG.debug(_("Trying opening {!r} exclusive ...").format(self.filename))
if self.simulate:
- LOG.debug("Simulation mode - don't real writing in {!r}.".format(self.filename))
+ LOG.debug(_("Simulation mode - don't real writing in {!r}.").format(self.filename))
self._created = True
return
self.filename, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644)
except OSError as e:
error_tuple = sys.exc_info()
- msg = "Error on creating pidfile {!r}: {}".format(self.filename, e)
+ msg = _("Error on creating pidfile {f!r}: {e}").format(f=self.filename, e=e)
reraise(PidFileError, msg, error_tuple[2])
if self.verbose > 2:
- LOG.debug("Writing {} into {!r} ...".format(pid, self.filename))
+ LOG.debug(_("Writing {p} into {f!r} ...").format(p=pid, f=self.filename))
out = to_utf8("%d\n" % (pid))
try:
"""
if not self.created:
- msg = "Calling recreate() on a not self created pidfile."
+ msg = _("Calling {} on a not self created pidfile.").format('recreate()')
raise PidFileError(msg)
if pid:
pid = int(pid)
if pid <= 0:
- msg = "Invalid PID {} for creating pidfile {!r} given.".format(pid, self.filename)
+ msg = "Invalid PID {p} for creating pidfile {f!r} given.".format(
+ p=pid, f=self.filename)
raise PidFileError(msg)
else:
pid = os.getpid()
if self.verbose > 1:
- LOG.debug("Trying opening {!r} for recreate ...".format(self.filename))
+ LOG.debug(_("Trying opening {!r} for recreate ...").format(self.filename))
if self.simulate:
- LOG.debug("Simulation mode - don't real writing in {!r}.".format(self.filename))
+ LOG.debug(_("Simulation mode - don't real writing in {!r}.").format(self.filename))
return
fh = None
fh = open(self.filename, 'w', **self.open_args)
except OSError as e:
error_tuple = sys.exc_info()
- msg = "Error on recreating pidfile {!r}: {}".format(self.filename, e)
+ msg = _("Error on recreating pidfile {f!r}: {e}").format(f=self.filename, e=e)
reraise(PidFileError, msg, error_tuple[2])
if self.verbose > 2:
- LOG.debug("Writing {} into {!r} ...".format(pid, self.filename))
+ LOG.debug(_("Writing {p} into {f!r} ...").format(p=pid, f=self.filename))
try:
fh.write("%d\n" % (pid))
if not os.path.exists(self.filename):
if not os.path.exists(self.parent_dir):
- reason = "Pidfile parent directory {!r} doesn't exists.".format(
+ reason = _("Pidfile parent directory {!r} doesn't exists.").format(
self.parent_dir)
raise InvalidPidFileError(self.filename, reason)
if not os.path.isdir(self.parent_dir):
- reason = "Pidfile parent directory {!r} is not a directory.".format(
+ reason = _("Pidfile parent directory {!r} is not a directory.").format(
self.parent_dir)
raise InvalidPidFileError(self.filename, reason)
if not os.access(self.parent_dir, os.X_OK):
- reason = "No write access to pidfile parent directory {!r}.".format(
+ reason = _("No write access to pidfile parent directory {!r}.").format(
self.parent_dir)
raise InvalidPidFileError(self.filename, reason)
return False
if not os.path.isfile(self.filename):
- reason = "It is not a regular file."
+ reason = _("It is not a regular file.")
raise InvalidPidFileError(self.filename, self.parent_dir)
# ---------
return ReadTimeoutError(self.timeout, self.filename)
if self.verbose > 1:
- LOG.debug("Reading content of pidfile {!r} ...".format(self.filename))
+ LOG.debug(_("Reading content of pidfile {!r} ...").format(self.filename))
signal.signal(signal.SIGALRM, pidfile_read_alarm_caller)
signal.alarm(self.timeout)
if match:
pid = int(match.group(1))
else:
- msg = "No useful information found in pidfile {!r}: {!r}".format(self.filename, line)
+ msg = _("No useful information found in pidfile {f!r}: {z!r}").format(
+ f=self.filename, z=line)
return True
if self.verbose > 1:
- LOG.debug("Trying check for process with PID {} ...".format(pid))
+ LOG.debug(_("Trying check for process with PID {} ...").format(pid))
try:
os.kill(pid, 0)
except OSError as err:
if err.errno == errno.ESRCH:
- LOG.info("Process with PID {} anonymous died.".format(pid))
+ LOG.info(_("Process with PID {} anonymous died.").format(pid))
return True
elif err.errno == errno.EPERM:
error_tuple = sys.exc_info()
- msg = "No permission to signal the process {} ...".format(pid)
+ msg = _("No permission to signal the process {} ...").format(pid)
reraise(PidFileError, msg, error_tuple[2])
else:
error_tuple = sys.exc_info()
- msg = "Got a {}: {}.".format(err.__class__.__name__, err)
+ msg = _("Got a {c}: {e}.").format(err.__class__.__name__, err)
reraise(PidFileError, msg, error_tuple[2])
else:
raise PidFileInUseError(self.filename, pid)