sepolgen: Decode output from Popen in Python3
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/1 sepolgen: Decode output from Popen in Python3
In Python3 output from Popen communicate function returns bytes, to handle output as a string it is needed to properly decode it.
Signed-off-by Robert Kuska <[email protected]>
--- sepolgen/src/sepolgen/audit.py | 7 +++++++ sepolgen/src/sepolgen/util.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+)
sepolgen/src/sepolgen/audit.py +7 -0
@@ -22,6 +22,7 @@ import sys from . import refpolicy from . import access +from . import util # Convenience functions def get_audit_boot_msgs():
@@ -42,6 +43,8 @@ def get_audit_boot_msgs(): boottime = time.strftime("%X", s) output = subprocess.Popen(["/sbin/ausearch", "-m", "AVC,USER_AVC,MAC_POLICY_LOAD,DAEMON_START,SELINUX_ERR", "-ts", bootdate, boottime], stdout=subprocess.PIPE).communicate()[0] + if util.PY3: + output = util.decode_input(output) return output def get_audit_msgs():
@@ -55,6 +58,8 @@ def get_audit_msgs(): import subprocess output = subprocess.Popen(["/sbin/ausearch", "-m", "AVC,USER_AVC,MAC_POLICY_LOAD,DAEMON_START,SELINUX_ERR"], stdout=subprocess.PIPE).communicate()[0] + if util.PY3: + output = util.decode_input(output) return output def get_dmesg_msgs():
@@ -66,6 +71,8 @@ def get_dmesg_msgs(): import subprocess output = subprocess.Popen(["/bin/dmesg"], stdout=subprocess.PIPE).communicate()[0] + if util.PY3: + output = util.decode_input(output) return output # Classes representing audit messages
sepolgen/src/sepolgen/util.py +14 -0
@@ -103,6 +103,20 @@ def encode_input(text): encoded_text = text.encode('utf-8') return encoded_text +def decode_input(text): + import locale + """Decode given text via preferred system encoding""" + # locale will often find out the correct encoding + encoding = locale.getpreferredencoding() + try: + decoded_text = text.decode(encoding) + except UnicodeError: + # if it fails to find correct encoding then ascii is used + # which may lead to UnicodeError if `text` contains non ascii signs + # utf-8 is our guess to fix the situation + decoded_text = text.decode('utf-8') + return decoded_text + class Comparison(): """Class used when implementing rich comparison.
Signature · 1 line
2.4.3
Stephen Smalley
AppliedQuoted 5 lines · Robert Kuska
In Python3 output from Popen communicate function returns bytes, to handle output as a string it is needed to properly decode it.
Signed-off-by Robert Kuska <[email protected]>
Thanks, applied.
Quoted 70 lines
--- sepolgen/src/sepolgen/audit.py | 7 +++++++ sepolgen/src/sepolgen/util.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+)sepolgen/src/sepolgen/audit.py +7
-0@@ -22,6 +22,7 @@ import sys from . import refpolicy from . import access +from . import util # Convenience functions def get_audit_boot_msgs():@@ -42,6 +43,8 @@ def get_audit_boot_msgs(): boottime = time.strftime("%X", s) output = subprocess.Popen(["/sbin/ausearch", "-m", "AVC,USER_AVC,MAC_POLICY_LOAD,DAEMON_START,SELINUX_ERR", "-ts", bootdate, boottime], stdout=subprocess.PIPE).communicate()[0] + if util.PY3: + output = util.decode_input(output) return output def get_audit_msgs():@@ -55,6 +58,8 @@ def get_audit_msgs(): import subprocess output = subprocess.Popen(["/sbin/ausearch", "-m", "AVC,USER_AVC,MAC_POLICY_LOAD,DAEMON_START,SELINUX_ERR"], stdout=subprocess.PIPE).communicate()[0] + if util.PY3: + output = util.decode_input(output) return output def get_dmesg_msgs():@@ -66,6 +71,8 @@ def get_dmesg_msgs(): import subprocess output = subprocess.Popen(["/bin/dmesg"], stdout=subprocess.PIPE).communicate()[0] + if util.PY3: + output = util.decode_input(output) return output # Classes representing audit messagessepolgen/src/sepolgen/util.py +14
-0@@ -103,6 +103,20 @@ def encode_input(text): encoded_text = text.encode('utf-8') return encoded_text +def decode_input(text): + import locale + """Decode given text via preferred system encoding""" + # locale will often find out the correct encoding + encoding = locale.getpreferredencoding() + try: + decoded_text = text.decode(encoding) + except UnicodeError: + # if it fails to find correct encoding then ascii is used + # which may lead to UnicodeError if `text` contains non ascii signs + # utf-8 is our guess to fix the situation + decoded_text = text.decode('utf-8') + return decoded_text + class Comparison(): """Class used when implementing rich comparison.