Address consider-using-with.

This commit is contained in:
Felix Fontein 2025-10-25 01:41:59 +02:00
parent 40c240ac2b
commit ecb2d8b01e
3 changed files with 39 additions and 37 deletions

View File

@ -379,7 +379,6 @@ disable=raw-checker-failed,
wrong-import-order, wrong-import-order,
wrong-import-position, wrong-import-position,
# To clean up: # To clean up:
consider-using-with,
fixme, fixme,
import-error, # TODO figure out why pylint cannot find the module import-error, # TODO figure out why pylint cannot find the module
no-name-in-module, # TODO figure out why pylint cannot find the module no-name-in-module, # TODO figure out why pylint cannot find the module

View File

@ -525,6 +525,7 @@ class Connection(ConnectionBase):
) )
args = [to_bytes(i, errors="surrogate_or_strict") for i in args] args = [to_bytes(i, errors="surrogate_or_strict") for i in args]
try: try:
# pylint: disable-next=consider-using-with
p = subprocess.Popen( p = subprocess.Popen(
args, stdin=in_file, stdout=subprocess.PIPE, stderr=subprocess.PIPE 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" to_bytes(actual_out_path, errors="surrogate_or_strict"), "wb"
) as out_file: ) as out_file:
try: try:
# pylint: disable-next=consider-using-with
pp = subprocess.Popen( pp = subprocess.Popen(
args, args,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,

View File

@ -97,49 +97,50 @@ def create_archive(
) -> t.IO[bytes]: ) -> t.IO[bytes]:
extra_files = extra_files or [] extra_files = extra_files or []
if not fileobj: if not fileobj:
# pylint: disable-next=consider-using-with
fileobj = tempfile.NamedTemporaryFile() 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) with tarfile.open(mode="w:gz" if gzip else "w", fileobj=fileobj) as tarf:
if i is None: if files is None:
# This happens when we encounter a socket file. We can safely files = build_file_list(root)
# ignore it and proceed. extra_names = set(e[0] for e in extra_files)
continue # type: ignore 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 i = tarf.gettarinfo(full_path, arcname=path)
if i.mtime < 0 or i.mtime > 8**11 - 1: if i is None:
i.mtime = int(i.mtime) # This happens when we encounter a socket file. We can safely
# ignore it and proceed.
continue # type: ignore
if IS_WINDOWS_PLATFORM: # Workaround https://bugs.python.org/issue32713
# Windows does not keep track of the execute bit, so we make files if i.mtime < 0 or i.mtime > 8**11 - 1:
# and directories executable by default. i.mtime = int(i.mtime)
i.mode = i.mode & 0o755 | 0o111
if i.isfile(): if IS_WINDOWS_PLATFORM:
try: # Windows does not keep track of the execute bit, so we make files
with open(full_path, "rb") as f: # and directories executable by default.
tarf.addfile(i, f) i.mode = i.mode & 0o755 | 0o111
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: if i.isfile():
info = tarfile.TarInfo(name) try:
contents_encoded = contents.encode("utf-8") with open(full_path, "rb") as f:
info.size = len(contents_encoded) tarf.addfile(i, f)
tarf.addfile(info, io.BytesIO(contents_encoded)) 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) fileobj.seek(0)
return fileobj return fileobj