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 <module>
    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 <plautrba@redhat.com>
This commit is contained in:
Petr Lautrbach 2016-08-02 15:40:28 +02:00 committed by James Carter
parent 4c5b8a9568
commit 2e60a2c80e
2 changed files with 15 additions and 10 deletions

View File

@ -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:

View File

@ -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