소스 검색

api: extract function to make pylint happy

Marek Marczykowski-Górecki 6 년 전
부모
커밋
66b88cc943
1개의 변경된 파일21개의 추가작업 그리고 14개의 파일을 삭제
  1. 21 14
      qubes/api/__init__.py

+ 21 - 14
qubes/api/__init__.py

@@ -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),