mirror of
https://github.com/ansible-collections/community.docker.git
synced 2025-12-18 21:02:36 +00:00
Refactoring. (#415)
This commit is contained in:
parent
623786c659
commit
9e57f29b3b
2
changelogs/fragments/415-socket-improvements.yml
Normal file
2
changelogs/fragments/415-socket-improvements.yml
Normal file
@ -0,0 +1,2 @@
|
||||
minor_changes:
|
||||
- "socker_handler and socket_helper module utils - improve Python forward compatibilty, create helper functions for file blocking/unblocking (https://github.com/ansible-collections/community.docker/pull/415)."
|
||||
@ -10,7 +10,7 @@ import os.path
|
||||
import socket as pysocket
|
||||
|
||||
from ansible.module_utils.basic import missing_required_lib
|
||||
from ansible.module_utils.six import PY3
|
||||
from ansible.module_utils.six import PY2
|
||||
|
||||
try:
|
||||
from docker.utils import socket as docker_socket
|
||||
@ -89,7 +89,7 @@ class DockerSocketHandlerBase(object):
|
||||
return
|
||||
else:
|
||||
raise
|
||||
elif PY3 and isinstance(self._sock, getattr(pysocket, 'SocketIO')):
|
||||
elif not PY2 and isinstance(self._sock, getattr(pysocket, 'SocketIO')):
|
||||
data = self._sock.read()
|
||||
else:
|
||||
data = os.read(self._sock.fileno())
|
||||
|
||||
@ -10,7 +10,15 @@ import os
|
||||
import os.path
|
||||
import socket as pysocket
|
||||
|
||||
from ansible.module_utils.six import PY3
|
||||
from ansible.module_utils.six import PY2
|
||||
|
||||
|
||||
def make_file_unblocking(file):
|
||||
fcntl.fcntl(file.fileno(), fcntl.F_SETFL, fcntl.fcntl(file.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK)
|
||||
|
||||
|
||||
def make_file_blocking(file):
|
||||
fcntl.fcntl(file.fileno(), fcntl.F_SETFL, fcntl.fcntl(file.fileno(), fcntl.F_GETFL) & ~os.O_NONBLOCK)
|
||||
|
||||
|
||||
def make_unblocking(sock):
|
||||
@ -19,7 +27,7 @@ def make_unblocking(sock):
|
||||
elif hasattr(sock, 'setblocking'):
|
||||
sock.setblocking(0)
|
||||
else:
|
||||
fcntl.fcntl(sock.fileno(), fcntl.F_SETFL, fcntl.fcntl(sock.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK)
|
||||
make_file_unblocking(sock)
|
||||
|
||||
|
||||
def _empty_writer(msg):
|
||||
@ -36,7 +44,7 @@ def shutdown_writing(sock, log=_empty_writer):
|
||||
# probably: "TypeError: shutdown() takes 1 positional argument but 2 were given"
|
||||
log('Shutting down for writing not possible; trying shutdown instead: {0}'.format(e))
|
||||
sock.shutdown()
|
||||
elif PY3 and isinstance(sock, getattr(pysocket, 'SocketIO')):
|
||||
elif not PY2 and isinstance(sock, getattr(pysocket, 'SocketIO')):
|
||||
sock._sock.shutdown(pysocket.SHUT_WR)
|
||||
else:
|
||||
log('No idea how to signal end of writing')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user