Merge lp:~xnox/apparmor/py3 into lp:apparmor/2.12
- py3
- Merge into master
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 | ||||
Related bugs: |
|
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 |
Commit message
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=
PYTHON=
PYTHON_VERSION=2.7
PYTHON_
To build libapparmor (including the python swig bindings) with python3 it seems like you must set these two variables:
PYTHON=
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/
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.
Barry Warsaw (barry) wrote : | # |
- 2053. By Dimitri John Ledkov
-
newline parity with print statement vs sys.stdout.write
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.
> - print open(i).read()
> + sys.stdout.
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.
Dimitri John Ledkov (xnox) wrote : | # |
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-
>> AppArmor Developers (apparmor-dev)
>>
>> For more details, see:
>> https:/
>>
>> 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_
>>
>> Currently the install defaults to python2, with fallback to python3
>>
>> This does not have anything to do with swig python binding.
>
> === modified file 'utils/
> --- utils/apparmor/
> +++ utils/apparmor/
>> @@ -70,7 +70,8 @@
>> try:
>> sp = subprocess.
>> stderr=
>> - 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.
>> - 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.
>
Makes sense. Cjwatson was arguing for version checks, as they are easy
to grep for when eventually going python3 only.
> === modified file 'utils/
> --- utils/test/
> +++ utils/test/
>> @@ -101,6 +101,7 @@
>> def tearDown(self):
>> '''Teardown for tests'''
>> if os.path.
>> + sys.stdout.
>> recursive_
>>
>> #
>
> 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...
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
- 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
Jamie Strandboge (jdstrand) wrote : | # |
Thanks for your work on this, it looks great! :)
Preview Diff
1 | === modified file 'common/Make.rules' | |||
2 | --- common/Make.rules 2012-04-11 16:16:47 +0000 | |||
3 | +++ common/Make.rules 2012-06-12 13:52:18 +0000 | |||
4 | @@ -27,6 +27,10 @@ | |||
5 | 27 | DISTRIBUTION=AppArmor | 27 | DISTRIBUTION=AppArmor |
6 | 28 | VERSION=$(shell cat common/Version) | 28 | VERSION=$(shell cat common/Version) |
7 | 29 | 29 | ||
8 | 30 | # Convenience functions | ||
9 | 31 | pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH))))) | ||
10 | 32 | map = $(foreach a,$(2),$(call $(1),$(a))) | ||
11 | 33 | |||
12 | 30 | # OVERRIDABLE variables | 34 | # OVERRIDABLE variables |
13 | 31 | # Set these variables before including Make.rules to change its behavior | 35 | # Set these variables before including Make.rules to change its behavior |
14 | 32 | # SPECFILE - for packages that have a non-standard specfile name | 36 | # SPECFILE - for packages that have a non-standard specfile name |
15 | @@ -127,6 +131,17 @@ | |||
16 | 127 | 131 | ||
17 | 128 | endif | 132 | endif |
18 | 129 | 133 | ||
19 | 134 | ifndef PYTHON_VERSIONS | ||
20 | 135 | PYTHON_VERSIONS = $(call map, pathsearch, python2 python3) | ||
21 | 136 | endif | ||
22 | 137 | |||
23 | 138 | ifndef PYTHON | ||
24 | 139 | PYTHON = $(firstword ${PYTHON_VERSIONS}) | ||
25 | 140 | endif | ||
26 | 141 | |||
27 | 142 | #Helper function to be used with $(call pyalldo, run_test_with_all.py) | ||
28 | 143 | pyalldo=set -e; $(foreach py, $(PYTHON_VERSIONS), $(py) $(1);) | ||
29 | 144 | |||
30 | 130 | .PHONY: version | 145 | .PHONY: version |
31 | 131 | .SILENT: version | 146 | .SILENT: version |
32 | 132 | version: | 147 | version: |
33 | 133 | 148 | ||
34 | === modified file 'libraries/libapparmor/m4/ac_python_devel.m4' | |||
35 | --- libraries/libapparmor/m4/ac_python_devel.m4 2012-04-25 19:15:19 +0000 | |||
36 | +++ libraries/libapparmor/m4/ac_python_devel.m4 2012-06-12 13:52:18 +0000 | |||
37 | @@ -17,9 +17,9 @@ | |||
38 | 17 | # Check for a version of Python >= 2.1.0 | 17 | # Check for a version of Python >= 2.1.0 |
39 | 18 | # | 18 | # |
40 | 19 | AC_MSG_CHECKING([for a version of Python >= '2.1.0']) | 19 | AC_MSG_CHECKING([for a version of Python >= '2.1.0']) |
44 | 20 | ac_supports_python_ver=`$PYTHON -c "import sys, string; \ | 20 | ac_supports_python_ver=`$PYTHON -c "import sys; \ |
45 | 21 | ver = string.split(sys.version)[[0]]; \ | 21 | ver = sys.version.split()[[0]]; \ |
46 | 22 | print ver >= '2.1.0'"` | 22 | sys.stdout.write(str(ver >= '2.1.0'))"` |
47 | 23 | if test "$ac_supports_python_ver" != "True"; then | 23 | if test "$ac_supports_python_ver" != "True"; then |
48 | 24 | if test -z "$PYTHON_NOVERSIONCHECK"; then | 24 | if test -z "$PYTHON_NOVERSIONCHECK"; then |
49 | 25 | AC_MSG_RESULT([no]) | 25 | AC_MSG_RESULT([no]) |
50 | @@ -44,9 +44,9 @@ | |||
51 | 44 | # | 44 | # |
52 | 45 | if test -n "$1"; then | 45 | if test -n "$1"; then |
53 | 46 | AC_MSG_CHECKING([for a version of Python $1]) | 46 | AC_MSG_CHECKING([for a version of Python $1]) |
57 | 47 | ac_supports_python_ver=`$PYTHON -c "import sys, string; \ | 47 | ac_supports_python_ver=`$PYTHON -c "import sys; \ |
58 | 48 | ver = string.split(sys.version)[[0]]; \ | 48 | ver = sys.version.split()[[0]]; \ |
59 | 49 | print ver $1"` | 49 | sys.stdout.write("%s\n" % (ver == $1))"` |
60 | 50 | if test "$ac_supports_python_ver" = "True"; then | 50 | if test "$ac_supports_python_ver" = "True"; then |
61 | 51 | AC_MSG_RESULT([yes]) | 51 | AC_MSG_RESULT([yes]) |
62 | 52 | else | 52 | else |
63 | @@ -80,8 +80,8 @@ | |||
64 | 80 | # | 80 | # |
65 | 81 | AC_MSG_CHECKING([for Python include path]) | 81 | AC_MSG_CHECKING([for Python include path]) |
66 | 82 | if test -z "$PYTHON_CPPFLAGS"; then | 82 | if test -z "$PYTHON_CPPFLAGS"; then |
69 | 83 | python_path=`$PYTHON -c "import distutils.sysconfig; \ | 83 | python_path=`$PYTHON -c "import sys; import distutils.sysconfig;\ |
70 | 84 | print distutils.sysconfig.get_python_inc();"` | 84 | sys.stdout.write('%s\n' % distutils.sysconfig.get_python_inc());"` |
71 | 85 | if test -n "${python_path}"; then | 85 | if test -n "${python_path}"; then |
72 | 86 | python_path="-I$python_path" | 86 | python_path="-I$python_path" |
73 | 87 | fi | 87 | fi |
74 | @@ -97,22 +97,20 @@ | |||
75 | 97 | if test -z "$PYTHON_LDFLAGS"; then | 97 | if test -z "$PYTHON_LDFLAGS"; then |
76 | 98 | # (makes two attempts to ensure we've got a version number | 98 | # (makes two attempts to ensure we've got a version number |
77 | 99 | # from the interpreter) | 99 | # from the interpreter) |
81 | 100 | py_version=`$PYTHON -c "from distutils.sysconfig import *; \ | 100 | py_version=`$PYTHON -c "import sys; from distutils.sysconfig import *; \ |
82 | 101 | from string import join; \ | 101 | sys.stdout.write('%s\n' % ''.join(get_config_vars('VERSION')))"` |
80 | 102 | print join(get_config_vars('VERSION'))"` | ||
83 | 103 | if test "$py_version" == "[None]"; then | 102 | if test "$py_version" == "[None]"; then |
84 | 104 | if test -n "$PYTHON_VERSION"; then | 103 | if test -n "$PYTHON_VERSION"; then |
85 | 105 | py_version=$PYTHON_VERSION | 104 | py_version=$PYTHON_VERSION |
86 | 106 | else | 105 | else |
87 | 107 | py_version=`$PYTHON -c "import sys; \ | 106 | py_version=`$PYTHON -c "import sys; \ |
89 | 108 | print sys.version[[:3]]"` | 107 | sys.stdout.write("%s\n" % sys.version[[:3]])"` |
90 | 109 | fi | 108 | fi |
91 | 110 | fi | 109 | fi |
92 | 111 | 110 | ||
97 | 112 | PYTHON_LDFLAGS=`$PYTHON -c "from distutils.sysconfig import *; \ | 111 | PYTHON_LDFLAGS=`$PYTHON -c "import sys; from distutils.sysconfig import *; \ |
98 | 113 | from string import join; \ | 112 | sys.stdout.write('-L' + get_python_lib(0,1) + ' -lpython\n')"`$py_version`$PYTHON -c \ |
99 | 114 | print '-L' + get_python_lib(0,1), \ | 113 | "import sys; sys.stdout.write('%s' % getattr(sys,'abiflags',''))"` |
96 | 115 | '-lpython';"`$py_version | ||
100 | 116 | fi | 114 | fi |
101 | 117 | AC_MSG_RESULT([$PYTHON_LDFLAGS]) | 115 | AC_MSG_RESULT([$PYTHON_LDFLAGS]) |
102 | 118 | AC_SUBST([PYTHON_LDFLAGS]) | 116 | AC_SUBST([PYTHON_LDFLAGS]) |
103 | @@ -122,8 +120,8 @@ | |||
104 | 122 | # | 120 | # |
105 | 123 | AC_MSG_CHECKING([for Python site-packages path]) | 121 | AC_MSG_CHECKING([for Python site-packages path]) |
106 | 124 | if test -z "$PYTHON_SITE_PKG"; then | 122 | if test -z "$PYTHON_SITE_PKG"; then |
109 | 125 | PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \ | 123 | PYTHON_SITE_PKG=`$PYTHON -c "import sys; import distutils.sysconfig; \ |
110 | 126 | print distutils.sysconfig.get_python_lib(0,0);"` | 124 | sys.stdout.write('%s\n' % distutils.sysconfig.get_python_lib(0,0));"` |
111 | 127 | fi | 125 | fi |
112 | 128 | AC_MSG_RESULT([$PYTHON_SITE_PKG]) | 126 | AC_MSG_RESULT([$PYTHON_SITE_PKG]) |
113 | 129 | AC_SUBST([PYTHON_SITE_PKG]) | 127 | AC_SUBST([PYTHON_SITE_PKG]) |
114 | @@ -133,9 +131,9 @@ | |||
115 | 133 | # | 131 | # |
116 | 134 | AC_MSG_CHECKING(python extra libraries) | 132 | AC_MSG_CHECKING(python extra libraries) |
117 | 135 | if test -z "$PYTHON_EXTRA_LIBS"; then | 133 | if test -z "$PYTHON_EXTRA_LIBS"; then |
121 | 136 | PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \ | 134 | PYTHON_EXTRA_LIBS=`$PYTHON -c "import sys; import distutils.sysconfig; \ |
122 | 137 | conf = distutils.sysconfig.get_config_var; \ | 135 | conf = distutils.sysconfig.get_config_var; \ |
123 | 138 | print conf('LOCALMODLIBS'), conf('LIBS')"` | 136 | sys.stdout.write('%s %s\n' % (conf('LOCALMODLIBS'), conf('LIBS')))"` |
124 | 139 | fi | 137 | fi |
125 | 140 | AC_MSG_RESULT([$PYTHON_EXTRA_LIBS]) | 138 | AC_MSG_RESULT([$PYTHON_EXTRA_LIBS]) |
126 | 141 | AC_SUBST(PYTHON_EXTRA_LIBS) | 139 | AC_SUBST(PYTHON_EXTRA_LIBS) |
127 | @@ -145,9 +143,9 @@ | |||
128 | 145 | # | 143 | # |
129 | 146 | AC_MSG_CHECKING(python extra linking flags) | 144 | AC_MSG_CHECKING(python extra linking flags) |
130 | 147 | if test -z "$PYTHON_EXTRA_LDFLAGS"; then | 145 | if test -z "$PYTHON_EXTRA_LDFLAGS"; then |
134 | 148 | PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \ | 146 | PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import sys; import distutils.sysconfig; \ |
135 | 149 | conf = distutils.sysconfig.get_config_var; \ | 147 | conf = distutils.sysconfig.get_config_var; \ |
136 | 150 | print conf('LINKFORSHARED')"` | 148 | sys.stdout.write('%s\n' % conf('LINKFORSHARED'))"` |
137 | 151 | fi | 149 | fi |
138 | 152 | AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS]) | 150 | AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS]) |
139 | 153 | AC_SUBST(PYTHON_EXTRA_LDFLAGS) | 151 | AC_SUBST(PYTHON_EXTRA_LDFLAGS) |
140 | 154 | 152 | ||
141 | === modified file 'utils/Makefile' | |||
142 | --- utils/Makefile 2012-05-08 05:37:48 +0000 | |||
143 | +++ utils/Makefile 2012-06-12 13:52:18 +0000 | |||
144 | @@ -65,7 +65,7 @@ | |||
145 | 65 | $(MAKE) install_manpages DESTDIR=${DESTDIR} | 65 | $(MAKE) install_manpages DESTDIR=${DESTDIR} |
146 | 66 | $(MAKE) -C vim install DESTDIR=${DESTDIR} | 66 | $(MAKE) -C vim install DESTDIR=${DESTDIR} |
147 | 67 | ln -sf aa-status.8 ${DESTDIR}/${MANDIR}/man8/apparmor_status.8 | 67 | ln -sf aa-status.8 ${DESTDIR}/${MANDIR}/man8/apparmor_status.8 |
149 | 68 | python ${PYSETUP} install --prefix=${PYPREFIX} --root=${DESTDIR} --version=${VERSION} | 68 | ${PYTHON} ${PYSETUP} install --prefix=${PYPREFIX} --root=${DESTDIR} --version=${VERSION} |
150 | 69 | 69 | ||
151 | 70 | .PHONY: clean | 70 | .PHONY: clean |
152 | 71 | ifndef VERBOSE | 71 | ifndef VERBOSE |
153 | @@ -105,6 +105,4 @@ | |||
154 | 105 | test -s $$tmpfile && cat $$tmpfile && rm -f $$tmpfile && exit 1; \ | 105 | test -s $$tmpfile && cat $$tmpfile && rm -f $$tmpfile && exit 1; \ |
155 | 106 | done || true; \ | 106 | done || true; \ |
156 | 107 | rm -f $$tmpfile | 107 | rm -f $$tmpfile |
160 | 108 | for i in test/* ; do \ | 108 | $(foreach test, $(wildcard test/test-*.py), $(call pyalldo, $(test))) |
158 | 109 | python $$i || exit 1; \ | ||
159 | 110 | done | ||
161 | 111 | 109 | ||
162 | === modified file 'utils/aa-easyprof' | |||
163 | --- utils/aa-easyprof 2012-05-09 18:05:07 +0000 | |||
164 | +++ utils/aa-easyprof 2012-06-12 13:52:18 +0000 | |||
165 | @@ -35,7 +35,7 @@ | |||
166 | 35 | 35 | ||
167 | 36 | try: | 36 | try: |
168 | 37 | easyp = apparmor.easyprof.AppArmorEasyProfile(binary, opt) | 37 | easyp = apparmor.easyprof.AppArmorEasyProfile(binary, opt) |
170 | 38 | except AppArmorException, e: | 38 | except AppArmorException as e: |
171 | 39 | error(e.value) | 39 | error(e.value) |
172 | 40 | except Exception: | 40 | except Exception: |
173 | 41 | raise | 41 | raise |
174 | @@ -61,5 +61,5 @@ | |||
175 | 61 | # if we made it here, generate a profile | 61 | # if we made it here, generate a profile |
176 | 62 | params = apparmor.easyprof.gen_policy_params(binary, opt) | 62 | params = apparmor.easyprof.gen_policy_params(binary, opt) |
177 | 63 | p = easyp.gen_policy(**params) | 63 | p = easyp.gen_policy(**params) |
179 | 64 | print p, | 64 | sys.stdout.write('%s\n' % p) |
180 | 65 | 65 | ||
181 | 66 | 66 | ||
182 | === modified file 'utils/apparmor/easyprof.py' | |||
183 | --- utils/apparmor/easyprof.py 2012-05-08 05:37:48 +0000 | |||
184 | +++ utils/apparmor/easyprof.py 2012-06-12 13:52:18 +0000 | |||
185 | @@ -8,6 +8,8 @@ | |||
186 | 8 | # | 8 | # |
187 | 9 | # ------------------------------------------------------------------ | 9 | # ------------------------------------------------------------------ |
188 | 10 | 10 | ||
189 | 11 | from __future__ import with_statement | ||
190 | 12 | |||
191 | 11 | import codecs | 13 | import codecs |
192 | 12 | import glob | 14 | import glob |
193 | 13 | import optparse | 15 | import optparse |
194 | @@ -40,7 +42,7 @@ | |||
195 | 40 | def error(out, exit_code=1, do_exit=True): | 42 | def error(out, exit_code=1, do_exit=True): |
196 | 41 | '''Print error message and exit''' | 43 | '''Print error message and exit''' |
197 | 42 | try: | 44 | try: |
199 | 43 | print >> sys.stderr, "ERROR: %s" % (out) | 45 | sys.stderr.write("ERROR: %s\n" % (out)) |
200 | 44 | except IOError: | 46 | except IOError: |
201 | 45 | pass | 47 | pass |
202 | 46 | 48 | ||
203 | @@ -51,7 +53,7 @@ | |||
204 | 51 | def warn(out): | 53 | def warn(out): |
205 | 52 | '''Print warning message''' | 54 | '''Print warning message''' |
206 | 53 | try: | 55 | try: |
208 | 54 | print >> sys.stderr, "WARN: %s" % (out) | 56 | sys.stderr.write("WARN: %s\n" % (out)) |
209 | 55 | except IOError: | 57 | except IOError: |
210 | 56 | pass | 58 | pass |
211 | 57 | 59 | ||
212 | @@ -59,7 +61,7 @@ | |||
213 | 59 | def msg(out, output=sys.stdout): | 61 | def msg(out, output=sys.stdout): |
214 | 60 | '''Print message''' | 62 | '''Print message''' |
215 | 61 | try: | 63 | try: |
217 | 62 | print >> output, "%s" % (out) | 64 | sys.stdout.write("%s\n" % (out)) |
218 | 63 | except IOError: | 65 | except IOError: |
219 | 64 | pass | 66 | pass |
220 | 65 | 67 | ||
221 | @@ -70,7 +72,7 @@ | |||
222 | 70 | try: | 72 | try: |
223 | 71 | sp = subprocess.Popen(command, stdout=subprocess.PIPE, | 73 | sp = subprocess.Popen(command, stdout=subprocess.PIPE, |
224 | 72 | stderr=subprocess.STDOUT) | 74 | stderr=subprocess.STDOUT) |
226 | 73 | except OSError, ex: | 75 | except OSError as ex: |
227 | 74 | return [127, str(ex)] | 76 | return [127, str(ex)] |
228 | 75 | 77 | ||
229 | 76 | out = sp.communicate()[0] | 78 | out = sp.communicate()[0] |
230 | @@ -82,7 +84,7 @@ | |||
231 | 82 | try: | 84 | try: |
232 | 83 | sp1 = subprocess.Popen(command1, stdout=subprocess.PIPE) | 85 | sp1 = subprocess.Popen(command1, stdout=subprocess.PIPE) |
233 | 84 | sp2 = subprocess.Popen(command2, stdin=sp1.stdout) | 86 | sp2 = subprocess.Popen(command2, stdin=sp1.stdout) |
235 | 85 | except OSError, ex: | 87 | except OSError as ex: |
236 | 86 | return [127, str(ex)] | 88 | return [127, str(ex)] |
237 | 87 | 89 | ||
238 | 88 | out = sp2.communicate()[0] | 90 | out = sp2.communicate()[0] |
239 | @@ -93,7 +95,7 @@ | |||
240 | 93 | '''Print debug message''' | 95 | '''Print debug message''' |
241 | 94 | if DEBUGGING: | 96 | if DEBUGGING: |
242 | 95 | try: | 97 | try: |
244 | 96 | print >> sys.stderr, "DEBUG: %s" % (out) | 98 | sys.stderr.write("DEBUG: %s\n" % (out)) |
245 | 97 | except IOError: | 99 | except IOError: |
246 | 98 | pass | 100 | pass |
247 | 99 | 101 | ||
248 | @@ -181,6 +183,8 @@ | |||
249 | 181 | fn = policy | 183 | fn = policy |
250 | 182 | else: | 184 | else: |
251 | 183 | f, fn = tempfile.mkstemp(prefix='aa-easyprof') | 185 | f, fn = tempfile.mkstemp(prefix='aa-easyprof') |
252 | 186 | if not isinstance(policy, bytes): | ||
253 | 187 | policy = policy.encode('utf-8') | ||
254 | 184 | os.write(f, policy) | 188 | os.write(f, policy) |
255 | 185 | os.close(f) | 189 | os.close(f) |
256 | 186 | 190 | ||
257 | @@ -219,9 +223,9 @@ | |||
258 | 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): |
259 | 220 | self.dirs['policygroups'] = os.path.abspath(opt.policy_groups_dir) | 224 | self.dirs['policygroups'] = os.path.abspath(opt.policy_groups_dir) |
260 | 221 | 225 | ||
262 | 222 | if not self.dirs.has_key('templates'): | 226 | if not 'templates' in self.dirs: |
263 | 223 | raise AppArmorException("Could not find templates directory") | 227 | raise AppArmorException("Could not find templates directory") |
265 | 224 | if not self.dirs.has_key('policygroups'): | 228 | if not 'policygroups' in self.dirs: |
266 | 225 | raise AppArmorException("Could not find policygroups directory") | 229 | raise AppArmorException("Could not find policygroups directory") |
267 | 226 | 230 | ||
268 | 227 | self.aa_topdir = "/etc/apparmor.d" | 231 | self.aa_topdir = "/etc/apparmor.d" |
269 | @@ -445,11 +449,12 @@ | |||
270 | 445 | 449 | ||
271 | 446 | def print_basefilenames(files): | 450 | def print_basefilenames(files): |
272 | 447 | for i in files: | 451 | for i in files: |
274 | 448 | print "%s" % (os.path.basename(i)) | 452 | sys.stdout.write("%s\n" % (os.path.basename(i))) |
275 | 449 | 453 | ||
276 | 450 | def print_files(files): | 454 | def print_files(files): |
277 | 451 | for i in files: | 455 | for i in files: |
279 | 452 | print open(i).read() | 456 | with open(i) as f: |
280 | 457 | sys.stdout.write(f.read()+"\n") | ||
281 | 453 | 458 | ||
282 | 454 | def parse_args(args=None): | 459 | def parse_args(args=None): |
283 | 455 | '''Parse arguments''' | 460 | '''Parse arguments''' |
284 | 456 | 461 | ||
285 | === modified file 'utils/test/test-aa-easyprof.py' | |||
286 | --- utils/test/test-aa-easyprof.py 2012-05-09 18:05:07 +0000 | |||
287 | +++ utils/test/test-aa-easyprof.py 2012-06-12 13:52:18 +0000 | |||
288 | @@ -101,6 +101,7 @@ | |||
289 | 101 | def tearDown(self): | 101 | def tearDown(self): |
290 | 102 | '''Teardown for tests''' | 102 | '''Teardown for tests''' |
291 | 103 | if os.path.exists(self.tmpdir): | 103 | if os.path.exists(self.tmpdir): |
292 | 104 | sys.stdout.write("%s\n" % self.tmpdir) | ||
293 | 104 | recursive_rm(self.tmpdir) | 105 | recursive_rm(self.tmpdir) |
294 | 105 | 106 | ||
295 | 106 | # | 107 | # |
296 | @@ -328,7 +329,7 @@ | |||
297 | 328 | def test_binary_symlink(self): | 329 | def test_binary_symlink(self): |
298 | 329 | '''Test binary (symlink)''' | 330 | '''Test binary (symlink)''' |
299 | 330 | exe = os.path.join(self.tmpdir, 'exe') | 331 | exe = os.path.join(self.tmpdir, 'exe') |
301 | 331 | open(exe, 'wa').close() | 332 | open(exe, 'a').close() |
302 | 332 | symlink = exe + ".lnk" | 333 | symlink = exe + ".lnk" |
303 | 333 | os.symlink(exe, symlink) | 334 | os.symlink(exe, symlink) |
304 | 334 | 335 | ||
305 | @@ -441,7 +442,7 @@ | |||
306 | 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)) |
307 | 442 | 443 | ||
308 | 443 | if debugging: | 444 | if debugging: |
310 | 444 | print p | 445 | sys.stdout.write("%s\n" % p) |
311 | 445 | 446 | ||
312 | 446 | return p | 447 | return p |
313 | 447 | 448 | ||
314 | @@ -859,7 +860,7 @@ | |||
315 | 859 | # Create the necessary files to import aa-easyprof | 860 | # Create the necessary files to import aa-easyprof |
316 | 860 | init = os.path.join(os.path.dirname(absfn), '__init__.py') | 861 | init = os.path.join(os.path.dirname(absfn), '__init__.py') |
317 | 861 | if not os.path.exists(init): | 862 | if not os.path.exists(init): |
319 | 862 | open(init, 'wa').close() | 863 | open(init, 'a').close() |
320 | 863 | created.append(init) | 864 | created.append(init) |
321 | 864 | 865 | ||
322 | 865 | symlink = os.path.join(os.path.dirname(absfn), 'easyprof.py') | 866 | symlink = os.path.join(os.path.dirname(absfn), 'easyprof.py') |
323 | 866 | 867 | ||
324 | === modified file 'utils/vim/Makefile' | |||
325 | --- utils/vim/Makefile 2012-03-23 16:02:20 +0000 | |||
326 | +++ utils/vim/Makefile 2012-06-12 13:52:18 +0000 | |||
327 | @@ -14,12 +14,15 @@ | |||
328 | 14 | all: apparmor.vim | 14 | all: apparmor.vim |
329 | 15 | 15 | ||
330 | 16 | apparmor.vim: apparmor.vim.in Makefile create-apparmor.vim.py | 16 | apparmor.vim: apparmor.vim.in Makefile create-apparmor.vim.py |
332 | 17 | python create-apparmor.vim.py > $@ | 17 | ${PYTHON} create-apparmor.vim.py > apparmor.vim |
333 | 18 | 18 | ||
334 | 19 | install: apparmor.vim | 19 | install: apparmor.vim |
335 | 20 | install -d $(VIM_INSTALL_PATH) | 20 | install -d $(VIM_INSTALL_PATH) |
336 | 21 | install -m 644 $< $(VIM_INSTALL_PATH) | 21 | install -m 644 $< $(VIM_INSTALL_PATH) |
337 | 22 | 22 | ||
338 | 23 | test: apparmor.vim.in Makefile create-apparmor.vim.py | ||
339 | 24 | #Testing with all pythons | ||
340 | 25 | $(call pyalldo, create-apparmor.vim.py > /dev/null) | ||
341 | 23 | 26 | ||
342 | 24 | clean: | 27 | clean: |
343 | 25 | rm -f apparmor.vim common | 28 | rm -f apparmor.vim common |
344 | 26 | 29 | ||
345 | === modified file 'utils/vim/create-apparmor.vim.py' | |||
346 | --- utils/vim/create-apparmor.vim.py 2012-06-08 21:30:22 +0000 | |||
347 | +++ utils/vim/create-apparmor.vim.py 2012-06-12 13:52:18 +0000 | |||
348 | @@ -10,7 +10,6 @@ | |||
349 | 10 | # Christian Boltz <apparmor@cboltz.de> | 10 | # Christian Boltz <apparmor@cboltz.de> |
350 | 11 | 11 | ||
351 | 12 | from __future__ import with_statement | 12 | from __future__ import with_statement |
352 | 13 | import os | ||
353 | 14 | import re | 13 | import re |
354 | 15 | import subprocess | 14 | import subprocess |
355 | 16 | import sys | 15 | import sys |
356 | @@ -30,9 +29,9 @@ | |||
357 | 30 | return a textual error if it failed.''' | 29 | return a textual error if it failed.''' |
358 | 31 | 30 | ||
359 | 32 | try: | 31 | try: |
363 | 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) |
364 | 34 | except OSError, e: | 33 | except OSError as ex: |
365 | 35 | return [127, str(e)] | 34 | return [127, str(ex)] |
366 | 36 | 35 | ||
367 | 37 | out, outerr = sp.communicate(input) | 36 | out, outerr = sp.communicate(input) |
368 | 38 | 37 | ||
369 | @@ -47,7 +46,7 @@ | |||
370 | 47 | # get capabilities list | 46 | # get capabilities list |
371 | 48 | (rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_capabilities']) | 47 | (rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_capabilities']) |
372 | 49 | if rc != 0: | 48 | if rc != 0: |
374 | 50 | print >>sys.stderr, ("make list_capabilities failed: " + output) | 49 | sys.stderr.write("make list_capabilities failed: " + output) |
375 | 51 | exit(rc) | 50 | exit(rc) |
376 | 52 | 51 | ||
377 | 53 | capabilities = re.sub('CAP_', '', output.strip()).lower().split(" ") | 52 | capabilities = re.sub('CAP_', '', output.strip()).lower().split(" ") |
378 | @@ -59,7 +58,7 @@ | |||
379 | 59 | # get network protos list | 58 | # get network protos list |
380 | 60 | (rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_af_names']) | 59 | (rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_af_names']) |
381 | 61 | if rc != 0: | 60 | if rc != 0: |
383 | 62 | print >>sys.stderr, ("make list_af_names failed: " + output) | 61 | sys.stderr.write("make list_af_names failed: " + output) |
384 | 63 | exit(rc) | 62 | exit(rc) |
385 | 64 | 63 | ||
386 | 65 | af_names = [] | 64 | af_names = [] |
387 | @@ -106,7 +105,7 @@ | |||
388 | 106 | } | 105 | } |
389 | 107 | 106 | ||
390 | 108 | def my_repl(matchobj): | 107 | def my_repl(matchobj): |
392 | 109 | #print matchobj.group(1) | 108 | matchobj.group(1) |
393 | 110 | if matchobj.group(1) in aa_regex_map: | 109 | if matchobj.group(1) in aa_regex_map: |
394 | 111 | return aa_regex_map[matchobj.group(1)] | 110 | return aa_regex_map[matchobj.group(1)] |
395 | 112 | 111 | ||
396 | @@ -164,16 +163,16 @@ | |||
397 | 164 | 163 | ||
398 | 165 | regex = "@@(" + "|".join(aa_regex_map) + ")@@" | 164 | regex = "@@(" + "|".join(aa_regex_map) + ")@@" |
399 | 166 | 165 | ||
402 | 167 | print '" generated from apparmor.vim.in by create-apparmor.vim.py' | 166 | sys.stdout.write('" generated from apparmor.vim.in by create-apparmor.vim.py\n') |
403 | 168 | print '" do not edit this file - edit apparmor.vim.in or create-apparmor.vim.py instead' + "\n" | 167 | sys.stdout.write('" do not edit this file - edit apparmor.vim.in or create-apparmor.vim.py instead' + "\n\n") |
404 | 169 | 168 | ||
406 | 170 | with file("apparmor.vim.in") as template: | 169 | with open("apparmor.vim.in") as template: |
407 | 171 | for line in template: | 170 | for line in template: |
408 | 172 | line = re.sub(regex, my_repl, line.rstrip()) | 171 | line = re.sub(regex, my_repl, line.rstrip()) |
415 | 173 | print line | 172 | sys.stdout.write('%s\n' % line) |
416 | 174 | 173 | ||
417 | 175 | print "\n\n\n" | 174 | sys.stdout.write("\n\n\n\n") |
418 | 176 | 175 | ||
419 | 177 | print '" file rules added with create_file_rule()' | 176 | sys.stdout.write('" file rules added with create_file_rule()\n') |
420 | 178 | print re.sub(regex, my_repl, filerule) | 177 | sys.stdout.write(re.sub(regex, my_repl, filerule)+'\n') |
421 | 179 | 178 |
On Jun 11, 2012, at 05:04 PM, Dmitrijs Ledkovs wrote:
>Dmitrijs Ledkovs has proposed merging lp:~dmitrij.ledkov/apparmor/py3 into lp:apparmor. foundations) /code.launchpad .net/~dmitrij. ledkov/ apparmor/ py3/+merge/ 109682 VERSIONS= python2 before running check/test targets.
>
>Requested reviews:
> Canonical Foundations Team (canonical-
> AppArmor Developers (apparmor-dev)
>
>For more details, see:
>https:/
>
>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_
>
>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' easyprof. py 2012-05-08 05:37:48 +0000 easyprof. py 2012-06-11 17:03:23 +0000 Popen(command, stdout= subprocess. PIPE, subprocess. STDOUT)
--- utils/apparmor/
+++ utils/apparmor/
> @@ -70,7 +70,8 @@
> try:
> sp = subprocess.
> stderr=
> - 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 @@ mkstemp( prefix= 'aa-easyprof' )
> fn = policy
> else:
> f, fn = tempfile.
> - 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): encode( 'utf-8' )
policy = policy.
=== modified file 'utils/ test/test- aa-easyprof. py' test-aa- easyprof. py 2012-05-09 18:05:07 +0000 test-aa- easyprof. py 2012-06-11 17:03:23 +0000 exists( self.tmpdir) : write(" %s\n" % self.tmpdir) rm(self. tmpdir)
--- utils/test/
+++ utils/test/
> @@ -101,6 +101,7 @@
> def tearDown(self):
> '''Teardown for tests'''
> if os.path.
> + sys.stdout.
> recursive_
>
> #
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.