From 2e60a2c80e785b2adbcfd9e5ad3b7ab3f1d6e70e Mon Sep 17 00:00:00 2001 From: Petr Lautrbach Date: Tue, 2 Aug 2016 15:40:28 +0200 Subject: [PATCH] policycoreutils: Don't use subprocess.getstatusoutput() in Python 2 code The commit 7574a50f tried to improve compatibility with Python 3. It changed the code to use subprocess.getstatusoutput() instead of commands.getstatusoutput(). Unfortunately subprocess.getstatusoutput() is not available in Python 2. This patch changes how getstatusoutput() is imported so the code works on Python 2 and Python 3. Fixes: $ chcat -d something Traceback (most recent call last): File "/usr/bin/chcat", line 432, in sys.exit(chcat_replace(["s0"], cmds, login_ind)) File "/usr/bin/chcat", line 271, in chcat_replace rc = subprocess.getstatusoutput(cmd) AttributeError: 'module' object has no attribute 'getstatusoutput' Signed-off-by: Petr Lautrbach --- policycoreutils/sandbox/start | 8 +++++--- policycoreutils/scripts/chcat | 17 ++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/policycoreutils/sandbox/start b/policycoreutils/sandbox/start index fc406e1e..7f83532f 100644 --- a/policycoreutils/sandbox/start +++ b/policycoreutils/sandbox/start @@ -1,10 +1,12 @@ #! /usr/bin/python -Es -import gtk -import subprocess +try: + from subprocess import getstatusoutput +except ImportError: + from commands import getstatusoutput import sys rc = [-1, ''] try: - rc = subprocess.getstatusoutput(sys.argv[1]) + rc = getstatusoutput(sys.argv[1]) except: pass if rc[0] == 0: diff --git a/policycoreutils/scripts/chcat b/policycoreutils/scripts/chcat index 2269ee0a..4bd9fc6a 100755 --- a/policycoreutils/scripts/chcat +++ b/policycoreutils/scripts/chcat @@ -22,7 +22,10 @@ # 02111-1307 USA # # -import subprocess +try: + from subprocess import getstatusoutput +except ImportError: + from commands import getstatusoutput import sys import os import pwd @@ -99,7 +102,7 @@ def chcat_user_add(newcat, users): cmd = "semanage login -a -r %s -s %s %s" % (new_serange, user[0], u) else: cmd = "semanage login -m -r %s -s %s %s" % (new_serange, user[0], u) - rc = subprocess.getstatusoutput(cmd) + rc = getstatusoutput(cmd) if rc[0] != 0: print(rc[1]) errors += 1 @@ -138,7 +141,7 @@ def chcat_add(orig, newcat, objects, login_ind): else: cat_string = cat cmd = 'chcon -l %s:%s %s' % (sensitivity, cat_string, f) - rc = subprocess.getstatusoutput(cmd) + rc = getstatusoutput(cmd) if rc[0] != 0: print(rc[1]) errors += 1 @@ -179,7 +182,7 @@ def chcat_user_remove(newcat, users): cmd = "semanage login -a -r %s -s %s %s" % (new_serange, user[0], u) else: cmd = "semanage login -m -r %s -s %s %s" % (new_serange, user[0], u) - rc = subprocess.getstatusoutput(cmd) + rc = getstatusoutput(cmd) if rc[0] != 0: print(rc[1]) errors += 1 @@ -224,7 +227,7 @@ def chcat_remove(orig, newcat, objects, login_ind): cmd = 'chcon -l %s %s' % (sensitivity, f) else: cmd = 'chcon -l %s:%s %s' % (sensitivity, cat, f) - rc = subprocess.getstatusoutput(cmd) + rc = getstatusoutput(cmd) if rc[0] != 0: print(rc[1]) errors += 1 @@ -252,7 +255,7 @@ def chcat_user_replace(newcat, users): cmd = "semanage login -a -r %s -s %s %s" % (new_serange, user[0], u) else: cmd = "semanage login -m -r %s -s %s %s" % (new_serange, user[0], u) - rc = subprocess.getstatusoutput(cmd) + rc = getstatusoutput(cmd) if rc[0] != 0: print(rc[1]) errors += 1 @@ -275,7 +278,7 @@ def chcat_replace(newcat, objects, login_ind): for f in objects: cmd = "%s %s" % (cmd, f) - rc = subprocess.getstatusoutput(cmd) + rc = getstatusoutput(cmd) if rc[0] != 0: print(rc[1]) errors += 1