explicit encoding for text file operations

Fixes: https://pagure.io/koji/issue/2641
This commit is contained in:
Tomas Kopecek 2021-01-14 16:37:47 +01:00
parent ffa0912bc0
commit 96ae0ecef5
29 changed files with 136 additions and 156 deletions

View file

@ -29,6 +29,7 @@ import datetime
import errno
import hashlib
import imp
import json
import logging
import logging.handlers
import optparse
@ -1282,9 +1283,8 @@ def parse_pom(path=None, contents=None):
values = {}
handler = POMHandler(values, fields)
if path:
fd = open(path)
contents = fd.read()
fd.close()
with open(path, encoding='utf-8') as fd:
contents = fd.read()
if not contents:
raise GenericError(
@ -1353,6 +1353,16 @@ def hex_string(s):
return ''.join(['%02x' % _ord(x) for x in s])
def load_json(filepath):
"""Loads json from file"""
return json.load(open(filepath, 'rt', encoding='utf-8'))
def dump_json(filepath, data, indent=4, sort_keys=False):
"""Write json to file"""
json.dump(data, open(filepath, 'wt', encoding='utf-8'), indent=indent, sort_keys=sort_keys)
def make_groups_spec(grplist, name='buildsys-build', buildgroup=None):
"""Return specfile contents representing the group"""
if buildgroup is None:
@ -1611,9 +1621,7 @@ def genMockConfig(name, arch, managed=False, repoid=None, tag_name=None, **opts)
if opts.get('use_host_resolv', False) and os.path.exists('/etc/hosts'):
# if we're setting up DNS,
# also copy /etc/hosts from the host
etc_hosts = open('/etc/hosts')
files['etc/hosts'] = etc_hosts.read()
etc_hosts.close()
files['etc/hosts'] = open('/etc/hosts', 'rt', encoding='utf-8').read()
mavenrc = ''
if opts.get('maven_opts'):
mavenrc = 'export MAVEN_OPTS="%s"\n' % ' '.join(opts['maven_opts'])
@ -1832,7 +1840,7 @@ def openRemoteFile(relpath, topurl=None, topdir=None, tempdir=None):
downloadFile(url, path=relpath, fo=fo)
elif topdir:
fn = "%s/%s" % (topdir, relpath)
fo = open(fn)
fo = open(fn, 'rb')
else:
raise GenericError("No access method for remote file: %s" % relpath)
return fo

View file

@ -237,7 +237,7 @@ def _try_read_cpuinfo():
""" Try to read /proc/cpuinfo ... if we can't ignore errors (ie. proc not
mounted). """
try:
return open("/proc/cpuinfo", "r")
return open("/proc/cpuinfo", "rt")
except Exception:
return []
@ -383,9 +383,8 @@ def getCanonX86_64Arch(arch):
def getCanonArch(skipRpmPlatform=0):
if not skipRpmPlatform and os.access("/etc/rpm/platform", os.R_OK):
try:
f = open("/etc/rpm/platform", "r")
line = f.readline()
f.close()
with open("/etc/rpm/platform", "rt", encoding='utf-8') as f:
line = f.readline()
(arch, vendor, opersys) = line.split("-", 2)
return arch
except Exception:

View file

@ -790,7 +790,7 @@ class TaskManager(object):
fn = "%s/%s" % (configdir, f)
if not os.path.isfile(fn):
continue
fo = open(fn, 'r')
fo = open(fn, 'rt', encoding='utf-8')
id = None
name = None
for n in range(10):
@ -1089,7 +1089,7 @@ class TaskManager(object):
proc_path = '/proc/%i/stat' % pid
if not os.path.isfile(proc_path):
return None
proc_file = open(proc_path)
proc_file = open(proc_path, 'rt', encoding='utf-8')
procstats = [not field.isdigit() and field or int(field)
for field in proc_file.read().split()]
proc_file.close()
@ -1097,7 +1097,7 @@ class TaskManager(object):
cmd_path = '/proc/%i/cmdline' % pid
if not os.path.isfile(cmd_path):
return None
cmd_file = open(cmd_path)
cmd_file = open(cmd_path, 'rt', encoding='utf-8')
procstats[1] = cmd_file.read().replace('\0', ' ').strip()
cmd_file.close()
if not procstats[1]: