Merge lp:~xnox/apparmor/py3 into lp:apparmor/2.12

Proposed by Dimitri John Ledkov
Status: Merged
Merged at revision: 2052
Proposed branch: lp:~xnox/apparmor/py3
Merge into: lp:apparmor/2.12
Diff against target: 420 lines (+79/-60)
8 files modified
common/Make.rules (+15/-0)
libraries/libapparmor/m4/ac_python_devel.m4 (+22/-24)
utils/Makefile (+2/-4)
utils/aa-easyprof (+2/-2)
utils/apparmor/easyprof.py (+15/-10)
utils/test/test-aa-easyprof.py (+4/-3)
utils/vim/Makefile (+4/-1)
utils/vim/create-apparmor.vim.py (+15/-16)
To merge this branch: bzr merge lp:~xnox/apparmor/py3
Reviewer Review Type Date Requested Status
Jamie Strandboge Approve
Adam Conrad (community) Approve
Canonical Foundations Team Pending
AppArmor Developers Pending
Review via email: mp+109682@code.launchpad.net

Description of the change

This is a complete port to make apparmor Python2/Python3 bilingual.
There are 3 variables in use:
PYTHON, used by common/Make.rule and libapparmor configure script pointing to a python interpreter
PYTHON_VERSION, used by libapparmor configure script with just version number e.g. '3.2' or '2.7'
PYTHON_VERSIONS, list of all pythons to run checks/tests with for python utilities e.g. 'python2 python3'

If none are specified, defaults will be autodetected to:
PYTHON=/usr/bin/python2 in Make.rule
PYTHON=/usr/bin/python2.7 in configure
PYTHON_VERSION=2.7
PYTHON_VERSIONS='python2 python3'

To build libapparmor (including the python swig bindings) with python3 it seems like you must set these two variables:
PYTHON=/usr/bin/python3.2
PYTHON_VERSIONS=3.2

All test and check targets pass and build succeeds.
Scripts are functional.
Swig python module can import and doesn't segfault in smoke testing.

TODO:
/usr/bin/aa-easyprof gets installed without updated shebang to point at python3
ac_python_devel patch should probably be submitted to autoconf-archive (updating it's ax_python_devel), not sure about the install location as historically a*_python_devel installs into site-packages, instead of dist-packages.

To post a comment you must log in.
Revision history for this message
Barry Warsaw (barry) wrote :

On Jun 11, 2012, at 05:04 PM, Dmitrijs Ledkovs wrote:

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

=== 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)]

I think in general, a better way of doing this would be to change the except
line to

    except OSError as ex:

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

I generally don't like version checks unless there's no other way. In this
case, you could do something like this instead:

    if not isinstance(policy, bytes):
        policy = policy.encode('utf-8')

=== 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)
>
> #

Why did you decide against using print(). I'm sure there's a good reason, but
it might be useful in at least some of these cases to explain in comments why
sys.stdout.write() is being used instead of print(). Otherwise, when the next
person comes along they probably won't understand why this idiom was chosen.

lp:~xnox/apparmor/py3 updated
2053. By Dimitri John Ledkov

newline parity with print statement vs sys.stdout.write

Revision history for this message
Adam Conrad (adconrad) wrote :

Looks good to me, but I can't commit to the parent branch, so waiting on someone from AppArmor Devs to pick it up and run with it.

review: Approve
Revision history for this message
Evan (ev) wrote :

> - print open(i).read()
> + sys.stdout.write(open(i).read()+"\n")

This will leak fds, which python wonderfully loudly complains about in Python 3. It's also a good opportunity to replace any pairs of open() and close() with a with statement. If an open() and close() isn't wrapped in a with or try/finally, I would argue that is a bug.

Revision history for this message
Dimitri John Ledkov (xnox) wrote :
Download full text (3.2 KiB)

On 11/06/12 21:34, Barry Warsaw wrote:
> On Jun 11, 2012, at 05:04 PM, Dmitrijs Ledkovs wrote:
>
>> 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.
>
> === 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)]
>
> I think in general, a better way of doing this would be to change the except
> line to
>
> except OSError as ex:
>

Ok, this does look better. This way was not documented on the Python/3
wiki page, nor in the porting to python3 book.

