Commit dac3c008 authored by Dmitrijs Ledkovs's avatar Dmitrijs Ledkovs

Initial port to python3 for utilities.

parent 5b6b2bbc
......@@ -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
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:
......
......@@ -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)))
......@@ -70,7 +70,8 @@ def cmd(command):
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 @@ 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:
ex = sys.exc_info()[1]
return [127, str(ex)]
out = sp2.communicate()[0]
......@@ -181,7 +183,8 @@ def verify_policy(policy):
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 @@ 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 +448,11 @@ 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()
sys.stdout.write(open(i).read()+"\n")
def parse_args(args=None):
'''Parse arguments'''
......
......@@ -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')
......
......@@ -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 > /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
......@@ -30,8 +30,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:
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 @@ 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 +60,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 = []
......@@ -164,16 +165,16 @@ filerule = filerule + create_file_rule ( 'sdEntryR', r'[rl]+', 'read entry, n
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
sys.stdout.write(line)
print "\n\n\n"
sys.stdout.write("\n\n\n\n")
print '" file rules added with create_file_rule()'
print re.sub(regex, my_repl, filerule)
sys.stdout.write('" file rules added with create_file_rule()\n')
sys.stdout.write(re.sub(regex, my_repl, filerule)+'\n')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment