Python code modernization, 4/n (#1162)

* Address attribute-defined-outside-init.

* Address broad-exception-raised.

* Address broad-exception-caught.

* Address consider-iterating-dictionary.

* Address consider-using-dict-comprehension.

* Address consider-using-f-string.

* Address consider-using-in.

* Address consider-using-max-builtin.

* Address some consider-using-with.

* Address invalid-name.

* Address keyword-arg-before-vararg.

* Address line-too-long.

* Address no-else-continue.

* Address no-else-raise.

* Address no-else-return.

* Remove broken dead code.

* Make consider-using-f-string changes compatible with older Python versions.

* Python 3.11 and earlier apparently do not like multi-line f-strings.
This commit is contained in:
Felix Fontein
2025-10-11 23:06:50 +02:00
committed by GitHub
parent 33c8a49191
commit cad22de628
59 changed files with 556 additions and 630 deletions
+6 -4
View File
@@ -70,14 +70,16 @@ except ImportError:
self.connection = self
self.connectionpool = self
self.RecentlyUsedContainer = object()
self.PoolManager = object()
self.RecentlyUsedContainer = object() # pylint: disable=invalid-name
self.PoolManager = object() # pylint: disable=invalid-name
self.match_hostname = object()
self.HTTPConnectionPool = _HTTPConnectionPool
self.HTTPConnectionPool = ( # pylint: disable=invalid-name
_HTTPConnectionPool
)
class FakeURLLIB3Connection:
def __init__(self):
self.HTTPConnection = _HTTPConnection
self.HTTPConnection = _HTTPConnection # pylint: disable=invalid-name
urllib3 = FakeURLLIB3()
urllib3_connection = FakeURLLIB3Connection()
+9 -21
View File
@@ -47,7 +47,7 @@ from ..transport.sshconn import PARAMIKO_IMPORT_ERROR, SSHHTTPAdapter
from ..transport.ssladapter import SSLHTTPAdapter
from ..transport.unixconn import UnixHTTPAdapter
from ..utils import config, json_stream, utils
from ..utils.decorators import check_resource, update_headers
from ..utils.decorators import update_headers
from ..utils.proxy import ProxyConfig
from ..utils.socket import consume_socket_output, demux_adaptor, frames_iter
from .daemon import DaemonApiMixin
@@ -278,8 +278,7 @@ class APIClient(_Session, DaemonApiMixin):
if kwargs.get("versioned_api", True):
return f"{self.base_url}/v{self._version}{pathfmt.format(*args)}"
else:
return f"{self.base_url}{pathfmt.format(*args)}"
return f"{self.base_url}{pathfmt.format(*args)}"
def _raise_for_status(self, response):
"""Raises stored :class:`APIError`, if one occurred."""
@@ -427,12 +426,11 @@ class APIClient(_Session, DaemonApiMixin):
if stream:
return gen
else:
try:
# Wait for all the frames, concatenate them, and return the result
return consume_socket_output(gen, demux=demux)
finally:
response.close()
try:
# Wait for all the frames, concatenate them, and return the result
return consume_socket_output(gen, demux=demux)
finally:
response.close()
def _disable_socket_timeout(self, socket):
"""Depending on the combination of python version and whether we are
@@ -462,14 +460,6 @@ class APIClient(_Session, DaemonApiMixin):
s.settimeout(None)
@check_resource("container")
def _check_is_tty(self, container):
cont = self.inspect_container(container)
return cont["Config"]["Tty"]
def _get_result(self, container, stream, res):
return self._get_result_tty(stream, res, self._check_is_tty(container))
def _get_result_tty(self, stream, res, is_tty):
# We should also use raw streaming (without keep-alive)
# if we are dealing with a tty-enabled container.
@@ -484,8 +474,7 @@ class APIClient(_Session, DaemonApiMixin):
sep = b""
if stream:
return self._multiplexed_response_stream_helper(res)
else:
return sep.join(list(self._multiplexed_buffer_helper(res)))
return sep.join(list(self._multiplexed_buffer_helper(res)))
def _unmount(self, *args):
for proto in args:
@@ -497,8 +486,7 @@ class APIClient(_Session, DaemonApiMixin):
except _InvalidSchema as e:
if self._custom_adapter:
return self._custom_adapter
else:
raise e
raise e
@property
def api_version(self):
+1 -1
View File
@@ -368,7 +368,7 @@ def _load_legacy_config(config_file):
}
}
}
except Exception as e:
except Exception as e: # pylint: disable=broad-exception-caught
log.debug(e)
pass
+3 -3
View File
@@ -36,7 +36,7 @@ def get_current_context_name_with_source():
json.load(f).get("currentContext", "default"),
f"configuration file {docker_cfg_path}",
)
except Exception:
except Exception: # pylint: disable=broad-exception-caught
pass
return "default", "fallback value"
@@ -54,7 +54,7 @@ def write_context_name_to_docker_config(name=None):
try:
with open(docker_cfg_path, "rt", encoding="utf-8") as f:
config = json.load(f)
except Exception as e:
except Exception as e: # pylint: disable=broad-exception-caught
return e
current_context = config.get("currentContext", None)
if current_context and not name:
@@ -68,7 +68,7 @@ def write_context_name_to_docker_config(name=None):
try:
with open(docker_cfg_path, "wt", encoding="utf-8") as f:
json.dump(config, f, indent=4)
except Exception as e:
except Exception as e: # pylint: disable=broad-exception-caught
return e
+11 -11
View File
@@ -42,7 +42,7 @@ class Context:
description=None,
):
if not name:
raise Exception("Name not provided")
raise ValueError("Name not provided")
self.name = name
self.context_type = None
self.orchestrator = orchestrator
@@ -136,7 +136,7 @@ class Context:
metadata = json.load(f)
except (OSError, KeyError, ValueError) as e:
# unknown format
raise Exception(
raise RuntimeError(
f"Detected corrupted meta file for context {name} : {e}"
) from e
@@ -157,7 +157,7 @@ class Context:
def _load_certs(self):
certs = {}
tls_dir = get_tls_dir(self.name)
for endpoint in self.endpoints.keys():
for endpoint in self.endpoints:
if not os.path.isdir(os.path.join(tls_dir, endpoint)):
continue
ca_cert = None
@@ -238,11 +238,11 @@ class Context:
return self.context_type is None
@property
def Name(self):
def Name(self): # pylint: disable=invalid-name
return self.name
@property
def Host(self):
def Host(self): # pylint: disable=invalid-name
if not self.orchestrator or self.orchestrator == "swarm":
endpoint = self.endpoints.get("docker", None)
if endpoint:
@@ -252,27 +252,27 @@ class Context:
return self.endpoints[self.orchestrator].get("Host", None)
@property
def Orchestrator(self):
def Orchestrator(self): # pylint: disable=invalid-name
return self.orchestrator
@property
def Metadata(self):
def Metadata(self): # pylint: disable=invalid-name
meta = {}
if self.orchestrator:
meta = {"StackOrchestrator": self.orchestrator}
return {"Name": self.name, "Metadata": meta, "Endpoints": self.endpoints}
@property
def TLSConfig(self):
def TLSConfig(self): # pylint: disable=invalid-name
key = self.orchestrator
if not key or key == "swarm":
key = "docker"
if key in self.tls_cfg.keys():
if key in self.tls_cfg:
return self.tls_cfg[key]
return None
@property
def TLSMaterial(self):
def TLSMaterial(self): # pylint: disable=invalid-name
certs = {}
for endpoint, tls in self.tls_cfg.items():
cert, key = tls.cert
@@ -280,5 +280,5 @@ class Context:
return {"TLSMaterial": certs}
@property
def Storage(self):
def Storage(self): # pylint: disable=invalid-name
return {"Storage": {"MetadataPath": self.meta_path, "TLSPath": self.tls_path}}
@@ -91,8 +91,7 @@ class Store:
raise errors.StoreError(
f"{self.program} not installed or not available in PATH"
)
else:
raise errors.StoreError(
f'Unexpected OS error "{e.strerror}", errno={e.errno}'
)
raise errors.StoreError(
f'Unexpected OS error "{e.strerror}", errno={e.errno}'
)
return output
@@ -28,9 +28,9 @@ except ImportError:
PYWIN32_IMPORT_ERROR = traceback.format_exc()
cERROR_PIPE_BUSY = 0xE7
cSECURITY_SQOS_PRESENT = 0x100000
cSECURITY_ANONYMOUS = 0
ERROR_PIPE_BUSY = 0xE7
SECURITY_SQOS_PRESENT = 0x100000
SECURITY_ANONYMOUS = 0
MAXIMUM_RETRY_COUNT = 10
@@ -55,7 +55,9 @@ class NpipeSocket:
def __init__(self, handle=None):
self._timeout = win32pipe.NMPWAIT_USE_DEFAULT_WAIT
self._handle = handle
self._address = None
self._closed = False
self.flags = None
def accept(self):
raise NotImplementedError()
@@ -77,8 +79,8 @@ class NpipeSocket:
None,
win32file.OPEN_EXISTING,
(
cSECURITY_ANONYMOUS
| cSECURITY_SQOS_PRESENT
SECURITY_ANONYMOUS
| SECURITY_SQOS_PRESENT
| win32file.FILE_FLAG_OVERLAPPED
),
0,
@@ -86,7 +88,7 @@ class NpipeSocket:
except win32pipe.error as e:
# See Remarks:
# https://msdn.microsoft.com/en-us/library/aa365800.aspx
if e.winerror == cERROR_PIPE_BUSY:
if e.winerror == ERROR_PIPE_BUSY:
# Another program or thread has grabbed our pipe instance
# before we got to it. Wait for availability and attempt to
# connect again.
@@ -72,7 +72,7 @@ class SSHSocket(socket.socket):
env.pop("LD_LIBRARY_PATH", None)
env.pop("SSL_CERT_FILE", None)
self.proc = subprocess.Popen(
self.proc = subprocess.Popen( # pylint: disable=consider-using-with
args,
env=env,
stdout=subprocess.PIPE,
@@ -82,7 +82,7 @@ class SSHSocket(socket.socket):
def _write(self, data):
if not self.proc or self.proc.stdin.closed:
raise Exception(
raise RuntimeError(
"SSH subprocess not initiated. connect() must be called first."
)
written = self.proc.stdin.write(data)
@@ -97,7 +97,7 @@ class SSHSocket(socket.socket):
def recv(self, n):
if not self.proc:
raise Exception(
raise RuntimeError(
"SSH subprocess not initiated. connect() must be called first."
)
return self.proc.stdout.read(n)
+16 -13
View File
@@ -125,19 +125,22 @@ def create_archive(root, files=None, fileobj=None, gzip=False, extra_files=None)
def mkbuildcontext(dockerfile):
f = tempfile.NamedTemporaryFile()
t = tarfile.open(mode="w", fileobj=f)
if isinstance(dockerfile, io.StringIO):
raise TypeError("Please use io.BytesIO to create in-memory Dockerfiles")
elif isinstance(dockerfile, io.BytesIO):
dfinfo = tarfile.TarInfo("Dockerfile")
dfinfo.size = len(dockerfile.getvalue())
dockerfile.seek(0)
else:
dfinfo = t.gettarinfo(fileobj=dockerfile, arcname="Dockerfile")
t.addfile(dfinfo, dockerfile)
t.close()
f.seek(0)
f = tempfile.NamedTemporaryFile() # pylint: disable=consider-using-with
try:
with tarfile.open(mode="w", fileobj=f) as t:
if isinstance(dockerfile, io.StringIO):
raise TypeError("Please use io.BytesIO to create in-memory Dockerfiles")
if isinstance(dockerfile, io.BytesIO):
dfinfo = tarfile.TarInfo("Dockerfile")
dfinfo.size = len(dockerfile.getvalue())
dockerfile.seek(0)
else:
dfinfo = t.gettarinfo(fileobj=dockerfile, arcname="Dockerfile")
t.addfile(dfinfo, dockerfile)
f.seek(0)
except Exception: # noqa: E722
f.close()
raise
return f
+1 -2
View File
@@ -68,8 +68,7 @@ def home_dir():
"""
if IS_WINDOWS_PLATFORM:
return os.environ.get("USERPROFILE", "")
else:
return os.path.expanduser("~")
return os.path.expanduser("~")
def load_general_config(config_path=None):
@@ -17,23 +17,6 @@ from .. import errors
from . import utils
def check_resource(resource_name):
def decorator(f):
@functools.wraps(f)
def wrapped(self, resource_id=None, *args, **kwargs):
if resource_id is None and kwargs.get(resource_name):
resource_id = kwargs.pop(resource_name)
if isinstance(resource_id, dict):
resource_id = resource_id.get("Id", resource_id.get("ID"))
if not resource_id:
raise errors.NullResource("Resource ID was not provided")
return f(self, resource_id, *args, **kwargs)
return wrapped
return decorator
def minimum_version(version):
def decorator(f):
@functools.wraps(f)
+5 -6
View File
@@ -90,9 +90,8 @@ def split_port(port):
if external is not None and len(internal) != len(external):
raise ValueError("Port ranges don't match in length")
return internal, external
else:
if not external:
external = [None] * len(internal)
elif len(internal) != len(external):
raise ValueError("Port ranges don't match in length")
return internal, [(host, ext_port) for ext_port in external]
if not external:
external = [None] * len(internal)
elif len(internal) != len(external):
raise ValueError("Port ranges don't match in length")
return internal, [(host, ext_port) for ext_port in external]
+3 -5
View File
@@ -111,8 +111,7 @@ def frames_iter(socket, tty):
"""
if tty:
return ((STDOUT, frame) for frame in frames_iter_tty(socket))
else:
return frames_iter_no_tty(socket)
return frames_iter_no_tty(socket)
def frames_iter_no_tty(socket):
@@ -194,7 +193,6 @@ def demux_adaptor(stream_id, data):
"""
if stream_id == STDOUT:
return (data, None)
elif stream_id == STDERR:
if stream_id == STDERR:
return (None, data)
else:
raise ValueError(f"{stream_id} is not a valid stream")
raise ValueError(f"{stream_id} is not a valid stream")
+9 -11
View File
@@ -75,10 +75,9 @@ def compare_version(v1, v2):
s2 = StrictVersion(v2)
if s1 == s2:
return 0
elif s1 > s2:
if s1 > s2:
return -1
else:
return 1
return 1
def version_lt(v1, v2):
@@ -250,7 +249,7 @@ def parse_host(addr, is_win32=False, tls=False):
# These protos are valid aliases for our library but not for the
# official spec
if proto == "http" or proto == "https":
if proto in ("http", "https"):
tls = proto == "https"
proto = "tcp"
elif proto == "http+unix":
@@ -273,12 +272,11 @@ def parse_host(addr, is_win32=False, tls=False):
raise errors.DockerException(
f"Invalid bind address format: no path allowed for this protocol: {addr}"
)
else:
path = parsed_url.path
if proto == "unix" and parsed_url.hostname is not None:
# For legacy reasons, we consider unix://path
# to be valid and equivalent to unix:///path
path = "/".join((parsed_url.hostname, path))
path = parsed_url.path
if proto == "unix" and parsed_url.hostname is not None:
# For legacy reasons, we consider unix://path
# to be valid and equivalent to unix:///path
path = "/".join((parsed_url.hostname, path))
netloc = parsed_url.netloc
if proto in ("tcp", "ssh"):
@@ -419,7 +417,7 @@ def parse_bytes(s):
else:
digits_part = s[:-1]
if suffix in units.keys() or suffix.isdigit():
if suffix in units or suffix.isdigit():
try:
digits = float(digits_part)
except ValueError:
+12 -12
View File
@@ -98,7 +98,7 @@ MIN_DOCKER_VERSION = "1.8.0"
if not HAS_DOCKER_PY:
docker_version = None
docker_version = None # pylint: disable=invalid-name
# No Docker SDK for Python. Create a place holder client to allow
# instantiation of AnsibleModule and proper error handing
@@ -194,7 +194,7 @@ class AnsibleDockerClientBase(Client):
def __init__(self, min_docker_version=None, min_docker_api_version=None):
if min_docker_version is None:
min_docker_version = MIN_DOCKER_VERSION
NEEDS_DOCKER_PY2 = LooseVersion(min_docker_version) >= LooseVersion("2.0.0")
needs_docker_py2 = LooseVersion(min_docker_version) >= LooseVersion("2.0.0")
self.docker_py_version = LooseVersion(docker_version)
@@ -218,7 +218,7 @@ class AnsibleDockerClientBase(Client):
f"Error: Docker SDK for Python version is {docker_version} ({platform.node()}'s Python {sys.executable})."
f" Minimum version required is {min_docker_version}."
)
if not NEEDS_DOCKER_PY2:
if not needs_docker_py2:
# The minimal required version is < 2.0 (and the current version as well).
# Advertise docker (instead of docker-py).
msg += DOCKERPYUPGRADE_RECOMMEND_DOCKER
@@ -237,7 +237,7 @@ class AnsibleDockerClientBase(Client):
self.docker_api_version_str = self.api_version
except APIError as exc:
self.fail(f"Docker API error: {exc}")
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error connecting: {exc}")
self.docker_api_version = LooseVersion(self.docker_api_version_str)
@@ -409,7 +409,7 @@ class AnsibleDockerClientBase(Client):
return result
except NotFound:
return None
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting container: {exc}")
def get_container(self, name=None):
@@ -441,7 +441,7 @@ class AnsibleDockerClientBase(Client):
break
except SSLError as exc:
self._handle_ssl_error(exc)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error retrieving container list: {exc}")
if result is None:
@@ -470,7 +470,7 @@ class AnsibleDockerClientBase(Client):
break
except SSLError as exc:
self._handle_ssl_error(exc)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error retrieving network list: {exc}")
if result is not None:
@@ -483,7 +483,7 @@ class AnsibleDockerClientBase(Client):
self.log("Completed network inspection")
except NotFound:
return None
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting network: {exc}")
return result
@@ -533,7 +533,7 @@ class AnsibleDockerClientBase(Client):
except NotFound:
self.log(f"Image {name}:{tag} not found.")
return None
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting image {name}:{tag} - {exc}")
return inspection
@@ -555,7 +555,7 @@ class AnsibleDockerClientBase(Client):
self.fail(f"Error inspecting image ID {image_id} - {exc}")
self.log(f"Image {image_id} not found.")
return None
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting image ID {image_id} - {exc}")
return inspection
@@ -567,7 +567,7 @@ class AnsibleDockerClientBase(Client):
"""
try:
response = self.images(name=name)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error searching for image {name} - {exc}")
images = response
if tag:
@@ -606,7 +606,7 @@ class AnsibleDockerClientBase(Client):
)
else:
self.fail(f"Error pulling {name} - {line.get('error')}")
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error pulling image {name}:{tag} - {exc}")
new_tag = self.find_image(name, tag)
+9 -9
View File
@@ -128,7 +128,7 @@ class AnsibleDockerClientBase(Client):
)
except APIError as exc:
self.fail(f"Docker API error: {exc}")
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error connecting: {exc}")
self.docker_api_version = LooseVersion(self.docker_api_version_str)
@@ -308,7 +308,7 @@ class AnsibleDockerClientBase(Client):
return result
except NotFound:
return None
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting container: {exc}")
def get_container(self, name=None):
@@ -347,7 +347,7 @@ class AnsibleDockerClientBase(Client):
break
except SSLError as exc:
self._handle_ssl_error(exc)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error retrieving container list: {exc}")
if result is None:
@@ -377,7 +377,7 @@ class AnsibleDockerClientBase(Client):
break
except SSLError as exc:
self._handle_ssl_error(exc)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error retrieving network list: {exc}")
if result is not None:
@@ -390,7 +390,7 @@ class AnsibleDockerClientBase(Client):
self.log("Completed network inspection")
except NotFound:
return None
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting network: {exc}")
return result
@@ -412,7 +412,7 @@ class AnsibleDockerClientBase(Client):
else:
params["filters"] = convert_filters({"reference": name})
images = self.get_json("/images/json", params=params)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error searching for image {name} - {exc}")
if tag:
lookup = f"{name}:{tag}"
@@ -472,7 +472,7 @@ class AnsibleDockerClientBase(Client):
except NotFound:
self.log(f"Image {name}:{tag} not found.")
return None
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting image {name}:{tag} - {exc}")
self.log(f"Image {name}:{tag} not found.")
@@ -493,7 +493,7 @@ class AnsibleDockerClientBase(Client):
self.fail(f"Error inspecting image ID {image_id} - {exc}")
self.log(f"Image {image_id} not found.")
return None
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting image ID {image_id} - {exc}")
def pull_image(self, name, tag="latest", image_platform=None):
@@ -535,7 +535,7 @@ class AnsibleDockerClientBase(Client):
)
else:
self.fail(f"Error pulling {name} - {line.get('error')}")
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error pulling image {name}:{tag} - {exc}")
new_tag = self.find_image(name, tag)
+2 -2
View File
@@ -159,7 +159,7 @@ class AnsibleDockerClientBase:
self.warn(to_native(stderr))
try:
data = json.loads(stdout)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(
f"Error while parsing JSON output of {self._compose_cmd_str(args)}: {exc}\nJSON output: {to_native(stdout)}"
)
@@ -177,7 +177,7 @@ class AnsibleDockerClientBase:
line = line.strip()
if line.startswith(b"{"):
result.append(json.loads(line))
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(
f"Error while parsing JSON output of {self._compose_cmd_str(args)}: {exc}\nJSON output: {to_native(stdout)}"
)
+16 -16
View File
@@ -176,9 +176,13 @@ _RE_PULL_EVENT = re.compile(
r"\s*"
r"(?P<service>\S+)"
r"\s+"
r"(?P<status>%s)"
f"(?P<status>{'|'.join(re.escape(status) for status in DOCKER_STATUS_PULL)})"
r"\s*"
r"$" % "|".join(re.escape(status) for status in DOCKER_STATUS_PULL)
r"$"
)
_DOCKER_PULL_PROGRESS_WD = sorted(
DOCKER_PULL_PROGRESS_DONE | DOCKER_PULL_PROGRESS_WORKING
)
_RE_PULL_PROGRESS = re.compile(
@@ -186,14 +190,10 @@ _RE_PULL_PROGRESS = re.compile(
r"\s*"
r"(?P<layer>\S+)"
r"\s+"
r"(?P<status>%s)"
f"(?P<status>{'|'.join(re.escape(status) for status in _DOCKER_PULL_PROGRESS_WD)})"
r"\s*"
r"(?:|\s\[[^]]+\]\s+\S+\s*|\s+[0-9.kKmMgGbB]+/[0-9.kKmMgGbB]+\s*)"
r"$"
% "|".join(
re.escape(status)
for status in sorted(DOCKER_PULL_PROGRESS_DONE | DOCKER_PULL_PROGRESS_WORKING)
)
)
_RE_ERROR_EVENT = re.compile(
@@ -201,10 +201,10 @@ _RE_ERROR_EVENT = re.compile(
r"\s*"
r"(?P<resource_id>\S+)"
r"\s+"
r"(?P<status>%s)"
f"(?P<status>{'|'.join(re.escape(status) for status in DOCKER_STATUS_ERROR)})"
r"\s*"
r"(?P<msg>\S.*\S)?"
r"$" % "|".join(re.escape(status) for status in DOCKER_STATUS_ERROR)
r"$"
)
_RE_WARNING_EVENT = re.compile(
@@ -212,10 +212,10 @@ _RE_WARNING_EVENT = re.compile(
r"\s*"
r"(?P<resource_id>\S+)"
r"\s+"
r"(?P<status>%s)"
f"(?P<status>{'|'.join(re.escape(status) for status in DOCKER_STATUS_WARNING)})"
r"\s*"
r"(?P<msg>\S.*\S)?"
r"$" % "|".join(re.escape(status) for status in DOCKER_STATUS_WARNING)
r"$"
)
_RE_CONTINUE_EVENT = re.compile(r"^\s*(?P<resource_id>\S+)\s+-\s*(?P<msg>\S(?:|.*\S))$")
@@ -405,7 +405,7 @@ def parse_json_events(stderr, warn_function=None):
continue
try:
line_data = json.loads(line)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
if warn_function:
warn_function(
f"Cannot parse event from line: {line!r}: {exc}. Please report this at "
@@ -544,7 +544,7 @@ def parse_events(stderr, dry_run=False, warn_function=None, nonzero_rc=False):
line, warn_missing_dry_run_prefix, warn_function
)
continue
elif parsed:
if parsed:
continue
match = _RE_BUILD_PROGRESS_EVENT.match(line)
if match:
@@ -748,7 +748,7 @@ class BaseComposeManager(DockerBaseClass):
encoding="utf-8",
Dumper=_SafeDumper,
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error writing to {compose_file} - {exc}")
else:
self.project_src = os.path.abspath(parameters["project_src"])
@@ -804,7 +804,7 @@ class BaseComposeManager(DockerBaseClass):
if version == "dev":
return None
return version.lstrip("v")
except Exception:
except Exception: # pylint: disable=broad-exception-caught
return None
def get_compose_version_from_api(self):
@@ -946,6 +946,6 @@ class BaseComposeManager(DockerBaseClass):
for directory in self.cleanup_dirs:
try:
shutil.rmtree(directory, True)
except Exception:
except Exception: # pylint: disable=broad-exception-caught
# should not happen, but simply ignore to be on the safe side
pass
+19 -23
View File
@@ -133,38 +133,35 @@ def parse_line(line, logrus_mode=False):
key.append(cur)
parser.next()
return _Mode.KEY
elif cur == "=":
if cur == "=":
parser.next()
return _Mode.EQUAL
else:
if logrus_mode:
raise InvalidLogFmt('Key must always be followed by "=" in logrus mode')
handle_kv(has_no_value=True)
parser.next()
return _Mode.GARBAGE
if logrus_mode:
raise InvalidLogFmt('Key must always be followed by "=" in logrus mode')
handle_kv(has_no_value=True)
parser.next()
return _Mode.GARBAGE
def parse_equal(cur):
if _is_ident(cur):
value.append(cur)
parser.next()
return _Mode.IDENT_VALUE
elif cur == '"':
if cur == '"':
parser.next()
return _Mode.QUOTED_VALUE
else:
handle_kv()
parser.next()
return _Mode.GARBAGE
handle_kv()
parser.next()
return _Mode.GARBAGE
def parse_ident_value(cur):
if _is_ident(cur):
value.append(cur)
parser.next()
return _Mode.IDENT_VALUE
else:
handle_kv()
parser.next()
return _Mode.GARBAGE
handle_kv()
parser.next()
return _Mode.GARBAGE
def parse_quoted_value(cur):
if cur == "\\":
@@ -182,16 +179,15 @@ def parse_line(line, logrus_mode=False):
value.append(parser.parse_unicode_sequence())
parser.next()
return _Mode.QUOTED_VALUE
elif cur == '"':
if cur == '"':
handle_kv()
parser.next()
return _Mode.GARBAGE
elif cur < " ":
if cur < " ":
raise InvalidLogFmt("Control characters in quoted string are not allowed")
else:
value.append(cur)
parser.next()
return _Mode.QUOTED_VALUE
value.append(cur)
parser.next()
return _Mode.QUOTED_VALUE
parsers = {
_Mode.GARBAGE: parse_garbage,
@@ -204,7 +200,7 @@ def parse_line(line, logrus_mode=False):
mode = parsers[mode](parser.cur())
if mode == _Mode.KEY and logrus_mode:
raise InvalidLogFmt('Key must always be followed by "=" in logrus mode')
if mode == _Mode.KEY or mode == _Mode.EQUAL:
if mode in (_Mode.KEY, _Mode.EQUAL):
handle_kv(has_no_value=True)
elif mode == _Mode.IDENT_VALUE:
handle_kv()
+14 -15
View File
@@ -58,7 +58,7 @@ def _get_ansible_type(value_type):
if value_type == "set":
return "list"
if value_type not in ("list", "dict", "bool", "int", "float", "str"):
raise Exception(f'Invalid type "{value_type}"')
raise ValueError(f'Invalid type "{value_type}"')
return value_type
@@ -87,13 +87,13 @@ class Option:
needs_elements = self.value_type in ("list", "set")
needs_ansible_elements = self.ansible_type in ("list",)
if elements is not None and not needs_elements:
raise Exception("elements only allowed for lists/sets")
raise ValueError("elements only allowed for lists/sets")
if elements is None and needs_elements:
raise Exception("elements required for lists/sets")
raise ValueError("elements required for lists/sets")
if ansible_elements is not None and not needs_ansible_elements:
raise Exception("Ansible elements only allowed for Ansible lists")
raise ValueError("Ansible elements only allowed for Ansible lists")
if (elements is None and ansible_elements is None) and needs_ansible_elements:
raise Exception("Ansible elements required for Ansible lists")
raise ValueError("Ansible elements required for Ansible lists")
self.elements = elements if needs_elements else None
self.ansible_elements = (
(ansible_elements or _get_ansible_type(elements))
@@ -104,7 +104,7 @@ class Option:
self.ansible_type == "list" and self.ansible_elements == "dict"
) or (self.ansible_type == "dict")
if ansible_suboptions is not None and not needs_suboptions:
raise Exception(
raise ValueError(
"suboptions only allowed for Ansible lists with dicts, or Ansible dicts"
)
if (
@@ -113,7 +113,7 @@ class Option:
and not needs_no_suboptions
and not not_an_ansible_option
):
raise Exception(
raise ValueError(
"suboptions required for Ansible lists with dicts, or Ansible dicts"
)
self.ansible_suboptions = ansible_suboptions if needs_suboptions else None
@@ -431,16 +431,15 @@ def _parse_port_range(range_or_port, module):
if "-" in range_or_port:
try:
start, end = [int(port) for port in range_or_port.split("-")]
except Exception:
except ValueError:
module.fail_json(msg=f'Invalid port range: "{range_or_port}"')
if end < start:
module.fail_json(msg=f'Invalid port range: "{range_or_port}"')
return list(range(start, end + 1))
else:
try:
return [int(range_or_port)]
except Exception:
module.fail_json(msg=f'Invalid port: "{range_or_port}"')
try:
return [int(range_or_port)]
except ValueError:
module.fail_json(msg=f'Invalid port: "{range_or_port}"')
def _split_colon_ipv6(text, module):
@@ -707,7 +706,7 @@ def _preprocess_mounts(module, values):
if mount_dict["tmpfs_mode"] is not None:
try:
mount_dict["tmpfs_mode"] = int(mount_dict["tmpfs_mode"], 8)
except Exception:
except ValueError:
module.fail_json(
msg=f'tmp_fs mode of mount "{target}" is not an octal string!'
)
@@ -747,7 +746,7 @@ def _preprocess_mounts(module, values):
check_collision(container, "volumes")
new_vols.append(f"{host}:{container}:{mode}")
continue
elif len(parts) == 2:
if len(parts) == 2:
if not _is_volume_permissions(parts[1]) and re.match(
r"[.~]", parts[0]
):
@@ -124,7 +124,7 @@ def _get_ansible_type(our_type):
if our_type == "set":
return "list"
if our_type not in ("list", "dict", "bool", "int", "float", "str"):
raise Exception(f'Invalid type "{our_type}"')
raise ValueError(f'Invalid type "{our_type}"')
return our_type
@@ -266,7 +266,7 @@ class DockerAPIEngineDriver(EngineDriver):
# Ensure driver_opts values are strings
for key, val in value.items():
if not isinstance(val, str):
raise Exception(
raise ValueError(
f"driver_opts values must be strings, got {type(val).__name__} for key '{key}'"
)
params[dest_para] = value
@@ -278,7 +278,7 @@ class DockerAPIEngineDriver(EngineDriver):
params[dest_para] = value
if parameters:
ups = ", ".join([f'"{p}"' for p in sorted(parameters)])
raise Exception(
raise ValueError(
f"Unknown parameter(s) for connect_container_to_network for Docker API driver: {ups}"
)
ipam_config = {}
@@ -347,8 +347,7 @@ class DockerAPIEngineDriver(EngineDriver):
)
output = client._get_result_tty(False, res, config["Config"]["Tty"])
return output, True
else:
return f"Result logged using `{logging_driver}` driver", False
return f"Result logged using `{logging_driver}` driver", False
def update_container(self, client, container_id, update_parameters):
result = client.post_json_to_json(
@@ -399,13 +398,13 @@ class DockerAPIEngineDriver(EngineDriver):
# New docker daemon versions do not allow containers to be removed
# if they are paused. Make sure we do not end up in an infinite loop.
if count == 3:
raise Exception(f"{exc} [tried to unpause three times]")
raise RuntimeError(f"{exc} [tried to unpause three times]")
count += 1
# Unpause
try:
self.unpause_container(client, container_id)
except Exception as exc2:
raise Exception(f"{exc2} [while unpausing]")
raise RuntimeError(f"{exc2} [while unpausing]")
# Now try again
continue
raise
@@ -430,13 +429,13 @@ class DockerAPIEngineDriver(EngineDriver):
# New docker daemon versions do not allow containers to be removed
# if they are paused. Make sure we do not end up in an infinite loop.
if count == 3:
raise Exception(f"{exc} [tried to unpause three times]")
raise RuntimeError(f"{exc} [tried to unpause three times]")
count += 1
# Unpause
try:
self.unpause_container(client, container_id)
except Exception as exc2:
raise Exception(f"{exc2} [while unpausing]")
raise RuntimeError(f"{exc2} [while unpausing]")
# Now try again
continue
if (
@@ -1060,7 +1059,7 @@ def _get_network_id(module, client, network_name):
network_id = network["Id"]
break
return network_id
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
client.fail(f"Error getting network id for {network_name} - {exc}")
@@ -134,6 +134,7 @@ class ContainerManager(DockerBaseClass):
"The value of default_host_ip must be an empty string, an IPv4 address, "
f'or an IPv6 address. Got "{self.param_default_host_ip}" instead.'
)
self.parameters = None
def _collect_all_options(self, active_options):
all_options = {}
@@ -480,7 +481,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.unpause_container(
self.client, container.id
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(
f"Error {'pausing' if self.param_paused else 'unpausing'} container {container.id}: {exc}"
)
@@ -567,13 +568,13 @@ class ContainerManager(DockerBaseClass):
if not image or self.param_pull == "always":
if not self.check_mode:
self.log("Pull the image.")
image, alreadyToLatest = self.engine_driver.pull_image(
image, already_to_latest = self.engine_driver.pull_image(
self.client,
repository,
tag,
image_platform=self.module.params["platform"],
)
if alreadyToLatest:
if already_to_latest:
self.results["changed"] = False
self.results["actions"].append(
dict(pulled_image=f"{repository}:{tag}", changed=False)
@@ -950,7 +951,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.disconnect_container_from_network(
self.client, container.id, diff["parameter"]["id"]
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(
f"Error disconnecting container from network {diff['parameter']['name']} - {exc}"
)
@@ -975,7 +976,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.connect_container_to_network(
self.client, container.id, diff["parameter"]["id"], params
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(
f"Error connecting container to network {diff['parameter']['name']} - {exc}"
)
@@ -989,7 +990,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.disconnect_container_from_network(
self.client, container.id, network["name"]
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(
f"Error disconnecting container from network {network['name']} - {exc}"
)
@@ -1027,7 +1028,7 @@ class ContainerManager(DockerBaseClass):
container_id = self.engine_driver.create_container(
self.client, self.param_name, create_parameters, networks=networks
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error creating container: {exc}")
return self._get_container(container_id)
return new_container
@@ -1039,7 +1040,7 @@ class ContainerManager(DockerBaseClass):
if not self.check_mode:
try:
self.engine_driver.start_container(self.client, container_id)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error starting container {container_id}: {exc}")
if self.module.params["detach"] is False:
@@ -1096,7 +1097,7 @@ class ContainerManager(DockerBaseClass):
link=link,
force=force,
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.client.fail(f"Error removing container {container_id}: {exc}")
def container_update(self, container_id, update_parameters):
@@ -1112,7 +1113,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.update_container(
self.client, container_id, update_parameters
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error updating container {container_id}: {exc}")
return self._get_container(container_id)
@@ -1126,7 +1127,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.kill_container(
self.client, container_id, kill_signal=self.param_kill_signal
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error killing container {container_id}: {exc}")
def container_restart(self, container_id):
@@ -1139,7 +1140,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.restart_container(
self.client, container_id, self.module.params["stop_timeout"] or 10
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error restarting container {container_id}: {exc}")
return self._get_container(container_id)
@@ -1156,7 +1157,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.stop_container(
self.client, container_id, self.module.params["stop_timeout"]
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error stopping container {container_id}: {exc}")
+2 -3
View File
@@ -78,15 +78,14 @@ class DockerSocketHandlerBase:
if hasattr(self._sock, "recv"):
try:
data = self._sock.recv(262144)
except Exception as e:
except Exception as e: # pylint: disable=broad-exception-caught
# After calling self._sock.shutdown(), OpenSSL's/urllib3's
# WrappedSocket seems to eventually raise ZeroReturnError in
# case of EOF
if "OpenSSL.SSL.ZeroReturnError" in str(type(e)):
self._eof = True
return
else:
raise
raise
elif isinstance(self._sock, getattr(pysocket, "SocketIO")):
data = self._sock.read()
else:
+2 -3
View File
@@ -67,7 +67,6 @@ def write_to_socket(sock, data):
# WrappedSocket (urllib3/contrib/pyopenssl) does not have `send`, but
# only `sendall`, which uses `_send_until_done` under the hood.
return sock._send_until_done(data)
elif hasattr(sock, "send"):
if hasattr(sock, "send"):
return sock.send(data)
else:
return os.write(sock.fileno(), data)
return os.write(sock.fileno(), data)
+11 -16
View File
@@ -74,22 +74,18 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
swarm_info = json.loads(json_str)
if swarm_info["Swarm"]["NodeID"]:
return True
if swarm_info["Swarm"]["LocalNodeState"] in (
return swarm_info["Swarm"]["LocalNodeState"] in (
"active",
"pending",
"locked",
):
return True
)
return False
else:
try:
node_info = self.get_node_inspect(node_id=node_id)
except APIError:
return
try:
node_info = self.get_node_inspect(node_id=node_id)
except APIError:
return
if node_info["ID"] is not None:
return True
return False
return node_info["ID"] is not None
def check_if_swarm_manager(self):
"""
@@ -138,8 +134,7 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
True if node is part of swarm but its state is down, False otherwise
"""
if repeat_check < 1:
repeat_check = 1
repeat_check = max(1, repeat_check)
if node_id is None:
node_id = self.get_swarm_node_id()
@@ -179,7 +174,7 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
if skip_missing:
return None
self.fail(f"Error while reading from Swarm manager: {exc}")
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting swarm node: {exc}")
json_str = json.dumps(node_info, ensure_ascii=False)
@@ -215,7 +210,7 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
"Cannot inspect node: To inspect node execute module on Swarm Manager"
)
self.fail(f"Error while reading from Swarm manager: {exc}")
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting swarm node: {exc}")
json_str = json.dumps(node_info, ensure_ascii=False)
@@ -295,7 +290,7 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
"Cannot inspect service: To inspect service execute module on Swarm Manager"
)
self.fail(f"Error inspecting swarm service: {exc}")
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting swarm service: {exc}")
json_str = json.dumps(service_info, ensure_ascii=False)
+22 -29
View File
@@ -56,13 +56,11 @@ DOCKER_COMMON_ARGS = dict(
debug=dict(type="bool", default=False),
)
DOCKER_COMMON_ARGS_VARS = dict(
[
[option_name, f"ansible_docker_{option_name}"]
for option_name in DOCKER_COMMON_ARGS
if option_name != "debug"
]
)
DOCKER_COMMON_ARGS_VARS = {
option_name: f"ansible_docker_{option_name}"
for option_name in DOCKER_COMMON_ARGS
if option_name != "debug"
}
DOCKER_MUTUALLY_EXCLUSIVE = []
@@ -100,10 +98,9 @@ def sanitize_result(data):
"""
if isinstance(data, dict):
return dict((k, sanitize_result(v)) for k, v in data.items())
elif isinstance(data, (list, tuple)):
if isinstance(data, (list, tuple)):
return [sanitize_result(v) for v in data]
else:
return data
return data
def log_debug(msg, pretty_print=False):
@@ -196,31 +193,28 @@ def compare_generic(a, b, method, datatype):
# Do proper comparison (both objects not None)
if datatype == "value":
return a == b
elif datatype == "list":
if datatype == "list":
if method == "strict":
return a == b
else:
i = 0
for v in a:
while i < len(b) and b[i] != v:
i += 1
if i == len(b):
return False
i = 0
for v in a:
while i < len(b) and b[i] != v:
i += 1
return True
elif datatype == "dict":
if i == len(b):
return False
i += 1
return True
if datatype == "dict":
if method == "strict":
return a == b
else:
return compare_dict_allow_more_present(a, b)
elif datatype == "set":
return compare_dict_allow_more_present(a, b)
if datatype == "set":
set_a = set(a)
set_b = set(b)
if method == "strict":
return set_a == set_b
else:
return set_b >= set_a
elif datatype == "set(dict)":
return set_b >= set_a
if datatype == "set(dict)":
for av in a:
found = False
for bv in b:
@@ -337,10 +331,9 @@ def clean_dict_booleans_for_docker_api(data, allow_sequences=False):
def sanitize(value):
if value is True:
return "true"
elif value is False:
if value is False:
return "false"
else:
return str(value)
return str(value)
result = dict()
if data is not None: