From 65910f2c336485d19fc2e2e619b7b535cb94ddd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Thu, 24 Aug 2017 15:01:33 +0200 Subject: [PATCH] Open files as binary where needed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In many cases we need to open files as binary to avoid errors on Py3 about writing binary data to file opened in text mode. Signed-off-by: Lubomír Sedlář --- bin/comps_filter | 8 ++++---- pungi/phases/pkgset/sources/source_koji.py | 8 +++++--- pungi/phases/pkgset/sources/source_repos.py | 9 ++++----- pungi/wrappers/comps.py | 2 +- tests/helpers.py | 2 +- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/bin/comps_filter b/bin/comps_filter index 107214d4..67e568a5 100755 --- a/bin/comps_filter +++ b/bin/comps_filter @@ -142,7 +142,7 @@ class CompsFilter(object): def write(self, file_obj): self.tree.write(file_obj, pretty_print=self.reindent, xml_declaration=True, encoding=self.encoding) - file_obj.write("\n") + file_obj.write(b"\n") def main(): @@ -174,8 +174,8 @@ def main(): opts = parser.parse_args() - file_obj = open(opts.comps_file, "r") - f = CompsFilter(file_obj, reindent=not opts.no_reindent) + with open(opts.comps_file, "rb") as file_obj: + f = CompsFilter(file_obj, reindent=not opts.no_reindent) f.filter_packages(opts.arch, opts.arch_only_packages) f.filter_groups(opts.arch, opts.arch_only_groups) f.filter_environments(opts.arch, opts.arch_only_environments) @@ -199,7 +199,7 @@ def main(): if opts.remove_environments: f.remove_environments() - f.write(open(opts.output, 'w') if opts.output else sys.stdout) + f.write(open(opts.output, 'wb') if opts.output else sys.stdout) if __name__ == "__main__": diff --git a/pungi/phases/pkgset/sources/source_koji.py b/pungi/phases/pkgset/sources/source_koji.py index 8034926f..ab08fe5b 100644 --- a/pungi/phases/pkgset/sources/source_koji.py +++ b/pungi/phases/pkgset/sources/source_koji.py @@ -205,7 +205,8 @@ def populate_global_pkgset(compose, koji_wrapper, path_prefix, event_id): if compose.DEBUG and os.path.isfile(global_pkgset_path): msg = "Populating the global package set from tag '%s'" % compose_tags compose.log_warning("[SKIP ] %s" % msg) - global_pkgset = pickle.load(open(global_pkgset_path, "r")) + with open(global_pkgset_path, "rb") as f: + global_pkgset = pickle.load(f) else: global_pkgset = pungi.phases.pkgset.pkgsets.KojiPackageSet( koji_wrapper, compose.conf["sigkeys"], logger=compose._logger, @@ -239,8 +240,9 @@ def populate_global_pkgset(compose, koji_wrapper, path_prefix, event_id): global_pkgset = pkgset else: global_pkgset.merge(pkgset, None, list(all_arches)) - with open(global_pkgset_path, 'w') as f: - f.write(pickle.dumps(global_pkgset)) + with open(global_pkgset_path, 'wb') as f: + data = pickle.dumps(global_pkgset) + f.write(data) # write global package list global_pkgset.save_file_list( diff --git a/pungi/phases/pkgset/sources/source_repos.py b/pungi/phases/pkgset/sources/source_repos.py index f8e36479..b8ca232d 100644 --- a/pungi/phases/pkgset/sources/source_repos.py +++ b/pungi/phases/pkgset/sources/source_repos.py @@ -131,15 +131,14 @@ def populate_global_pkgset(compose, file_list, path_prefix): global_pkgset_path = os.path.join(compose.paths.work.topdir(arch="global"), "packages.pickle") if compose.DEBUG and os.path.isfile(global_pkgset_path): compose.log_warning("[SKIP ] %s" % msg) - pkgset = pickle.load(open(global_pkgset_path, "r")) + with open(global_pkgset_path, "rb") as f: + pkgset = pickle.load(f) else: compose.log_info(msg) pkgset = pungi.phases.pkgset.pkgsets.FilelistPackageSet(compose.conf["sigkeys"], logger=compose._logger, arches=ALL_ARCHES) pkgset.populate(file_list) - f = open(global_pkgset_path, "w") - data = pickle.dumps(pkgset) - f.write(data) - f.close() + with open(global_pkgset_path, "wb") as f: + pickle.dump(pkgset, f) # write global package list pkgset.save_file_list(compose.paths.work.package_list(arch="global"), remove_path_prefix=path_prefix) diff --git a/pungi/wrappers/comps.py b/pungi/wrappers/comps.py index a392158b..852a6861 100644 --- a/pungi/wrappers/comps.py +++ b/pungi/wrappers/comps.py @@ -71,7 +71,7 @@ class CompsWrapper(object): if not target_file: target_file = self.comps_file - with open(target_file, "w") as stream: + with open(target_file, "wb") as stream: stream.write(comps_obj.toprettyxml(indent=" ", encoding="UTF-8")) def generate_comps(self): diff --git a/tests/helpers.py b/tests/helpers.py index 11049935..be6ac6d9 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -141,7 +141,7 @@ def touch(path, content=None): os.makedirs(os.path.dirname(path)) except OSError: pass - with open(path, 'w') as f: + with open(path, 'wb') as f: f.write(content) return path