mirror of
https://github.com/ansible-collections/community.docker.git
synced 2026-07-29 11:55:04 +00:00
Python code modernization, 8/n (#1179)
* Use to_text instead of to_native. * Remove no longer needed pylint ignores. * Remove another pylint ignore. * Remove no longer needed ignore. * Address redefined-outer-name. * Address consider-using-with.
This commit is contained in:
@@ -652,6 +652,8 @@ class APIClient(_Session):
|
||||
|
||||
def get_adapter(self, url: str) -> BaseAdapter:
|
||||
try:
|
||||
# pylint finds our Session stub instead of requests.Session:
|
||||
# pylint: disable-next=no-member
|
||||
return super().get_adapter(url)
|
||||
except _InvalidSchema as e:
|
||||
if self._custom_adapter:
|
||||
|
||||
@@ -13,7 +13,7 @@ from __future__ import annotations
|
||||
|
||||
import typing as t
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
|
||||
from ._import_helper import HTTPError as _HTTPError
|
||||
|
||||
@@ -39,7 +39,7 @@ def create_api_error_from_http_exception(e: _HTTPError) -> t.NoReturn:
|
||||
try:
|
||||
explanation = response.json()["message"]
|
||||
except ValueError:
|
||||
explanation = to_native((response.content or "").strip())
|
||||
explanation = to_text((response.content or "").strip())
|
||||
cls = APIError
|
||||
if response.status_code == 404:
|
||||
if explanation and (
|
||||
|
||||
@@ -16,6 +16,8 @@ from .._import_helper import HTTPAdapter as _HTTPAdapter
|
||||
|
||||
class BaseHTTPAdapter(_HTTPAdapter):
|
||||
def close(self) -> None:
|
||||
# pylint finds our HTTPAdapter stub instead of requests.adapters.HTTPAdapter:
|
||||
# pylint: disable-next=no-member
|
||||
super().close()
|
||||
if hasattr(self, "pools"):
|
||||
self.pools.clear()
|
||||
|
||||
@@ -58,7 +58,11 @@ class SSLHTTPAdapter(BaseHTTPAdapter):
|
||||
We already take care of a normal poolmanager via init_poolmanager
|
||||
|
||||
But we still need to take care of when there is a proxy poolmanager
|
||||
|
||||
Note that this method is no longer called for newer requests versions.
|
||||
"""
|
||||
# pylint finds our HTTPAdapter stub instead of requests.adapters.HTTPAdapter:
|
||||
# pylint: disable-next=no-member
|
||||
conn = super().get_connection(*args, **kwargs)
|
||||
if (
|
||||
self.assert_hostname is not None
|
||||
|
||||
@@ -97,49 +97,50 @@ def create_archive(
|
||||
) -> t.IO[bytes]:
|
||||
extra_files = extra_files or []
|
||||
if not fileobj:
|
||||
# pylint: disable-next=consider-using-with
|
||||
fileobj = tempfile.NamedTemporaryFile()
|
||||
t = tarfile.open(mode="w:gz" if gzip else "w", fileobj=fileobj)
|
||||
if files is None:
|
||||
files = build_file_list(root)
|
||||
extra_names = set(e[0] for e in extra_files)
|
||||
for path in files:
|
||||
if path in extra_names:
|
||||
# Extra files override context files with the same name
|
||||
continue
|
||||
full_path = os.path.join(root, path)
|
||||
|
||||
i = t.gettarinfo(full_path, arcname=path)
|
||||
if i is None:
|
||||
# This happens when we encounter a socket file. We can safely
|
||||
# ignore it and proceed.
|
||||
continue # type: ignore
|
||||
with tarfile.open(mode="w:gz" if gzip else "w", fileobj=fileobj) as tarf:
|
||||
if files is None:
|
||||
files = build_file_list(root)
|
||||
extra_names = set(e[0] for e in extra_files)
|
||||
for path in files:
|
||||
if path in extra_names:
|
||||
# Extra files override context files with the same name
|
||||
continue
|
||||
full_path = os.path.join(root, path)
|
||||
|
||||
# Workaround https://bugs.python.org/issue32713
|
||||
if i.mtime < 0 or i.mtime > 8**11 - 1:
|
||||
i.mtime = int(i.mtime)
|
||||
i = tarf.gettarinfo(full_path, arcname=path)
|
||||
if i is None:
|
||||
# This happens when we encounter a socket file. We can safely
|
||||
# ignore it and proceed.
|
||||
continue # type: ignore
|
||||
|
||||
if IS_WINDOWS_PLATFORM:
|
||||
# Windows does not keep track of the execute bit, so we make files
|
||||
# and directories executable by default.
|
||||
i.mode = i.mode & 0o755 | 0o111
|
||||
# Workaround https://bugs.python.org/issue32713
|
||||
if i.mtime < 0 or i.mtime > 8**11 - 1:
|
||||
i.mtime = int(i.mtime)
|
||||
|
||||
if i.isfile():
|
||||
try:
|
||||
with open(full_path, "rb") as f:
|
||||
t.addfile(i, f)
|
||||
except IOError as exc:
|
||||
raise IOError(f"Can not read file in context: {full_path}") from exc
|
||||
else:
|
||||
# Directories, FIFOs, symlinks... do not need to be read.
|
||||
t.addfile(i, None)
|
||||
if IS_WINDOWS_PLATFORM:
|
||||
# Windows does not keep track of the execute bit, so we make files
|
||||
# and directories executable by default.
|
||||
i.mode = i.mode & 0o755 | 0o111
|
||||
|
||||
for name, contents in extra_files:
|
||||
info = tarfile.TarInfo(name)
|
||||
contents_encoded = contents.encode("utf-8")
|
||||
info.size = len(contents_encoded)
|
||||
t.addfile(info, io.BytesIO(contents_encoded))
|
||||
if i.isfile():
|
||||
try:
|
||||
with open(full_path, "rb") as f:
|
||||
tarf.addfile(i, f)
|
||||
except IOError as exc:
|
||||
raise IOError(f"Can not read file in context: {full_path}") from exc
|
||||
else:
|
||||
# Directories, FIFOs, symlinks... do not need to be read.
|
||||
tarf.addfile(i, None)
|
||||
|
||||
for name, contents in extra_files:
|
||||
info = tarfile.TarInfo(name)
|
||||
contents_encoded = contents.encode("utf-8")
|
||||
info.size = len(contents_encoded)
|
||||
tarf.addfile(info, io.BytesIO(contents_encoded))
|
||||
|
||||
t.close()
|
||||
fileobj.seek(0)
|
||||
return fileobj
|
||||
|
||||
@@ -147,7 +148,7 @@ def create_archive(
|
||||
def mkbuildcontext(dockerfile: io.BytesIO | t.IO[bytes]) -> t.IO[bytes]:
|
||||
f = tempfile.NamedTemporaryFile() # pylint: disable=consider-using-with
|
||||
try:
|
||||
with tarfile.open(mode="w", fileobj=f) as t:
|
||||
with tarfile.open(mode="w", fileobj=f) as tarf:
|
||||
if isinstance(dockerfile, io.StringIO): # type: ignore
|
||||
raise TypeError("Please use io.BytesIO to create in-memory Dockerfiles")
|
||||
if isinstance(dockerfile, io.BytesIO):
|
||||
@@ -155,8 +156,8 @@ def mkbuildcontext(dockerfile: io.BytesIO | t.IO[bytes]) -> t.IO[bytes]:
|
||||
dfinfo.size = len(dockerfile.getvalue())
|
||||
dockerfile.seek(0)
|
||||
else:
|
||||
dfinfo = t.gettarinfo(fileobj=dockerfile, arcname="Dockerfile")
|
||||
t.addfile(dfinfo, dockerfile)
|
||||
dfinfo = tarf.gettarinfo(fileobj=dockerfile, arcname="Dockerfile")
|
||||
tarf.addfile(dfinfo, dockerfile)
|
||||
f.seek(0)
|
||||
except Exception: # noqa: E722
|
||||
f.close()
|
||||
|
||||
@@ -14,7 +14,7 @@ import typing as t
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
||||
from ansible.module_utils.common.process import get_bin_path
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._api.auth import (
|
||||
resolve_repository_name,
|
||||
@@ -132,9 +132,7 @@ class AnsibleDockerClientBase:
|
||||
self.fail(
|
||||
"Cannot determine Docker Daemon information. Are you maybe using podman instead of docker?"
|
||||
)
|
||||
self.docker_api_version_str = to_native(
|
||||
self._version["Server"]["ApiVersion"]
|
||||
)
|
||||
self.docker_api_version_str = to_text(self._version["Server"]["ApiVersion"])
|
||||
self.docker_api_version = LooseVersion(self.docker_api_version_str)
|
||||
min_docker_api_version = min_docker_api_version or "1.25"
|
||||
if self.docker_api_version < LooseVersion(min_docker_api_version):
|
||||
@@ -191,12 +189,12 @@ class AnsibleDockerClientBase:
|
||||
*args, check_rc=check_rc, data=data, cwd=cwd, environ_update=environ_update
|
||||
)
|
||||
if warn_on_stderr and stderr:
|
||||
self.warn(to_native(stderr))
|
||||
self.warn(to_text(stderr))
|
||||
try:
|
||||
data = json.loads(stdout)
|
||||
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)}"
|
||||
f"Error while parsing JSON output of {self._compose_cmd_str(args)}: {exc}\nJSON output: {to_text(stdout)}"
|
||||
)
|
||||
return rc, data, stderr
|
||||
|
||||
@@ -213,7 +211,7 @@ class AnsibleDockerClientBase:
|
||||
*args, check_rc=check_rc, data=data, cwd=cwd, environ_update=environ_update
|
||||
)
|
||||
if warn_on_stderr and stderr:
|
||||
self.warn(to_native(stderr))
|
||||
self.warn(to_text(stderr))
|
||||
result = []
|
||||
try:
|
||||
for line in stdout.splitlines():
|
||||
@@ -222,7 +220,7 @@ class AnsibleDockerClientBase:
|
||||
result.append(json.loads(line))
|
||||
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)}"
|
||||
f"Error while parsing JSON output of {self._compose_cmd_str(args)}: {exc}\nJSON output: {to_text(stdout)}"
|
||||
)
|
||||
return rc, result, stderr
|
||||
|
||||
@@ -338,7 +336,7 @@ class AnsibleDockerClientBase:
|
||||
self.log(f"Image {name}:{tag} not found.")
|
||||
return None
|
||||
if rc != 0:
|
||||
self.fail(f"Error inspecting image {name}:{tag} - {to_native(stderr)}")
|
||||
self.fail(f"Error inspecting image {name}:{tag} - {to_text(stderr)}")
|
||||
return image[0]
|
||||
|
||||
self.log(f"Image {name}:{tag} not found.")
|
||||
@@ -367,11 +365,11 @@ class AnsibleDockerClientBase:
|
||||
rc, image, stderr = self.call_cli_json("image", "inspect", image_id)
|
||||
if not image:
|
||||
if not accept_missing_image:
|
||||
self.fail(f"Error inspecting image ID {image_id} - {to_native(stderr)}")
|
||||
self.fail(f"Error inspecting image ID {image_id} - {to_text(stderr)}")
|
||||
self.log(f"Image {image_id} not found.")
|
||||
return None
|
||||
if rc != 0:
|
||||
self.fail(f"Error inspecting image ID {image_id} - {to_native(stderr)}")
|
||||
self.fail(f"Error inspecting image ID {image_id} - {to_text(stderr)}")
|
||||
return image[0]
|
||||
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ from collections import namedtuple
|
||||
from shlex import quote
|
||||
|
||||
from ansible.module_utils.basic import missing_required_lib
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._logfmt import (
|
||||
InvalidLogFmt as _InvalidLogFmt,
|
||||
@@ -418,7 +418,7 @@ def parse_json_events(
|
||||
ResourceType.UNKNOWN,
|
||||
None,
|
||||
"Warning",
|
||||
to_native(line[len(b"Warning: ") :]),
|
||||
to_text(line[len(b"Warning: ") :]),
|
||||
)
|
||||
events.append(event)
|
||||
continue
|
||||
@@ -557,7 +557,7 @@ def parse_events(
|
||||
if stderr_lines and stderr_lines[-1] == b"":
|
||||
del stderr_lines[-1]
|
||||
for index, line in enumerate(stderr_lines):
|
||||
line = to_native(line.strip())
|
||||
line = to_text(line.strip())
|
||||
if not line:
|
||||
continue
|
||||
warn_missing_dry_run_prefix = False
|
||||
@@ -731,8 +731,8 @@ def update_failed(
|
||||
result["failed"] = True
|
||||
result["msg"] = "\n".join(errors)
|
||||
result["cmd"] = " ".join(quote(arg) for arg in [cli] + args)
|
||||
result["stdout"] = to_native(stdout)
|
||||
result["stderr"] = to_native(stderr)
|
||||
result["stdout"] = to_text(stdout)
|
||||
result["stderr"] = to_text(stderr)
|
||||
result["rc"] = rc
|
||||
return True
|
||||
|
||||
@@ -978,8 +978,8 @@ class BaseComposeManager(DockerBaseClass):
|
||||
ignore_build_events=ignore_build_events,
|
||||
)
|
||||
result["actions"] = result.get("actions", []) + extract_actions(events)
|
||||
result["stdout"] = combine_text_output(result.get("stdout"), to_native(stdout))
|
||||
result["stderr"] = combine_text_output(result.get("stderr"), to_native(stderr))
|
||||
result["stdout"] = combine_text_output(result.get("stdout"), to_text(stdout))
|
||||
result["stderr"] = combine_text_output(result.get("stderr"), to_text(stderr))
|
||||
|
||||
def update_failed(
|
||||
self,
|
||||
|
||||
@@ -18,7 +18,7 @@ import stat
|
||||
import tarfile
|
||||
import typing as t
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_bytes, to_native, to_text
|
||||
from ansible.module_utils.common.text.converters import to_bytes, to_text
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._api.errors import (
|
||||
APIError,
|
||||
@@ -223,7 +223,7 @@ def put_file(
|
||||
) -> None:
|
||||
"""Transfer a file from local to Docker container."""
|
||||
if not os.path.exists(to_bytes(in_path, errors="surrogate_or_strict")):
|
||||
raise DockerFileNotFound(f"file or module does not exist: {to_native(in_path)}")
|
||||
raise DockerFileNotFound(f"file or module does not exist: {to_text(in_path)}")
|
||||
|
||||
b_in_path = to_bytes(in_path, errors="surrogate_or_strict")
|
||||
|
||||
|
||||
@@ -790,17 +790,17 @@ def _preprocess_mounts(
|
||||
) -> dict[str, t.Any]:
|
||||
last: dict[str, str] = {}
|
||||
|
||||
def check_collision(t: str, name: str) -> None:
|
||||
if t in last:
|
||||
if name == last[t]:
|
||||
def check_collision(target: str, name: str) -> None:
|
||||
if target in last:
|
||||
if name == last[target]:
|
||||
module.fail_json(
|
||||
msg=f'The mount point "{t}" appears twice in the {name} option'
|
||||
msg=f'The mount point "{target}" appears twice in the {name} option'
|
||||
)
|
||||
else:
|
||||
module.fail_json(
|
||||
msg=f'The mount point "{t}" appears both in the {name} and {last[t]} option'
|
||||
msg=f'The mount point "{target}" appears both in the {name} and {last[target]} option'
|
||||
)
|
||||
last[t] = name
|
||||
last[target] = name
|
||||
|
||||
if "mounts" in values:
|
||||
mounts = []
|
||||
|
||||
@@ -10,7 +10,7 @@ from __future__ import annotations
|
||||
import base64
|
||||
import random
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_bytes, to_native, to_text
|
||||
from ansible.module_utils.common.text.converters import to_bytes, to_text
|
||||
|
||||
|
||||
def generate_insecure_key() -> bytes:
|
||||
@@ -31,7 +31,7 @@ def scramble(value: str, key: bytes) -> str:
|
||||
b_value = to_bytes(value)
|
||||
k = key[0]
|
||||
b_value = bytes([k ^ b for b in b_value])
|
||||
return f"=S={to_native(base64.b64encode(b_value))}"
|
||||
return f"=S={to_text(base64.b64encode(b_value))}"
|
||||
|
||||
|
||||
def unscramble(value: str, key: bytes) -> str:
|
||||
|
||||
Reference in New Issue
Block a user