api: extract function to make pylint happy

This commit is contained in:
Marek Marczykowski-Górecki 2017-07-12 12:48:34 +02:00
parent 2df81adab1
commit 66b88cc943
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724

View File

@ -345,6 +345,26 @@ class QubesDaemonProtocol(asyncio.Protocol):
self.transport.write(str(exc).encode('utf-8') + b'\0')
def cleanup_socket(sockpath, force):
'''Remove socket if stale, or force=True
:param sockpath: path to a socket
:param force: should remove even if still used
'''
if force:
os.unlink(sockpath)
else:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
sock.connect(sockpath)
except ConnectionRefusedError:
# dead socket, remove it anyway
os.unlink(sockpath)
else:
# woops, someone is listening
sock.close()
raise FileExistsError(errno.EEXIST,
'socket already exists: {!r}'.format(sockpath))
@asyncio.coroutine
def create_servers(*args, force=False, loop=None, **kwargs):
'''Create multiple Qubes API servers
@ -375,20 +395,7 @@ def create_servers(*args, force=False, loop=None, **kwargs):
type(handler).__name__)
if os.path.exists(sockpath):
if force:
os.unlink(sockpath)
else:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
sock.connect(sockpath)
except ConnectionRefusedError:
# dead socket, remove it anyway
os.unlink(sockpath)
else:
# woops, someone is listening
sock.close()
raise FileExistsError(errno.EEXIST,
'socket already exists: {!r}'.format(sockpath))
cleanup_socket(sockpath, force)
server = yield from loop.create_unix_server(
functools.partial(QubesDaemonProtocol, handler, **kwargs),