From a9fea52d582034973feaf7fe77a5730ab0ab4849 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Tue, 14 Mar 2006 00:38:24 +0000 Subject: Add a new OperationNotPermitted exception and use it to wrap apply_permissions exceptions. svn path=/main/trunk/; revision=2871 --- pym/portage.py | 14 +++++++------- pym/portage_exception.py | 2 ++ pym/portage_util.py | 40 +++++++++++++++++++++++----------------- 3 files changed, 32 insertions(+), 24 deletions(-) (limited to 'pym') diff --git a/pym/portage.py b/pym/portage.py index a15ec1c0b..ad040dd70 100644 --- a/pym/portage.py +++ b/pym/portage.py @@ -1836,14 +1836,14 @@ def fetch(myuris, mysettings, listonly=0, fetchonly=0, locks_in_subdir=".locks", can_fetch=False else: def distdir_perms(filename): + all_applied = True try: - portage_util.apply_secpass_permissions(filename, gid=portage_gid, mode=0775) - except OSError, oe: - if oe.errno == errno.EPERM: - writemsg("!!! Unable to apply group permissions to '%s'. Non-root users may experience issues.\n" - % filename) - else: - raise oe + all_applied = portage_util.apply_secpass_permissions(filename, gid=portage_gid, mode=0775) + except portage_exceptions.OperationNotPermitted: + all_applied = False + if not all_applied: + writemsg("!!! Unable to apply group permissions to '%s'." + \ + " Non-root users may experience issues.\n" % filename) distdir_perms(mysettings["DISTDIR"]) if use_locks and locks_in_subdir: distlocks_subdir = os.path.join(mysettings["DISTDIR"], locks_in_subdir) diff --git a/pym/portage_exception.py b/pym/portage_exception.py index 27294da6e..0d0206df1 100644 --- a/pym/portage_exception.py +++ b/pym/portage_exception.py @@ -46,6 +46,8 @@ class FileNotFound(InvalidLocation): class DirectoryNotFound(InvalidLocation): """A directory was not found when it was expected to exist""" +class OperationNotPermitted(PortageException): + """An operation was not permitted operating system""" class CommandNotFound(PortageException): """A required binary was not available or executable""" diff --git a/pym/portage_util.py b/pym/portage_util.py index 5b8d87d55..205db9b36 100644 --- a/pym/portage_util.py +++ b/pym/portage_util.py @@ -2,8 +2,9 @@ # Distributed under the terms of the GNU General Public License v2 # $Id: /var/cvsroot/gentoo-src/portage/pym/portage_util.py,v 1.11.2.6 2005/04/23 07:26:04 jstubbs Exp $ +from portage_exception import FileNotFound, OperationNotPermitted -import sys,string,shlex,os +import sys,string,shlex,os,errno try: import cPickle except ImportError: @@ -458,16 +459,23 @@ def apply_permissions(filename, uid=-1, gid=-1, mode=0, stat_cached=None): """Apply user, group, and mode bits to a file if the existing bits do not already match.""" - - if stat_cached is None: - stat_cached = os.stat(filename) - - if (uid != -1 and uid != stat_cached.st_uid) or \ - (gid != -1 and gid != stat_cached.st_gid): - os.chown(filename, uid, gid) - - if mode & stat_cached.st_mode != mode: - os.chmod(filename, mode | stat_cached.st_mode) + try: + if stat_cached is None: + stat_cached = os.stat(filename) + + if (uid != -1 and uid != stat_cached.st_uid) or \ + (gid != -1 and gid != stat_cached.st_gid): + os.chown(filename, uid, gid) + + if mode & stat_cached.st_mode != mode: + os.chmod(filename, mode | stat_cached.st_mode) + except OSError, oe: + if oe.errno == errno.EPERM: + raise OperationNotPermitted(oe) + elif oe.errno == errno.ENOENT: + raise FileNotFound(oe) + else: + raise oe def apply_stat_permissions(filename, newstat, stat_cached=None): """A wrapper around apply_secpass_permissions that gets @@ -530,12 +538,10 @@ class atomic_ofstream(file): if not self._aborted: try: apply_stat_permissions(self.name, os.stat(self._real_name)) - except OSError, oe: - import errno - if oe.errno in (errno.ENOENT,errno.EPERM): - pass - else: - raise oe + except OperationNotPermitted: + pass + except FileNotFound: + pass os.rename(self.name, self._real_name) finally: # Make sure we cleanup the temp file -- cgit v1.2.3-1-g7c22