2013-05-13 11:29:14 +02:00
|
|
|
#! /usr/bin/env python
|
|
|
|
|
2016-01-26 13:01:28 +01:00
|
|
|
import atexit
|
2013-05-13 11:29:14 +02:00
|
|
|
import errno
|
|
|
|
from multiprocessing import Process
|
2016-01-26 13:01:28 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import shared
|
|
|
|
|
|
|
|
try:
|
|
|
|
import fcntl # @UnresolvedImport
|
|
|
|
except:
|
|
|
|
pass
|
2013-05-13 11:29:14 +02:00
|
|
|
|
|
|
|
class singleinstance:
|
|
|
|
"""
|
2015-11-15 00:12:19 +01:00
|
|
|
Implements a single instance application by creating a lock file at appdata.
|
2013-05-13 11:29:14 +02:00
|
|
|
|
|
|
|
This is based upon the singleton class from tendo https://github.com/pycontribs/tendo
|
|
|
|
which is under the Python Software Foundation License version 2
|
|
|
|
"""
|
2015-11-15 00:12:19 +01:00
|
|
|
def __init__(self, flavor_id="", daemon=False):
|
2013-05-13 11:29:14 +02:00
|
|
|
self.initialized = False
|
2016-06-30 12:30:05 +02:00
|
|
|
self.counter = 0
|
2016-01-26 13:01:28 +01:00
|
|
|
self.daemon = daemon
|
2015-11-15 00:12:19 +01:00
|
|
|
self.lockfile = os.path.normpath(os.path.join(shared.appdata, 'singleton%s.lock' % flavor_id))
|
|
|
|
|
2016-06-30 12:30:05 +02:00
|
|
|
if not self.daemon and not shared.curses:
|
2015-11-15 00:12:19 +01:00
|
|
|
# Tells the already running (if any) application to get focus.
|
|
|
|
import bitmessageqt
|
|
|
|
bitmessageqt.init()
|
2013-05-13 11:29:14 +02:00
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
try:
|
|
|
|
# file already exists, we try to remove (in case previous execution was interrupted)
|
|
|
|
if os.path.exists(self.lockfile):
|
|
|
|
os.unlink(self.lockfile)
|
|
|
|
self.fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR)
|
|
|
|
except OSError:
|
|
|
|
type, e, tb = sys.exc_info()
|
|
|
|
if e.errno == 13:
|
|
|
|
print 'Another instance of this application is already running'
|
|
|
|
sys.exit(-1)
|
|
|
|
print(e.errno)
|
|
|
|
raise
|
|
|
|
else: # non Windows
|
|
|
|
self.fp = open(self.lockfile, 'w')
|
|
|
|
try:
|
|
|
|
fcntl.lockf(self.fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
|
|
except IOError:
|
|
|
|
print 'Another instance of this application is already running'
|
|
|
|
sys.exit(-1)
|
|
|
|
self.initialized = True
|
2016-01-26 13:01:28 +01:00
|
|
|
atexit.register(self.cleanup)
|
2013-05-13 11:29:14 +02:00
|
|
|
|
2016-01-26 13:01:28 +01:00
|
|
|
def cleanup(self):
|
2013-05-13 11:29:14 +02:00
|
|
|
if not self.initialized:
|
|
|
|
return
|
2016-06-30 12:30:05 +02:00
|
|
|
self.counter += 1
|
|
|
|
if self.daemon and self.counter < 3:
|
|
|
|
# these are the two initial forks while daemonizing
|
|
|
|
return
|
2016-01-26 13:01:28 +01:00
|
|
|
print "Cleaning up lockfile"
|
2013-05-13 11:29:14 +02:00
|
|
|
try:
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
if hasattr(self, 'fd'):
|
|
|
|
os.close(self.fd)
|
|
|
|
os.unlink(self.lockfile)
|
|
|
|
else:
|
|
|
|
fcntl.lockf(self.fp, fcntl.LOCK_UN)
|
|
|
|
if os.path.isfile(self.lockfile):
|
|
|
|
os.unlink(self.lockfile)
|
|
|
|
except Exception, e:
|
2016-01-26 13:01:28 +01:00
|
|
|
pass
|