policycoreutils/sandbox: improve compatibility with Python 3

- gettext.install() only takes optional "unicode" keyword argument in
  Python 2, and its default value is "False". This keyword argument
  doesn't exist in Python 3
- __builtin__ module has been renamed to "builtins" in Python 3
- raw_input() has been renamed to input() in Python 3
- specify octal literals in form compatible with both Python 2 and 3
- migrate from commands to subprocess
- replace print statement with print function
- use reserved word `as` in try-except
- replace deprecated assert_() method with assertTrue() in unit tests

Signed-off-by: Michal Srb <msrb@redhat.com>
This commit is contained in:
Michal Srb 2015-07-21 02:38:20 +02:00 committed by Stephen Smalley
parent d135951152
commit a9ce2e7358
3 changed files with 31 additions and 25 deletions

View File

@ -25,7 +25,6 @@ import selinux
import signal
from tempfile import mkdtemp
import pwd
import commands
import sepolicy
PROGNAME = "policycoreutils"
@ -36,13 +35,16 @@ gettext.bindtextdomain(PROGNAME, "/usr/share/locale")
gettext.textdomain(PROGNAME)
try:
gettext.install(PROGNAME,
localedir = "/usr/share/locale",
unicode=False,
codeset = 'utf-8')
gettext.install(PROGNAME,
localedir = "/usr/share/locale",
codeset = 'utf-8')
except IOError:
import __builtin__
__builtin__.__dict__['_'] = unicode
try:
import builtins
builtins.__dict__['_'] = str
except ImportError:
import __builtin__
__builtin__.__dict__['_'] = unicode
DEFAULT_WINDOWSIZE = "1000x700"
DEFAULT_TYPE = "sandbox_t"
@ -86,7 +88,7 @@ def copyfile(file, srcdir, dest):
else:
shutil.copy2(file, dest)
except shutil.Error, elist:
except shutil.Error as elist:
for e in elist.message:
sys.stderr.write(e[2])
@ -107,7 +109,11 @@ def savefile(new, orig, X_ind):
if rc == gtk.RESPONSE_YES:
copy = True
else:
ans = raw_input(_("Do you want to save changes to '%s' (y/N): ") % orig)
try:
input = raw_input
except NameError:
pass
ans = input(_("Do you want to save changes to '%s' (y/N): ") % orig)
if(re.match(_("[yY]"),ans)):
copy = True
if(copy):
@ -228,9 +234,9 @@ class Sandbox:
for i in fd.readlines():
try:
self.__include(option, opt, i[:-1], parser)
except IOError, e:
except IOError as e:
sys.stderr.write(str(e))
except TypeError, e:
except TypeError as e:
sys.stderr.write(str(e))
fd.close()
@ -263,7 +269,7 @@ dbus-launch --exit-with-session %s
kill -TERM $WM_PID 2> /dev/null
""" % (command, wm, command))
fd.close()
os.chmod(execfile, 0700)
os.chmod(execfile, 0o700)
def usage(self, message = ""):
error_exit("%s\n%s" % (self.__parser.usage, message))
@ -492,13 +498,13 @@ if __name__ == '__main__':
try:
sandbox = Sandbox()
rc = sandbox.main()
except OSError, error:
except OSError as error:
error_exit(error)
except ValueError, error:
except ValueError as error:
error_exit(error.args[0])
except KeyError, error:
except KeyError as error:
error_exit(_("Invalid value %s") % error.args[0])
except IOError, error:
except IOError as error:
error_exit(error)
except KeyboardInterrupt:
rc = 0

View File

@ -1,9 +1,9 @@
#! /usr/bin/python -Es
import gtk, commands, sys
import gtk, subprocess, sys
rc = [-1,'']
try:
rc=commands.getstatusoutput(sys.argv[1])
rc=subprocess.getstatusoutput(sys.argv[1])
except:
pass
if rc[0] == 0:
print rc[1]
print(rc[1])

View File

@ -4,18 +4,18 @@ from subprocess import Popen, PIPE
class SandboxTests(unittest.TestCase):
def assertDenied(self, err):
self.assert_('Permission denied' in err,
self.assertTrue(b'Permission denied' in err,
'"Permission denied" not found in %r' % err)
def assertNotFound(self, err):
self.assert_('not found' in err,
self.assertTrue(b'not found' in err,
'"not found" not found in %r' % err)
def assertFailure(self, status):
self.assert_(status != 0,
self.assertTrue(status != 0,
'"Succeeded when it should have failed')
def assertSuccess(self, status, err):
self.assert_(status == 0,
self.assertTrue(status == 0,
'"Sandbox should have succeeded for this test %r' % err)
def test_simple_success(self):
@ -23,7 +23,7 @@ class SandboxTests(unittest.TestCase):
p1 = Popen(['cat', '/etc/passwd'], stdout = PIPE)
p2 = Popen(['sandbox', 'grep', 'root'], stdin = p1.stdout, stdout=PIPE)
out, err = p2.communicate()
self.assert_('root' in out)
self.assertTrue(b'root' in out)
def test_cant_kill(self):
"Verify that we cannot send kill signal in the sandbox"
@ -95,4 +95,4 @@ if __name__ == "__main__":
if selinux.security_getenforce() == 1:
unittest.main()
else:
print "SELinux must be in enforcing mode for this test"
print("SELinux must be in enforcing mode for this test")