2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-09-02 15:25:27 +00:00

Initial port to python3 for utilities.

This commit is contained in:
Dmitrijs Ledkovs
2012-06-11 17:56:21 +01:00
parent 5b6b2bbc01
commit dac3c00862
6 changed files with 47 additions and 26 deletions

View File

@@ -27,6 +27,10 @@
DISTRIBUTION=AppArmor DISTRIBUTION=AppArmor
VERSION=$(shell cat common/Version) VERSION=$(shell cat common/Version)
# Convenience functions
pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
map = $(foreach a,$(2),$(call $(1),$(a)))
# OVERRIDABLE variables # OVERRIDABLE variables
# Set these variables before including Make.rules to change its behavior # Set these variables before including Make.rules to change its behavior
# SPECFILE - for packages that have a non-standard specfile name # SPECFILE - for packages that have a non-standard specfile name
@@ -127,6 +131,17 @@ endif
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 .PHONY: version
.SILENT: version .SILENT: version
version: version:

View File

@@ -65,7 +65,7 @@ install: ${MANPAGES} ${HTMLMANPAGES}
$(MAKE) install_manpages DESTDIR=${DESTDIR} $(MAKE) install_manpages DESTDIR=${DESTDIR}
$(MAKE) -C vim install DESTDIR=${DESTDIR} $(MAKE) -C vim install DESTDIR=${DESTDIR}
ln -sf aa-status.8 ${DESTDIR}/${MANDIR}/man8/apparmor_status.8 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 .PHONY: clean
ifndef VERBOSE ifndef VERBOSE
@@ -105,6 +105,4 @@ check: check_severity_db
test -s $$tmpfile && cat $$tmpfile && rm -f $$tmpfile && exit 1; \ test -s $$tmpfile && cat $$tmpfile && rm -f $$tmpfile && exit 1; \
done || true; \ done || true; \
rm -f $$tmpfile rm -f $$tmpfile
for i in test/* ; do \ $(foreach test, $(wildcard test/test-*.py), $(call pyalldo, $(test)))
python $$i || exit 1; \
done

View File

@@ -70,7 +70,8 @@ def cmd(command):
try: try:
sp = subprocess.Popen(command, stdout=subprocess.PIPE, sp = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT)
except OSError, ex: except OSError:
ex = sys.exc_info()[1]
return [127, str(ex)] return [127, str(ex)]
out = sp.communicate()[0] out = sp.communicate()[0]
@@ -82,7 +83,8 @@ def cmd_pipe(command1, command2):
try: try:
sp1 = subprocess.Popen(command1, stdout=subprocess.PIPE) sp1 = subprocess.Popen(command1, stdout=subprocess.PIPE)
sp2 = subprocess.Popen(command2, stdin=sp1.stdout) sp2 = subprocess.Popen(command2, stdin=sp1.stdout)
except OSError, ex: except OSError:
ex = sys.exc_info()[1]
return [127, str(ex)] return [127, str(ex)]
out = sp2.communicate()[0] out = sp2.communicate()[0]
@@ -181,7 +183,8 @@ def verify_policy(policy):
fn = policy fn = policy
else: else:
f, fn = tempfile.mkstemp(prefix='aa-easyprof') 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) os.close(f)
rc, out = cmd([exe, '-p', fn]) 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): if opt.policy_groups_dir and os.path.isdir(opt.policy_groups_dir):
self.dirs['policygroups'] = os.path.abspath(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") 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") raise AppArmorException("Could not find policygroups directory")
self.aa_topdir = "/etc/apparmor.d" self.aa_topdir = "/etc/apparmor.d"
@@ -445,11 +448,11 @@ class AppArmorEasyProfile:
def print_basefilenames(files): def print_basefilenames(files):
for i in files: for i in files:
print "%s" % (os.path.basename(i)) sys.stdout.write("%s\n" % (os.path.basename(i)))
def print_files(files): def print_files(files):
for i in files: for i in files:
print open(i).read() sys.stdout.write(open(i).read()+"\n")
def parse_args(args=None): def parse_args(args=None):
'''Parse arguments''' '''Parse arguments'''

View File

@@ -101,6 +101,7 @@ TEMPLATES_DIR="%s/templates"
def tearDown(self): def tearDown(self):
'''Teardown for tests''' '''Teardown for tests'''
if os.path.exists(self.tmpdir): if os.path.exists(self.tmpdir):
sys.stdout.write("%s\n" % self.tmpdir)
recursive_rm(self.tmpdir) recursive_rm(self.tmpdir)
# #
@@ -328,7 +329,7 @@ POLICYGROUPS_DIR="%s/templates"
def test_binary_symlink(self): def test_binary_symlink(self):
'''Test binary (symlink)''' '''Test binary (symlink)'''
exe = os.path.join(self.tmpdir, 'exe') exe = os.path.join(self.tmpdir, 'exe')
open(exe, 'wa').close() open(exe, 'a').close()
symlink = exe + ".lnk" symlink = exe + ".lnk"
os.symlink(exe, symlink) 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)) self.assertFalse(inv_s in p, "Found '%s' in :\n%s" % (inv_s, p))
if debugging: if debugging:
print p sys.stdout.write("%s\n" % p)
return p return p
@@ -859,7 +860,7 @@ if __name__ == '__main__':
# Create the necessary files to import aa-easyprof # Create the necessary files to import aa-easyprof
init = os.path.join(os.path.dirname(absfn), '__init__.py') init = os.path.join(os.path.dirname(absfn), '__init__.py')
if not os.path.exists(init): if not os.path.exists(init):
open(init, 'wa').close() open(init, 'a').close()
created.append(init) created.append(init)
symlink = os.path.join(os.path.dirname(absfn), 'easyprof.py') symlink = os.path.join(os.path.dirname(absfn), 'easyprof.py')

View File

@@ -14,12 +14,15 @@ VIM_INSTALL_PATH=${DESTDIR}/usr/share/apparmor
all: apparmor.vim all: apparmor.vim
apparmor.vim: apparmor.vim.in Makefile create-apparmor.vim.py 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: apparmor.vim
install -d $(VIM_INSTALL_PATH) install -d $(VIM_INSTALL_PATH)
install -m 644 $< $(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: clean:
rm -f apparmor.vim common rm -f apparmor.vim common

View File

@@ -30,8 +30,9 @@ def cmd(command, input = None, stderr = subprocess.STDOUT, stdout = subprocess.P
return a textual error if it failed.''' return a textual error if it failed.'''
try: try:
sp = subprocess.Popen(command, stdin=stdin, stdout=stdout, stderr=stderr, close_fds=True) sp = subprocess.Popen(command, stdin=stdin, stdout=stdout, stderr=stderr, close_fds=True, universal_newlines=True)
except OSError, e: except OSError:
e = sys.exc_info()[1]
return [127, str(e)] return [127, str(e)]
out, outerr = sp.communicate(input) out, outerr = sp.communicate(input)
@@ -47,7 +48,7 @@ def cmd(command, input = None, stderr = subprocess.STDOUT, stdout = subprocess.P
# get capabilities list # get capabilities list
(rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_capabilities']) (rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_capabilities'])
if rc != 0: if rc != 0:
print >>sys.stderr, ("make list_capabilities failed: " + output) sys.stderr.write("make list_capabilities failed: " + output)
exit(rc) exit(rc)
capabilities = re.sub('CAP_', '', output.strip()).lower().split(" ") capabilities = re.sub('CAP_', '', output.strip()).lower().split(" ")
@@ -59,7 +60,7 @@ for cap in capabilities:
# get network protos list # get network protos list
(rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_af_names']) (rc, output) = cmd(['make', '-s', '--no-print-directory', 'list_af_names'])
if rc != 0: if rc != 0:
print >>sys.stderr, ("make list_af_names failed: " + output) sys.stderr.write("make list_af_names failed: " + output)
exit(rc) exit(rc)
af_names = [] af_names = []
@@ -164,16 +165,16 @@ filerule = filerule + create_file_rule ( 'sdEntryR', r'[rl]+', 'read entry, n
regex = "@@(" + "|".join(aa_regex_map) + ")@@" regex = "@@(" + "|".join(aa_regex_map) + ")@@"
print '" generated from apparmor.vim.in by create-apparmor.vim.py' sys.stdout.write('" generated from apparmor.vim.in by create-apparmor.vim.py\n')
print '" do not edit this file - edit apparmor.vim.in or create-apparmor.vim.py instead' + "\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: for line in template:
line = re.sub(regex, my_repl, line.rstrip()) 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()' sys.stdout.write('" file rules added with create_file_rule()\n')
print re.sub(regex, my_repl, filerule) sys.stdout.write(re.sub(regex, my_repl, filerule)+'\n')