Backport patch for decoding output as UTF-8

This commit is contained in:
Lubomír Sedlář 2019-05-07 15:16:53 +02:00
parent e51dfab051
commit b31609f76e
2 changed files with 146 additions and 1 deletions

141
1185.patch Normal file
View File

@ -0,0 +1,141 @@
From c961fdc99e96a3c341273876b621d78733c70862 Mon Sep 17 00:00:00 2001
From: Lubomír Sedlář <lsedlar@redhat.com>
Date: May 07 2019 12:31:53 +0000
Subject: kojiwrapper: Decode output of runroot as UTF-8
Starting with Python 3.6, it is possible to specify encoding for output
of a `subprocess.Popen`. Try to use it to decode as UTF-8. Particularly
output of ostree runroot tasks is using unicode characters.
This should be safe: anything that is valid ASCII is also valid UTF-8.
Relates: https://pagure.io/dusty/failed-composes/issue/1642
Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
---
diff --git a/pungi/wrappers/kojiwrapper.py b/pungi/wrappers/kojiwrapper.py
index 3cd9ac9..48534ff 100644
--- a/pungi/wrappers/kojiwrapper.py
+++ b/pungi/wrappers/kojiwrapper.py
@@ -19,6 +19,7 @@ import re
import time
import threading
import contextlib
+import sys
import koji
from kobo.shortcuts import run
@@ -146,8 +147,16 @@ class KojiWrapper(object):
"""
task_id = None
with self.get_koji_cmd_env() as env:
- retcode, output = run(command, can_fail=True, logfile=log_file,
- show_cmd=True, env=env, universal_newlines=True)
+ extra_kwarg = {"encoding": "utf-8"} if sys.version_info >= (3, 6) else {}
+ retcode, output = run(
+ command,
+ can_fail=True,
+ logfile=log_file,
+ show_cmd=True,
+ env=env,
+ universal_newlines=True,
+ **extra_kwarg
+ )
if "--task-id" in command:
first_line = output.splitlines()[0]
if re.match(r'^\d+$', first_line):
diff --git a/tests/test_koji_wrapper.py b/tests/test_koji_wrapper.py
index 34d8b3a..39d405d 100644
--- a/tests/test_koji_wrapper.py
+++ b/tests/test_koji_wrapper.py
@@ -405,6 +405,21 @@ class LiveImageKojiWrapperTest(KojiWrapperBaseTestCase):
class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
+
+ def kwargs(self, **kwargs):
+ defaults= {
+ "can_fail": True,
+ "env": None,
+ "logfile": None,
+ "show_cmd": True,
+ "universal_newlines": True,
+ }
+ if sys.version_info >= (3, 6):
+ defaults["encoding"] = "utf-8"
+
+ defaults.update(kwargs)
+ return defaults
+
def test_get_cmd_minimal(self):
cmd = self.koji.get_runroot_cmd('tgt', 's390x', 'date', use_shell=False, task_id=False)
self.assertEqual(len(cmd), 7)
@@ -457,11 +472,7 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
result = self.koji.run_runroot_cmd(cmd)
self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': None})
- self.assertEqual(
- run.call_args_list,
- [mock.call(cmd, can_fail=True, env=None, logfile=None, show_cmd=True,
- universal_newlines=True)]
- )
+ self.assertEqual(run.call_args_list, [mock.call(cmd, **self.kwargs())])
@mock.patch('pungi.wrappers.kojiwrapper.run')
def test_run_runroot_cmd_with_task_id(self, run):
@@ -471,11 +482,7 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
result = self.koji.run_runroot_cmd(cmd)
self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': 1234})
- self.assertEqual(
- run.call_args_list,
- [mock.call(cmd, can_fail=True, env=None, logfile=None, show_cmd=True,
- universal_newlines=True)]
- )
+ self.assertEqual(run.call_args_list, [mock.call(cmd, **self.kwargs())])
@mock.patch('pungi.wrappers.kojiwrapper.run')
def test_run_runroot_cmd_with_task_id_and_fail(self, run):
@@ -485,11 +492,7 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
result = self.koji.run_runroot_cmd(cmd)
self.assertDictEqual(result, {'retcode': 1, 'output': output, 'task_id': None})
- self.assertEqual(
- run.call_args_list,
- [mock.call(cmd, can_fail=True, env=None, logfile=None, show_cmd=True,
- universal_newlines=True)]
- )
+ self.assertEqual(run.call_args_list, [mock.call(cmd, **self.kwargs())])
@mock.patch('pungi.wrappers.kojiwrapper.run')
def test_run_runroot_cmd_with_task_id_and_fail_but_emit_id(self, run):
@@ -499,11 +502,7 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
result = self.koji.run_runroot_cmd(cmd)
self.assertDictEqual(result, {'retcode': 1, 'output': output, 'task_id': 12345})
- self.assertEqual(
- run.call_args_list,
- [mock.call(cmd, can_fail=True, env=None, logfile=None, show_cmd=True,
- universal_newlines=True)]
- )
+ self.assertEqual(run.call_args_list, [mock.call(cmd, **self.kwargs())])
@mock.patch.dict('os.environ', {'FOO': 'BAR'}, clear=True)
@mock.patch('shutil.rmtree')
@@ -521,8 +520,11 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': None})
self.assertEqual(
run.call_args_list,
- [mock.call(cmd, can_fail=True, env={'KRB5CCNAME': 'DIR:/tmp/foo', 'FOO': 'BAR'},
- logfile=None, show_cmd=True, universal_newlines=True)]
+ [
+ mock.call(
+ cmd, **self.kwargs(env={'KRB5CCNAME': 'DIR:/tmp/foo', 'FOO': 'BAR'})
+ ),
+ ],
)

View File

@ -2,7 +2,7 @@
Name: pungi
Version: 4.1.36
Release: 2%{?dist}
Release: 3%{?dist}
Summary: Distribution compose tool
License: GPLv2
@ -10,6 +10,7 @@ URL: https://pagure.io/pungi
Source0: https://pagure.io/releases/%{name}/%{name}-%{version}.tar.bz2
Patch0: https://pagure.io/pungi/pull-request/1183.patch
Patch1: https://pagure.io/pungi/pull-request/1184.patch
Patch2: https://pagure.io/pungi/pull-request/1185.patch
BuildRequires: python3-nose
BuildRequires: python3-mock
@ -204,6 +205,9 @@ nosetests-3 --exe
%{_bindir}/%{name}-wait-for-signed-ostree-handler
%changelog
* Tue May 07 2019 Lubomír Sedlář <lsedlar@redhat.com> - 4.1.36-3
- Backport patch for decoding output as UTF-8
* Tue May 07 2019 Lubomír Sedlář <lsedlar@redhat.com> - 4.1.36-2
- Backport fixes for cloning git repos