Open files as binary where needed

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ář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2017-08-24 15:01:33 +02:00
parent fcbc3ed4ae
commit 65910f2c33
5 changed files with 15 additions and 14 deletions

View File

@ -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__":

View File

@ -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(

View File

@ -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)

View File

@ -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):

View File

@ -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