author Alexandre Fayolle <>
Mon, 22 May 2006 12:38:25 +0200
changeset 1 b9b031063093
parent 0 7710b138d4eb
permissions -rw-r--r--
packaging updates

# Copyright (c) 2004 LOGILAB S.A. (Paris, FRANCE).
# --
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
"""some miscellaneous 'system' utilities
__revision__ = '$Id:,v 1.7 2004-12-16 09:43:58 syt Exp $'

import sys
import os
from os.path import expanduser, exists, join, dirname

# locate oobrother's home directory
if os.environ.has_key('OOBROTHERHOME'):
    USER_HOME = expanduser('~')
    if USER_HOME == '~' or USER_HOME == '/root':
        OOBROTHER_HOME = ".oobrother"
        OOBROTHER_HOME = join(USER_HOME, '.oobrother')
if not exists(OOBROTHER_HOME):
    except OSError:
        print >> sys.stderr, 'Unable to create directory %s' % OOBROTHER_HOME

def call_in_directory(directory, func, *args, **kwargs):
    """call the function after a move in the directory, then go back to
    the original location

    no side effect on sys.path and sys.modules
    path = os.getcwd()
        return call_and_restore(func, *args, **kwargs)
def call_and_restore(func, *args, **kwargs):
    """call the function, avoiding side effect on sys.path and sys.modules
    path = sys.path[:]
    modules = dict(sys.modules) # .copy()
        return func(*args, **kwargs)
        sys.path = path
        for modname in sys.modules.keys():
            if not modname in modules:
                del sys.modules[modname]

class PathNormalizerMixIn:
    def set_base(self, thefile):
        """set the current base according to the given wrapped file"""
        if thefile.is_directory():
            self.__base = thefile.abspath
            self.__base = dirname(thefile.abspath)

    def normalize_path(self, filepath):
        """return a normalized version of the given file path"""
        return filepath[len(self.__base)+1:] or filepath
    def absolute_path(self, filepath):
        """return a normalized version of the given file path"""
        return join(self.__base, filepath)