Address unspecified-encoding.

This commit is contained in:
Felix Fontein 2025-10-07 22:19:49 +02:00
parent 057b04b90b
commit 92a810c6d6
15 changed files with 32 additions and 33 deletions

View File

@ -410,7 +410,6 @@ disable=raw-checker-failed,
unexpected-keyword-arg,
unnecessary-dunder-call,
unnecessary-pass,
unspecified-encoding,
unsupported-assignment-operation, # TODO: needs better typing info
unused-argument,
unused-variable,

View File

@ -166,7 +166,7 @@ class AuthConfig(dict):
if not config_file:
return cls({}, credstore_env)
try:
with open(config_file) as f:
with open(config_file, "rt", encoding="utf-8") as f:
config_dict = json.load(f)
except (IOError, KeyError, ValueError) as e:
# Likely missing new Docker config file or it is in an
@ -351,7 +351,7 @@ def _load_legacy_config(config_file):
log.debug("Attempting to parse legacy auth file format")
try:
data = []
with open(config_file) as f:
with open(config_file, "rt", encoding="utf-8") as f:
for line in f.readlines():
data.append(line.strip().split(" = ")[1])
if len(data) < 2:

View File

@ -151,7 +151,7 @@ class ContextAPI(object):
if filename == METAFILE:
filepath = os.path.join(dirname, filename)
try:
with open(filepath, "r") as f:
with open(filepath, "rt", encoding="utf-8") as f:
data = json.load(f)
name = data["Name"]
if name == "default":

View File

@ -32,7 +32,7 @@ def get_current_context_name_with_source():
docker_cfg_path = find_config_file()
if docker_cfg_path:
try:
with open(docker_cfg_path) as f:
with open(docker_cfg_path, "rt", encoding="utf-8") as f:
return (
json.load(f).get("currentContext", "default"),
f"configuration file {docker_cfg_path}",
@ -53,7 +53,7 @@ def write_context_name_to_docker_config(name=None):
config = {}
if docker_cfg_path:
try:
with open(docker_cfg_path) as f:
with open(docker_cfg_path, "rt", encoding="utf-8") as f:
config = json.load(f)
except Exception as e:
return e
@ -67,7 +67,7 @@ def write_context_name_to_docker_config(name=None):
if not docker_cfg_path:
docker_cfg_path = get_default_config_file()
try:
with open(docker_cfg_path, "w") as f:
with open(docker_cfg_path, "wt", encoding="utf-8") as f:
json.dump(config, f, indent=4)
except Exception as e:
return e

View File

@ -133,7 +133,7 @@ class Context(object):
metadata = {}
try:
with open(meta_file) as f:
with open(meta_file, "rt", encoding="utf-8") as f:
metadata = json.load(f)
except (OSError, KeyError, ValueError) as e:
# unknown format
@ -189,7 +189,7 @@ class Context(object):
meta_dir = get_meta_dir(self.name)
if not os.path.isdir(meta_dir):
os.makedirs(meta_dir)
with open(get_meta_file(self.name), "w") as f:
with open(get_meta_file(self.name), "wt", encoding="utf-8") as f:
f.write(json.dumps(self.Metadata))
tls_dir = get_tls_dir(self.name)

View File

@ -222,7 +222,7 @@ class SSHHTTPAdapter(BaseHTTPAdapter):
ssh_config_file = os.path.expanduser("~/.ssh/config")
if os.path.exists(ssh_config_file):
conf = paramiko.SSHConfig()
with open(ssh_config_file) as f:
with open(ssh_config_file, "rt", encoding="utf-8") as f:
conf.parse(f)
host_config = conf.lookup(base_url.hostname)
if "proxycommand" in host_config:

View File

@ -271,7 +271,7 @@ def process_dockerfile(dockerfile, path):
0
] or os.path.relpath(abs_dockerfile, path).startswith(".."):
# Dockerfile not in context - read data to insert into tar later
with open(abs_dockerfile) as df:
with open(abs_dockerfile, "rt", encoding="utf-8") as df:
return (f".dockerfile.{random.getrandbits(160):x}", df.read())
# Dockerfile is inside the context - return path relative to context root

View File

@ -80,7 +80,7 @@ def load_general_config(config_path=None):
return {}
try:
with open(config_file) as f:
with open(config_file, "rt", encoding="utf-8") as f:
return json.load(f)
except (IOError, ValueError) as e:
# In the case of a legacy `.dockercfg` file, we will not

View File

