Dmitrijs Ledkovs has proposed merging lp:~dmitrij.ledkov/apparmor/py3 into 
lp:apparmor.

Requested reviews:
  Canonical Foundations Team (canonical-foundations)
  AppArmor Developers (apparmor-dev)

For more details, see:
https://code.launchpad.net/~dmitrij.ledkov/apparmor/py3/+merge/109682

Initial port to python3 for utilities.

Python2 compatibility is not broken, all code should be 'bilingual'.

Adds helpers in the Make.rules to detect python2 and/or python3.

Runs test and check targets with both pythons, if available.

Python3 is entirely optional with these changes, but the test/check targets 
will fail if python3 incompatible code is introduced and python3 is available 
during build. If you do not want the build to fail export 
PYTHON_VERSIONS=python2 before running check/test targets.

Currently the install defaults to python2, with fallback to python3

This does not have anything to do with swig python binding.
-- 
https://code.launchpad.net/~dmitrij.ledkov/apparmor/py3/+merge/109682
Your team AppArmor Developers is requested to review the proposed merge of 
lp:~dmitrij.ledkov/apparmor/py3 into lp:apparmor.
=== modified file 'common/Make.rules'
--- common/Make.rules	2012-04-11 16:16:47 +0000
+++ common/Make.rules	2012-06-11 17:03:23 +0000
@@ -27,6 +27,10 @@
 DISTRIBUTION=AppArmor
 VERSION=$(shell cat common/Version)
 
+# 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
@@ -127,6 +131,17 @@
 
 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:

=== modified file 'utils/Makefile'
--- utils/Makefile	2012-05-08 05:37:48 +0000
+++ utils/Makefile	2012-06-11 17:03:23 +0000
@@ -65,7 +65,7 @@
 	$(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 @@
 		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)))

=== modified file 'utils/apparmor/easyprof.py'
--- utils/apparmor/easyprof.py	2012-05-08 05:37:48 +0000
+++ utils/apparmor/easyprof.py	2012-06-11 17:03:23 +0000
@@ -70,7 +70,8 @@
     try:
         sp = subprocess.Popen(command, stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
-    except OSError, ex:
+    except OSError:
+        ex = sys.exc_info()[1]
         return [127, str(ex)]
 
     out = sp.communicate()[0]
@@ -82,7 +83,8 @@
     try:
         sp1 = subprocess.Popen(command1, stdout=subprocess.PIPE)
         sp2 = subprocess.Popen(command2, stdin=sp1.stdout)
-    except OSError, ex:
+    except OSError:
+        ex = sys.exc_info()[1]
         return [127, str(ex)]
 
     out = sp2.communicate()[0]
@@ -181,7 +183,8 @@
         fn = policy
     else:
         f, fn = tempfile.mkstemp(prefix='aa-easyprof')
-        os.write(f, policy)
+        policy_bytes = bytes(policy, 'utf-8') if sys.version > '3' else policy
+        os.write(f, policy_bytes)
         os.close(f)
 
     rc, out = cmd([exe, '-p', fn])
@@ -219,9 +222,9 @@
         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 +448,11 @@
 
 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()
+        sys.stdout.write(open(i).read()+"\n")
 
 def parse_args(args=None):
     '''Parse arguments'''

=== modified file 'utils/test/test-aa-easyprof.py'
--- utils/test/test-aa-easyprof.py	2012-05-09 18:05:07 +0000
+++ utils/test/test-aa-easyprof.py	2012-06-11 17:03:23 +0000
@@ -101,6 +101,7 @@
     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 @@
     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 @@
         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 @@
     # 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')

=== modified file 'utils/vim/Makefile'
--- utils/vim/Makefile	2012-03-23 16:02:20 +0000
+++ utils/vim/Makefile	2012-06-11 17:03:23 +0000
@@ -14,12 +14,15 @@
 all: apparmor.vim
 
 apparmor.vim: apparmor.vim.in Makefile create-apparmor.vim.py
-	python create-apparmor.vim.py > $@
+	${PYTHON} create-apparmor.vim.py > /dev/null
 
 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

=== modified file 'utils/vim/create-apparmor.vim.py'
--- utils/vim/create-apparmor.vim.py	2012-06-08 21:30:22 +0000
+++ utils/vim/create-apparmor.vim.py	2012-06-11 17:03:23 +0000
@@ -30,8 +30,9 @@
     return a textual error if it failed.'''
 
     try:
-        sp = subprocess.Popen(command, stdin=stdin, stdout=stdout, stderr=stderr, close_fds=True)
-    except OSError, e:
+        sp = subprocess.Popen(command, stdin=stdin, stdout=stdout, stderr=stderr, close_fds=True, universal_newlines=True)
+    except OSError:
+        e = sys.exc_info()[1]
         return [127, str(e)]
 
     out, outerr = sp.communicate(input)
@@ -47,7 +48,7 @@
 # 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 +60,7 @@
 # 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 = []
@@ -164,16 +165,16 @@
 
 regex = "@@(" + "|".join(aa_regex_map) + ")@@"
 
-print '" generated from apparmor.vim.in by create-apparmor.vim.py'
-print '" do not edit this file - edit apparmor.vim.in or create-apparmor.vim.py instead' + "\n"
+sys.stdout.write('" generated from apparmor.vim.in by create-apparmor.vim.py\n')
+sys.stdout.write('" do not edit this file - edit apparmor.vim.in or create-apparmor.vim.py instead' + "\n")
 
-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
-
-print "\n\n\n"
-
-print '" file rules added with create_file_rule()'
-print re.sub(regex, my_repl, filerule)
+        sys.stdout.write(line)
+
+sys.stdout.write("\n\n\n\n")
+
+sys.stdout.write('" file rules added with create_file_rule()\n')
+sys.stdout.write(re.sub(regex, my_repl, filerule)+'\n')
 

-- 
AppArmor mailing list
[email protected]
Modify settings or unsubscribe at: 
https://lists.ubuntu.com/mailman/listinfo/apparmor

Reply via email to