From 37033287512b1ffae13e2a5f53bb670e0bf9eb18 Mon Sep 17 00:00:00 2001 From: Simon de Vlieger Date: Fri, 9 Sep 2022 11:19:07 +0200 Subject: [PATCH] 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. --- osbuild/api.py | 8 ++++---- osbuild/buildroot.py | 4 ++-- osbuild/main_cli.py | 2 +- osbuild/meta.py | 4 ++-- osbuild/util/lorax.py | 6 +++--- osbuild/util/osrelease.py | 2 +- osbuild/util/ostree.py | 16 ++++++++-------- osbuild/util/rhsm.py | 2 +- 8 files changed, 22 insertions(+), 22 deletions(-) diff --git a/osbuild/api.py b/osbuild/api.py index e6401b21..592ef9e4 100644 --- a/osbuild/api.py +++ b/osbuild/api.py @@ -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 = { diff --git a/osbuild/buildroot.py b/osbuild/buildroot.py index dc3f7bde..49e148d9 100644 --- a/osbuild/buildroot.py +++ b/osbuild/buildroot.py @@ -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") diff --git a/osbuild/main_cli.py b/osbuild/main_cli.py index 6d3aa15f..e75b39e1 100644 --- a/osbuild/main_cli.py +++ b/osbuild/main_cli.py @@ -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 diff --git a/osbuild/meta.py b/osbuild/meta.py index 5b540f5f..33fb9dec 100644 --- a/osbuild/meta.py +++ b/osbuild/meta.py @@ -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) diff --git a/osbuild/util/lorax.py b/osbuild/util/lorax.py index 4a9d277b..8adcd2d1 100644 --- a/osbuild/util/lorax.py +++ b/osbuild/util/lorax.py @@ -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) diff --git a/osbuild/util/osrelease.py b/osbuild/util/osrelease.py index 24e003b9..4dc4e67c 100644 --- a/osbuild/util/osrelease.py +++ b/osbuild/util/osrelease.py @@ -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: diff --git a/osbuild/util/ostree.py b/osbuild/util/ostree.py index 75dcc661..a4fe5586 100644 --- a/osbuild/util/ostree.py +++ b/osbuild/util/ostree.py @@ -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: diff --git a/osbuild/util/rhsm.py b/osbuild/util/rhsm.py index 3ab17292..aebe299a 100644 --- a/osbuild/util/rhsm.py +++ b/osbuild/util/rhsm.py @@ -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):