From a1ecebf5bf62ef58fc5e9b5a5f00ae5d294b18e8 Mon Sep 17 00:00:00 2001 From: Daniel Pavel Date: Fri, 7 Jun 2013 14:24:05 +0200 Subject: [PATCH] documentation and logging for --- lib/solaar/appinstance.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/solaar/appinstance.py b/lib/solaar/appinstance.py index 5e149914..fe46bf3f 100644 --- a/lib/solaar/appinstance.py +++ b/lib/solaar/appinstance.py @@ -12,18 +12,26 @@ import fcntl as _fcntl import os.path as _path import os as _os +from solaar import NAME +_program = NAME.lower() +del NAME + from logging import getLogger, DEBUG as _DEBUG -_log = getLogger('solaar.appinstance') +_log = getLogger(__name__) del getLogger def check(): + """Select a file lock location and try to acquire it. + Suitable locations are $XDG_RUNTIME_DIR, /run/lock, /var/lock, and $TMPDIR. + The first one found and writable is used. + """ # ensure no more than a single instance runs at a time lock_fd = None for p in _os.environ.get('XDG_RUNTIME_DIR'), '/run/lock', '/var/lock', _os.environ.get('TMPDIR', '/tmp'): # pick the first temporary writable folder if p and _path.isdir(p) and _os.access(p, _os.W_OK): - lock_path = _path.join(p, 'solaar.single-instance.%d' % _os.getuid()) + lock_path = _path.join(p, _program + '.single-instance.' + str(_os.getuid())) try: lock_fd = open(lock_path, 'wb') if _log.isEnabledFor(_DEBUG): @@ -36,23 +44,24 @@ def check(): try: _fcntl.flock(lock_fd, _fcntl.LOCK_EX | _fcntl.LOCK_NB) if _log.isEnabledFor(_DEBUG): - _log.debug("acquired single-instance lock (%s)", lock_fd) + _log.debug("acquired single-instance lock %s", lock_fd) return lock_fd except IOError as e: if e.errno == 11: - _log.warn("lock file is busy, solaar already running: %s", e) + _log.warn("lock file is busy, %s already running: %s", _program, e) import sys - sys.exit("solaar: error: Solaar is already running.") + sys.exit(_program + ": error: already running") else: raise else: import sys - print ("solaar: warning: failed to create single instance lock file, ignoring.", file=sys.stderr) + print (_program + ": warning: no suitable location for the lockfile found; ignoring", file=sys.stderr) def close(lock_fd): - if _log.isEnabledFor(_DEBUG): - _log.debug("releasing single-instance lock (%s)", lock_fd) + """Release a file lock.""" if lock_fd: _fcntl.flock(lock_fd, _fcntl.LOCK_UN) lock_fd.close() + if _log.isEnabledFor(_DEBUG): + _log.debug("released single-instance lock %s", lock_fd)