From 18bf66b0b51c1a2c2b99c9e6b314684981555492 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Tue, 11 Aug 2009 20:28:23 +0000 Subject: [PATCH] Update imports to import portage.os and portage.shutil (with unicode wrappers), and use _unicode_encode() where appropriate. svn path=/main/trunk/; revision=13996 --- pym/portage/__init__.py | 26 +++++++++++++------------- pym/portage/_selinux.py | 36 ++++++++++++++---------------------- 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/pym/portage/__init__.py b/pym/portage/__init__.py index 7c829e96c..1ed91a336 100644 --- a/pym/portage/__init__.py +++ b/pym/portage/__init__.py @@ -111,19 +111,6 @@ except ImportError, e: sys.stderr.write(" "+str(e)+"\n\n") raise - -try: - import portage._selinux as selinux -except OSError, e: - writemsg("!!! SELinux not loaded: %s\n" % str(e), noiselevel=-1) - del e -except ImportError: - pass - -# =========================================================================== -# END OF IMPORTS -- END OF IMPORTS -- END OF IMPORTS -- END OF IMPORTS -- END -# =========================================================================== - def _unicode_encode(s): if isinstance(s, unicode): s = s.encode('utf_8', 'replace') @@ -189,6 +176,19 @@ if sys.hexversion >= 0x3000000: return mod os = _unicode_module_wrapper(os) +shutil = _unicode_module_wrapper(shutil) + +try: + import portage._selinux as selinux +except OSError, e: + writemsg("!!! SELinux not loaded: %s\n" % str(e), noiselevel=-1) + del e +except ImportError: + pass + +# =========================================================================== +# END OF IMPORTS -- END OF IMPORTS -- END OF IMPORTS -- END OF IMPORTS -- END +# =========================================================================== def _shell_quote(s): """ diff --git a/pym/portage/_selinux.py b/pym/portage/_selinux.py index 1b5f530ce..e3c35ec5d 100644 --- a/pym/portage/_selinux.py +++ b/pym/portage/_selinux.py @@ -2,16 +2,15 @@ # Distributed under the terms of the GNU General Public License v2 # $Id$ -import os +import portage +from portage import os +from portage import shutil import selinux -import shutil from selinux import is_selinux_enabled, getfilecon, lgetfilecon def copyfile(src, dest): - if isinstance(src, unicode): - src = src.encode('utf_8', 'replace') - if isinstance(dest, unicode): - dest = dest.encode('utf_8', 'replace') + src = portage._unicode_encode(src) + dest = portage._unicode_encode(dest) (rc, ctx) = selinux.lgetfilecon(src) if rc < 0: raise OSError("copyfile: Failed getting context of \"%s\"." % src) @@ -30,10 +29,8 @@ def getcontext(): return ctx def mkdir(target, refdir): - if isinstance(target, unicode): - target = target.encode('utf_8', 'replace') - if isinstance(refdir, unicode): - refdir = refdir.encode('utf_8', 'replace') + target = portage._unicode_encode(target) + refdir = portage._unicode_encode(refdir) (rc, ctx) = selinux.getfilecon(refdir) if rc < 0: raise OSError( @@ -47,10 +44,8 @@ def mkdir(target, refdir): setfscreatecon() def rename(src, dest): - if isinstance(src, unicode): - src = src.encode('utf_8', 'replace') - if isinstance(dest, unicode): - dest = dest.encode('utf_8', 'replace') + src = portage._unicode_encode(src) + dest = portage._unicode_encode(dest) (rc, ctx) = selinux.lgetfilecon(src) if rc < 0: raise OSError("rename: Failed getting context of \"%s\"." % src) @@ -73,13 +68,13 @@ def setexec(ctx="\n"): raise OSError("setexec: Failed setting exec() context \"%s\"." % ctx) def setfscreate(ctx="\n"): - if isinstance(ctx, unicode): - ctx = ctx.encode('utf_8', 'replace') + ctx = portage._unicode_encode(ctx) if selinux.setfscreatecon(ctx) < 0: raise OSError( "setfscreate: Failed setting fs create context \"%s\"." % ctx) def spawn(selinux_type, spawn_func, mycommand, opt_name=None, **keywords): + selinux_type = portage._unicode_encode(selinux_type) con = settype(selinux_type) setexec(con) try: @@ -88,12 +83,9 @@ def spawn(selinux_type, spawn_func, mycommand, opt_name=None, **keywords): setexec() def symlink(target, link, reflnk): - if isinstance(target, unicode): - target = target.encode('utf_8', 'replace') - if isinstance(link, unicode): - link = link.encode('utf_8', 'replace') - if isinstance(reflnk, unicode): - reflnk = reflnk.encode('utf_8', 'replace') + target = portage._unicode_encode(target) + link = portage._unicode_encode(link) + reflnk = portage._unicode_encode(reflnk) (rc, ctx) = selinux.lgetfilecon(reflnk) if rc < 0: raise OSError( -- 2.26.2