@ -452,7 +452,7 @@ def parse_env_file(env_file):
"""
environment = {}
with open(env_file, "r") as f:
with open(env_file, "rt", encoding="utf-8") as f:
for line in f:
if line[0] == "#":

View File

@ -111,7 +111,7 @@ def log_debug(msg, pretty_print=False):
If ``pretty_print=True``, the message will be pretty-printed as JSON.
"""
with open("docker.log", "a") as log_file:
with open("docker.log", "at", encoding="utf-8") as log_file:
if pretty_print:
log_file.write(
json.dumps(msg, sort_keys=True, indent=4, separators=(",", ": "))

View File

@ -891,7 +891,7 @@ class ImageManager(DockerBaseClass):
dockerignore = os.path.join(self.build_path, ".dockerignore")
exclude = None
if os.path.exists(dockerignore):
with open(dockerignore) as f:
with open(dockerignore, "rt", encoding="utf-8") as f:
exclude = list(
filter(
lambda x: x != "" and x[0] != "#",

View File

@ -161,7 +161,7 @@ class DockerFileStore(object):
try:
# Attempt to read the existing config.
with open(self._config_path, "r") as f:
with open(self._config_path, "rt", encoding="utf-8") as f:
config = json.load(f)
except (ValueError, IOError):
# No config found or an invalid config found so we'll ignore it.

View File

@ -300,7 +300,7 @@ class LoadConfigTest(unittest.TestCase):
self.addCleanup(shutil.rmtree, folder)
cfg_path = os.path.join(folder, ".dockercfg")
auth_ = base64.b64encode(b"sakuya:izayoi").decode("ascii")
with open(cfg_path, "w") as f:
with open(cfg_path, "wt", encoding="utf-8") as f:
f.write(f"auth = {auth_}\n")
f.write("email = sakuya@scarlet.net")
@ -319,7 +319,7 @@ class LoadConfigTest(unittest.TestCase):
cfg_path = os.path.join(folder, ".dockercfg")
auth_ = base64.b64encode(b"sakuya:izayoi").decode("ascii")
email = "sakuya@scarlet.net"
with open(cfg_path, "w") as f:
with open(cfg_path, "wt", encoding="utf-8") as f:
json.dump({auth.INDEX_URL: {"auth": auth_, "email": email}}, f)
cfg = auth.load_config(cfg_path)
assert auth.resolve_authconfig(cfg) is not None
@ -336,7 +336,7 @@ class LoadConfigTest(unittest.TestCase):
cfg_path = os.path.join(folder, "config.json")
auth_ = base64.b64encode(b"sakuya:izayoi").decode("ascii")
email = "sakuya@scarlet.net"
with open(cfg_path, "w") as f:
with open(cfg_path, "wt", encoding="utf-8") as f:
json.dump({"auths": {auth.INDEX_URL: {"auth": auth_, "email": email}}}, f)
cfg = auth.load_config(cfg_path)
assert auth.resolve_authconfig(cfg) is not None
@ -355,7 +355,7 @@ class LoadConfigTest(unittest.TestCase):
auth_ = base64.b64encode(b"sakuya:izayoi").decode("ascii")
config = {registry: {"auth": f"{auth_}", "email": "sakuya@scarlet.net"}}
with open(dockercfg_path, "w") as f:
with open(dockercfg_path, "wt", encoding="utf-8") as f:
json.dump(config, f)
cfg = auth.load_config(dockercfg_path).auths
@ -376,7 +376,7 @@ class LoadConfigTest(unittest.TestCase):
auth_ = base64.b64encode(b"sakuya:izayoi").decode("ascii")
config = {registry: {"auth": f"{auth_}", "email": "sakuya@scarlet.net"}}
with open(dockercfg_path, "w") as f:
with open(dockercfg_path, "wt", encoding="utf-8") as f:
json.dump(config, f)
with mock.patch.dict(os.environ, {"DOCKER_CONFIG": folder}):
@ -400,7 +400,7 @@ class LoadConfigTest(unittest.TestCase):
"auths": {registry: {"auth": f"{auth_}", "email": "sakuya@scarlet.net"}}
}
with open(dockercfg_path, "w") as f:
with open(dockercfg_path, "wt", encoding="utf-8") as f:
json.dump(config, f)
with mock.patch.dict(os.environ, {"DOCKER_CONFIG": folder}):
@ -423,7 +423,7 @@ class LoadConfigTest(unittest.TestCase):
"auths": {registry: {"auth": f"{auth_}", "email": "sakuya@scarlet.net"}}
}
with open(dockercfg_path, "w") as f:
with open(dockercfg_path, "wt", encoding="utf-8") as f:
json.dump(config, f)
with mock.patch.dict(os.environ, {"DOCKER_CONFIG": folder}):
@ -440,7 +440,7 @@ class LoadConfigTest(unittest.TestCase):
self.addCleanup(shutil.rmtree, folder)
dockercfg_path = os.path.join(folder, "config.json")
config = {"detachKeys": "ctrl-q, ctrl-u, ctrl-i"}
with open(dockercfg_path, "w") as f:
with open(dockercfg_path, "wt", encoding="utf-8") as f:
json.dump(config, f)
cfg = auth.load_config(dockercfg_path)
@ -451,7 +451,7 @@ class LoadConfigTest(unittest.TestCase):
self.addCleanup(shutil.rmtree, folder)
dockercfg_path = os.path.join(folder, "config.json")
config = {"auths": {"scarlet.net": {"sakuya": "izayoi"}}}
with open(dockercfg_path, "w") as f:
with open(dockercfg_path, "wt", encoding="utf-8") as f:
json.dump(config, f)
cfg = auth.load_config(dockercfg_path)
@ -465,7 +465,7 @@ class LoadConfigTest(unittest.TestCase):
dockercfg_path = os.path.join(folder, "config.json")
auth_entry = encode_auth({"username": "sakuya"}).decode("ascii")
config = {"auths": {registry: {"auth": auth_entry, "identitytoken": token}}}
with open(dockercfg_path, "w") as f:
with open(dockercfg_path, "wt", encoding="utf-8") as f:
json.dump(config, f)
cfg = auth.load_config(dockercfg_path)

View File

@ -37,7 +37,7 @@ def make_tree(dirs, files):
os.makedirs(os.path.join(base, path))
for path in files:
with open(os.path.join(base, path), "w") as f:
with open(os.path.join(base, path), "wt", encoding="utf-8") as f:
f.write("content")
return base
@ -440,7 +440,7 @@ class TarTest(unittest.TestCase):
base = tempfile.mkdtemp()
full_path = os.path.join(base, "foo")
self.addCleanup(shutil.rmtree, base)
with open(full_path, "w") as f:
with open(full_path, "wt", encoding="utf-8") as f:
f.write("content")
os.chmod(full_path, 0o222)
with pytest.raises(IOError) as ei:
@ -452,7 +452,7 @@ class TarTest(unittest.TestCase):
def test_tar_with_file_symlinks(self):
base = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, base)
with open(os.path.join(base, "foo"), "w") as f:
with open(os.path.join(base, "foo"), "wt", encoding="utf-8") as f:
f.write("content")
os.makedirs(os.path.join(base, "bar"))
os.symlink("../foo", os.path.join(base, "bar/foo"))
@ -500,7 +500,7 @@ class TarTest(unittest.TestCase):
base = tempfile.mkdtemp()
filename = os.path.join(base, "th.txt")
self.addCleanup(shutil.rmtree, base)
with open(filename, "w") as f:
with open(filename, "wt", encoding="utf-8") as f:
f.write("Invisible Full Moon")
os.utime(filename, (12345, -3600.0))
with tar(base) as archive:

