From ecb2d8b01ec2fbd75f3b0ff7a13f7af8788c8f7e Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 25 Oct 2025 01:41:59 +0200 Subject: [PATCH] Address consider-using-with. --- .pylintrc | 1 - plugins/connection/docker.py | 2 + plugins/module_utils/_api/utils/build.py | 73 ++++++++++++------------ 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/.pylintrc b/.pylintrc index 906f4069..5d2837de 100644 --- a/.pylintrc +++ b/.pylintrc @@ -379,7 +379,6 @@ disable=raw-checker-failed, wrong-import-order, wrong-import-position, # To clean up: - consider-using-with, fixme, import-error, # TODO figure out why pylint cannot find the module no-name-in-module, # TODO figure out why pylint cannot find the module diff --git a/plugins/connection/docker.py b/plugins/connection/docker.py index 22b82c9c..cc0cf461 100644 --- a/plugins/connection/docker.py +++ b/plugins/connection/docker.py @@ -525,6 +525,7 @@ class Connection(ConnectionBase): ) args = [to_bytes(i, errors="surrogate_or_strict") for i in args] try: + # pylint: disable-next=consider-using-with p = subprocess.Popen( args, stdin=in_file, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) @@ -587,6 +588,7 @@ class Connection(ConnectionBase): to_bytes(actual_out_path, errors="surrogate_or_strict"), "wb" ) as out_file: try: + # pylint: disable-next=consider-using-with pp = subprocess.Popen( args, stdin=subprocess.PIPE, diff --git a/plugins/module_utils/_api/utils/build.py b/plugins/module_utils/_api/utils/build.py index cc54a55a..b46cc32a 100644 --- a/plugins/module_utils/_api/utils/build.py +++ b/plugins/module_utils/_api/utils/build.py @@ -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() - tarf = 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 = 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 + 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: - 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) + 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) - tarf.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)) - tarf.close() fileobj.seek(0) return fileobj