Noted.

>> @@ -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)
>
> I generally don't like version checks unless there's no other way. In this
> case, you could do something like this instead:
>
> if not isinstance(policy, bytes):
> policy = policy.encode('utf-8')
>

Makes sense. Cjwatson was arguing for version checks, as they are easy
to grep for when eventually going python3 only.

> === 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)
>>
>> #
>
> Why did you decide against using print(). I'm sure there's a good reason, but
> it might be useful in at least some of these cases to explain in comments why
> sys.stdout.write() is being used instead of print(). Otherwise, when the next
> person comes along they probably won't understand why this idiom was chosen.
>

You should ask this question to apparmor's upstre...

Read more...

Revision history for this message
Dimitri John Ledkov (xnox) wrote :

Pushed revision 2054:
* closes file descriptors
* uses except OSError as ex
* doesn't do version checks
* more stray print statements converted to sys.stdXXX.write() syntax

lp:~xnox/apparmor/py3 updated
2054. By Dimitri John Ledkov

* Use with open('file') as f, to prevent leaking file descriptors
* More print -> sys.stdXXX.write() conversions
* Use `except Error as ex` & no sys.version checks
* add with_statement import for py2.5 compat
* remove unused import

2055. By Dimitri John Ledkov

python2/3 compatible ac_python_devel.m4

2056. By Dimitri John Ledkov

Remaining typos

2057. By Dimitri John Ledkov

typo

Revision history for this message
Jamie Strandboge (jdstrand) wrote :

