From bd09ea910be66f0b3655030241387d1a3a856bc6 Mon Sep 17 00:00:00 2001 From: Christian Boltz Date: Tue, 7 May 2013 22:38:18 +0200 Subject: [PATCH] Backport the python3 compability changes from trunk r2052 to the 2.8 branch. Original commit message: committer: Jamie Strandboge Initial port to python3 for utilities. Thanks to Dmitrijs Ledkovs Acked-By: Jamie Strandboge Most of trunk r2052 also applies to the 2.8 branch. The only difference is the last section of changes in utils/vim/create-apparmor.vim.py Acked-by: John Johansen Acked-by: Steve Beattie Skimmed-by: Seth Arnold --- common/Make.rules | 15 +++++++ libraries/libapparmor/m4/ac_python_devel.m4 | 46 ++++++++++----------- utils/Makefile | 6 +-- utils/aa-easyprof | 4 +- utils/apparmor/easyprof.py | 25 ++++++----- utils/test/test-aa-easyprof.py | 7 ++-- utils/vim/Makefile | 5 ++- utils/vim/create-apparmor.vim.py | 17 ++++---- 8 files changed, 72 insertions(+), 53 deletions(-) diff --git a/common/Make.rules b/common/Make.rules index abd61390a..7320b21ea 100644 --- a/common/Make.rules +++ b/common/Make.rules @@ -32,6 +32,10 @@ ifndef AWK $(error awk utility required for build but not available) endif +# Convenience functions +pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH))))) +map = $(foreach a,$(2),$(call $(1),$(a))) + # OVERRIDABLE variables # Set these variables before including Make.rules to change its behavior # SPECFILE - for packages that have a non-standard specfile name @@ -132,6 +136,17 @@ endif endif +ifndef PYTHON_VERSIONS +PYTHON_VERSIONS = $(call map, pathsearch, python2 python3) +endif + +ifndef PYTHON +PYTHON = $(firstword ${PYTHON_VERSIONS}) +endif + +#Helper function to be used with $(call pyalldo, run_test_with_all.py) +pyalldo=set -e; $(foreach py, $(PYTHON_VERSIONS), $(py) $(1);) + .PHONY: version .SILENT: version version: diff --git a/libraries/libapparmor/m4/ac_python_devel.m4 b/libraries/libapparmor/m4/ac_python_devel.m4 index 8b5d414f2..cba3400fb 100644 --- a/libraries/libapparmor/m4/ac_python_devel.m4 +++ b/libraries/libapparmor/m4/ac_python_devel.m4 @@ -17,9 +17,9 @@ AC_DEFUN([AC_PYTHON_DEVEL],[ # Check for a version of Python >= 2.1.0 # AC_MSG_CHECKING([for a version of Python >= '2.1.0']) - ac_supports_python_ver=`$PYTHON -c "import sys, string; \ - ver = string.split(sys.version)[[0]]; \ - print ver >= '2.1.0'"` + ac_supports_python_ver=`$PYTHON -c "import sys; \ + ver = sys.version.split()[[0]]; \ + sys.stdout.write(str(ver >= '2.1.0'))"` if test "$ac_supports_python_ver" != "True"; then if test -z "$PYTHON_NOVERSIONCHECK"; then AC_MSG_RESULT([no]) @@ -44,9 +44,9 @@ to something else than an empty string. # if test -n "$1"; then AC_MSG_CHECKING([for a version of Python $1]) - ac_supports_python_ver=`$PYTHON -c "import sys, string; \ - ver = string.split(sys.version)[[0]]; \ - print ver $1"` + ac_supports_python_ver=`$PYTHON -c "import sys; \ + ver = sys.version.split()[[0]]; \ + sys.stdout.write("%s\n" % (ver == $1))"` if test "$ac_supports_python_ver" = "True"; then AC_MSG_RESULT([yes]) else @@ -80,8 +80,8 @@ $ac_distutils_result]) # AC_MSG_CHECKING([for Python include path]) if test -z "$PYTHON_CPPFLAGS"; then - python_path=`$PYTHON -c "import distutils.sysconfig; \ - print distutils.sysconfig.get_python_inc();"` + python_path=`$PYTHON -c "import sys; import distutils.sysconfig;\ +sys.stdout.write('%s\n' % distutils.sysconfig.get_python_inc());"` if test -n "${python_path}"; then python_path="-I$python_path" fi @@ -97,22 +97,20 @@ $ac_distutils_result]) if test -z "$PYTHON_LDFLAGS"; then # (makes two attempts to ensure we've got a version number # from the interpreter) - py_version=`$PYTHON -c "from distutils.sysconfig import *; \ - from string import join; \ - print join(get_config_vars('VERSION'))"` + py_version=`$PYTHON -c "import sys; from distutils.sysconfig import *; \ +sys.stdout.write('%s\n' % ''.join(get_config_vars('VERSION')))"` if test "$py_version" == "[None]"; then if test -n "$PYTHON_VERSION"; then py_version=$PYTHON_VERSION else py_version=`$PYTHON -c "import sys; \ - print sys.version[[:3]]"` +sys.stdout.write("%s\n" % sys.version[[:3]])"` fi fi - PYTHON_LDFLAGS=`$PYTHON -c "from distutils.sysconfig import *; \ - from string import join; \ - print '-L' + get_python_lib(0,1), \ - '-lpython';"`$py_version + PYTHON_LDFLAGS=`$PYTHON -c "import sys; from distutils.sysconfig import *; \ +sys.stdout.write('-L' + get_python_lib(0,1) + ' -lpython\n')"`$py_version`$PYTHON -c \ +"import sys; sys.stdout.write('%s' % getattr(sys,'abiflags',''))"` fi AC_MSG_RESULT([$PYTHON_LDFLAGS]) AC_SUBST([PYTHON_LDFLAGS]) @@ -122,8 +120,8 @@ $ac_distutils_result]) # AC_MSG_CHECKING([for Python site-packages path]) if test -z "$PYTHON_SITE_PKG"; then - PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \ - print distutils.sysconfig.get_python_lib(0,0);"` + PYTHON_SITE_PKG=`$PYTHON -c "import sys; import distutils.sysconfig; \ +sys.stdout.write('%s\n' % distutils.sysconfig.get_python_lib(0,0));"` fi AC_MSG_RESULT([$PYTHON_SITE_PKG]) AC_SUBST([PYTHON_SITE_PKG]) @@ -133,9 +131,9 @@ $ac_distutils_result]) # AC_MSG_CHECKING(python extra libraries) if test -z "$PYTHON_EXTRA_LIBS"; then - PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \ - conf = distutils.sysconfig.get_config_var; \ - print conf('LOCALMODLIBS'), conf('LIBS')"` + PYTHON_EXTRA_LIBS=`$PYTHON -c "import sys; import distutils.sysconfig; \ +conf = distutils.sysconfig.get_config_var; \ +sys.stdout.write('%s %s\n' % (conf('LOCALMODLIBS'), conf('LIBS')))"` fi AC_MSG_RESULT([$PYTHON_EXTRA_LIBS]) AC_SUBST(PYTHON_EXTRA_LIBS) @@ -145,9 +143,9 @@ $ac_distutils_result]) # AC_MSG_CHECKING(python extra linking flags) if test -z "$PYTHON_EXTRA_LDFLAGS"; then - PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \ - conf = distutils.sysconfig.get_config_var; \ - print conf('LINKFORSHARED')"` + PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import sys; import distutils.sysconfig; \ +conf = distutils.sysconfig.get_config_var; \ +sys.stdout.write('%s\n' % conf('LINKFORSHARED'))"` fi AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS]) AC_SUBST(PYTHON_EXTRA_LDFLAGS) diff --git a/utils/Makefile b/utils/Makefile index 0618a18d9..cfe28fe92 100644 --- a/utils/Makefile +++ b/utils/Makefile @@ -65,7 +65,7 @@ install: ${MANPAGES} ${HTMLMANPAGES} $(MAKE) install_manpages DESTDIR=${DESTDIR} $(MAKE) -C vim install DESTDIR=${DESTDIR} ln -sf aa-status.8 ${DESTDIR}/${MANDIR}/man8/apparmor_status.8 - python ${PYSETUP} install --prefix=${PYPREFIX} --root=${DESTDIR} --version=${VERSION} + ${PYTHON} ${PYSETUP} install --prefix=${PYPREFIX} --root=${DESTDIR} --version=${VERSION} .PHONY: clean ifndef VERBOSE @@ -105,6 +105,4 @@ check: check_severity_db test -s $$tmpfile && cat $$tmpfile && rm -f $$tmpfile && exit 1; \ done || true; \ rm -f $$tmpfile - for i in test/* ; do \ - python $$i || exit 1; \ - done + $(foreach test, $(wildcard test/test-*.py), $(call pyalldo, $(test))) diff --git a/utils/aa-easyprof b/utils/aa-easyprof index 44e9aaa69..a042c55ee 100755 --- a/utils/aa-easyprof +++ b/utils/aa-easyprof @@ -35,7 +35,7 @@ if __name__ == "__main__": try: easyp = apparmor.easyprof.AppArmorEasyProfile(binary, opt) - except AppArmorException, e: + except AppArmorException as e: error(e.value) except Exception: raise @@ -61,5 +61,5 @@ if __name__ == "__main__": # if we made it here, generate a profile params = apparmor.easyprof.gen_policy_params(binary, opt) p = easyp.gen_policy(**params) - print p, + sys.stdout.write('%s\n' % p) diff --git a/utils/apparmor/easyprof.py b/utils/apparmor/easyprof.py index 92074d375..da99dfda9 100644 --- a/utils/apparmor/easyprof.py +++ b/utils/apparmor/easyprof.py @@ -8,6 +8,8 @@ # # ------------------------------------------------------------------ +from __future__ import with_statement + import codecs import glob import optparse @@ -40,7 +42,7 @@ DEBUGGING = False def error(out, exit_code=1, do_exit=True): '''Print error message and exit''' try: - print >> sys.stderr, "ERROR: %s" % (out) + sys.stderr.write("ERROR: %s\n" % (out)) except IOError: pass @@ -51,7 +53,7 @@ def error(out, exit_code=1, do_exit=True): def warn(out): '''Print warning message''' try: - print >> sys.stderr, "WARN: %s" % (out) + sys.stderr.write("WARN: %s\n" % (out)) except IOError: pass @@ -59,7 +61,7 @@ def warn(out): def msg(out, output=sys.stdout): '''Print message''' try: - print >> output, "%s" % (out) + sys.stdout.write("%s\n" % (out)) except IOError: pass @@ -70,7 +72,7 @@ def cmd(command): try: sp = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - except OSError, ex: + except OSError as ex: return [127, str(ex)] out = sp.communicate()[0] @@ -82,7 +84,7 @@ def cmd_pipe(command1, command2): try: sp1 = subprocess.Popen(command1, stdout=subprocess.PIPE) sp2 = subprocess.Popen(command2, stdin=sp1.stdout) - except OSError, ex: + except OSError as ex: return [127, str(ex)] out = sp2.communicate()[0] @@ -93,7 +95,7 @@ def debug(out): '''Print debug message''' if DEBUGGING: try: - print >> sys.stderr, "DEBUG: %s" % (out) + sys.stderr.write("DEBUG: %s\n" % (out)) except IOError: pass @@ -181,6 +183,8 @@ def verify_policy(policy): fn = policy else: f, fn = tempfile.mkstemp(prefix='aa-easyprof') + if not isinstance(policy, bytes): + policy = policy.encode('utf-8') os.write(f, policy) os.close(f) @@ -219,9 +223,9 @@ class AppArmorEasyProfile: if opt.policy_groups_dir and os.path.isdir(opt.policy_groups_dir): self.dirs['policygroups'] = os.path.abspath(opt.policy_groups_dir) - if not self.dirs.has_key('templates'): + if not 'templates' in self.dirs: raise AppArmorException("Could not find templates directory") - if not self.dirs.has_key('policygroups'): + if not 'policygroups' in self.dirs: raise AppArmorException("Could not find policygroups directory") self.aa_topdir = "/etc/apparmor.d" @@ -445,11 +449,12 @@ class AppArmorEasyProfile: def print_basefilenames(files): for i in files: - print "%s" % (os.path.basename(i)) + sys.stdout.write("%s\n" % (os.path.basename(i))) def print_files(files): for i in files: - print open(i).read() + with open(i) as f: + sys.stdout.write(f.read()+"\n") def parse_args(args=None): '''Parse arguments''' diff --git a/utils/test/test-aa-easyprof.py b/utils/test/test-aa-easyprof.py index 9ced781ef..e59c13860 100755 --- a/utils/test/test-aa-easyprof.py +++ b/utils/test/test-aa-easyprof.py @@ -101,6 +101,7 @@ TEMPLATES_DIR="%s/templates" def tearDown(self): '''Teardown for tests''' if os.path.exists(self.tmpdir): + sys.stdout.write("%s\n" % self.tmpdir) recursive_rm(self.tmpdir) # @@ -328,7 +329,7 @@ POLICYGROUPS_DIR="%s/templates" def test_binary_symlink(self): '''Test binary (symlink)''' exe = os.path.join(self.tmpdir, 'exe') - open(exe, 'wa').close() + open(exe, 'a').close() symlink = exe + ".lnk" os.symlink(exe, symlink) @@ -441,7 +442,7 @@ POLICYGROUPS_DIR="%s/templates" self.assertFalse(inv_s in p, "Found '%s' in :\n%s" % (inv_s, p)) if debugging: - print p + sys.stdout.write("%s\n" % p) return p @@ -859,7 +860,7 @@ if __name__ == '__main__': # Create the necessary files to import aa-easyprof init = os.path.join(os.path.dirname(absfn), '__init__.py') if not os.path.exists(init): - open(init, 'wa').close() + open(init, 'a').close() created.append(init) symlink = os.path.join(os.path.dirname(absfn), 'easyprof.py') diff --git a/utils/vim/Makefile b/utils/vim/Makefile index 2cb0bb5df..2b64037ce 100644 --- a/utils/vim/Makefile +++ b/utils/vim/Makefile @@ -14,12 +14,15 @@ VIM_INSTALL_PATH=${DESTDIR}/usr/share/apparmor all: apparmor.vim apparmor.vim: apparmor.vim.in Makefile create-apparmor.vim.py - python create-apparmor.vim.py > $@ + ${PYTHON} create-apparmor.vim.py > apparmor.vim install: apparmor.vim install -d $(VIM_INSTALL_PATH) install -m 644 $< $(VIM_INSTALL_PATH) +test: apparmor.vim.in Makefile create-apparmor.vim.py + #Testing with all pythons + $(call pyalldo, create-apparmor.vim.py > /dev/null) clean: rm -f apparmor.vim common diff --git a/utils/vim/create-apparmor.vim.py b/utils/vim/create-apparmor.vim.py index dbfed0c67..3734c6adc 100644 --- a/utils/vim/create-apparmor.vim.py +++ b/utils/vim/create-apparmor.vim.py @@ -10,7 +10,6 @@ # Christian Boltz from __future__ import with_statement -import os import re import subprocess import sys @@ -30,9 +29,9 @@ def cmd(command, input = None, stderr = subprocess.STDOUT, stdout = subprocess.P return a textual error if it failed.''' try: - sp = subprocess.Popen(command, stdin=stdin, stdout=stdout, stderr=stderr, close_fds=True) - except OSError, e: - return [127, str(e)] + sp = subprocess.Popen(command, stdin=stdin, stdout=stdout, stderr=stderr, close_fds=True, universal_newlines=True) + except OSError as ex: + return [127, str(ex)] out, outerr = sp.communicate(input) @@ -47,7 +46,7 @@ def cmd(command, input = None, stderr = subprocess.STDOUT, stdout = subprocess.P # get capabilities list (rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_capabilities']) if rc != 0: - print >>sys.stderr, ("make list_capabilities failed: " + output) + sys.stderr.write("make list_capabilities failed: " + output) exit(rc) capabilities = re.sub('CAP_', '', output.strip()).lower().split(" ") @@ -59,7 +58,7 @@ for cap in capabilities: # get network protos list (rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_af_names']) if rc != 0: - print >>sys.stderr, ("make list_af_names failed: " + output) + sys.stderr.write("make list_af_names failed: " + output) exit(rc) af_names = [] @@ -105,7 +104,7 @@ aa_regex_map = { } def my_repl(matchobj): - #print matchobj.group(1) + matchobj.group(1) if matchobj.group(1) in aa_regex_map: return aa_regex_map[matchobj.group(1)] @@ -113,7 +112,7 @@ def my_repl(matchobj): regex = "@@(" + "|".join(aa_regex_map) + ")@@" -with file("apparmor.vim.in") as template: +with open("apparmor.vim.in") as template: for line in template: line = re.sub(regex, my_repl, line.rstrip()) - print line + sys.stdout.write('%s\n' % line)