View File

@ -96,7 +96,7 @@ class LoadConfigTest(unittest.TestCase):
"HttpHeaders": {"Name": "Spike", "Surname": "Spiegel"},
}
with open(dockercfg_path, "w") as f:
with open(dockercfg_path, "wt", encoding="utf-8") as f:
json.dump(config_data, f)
cfg = config.load_general_config(dockercfg_path)
@ -108,7 +108,7 @@ class LoadConfigTest(unittest.TestCase):
self.addCleanup(shutil.rmtree, folder)
dockercfg_path = os.path.join(folder, "config.json")
config_data = {"detachKeys": "ctrl-q, ctrl-u, ctrl-i"}
with open(dockercfg_path, "w") as f:
with open(dockercfg_path, "wt", encoding="utf-8") as f:
json.dump(config_data, f)
cfg = config.load_general_config(dockercfg_path)
@ -119,7 +119,7 @@ class LoadConfigTest(unittest.TestCase):
self.addCleanup(shutil.rmtree, folder)
dockercfg_path = os.path.join(folder, "config.json")
config_data = {"detachKeys": "ctrl-q, ctrl-u, ctrl-i"}
with open(dockercfg_path, "w") as f:
with open(dockercfg_path, "wt", encoding="utf-8") as f:
json.dump(config_data, f)
with mock.patch.dict(os.environ, {"DOCKER_CONFIG": folder}):