Thanks for your work on this, it looks great! :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'common/Make.rules'
--- common/Make.rules 2012-04-11 16:16:47 +0000
+++ common/Make.rules 2012-06-12 13:52:18 +0000
@@ -27,6 +27,10 @@
27DISTRIBUTION=AppArmor27DISTRIBUTION=AppArmor
28VERSION=$(shell cat common/Version)28VERSION=$(shell cat common/Version)
2929
30# Convenience functions
31pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
32map = $(foreach a,$(2),$(call $(1),$(a)))
33
30# OVERRIDABLE variables34# OVERRIDABLE variables
31# Set these variables before including Make.rules to change its behavior35# Set these variables before including Make.rules to change its behavior
32# SPECFILE - for packages that have a non-standard specfile name36# SPECFILE - for packages that have a non-standard specfile name
@@ -127,6 +131,17 @@
127131
128endif132endif
129133
134ifndef PYTHON_VERSIONS
135PYTHON_VERSIONS = $(call map, pathsearch, python2 python3)
136endif
137
138ifndef PYTHON
139PYTHON = $(firstword ${PYTHON_VERSIONS})
140endif
141
142#Helper function to be used with $(call pyalldo, run_test_with_all.py)
143pyalldo=set -e; $(foreach py, $(PYTHON_VERSIONS), $(py) $(1);)
144
130.PHONY: version145.PHONY: version
131.SILENT: version146.SILENT: version
132version:147version:
133148
=== modified file 'libraries/libapparmor/m4/ac_python_devel.m4'
--- libraries/libapparmor/m4/ac_python_devel.m4 2012-04-25 19:15:19 +0000
+++ libraries/libapparmor/m4/ac_python_devel.m4 2012-06-12 13:52:18 +0000
@@ -17,9 +17,9 @@
17 # Check for a version of Python >= 2.1.017 # Check for a version of Python >= 2.1.0
18 #18 #
19 AC_MSG_CHECKING([for a version of Python >= '2.1.0'])19 AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
20 ac_supports_python_ver=`$PYTHON -c "import sys, string; \20 ac_supports_python_ver=`$PYTHON -c "import sys; \
21 ver = string.split(sys.version)[[0]]; \21 ver = sys.version.split()[[0]]; \
22 print ver >= '2.1.0'"`22 sys.stdout.write(str(ver >= '2.1.0'))"`
23 if test "$ac_supports_python_ver" != "True"; then23 if test "$ac_supports_python_ver" != "True"; then
24 if test -z "$PYTHON_NOVERSIONCHECK"; then24 if test -z "$PYTHON_NOVERSIONCHECK"; then
25 AC_MSG_RESULT([no])25 AC_MSG_RESULT([no])
@@ -44,9 +44,9 @@
44 #44 #
45 if test -n "$1"; then45 if test -n "$1"; then
46 AC_MSG_CHECKING([for a version of Python $1])46 AC_MSG_CHECKING([for a version of Python $1])
47 ac_supports_python_ver=`$PYTHON -c "import sys, string; \47 ac_supports_python_ver=`$PYTHON -c "import sys; \
48 ver = string.split(sys.version)[[0]]; \48 ver = sys.version.split()[[0]]; \
49 print ver $1"`49 sys.stdout.write("%s\n" % (ver == $1))"`
50 if test "$ac_supports_python_ver" = "True"; then50 if test "$ac_supports_python_ver" = "True"; then
51 AC_MSG_RESULT([yes])51 AC_MSG_RESULT([yes])
52 else52 else
@@ -80,8 +80,8 @@
80 #80 #
81 AC_MSG_CHECKING([for Python include path])81 AC_MSG_CHECKING([for Python include path])
82 if test -z "$PYTHON_CPPFLAGS"; then82 if test -z "$PYTHON_CPPFLAGS"; then
83 python_path=`$PYTHON -c "import distutils.sysconfig; \83 python_path=`$PYTHON -c "import sys; import distutils.sysconfig;\
84 print distutils.sysconfig.get_python_inc();"`84sys.stdout.write('%s\n' % distutils.sysconfig.get_python_inc());"`
85 if test -n "${python_path}"; then85 if test -n "${python_path}"; then
86 python_path="-I$python_path"86 python_path="-I$python_path"
87 fi87 fi
@@ -97,22 +97,20 @@
97 if test -z "$PYTHON_LDFLAGS"; then97 if test -z "$PYTHON_LDFLAGS"; then
98 # (makes two attempts to ensure we've got a version number98 # (makes two attempts to ensure we've got a version number
99 # from the interpreter)99 # from the interpreter)
100 py_version=`$PYTHON -c "from distutils.sysconfig import *; \100 py_version=`$PYTHON -c "import sys; from distutils.sysconfig import *; \
101 from string import join; \101sys.stdout.write('%s\n' % ''.join(get_config_vars('VERSION')))"`
102 print join(get_config_vars('VERSION'))"`
103 if test "$py_version" == "[None]"; then102 if test "$py_version" == "[None]"; then
104 if test -n "$PYTHON_VERSION"; then103 if test -n "$PYTHON_VERSION"; then
105 py_version=$PYTHON_VERSION104 py_version=$PYTHON_VERSION
106 else105 else
107 py_version=`$PYTHON -c "import sys; \106 py_version=`$PYTHON -c "import sys; \
108 print sys.version[[:3]]"`107sys.stdout.write("%s\n" % sys.version[[:3]])"`
109 fi108 fi
110 fi109 fi
111110
112 PYTHON_LDFLAGS=`$PYTHON -c "from distutils.sysconfig import *; \111 PYTHON_LDFLAGS=`$PYTHON -c "import sys; from distutils.sysconfig import *; \
113 from string import join; \112sys.stdout.write('-L' + get_python_lib(0,1) + ' -lpython\n')"`$py_version`$PYTHON -c \
114 print '-L' + get_python_lib(0,1), \113"import sys; sys.stdout.write('%s' % getattr(sys,'abiflags',''))"`
115 '-lpython';"`$py_version
116 fi114 fi
117 AC_MSG_RESULT([$PYTHON_LDFLAGS])115 AC_MSG_RESULT([$PYTHON_LDFLAGS])
118 AC_SUBST([PYTHON_LDFLAGS])116 AC_SUBST([PYTHON_LDFLAGS])
@@ -122,8 +120,8 @@
122 #120 #
123 AC_MSG_CHECKING([for Python site-packages path])121 AC_MSG_CHECKING([for Python site-packages path])
124 if test -z "$PYTHON_SITE_PKG"; then122 if test -z "$PYTHON_SITE_PKG"; then
125 PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \123 PYTHON_SITE_PKG=`$PYTHON -c "import sys; import distutils.sysconfig; \
126 print distutils.sysconfig.get_python_lib(0,0);"`124sys.stdout.write('%s\n' % distutils.sysconfig.get_python_lib(0,0));"`
127 fi125 fi
128 AC_MSG_RESULT([$PYTHON_SITE_PKG])126 AC_MSG_RESULT([$PYTHON_SITE_PKG])
129 AC_SUBST([PYTHON_SITE_PKG])127 AC_SUBST([PYTHON_SITE_PKG])
@@ -133,9 +131,9 @@
133 #131 #
134 AC_MSG_CHECKING(python extra libraries)132 AC_MSG_CHECKING(python extra libraries)
135 if test -z "$PYTHON_EXTRA_LIBS"; then133 if test -z "$PYTHON_EXTRA_LIBS"; then
136 PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \134 PYTHON_EXTRA_LIBS=`$PYTHON -c "import sys; import distutils.sysconfig; \
137 conf = distutils.sysconfig.get_config_var; \135conf = distutils.sysconfig.get_config_var; \
138 print conf('LOCALMODLIBS'), conf('LIBS')"`136sys.stdout.write('%s %s\n' % (conf('LOCALMODLIBS'), conf('LIBS')))"`
139 fi137 fi
140 AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])138 AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
141 AC_SUBST(PYTHON_EXTRA_LIBS)139 AC_SUBST(PYTHON_EXTRA_LIBS)
@@ -145,9 +143,9 @@
145 #143 #
146 AC_MSG_CHECKING(python extra linking flags)144 AC_MSG_CHECKING(python extra linking flags)
147 if test -z "$PYTHON_EXTRA_LDFLAGS"; then145 if test -z "$PYTHON_EXTRA_LDFLAGS"; then
148 PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \146 PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import sys; import distutils.sysconfig; \
149 conf = distutils.sysconfig.get_config_var; \147conf = distutils.sysconfig.get_config_var; \
150 print conf('LINKFORSHARED')"`148sys.stdout.write('%s\n' % conf('LINKFORSHARED'))"`
151 fi149 fi
152 AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])150 AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
153 AC_SUBST(PYTHON_EXTRA_LDFLAGS)151 AC_SUBST(PYTHON_EXTRA_LDFLAGS)
154152
=== modified file 'utils/Makefile'
--- utils/Makefile 2012-05-08 05:37:48 +0000
+++ utils/Makefile 2012-06-12 13:52:18 +0000
@@ -65,7 +65,7 @@
65 $(MAKE) install_manpages DESTDIR=${DESTDIR}65 $(MAKE) install_manpages DESTDIR=${DESTDIR}
66 $(MAKE) -C vim install DESTDIR=${DESTDIR}66 $(MAKE) -C vim install DESTDIR=${DESTDIR}
67 ln -sf aa-status.8 ${DESTDIR}/${MANDIR}/man8/apparmor_status.867 ln -sf aa-status.8 ${DESTDIR}/${MANDIR}/man8/apparmor_status.8
68 python ${PYSETUP} install --prefix=${PYPREFIX} --root=${DESTDIR} --version=${VERSION}68 ${PYTHON} ${PYSETUP} install --prefix=${PYPREFIX} --root=${DESTDIR} --version=${VERSION}
6969
70.PHONY: clean70.PHONY: clean
71ifndef VERBOSE71ifndef VERBOSE
@@ -105,6 +105,4 @@
105 test -s $$tmpfile && cat $$tmpfile && rm -f $$tmpfile && exit 1; \105 test -s $$tmpfile && cat $$tmpfile && rm -f $$tmpfile && exit 1; \
106 done || true; \106 done || true; \
107 rm -f $$tmpfile107 rm -f $$tmpfile
108 for i in test/* ; do \108 $(foreach test, $(wildcard test/test-*.py), $(call pyalldo, $(test)))
109 python $$i || exit 1; \
110 done
111109
=== modified file 'utils/aa-easyprof'
--- utils/aa-easyprof 2012-05-09 18:05:07 +0000
+++ utils/aa-easyprof 2012-06-12 13:52:18 +0000
@@ -35,7 +35,7 @@
3535
36 try:36 try:
37 easyp = apparmor.easyprof.AppArmorEasyProfile(binary, opt)37 easyp = apparmor.easyprof.AppArmorEasyProfile(binary, opt)
38 except AppArmorException, e:38 except AppArmorException as e:
39 error(e.value)39 error(e.value)
40 except Exception:40 except Exception:
41 raise41 raise
@@ -61,5 +61,5 @@
61 # if we made it here, generate a profile61 # if we made it here, generate a profile
62 params = apparmor.easyprof.gen_policy_params(binary, opt)62 params = apparmor.easyprof.gen_policy_params(binary, opt)
63 p = easyp.gen_policy(**params)63 p = easyp.gen_policy(**params)
64 print p,64 sys.stdout.write('%s\n' % p)
6565
6666
=== modified file 'utils/apparmor/easyprof.py'
--- utils/apparmor/easyprof.py 2012-05-08 05:37:48 +0000
+++ utils/apparmor/easyprof.py 2012-06-12 13:52:18 +0000
@@ -8,6 +8,8 @@
8#8#
9# ------------------------------------------------------------------9# ------------------------------------------------------------------
1010
11from __future__ import with_statement
12
11import codecs13import codecs
12import glob14import glob
13import optparse15import optparse
@@ -40,7 +42,7 @@
40def error(out, exit_code=1, do_exit=True):42def error(out, exit_code=1, do_exit=True):
41 '''Print error message and exit'''43 '''Print error message and exit'''
42 try:44 try:
43 print >> sys.stderr, "ERROR: %s" % (out)45 sys.stderr.write("ERROR: %s\n" % (out))
44 except IOError:46 except IOError:
45 pass47 pass
4648
@@ -51,7 +53,7 @@
51def warn(out):53def warn(out):
52 '''Print warning message'''54 '''Print warning message'''
53 try:55 try:
54 print >> sys.stderr, "WARN: %s" % (out)56 sys.stderr.write("WARN: %s\n" % (out))
55 except IOError:57 except IOError:
56 pass58 pass
5759
@@ -59,7 +61,7 @@
59def msg(out, output=sys.stdout):61def msg(out, output=sys.stdout):
60 '''Print message'''62 '''Print message'''
61 try:63 try:
62 print >> output, "%s" % (out)64 sys.stdout.write("%s\n" % (out))
63 except IOError:65 except IOError:
64 pass66 pass
6567
@@ -70,7 +72,7 @@
70 try:72 try:
71 sp = subprocess.Popen(command, stdout=subprocess.PIPE,73 sp = subprocess.Popen(command, stdout=subprocess.PIPE,
72 stderr=subprocess.STDOUT)74 stderr=subprocess.STDOUT)
73 except OSError, ex:75 except OSError as ex:
74 return [127, str(ex)]76 return [127, str(ex)]
7577
76 out = sp.communicate()[0]78 out = sp.communicate()[0]
@@ -82,7 +84,7 @@
82 try:84 try:
83 sp1 = subprocess.Popen(command1, stdout=subprocess.PIPE)85 sp1 = subprocess.Popen(command1, stdout=subprocess.PIPE)
84 sp2 = subprocess.Popen(command2, stdin=sp1.stdout)86 sp2 = subprocess.Popen(command2, stdin=sp1.stdout)
85 except OSError, ex:87 except OSError as ex:
86 return [127, str(ex)]88 return [127, str(ex)]
8789
88 out = sp2.communicate()[0]90 out = sp2.communicate()[0]
@@ -93,7 +95,7 @@
93 '''Print debug message'''95 '''Print debug message'''
94 if DEBUGGING:96 if DEBUGGING:
95 try:97 try:
96 print >> sys.stderr, "DEBUG: %s" % (out)98 sys.stderr.write("DEBUG: %s\n" % (out))
97 except IOError:99 except IOError:
98 pass100 pass
99101
@@ -181,6 +183,8 @@
181 fn = policy183 fn = policy
182 else:184 else:
183 f, fn = tempfile.mkstemp(prefix='aa-easyprof')185 f, fn = tempfile.mkstemp(prefix='aa-easyprof')
186 if not isinstance(policy, bytes):
187 policy = policy.encode('utf-8')
184 os.write(f, policy)188 os.write(f, policy)
185 os.close(f)189 os.close(f)
186190
@@ -219,9 +223,9 @@
219 if opt.policy_groups_dir and os.path.isdir(opt.policy_groups_dir):223 if opt.policy_groups_dir and os.path.isdir(opt.policy_groups_dir):
220 self.dirs['policygroups'] = os.path.abspath(opt.policy_groups_dir)224 self.dirs['policygroups'] = os.path.abspath(opt.policy_groups_dir)
221225
222 if not self.dirs.has_key('templates'):226 if not 'templates' in self.dirs:
223 raise AppArmorException("Could not find templates directory")227 raise AppArmorException("Could not find templates directory")
224 if not self.dirs.has_key('policygroups'):228 if not 'policygroups' in self.dirs:
225 raise AppArmorException("Could not find policygroups directory")229 raise AppArmorException("Could not find policygroups directory")
226230
227 self.aa_topdir = "/etc/apparmor.d"231 self.aa_topdir = "/etc/apparmor.d"
@@ -445,11 +449,12 @@
445449
446def print_basefilenames(files):450def print_basefilenames(files):
447 for i in files:451 for i in files:
448 print "%s" % (os.path.basename(i))452 sys.stdout.write("%s\n" % (os.path.basename(i)))
449453
450def print_files(files):454def print_files(files):
451 for i in files:455 for i in files:
452 print open(i).read()456 with open(i) as f:
457 sys.stdout.write(f.read()+"\n")
453458
454def parse_args(args=None):459def parse_args(args=None):
455 '''Parse arguments'''460 '''Parse arguments'''
456461
=== 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-12 13:52:18 +0000
@@ -101,6 +101,7 @@
101 def tearDown(self):101 def tearDown(self):
102 '''Teardown for tests'''102 '''Teardown for tests'''
103 if os.path.exists(self.tmpdir):103 if os.path.exists(self.tmpdir):
104 sys.stdout.write("%s\n" % self.tmpdir)
104 recursive_rm(self.tmpdir)105 recursive_rm(self.tmpdir)
105106
106#107#
@@ -328,7 +329,7 @@
328 def test_binary_symlink(self):329 def test_binary_symlink(self):
329 '''Test binary (symlink)'''330 '''Test binary (symlink)'''
330 exe = os.path.join(self.tmpdir, 'exe')331 exe = os.path.join(self.tmpdir, 'exe')
331 open(exe, 'wa').close()332 open(exe, 'a').close()
332 symlink = exe + ".lnk"333 symlink = exe + ".lnk"
333 os.symlink(exe, symlink)334 os.symlink(exe, symlink)
334335
@@ -441,7 +442,7 @@
441 self.assertFalse(inv_s in p, "Found '%s' in :\n%s" % (inv_s, p))442 self.assertFalse(inv_s in p, "Found '%s' in :\n%s" % (inv_s, p))
442443
443 if debugging:444 if debugging:
444 print p445 sys.stdout.write("%s\n" % p)
445446
446 return p447 return p
447448
@@ -859,7 +860,7 @@
859 # Create the necessary files to import aa-easyprof860 # Create the necessary files to import aa-easyprof
860 init = os.path.join(os.path.dirname(absfn), '__init__.py')861 init = os.path.join(os.path.dirname(absfn), '__init__.py')
861 if not os.path.exists(init):862 if not os.path.exists(init):
862 open(init, 'wa').close()863 open(init, 'a').close()
863 created.append(init)864 created.append(init)
864865
865 symlink = os.path.join(os.path.dirname(absfn), 'easyprof.py')866 symlink = os.path.join(os.path.dirname(absfn), 'easyprof.py')
866867
=== modified file 'utils/vim/Makefile'
--- utils/vim/Makefile 2012-03-23 16:02:20 +0000
+++ utils/vim/Makefile 2012-06-12 13:52:18 +0000
@@ -14,12 +14,15 @@
14all: apparmor.vim14all: apparmor.vim
1515
16apparmor.vim: apparmor.vim.in Makefile create-apparmor.vim.py16apparmor.vim: apparmor.vim.in Makefile create-apparmor.vim.py
17 python create-apparmor.vim.py > $@17 ${PYTHON} create-apparmor.vim.py > apparmor.vim
1818
19install: apparmor.vim19install: apparmor.vim
20 install -d $(VIM_INSTALL_PATH)20 install -d $(VIM_INSTALL_PATH)
21 install -m 644 $< $(VIM_INSTALL_PATH)21 install -m 644 $< $(VIM_INSTALL_PATH)
2222
23test: apparmor.vim.in Makefile create-apparmor.vim.py
24 #Testing with all pythons
25 $(call pyalldo, create-apparmor.vim.py > /dev/null)
2326
24clean:27clean:
25 rm -f apparmor.vim common28 rm -f apparmor.vim common
2629
=== 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-12 13:52:18 +0000
@@ -10,7 +10,6 @@
10# Christian Boltz <apparmor@cboltz.de>10# Christian Boltz <apparmor@cboltz.de>
1111
12from __future__ import with_statement12from __future__ import with_statement
13import os
14import re13import re
15import subprocess14import subprocess
16import sys15import sys
@@ -30,9 +29,9 @@
30 return a textual error if it failed.'''29 return a textual error if it failed.'''
3130
32 try:31 try:
33 sp = subprocess.Popen(command, stdin=stdin, stdout=stdout, stderr=stderr, close_fds=True)32 sp = subprocess.Popen(command, stdin=stdin, stdout=stdout, stderr=stderr, close_fds=True, universal_newlines=True)
34 except OSError, e:33 except OSError as ex:
35 return [127, str(e)]34 return [127, str(ex)]
3635
37 out, outerr = sp.communicate(input)36 out, outerr = sp.communicate(input)
3837
@@ -47,7 +46,7 @@
47# get capabilities list46# get capabilities list
48(rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_capabilities'])47(rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_capabilities'])
49if rc != 0:48if rc != 0:
50 print >>sys.stderr, ("make list_capabilities failed: " + output)49 sys.stderr.write("make list_capabilities failed: " + output)
51 exit(rc)50 exit(rc)
5251
53capabilities = re.sub('CAP_', '', output.strip()).lower().split(" ")52capabilities = re.sub('CAP_', '', output.strip()).lower().split(" ")
@@ -59,7 +58,7 @@
59# get network protos list58# get network protos list
60(rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_af_names'])59(rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_af_names'])
61if rc != 0:60if rc != 0:
62 print >>sys.stderr, ("make list_af_names failed: " + output)61 sys.stderr.write("make list_af_names failed: " + output)
63 exit(rc)62 exit(rc)
6463
65af_names = []64af_names = []
@@ -106,7 +105,7 @@
106}105}
107106
108def my_repl(matchobj):107def my_repl(matchobj):
109 #print matchobj.group(1)108 matchobj.group(1)
110 if matchobj.group(1) in aa_regex_map:109 if matchobj.group(1) in aa_regex_map:
111 return aa_regex_map[matchobj.group(1)]110 return aa_regex_map[matchobj.group(1)]
112111
@@ -164,16 +163,16 @@
164163
165regex = "@@(" + "|".join(aa_regex_map) + ")@@"164regex = "@@(" + "|".join(aa_regex_map) + ")@@"
166165
167print '" generated from apparmor.vim.in by create-apparmor.vim.py'166sys.stdout.write('" generated from apparmor.vim.in by create-apparmor.vim.py\n')
168print '" do not edit this file - edit apparmor.vim.in or create-apparmor.vim.py instead' + "\n"167sys.stdout.write('" do not edit this file - edit apparmor.vim.in or create-apparmor.vim.py instead' + "\n\n")
169168
170with file("apparmor.vim.in") as template:169with open("apparmor.vim.in") as template:
171 for line in template:170 for line in template:
172 line = re.sub(regex, my_repl, line.rstrip())171 line = re.sub(regex, my_repl, line.rstrip())
173 print line172 sys.stdout.write('%s\n' % line)
174173
175print "\n\n\n"174sys.stdout.write("\n\n\n\n")
176175
177print '" file rules added with create_file_rule()'176sys.stdout.write('" file rules added with create_file_rule()\n')
178print re.sub(regex, my_repl, filerule)177sys.stdout.write(re.sub(regex, my_repl, filerule)+'\n')
179178

Subscribers

People subscribed via source and target branches