osbuild: explicit encodings for open()

Provides explicit encodings for all calls to `open()`, this is a newer
pylint warning but also just makes sense to do.
This commit is contained in:
Simon de Vlieger 2022-09-09 11:19:07 +02:00
parent 42552e0436
commit 3703328751
8 changed files with 22 additions and 22 deletions

View file

@ -147,7 +147,7 @@ class API(BaseAPI):
def _set_metadata(self, message, fds):
fd = message["metadata"]
with os.fdopen(fds.steal(fd), encoding="utf-8") as f:
with os.fdopen(fds.steal(fd), encoding="utf8") as f:
data = json.load(f)
self.metadata.update(data)
@ -195,7 +195,7 @@ def exception_handler(path="/run/osbuild/api/osbuild"):
def arguments(path="/run/osbuild/api/arguments"):
"""Retrieve the input arguments that were supplied to API"""
with open(path, "r", encoding="utf-8") as fp:
with open(path, "r", encoding="utf8") as fp:
data = json.load(fp)
return data
@ -205,9 +205,9 @@ def metadata(data: Dict, path="/run/osbuild/api/osbuild"):
def data_to_file():
with tempfile.TemporaryFile() as f:
f.write(json.dumps(data).encode('utf-8'))
f.write(json.dumps(data).encode('utf8'))
# re-open the file to get a read-only file descriptor
return open(f"/proc/self/fd/{f.fileno()}", "r")
return open(f"/proc/self/fd/{f.fileno()}", "r", encoding="utf8")
with jsoncomm.Socket.new_client(path) as client, data_to_file() as f:
msg = {

View file

@ -62,12 +62,12 @@ class ProcOverrides:
@property
def cmdline(self) -> str:
with open(os.path.join(self.path, "cmdline"), "r") as f:
with open(os.path.join(self.path, "cmdline"), "r", encoding="utf8") as f:
return f.read().strip()
@cmdline.setter
def cmdline(self, value) -> None:
with open(os.path.join(self.path, "cmdline"), "w") as f:
with open(os.path.join(self.path, "cmdline"), "w", encoding="utf8") as f:
f.write(value + "\n")
self.overrides.add("cmdline")

View file

@ -27,7 +27,7 @@ def parse_manifest(path):
if path == "-":
manifest = json.load(sys.stdin)
else:
with open(path) as f:
with open(path, encoding="utf8") as f:
manifest = json.load(f)
return manifest

View file

@ -422,7 +422,7 @@ class ModuleInfo:
path = os.path.join(root, base, name)
try:
with open(path) as f:
with open(path, encoding="utf8") as f:
data = f.read()
except FileNotFoundError:
return None
@ -572,7 +572,7 @@ class Index:
if klass == "Manifest":
path = f"{self.path}/schemas/osbuild{version}.json"
with contextlib.suppress(FileNotFoundError):
with open(path, "r") as f:
with open(path, "r", encoding="utf8") as f:
schema = json.load(f)
elif klass in ModuleInfo.MODULES:
info = self.get_module_info(klass, name)

View file

@ -25,7 +25,7 @@ def replace(target, patterns):
finder = [(re.compile(p), s) for p, s in patterns]
newfile = target + ".replace"
with open(target, "r") as i, open(newfile, "w") as o:
with open(target, "r", encoding="utf8") as i, open(newfile, "w", encoding="utf8") as o:
for line in i:
for p, s in finder:
line = p.sub(s, line)
@ -95,7 +95,7 @@ class Script:
dirname = os.path.dirname(target)
os.makedirs(dirname, exist_ok=True)
print(f"append '{target}' '{data}'")
with open(target, "a", encoding="utf-8") as f:
with open(target, "a", encoding="utf8") as f:
f.write(bytes(data, "utf8").decode("unicode_escape"))
f.write("\n")
@ -192,7 +192,7 @@ def brace_expand_line(line):
def render_template(path, args):
"""Render a template at `path` with arguments `args`"""
with open(path, "r") as f:
with open(path, "r", encoding="utf8") as f:
data = f.read()
tlp = mako.template.Template(text=data, filename=path)

View file

@ -26,7 +26,7 @@ def parse_files(*paths):
path = next((p for p in paths if os.path.exists(p)), None)
if path:
with open(path) as f:
with open(path, encoding="utf8") as f:
for line in f:
line = line.strip()
if not line:

View file

@ -102,7 +102,7 @@ class Treefile:
fd, name = tempfile.mkstemp(suffix=".json",
text=True)
with os.fdopen(fd, "w+") as f:
with os.fdopen(fd, "w+", encoding="utf8") as f:
self.dump(f)
yield name
@ -120,7 +120,7 @@ def rev_parse(repo: PathLike, ref: str) -> str:
repo = repo.decode("utf8")
r = subprocess.run(["ostree", "rev-parse", ref, f"--repo={repo}"],
encoding="utf-8",
encoding="utf8",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=False)
@ -141,7 +141,7 @@ def show(repo: PathLike, checksum: str) -> str:
repo = repo.decode("utf8")
r = subprocess.run(["ostree", "show", f"--repo={repo}", checksum],
encoding="utf-8",
encoding="utf8",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=False)
@ -188,7 +188,7 @@ class PasswdLike:
if not os.path.isfile(path):
return ret
with open(path, "r") as p:
with open(path, "r", encoding="utf8") as p:
ret.db = cls._passwd_lines_to_dict(p.readlines())
return ret
@ -198,7 +198,7 @@ class PasswdLike:
if not os.path.isfile(path):
return
with open(path, "r") as p:
with open(path, "r", encoding="utf8") as p:
additional_passwd_dict = self._passwd_lines_to_dict(p.readlines())
for name, passwd_line in additional_passwd_dict.items():
if name not in self.db:
@ -206,7 +206,7 @@ class PasswdLike:
def dump_to_file(self, path: PathLike):
"""Write the current database to a file"""
with open(path, "w") as p:
with open(path, "w", encoding="utf8") as p:
p.writelines(list(self.db.values()))
@staticmethod
@ -250,13 +250,13 @@ class SubIdsDB:
def read_from(self, path: PathLike) -> int:
"""Read a file and add the entries to the database"""
with open(path, "r", encoding="utf-8") as f:
with open(path, "r", encoding="utf8") as f:
return self.read(f)
def write_to(self, path: PathLike) -> None:
"""Write the database to a file"""
data = self.dumps()
with open(path, "w", encoding="utf-8") as f:
with open(path, "w", encoding="utf8") as f:
f.write(data)
def __bool__(self) -> bool:

View file

@ -43,7 +43,7 @@ class Subscriptions:
"""Read redhat.repo file and process the list of repositories in there."""
ret = cls(None)
with contextlib.suppress(FileNotFoundError):
with open("/etc/yum.repos.d/redhat.repo", "r") as fp:
with open("/etc/yum.repos.d/redhat.repo", "r", encoding="utf8") as fp:
ret = cls.parse_repo_file(fp)
with contextlib.suppress(RuntimeError):