backport py27 compatible file open with encoding

client and builder needs to run on py27 which doesn't support
open(encoding='utf-8')

Related: https://pagure.io/koji/issue/2641
This commit is contained in:
Tomas Kopecek 2021-02-12 15:34:26 +01:00
parent 9e376a22b0
commit c6e69b4f8b
10 changed files with 62 additions and 59 deletions

View file

@ -49,6 +49,7 @@ import warnings
import weakref
import xml.sax
import xml.sax.handler
from contextlib import contextmanager
from fnmatch import fnmatch
import dateutil.parser
@ -1266,6 +1267,15 @@ class POMHandler(xml.sax.handler.ContentHandler):
self.values.clear()
# BEGIN kojikamid dup #
def _open_text_file(path, mode='rt'):
# enforce utf-8 encoding for py3
if six.PY2:
return open(path, mode)
else:
return open(path, mode, encoding='utf-8')
# END kojikamid dup $
ENTITY_RE = re.compile(r'&[A-Za-z0-9]+;')
@ -1283,8 +1293,7 @@ def parse_pom(path=None, contents=None):
values = {}
handler = POMHandler(values, fields)
if path:
with open(path, encoding='utf-8') as fd:
contents = fd.read()
contents = _open_text_file(path).read()
if not contents:
raise GenericError(
@ -1355,12 +1364,12 @@ def hex_string(s):
def load_json(filepath):
"""Loads json from file"""
return json.load(open(filepath, 'rt', encoding='utf-8'))
return json.load(_open_text_file(filepath))
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)
json.dump(data, _open_text_file(filepath, 'wt'), indent=indent, sort_keys=sort_keys)
def make_groups_spec(grplist, name='buildsys-build', buildgroup=None):
@ -1621,7 +1630,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
files['etc/hosts'] = open('/etc/hosts', 'rt', encoding='utf-8').read()
files['etc/hosts'] = _open_text_file('/etc/hosts').read()
mavenrc = ''
if opts.get('maven_opts'):
mavenrc = 'export MAVEN_OPTS="%s"\n' % ' '.join(opts['maven_opts'])

View file

@ -383,7 +383,7 @@ def getCanonX86_64Arch(arch):
def getCanonArch(skipRpmPlatform=0):
if not skipRpmPlatform and os.access("/etc/rpm/platform", os.R_OK):
try:
with open("/etc/rpm/platform", "rt", encoding='utf-8') as f:
with open("/etc/rpm/platform", "rt") as f:
line = f.readline()
(arch, vendor, opersys) = line.split("-", 2)
return arch

View file

@ -790,7 +790,7 @@ class TaskManager(object):
fn = "%s/%s" % (configdir, f)
if not os.path.isfile(fn):
continue
fo = open(fn, 'rt', encoding='utf-8')
fo = koji._open_text_file(fn)
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, 'rt', encoding='utf-8')
proc_file = koji._open_text_file(proc_path)
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, 'rt', encoding='utf-8')
cmd_file = koji._open_text_file(cmd_path)
procstats[1] = cmd_file.read().replace('\0', ' ').strip()
cmd_file.close()
if not procstats[1]: