def testsubpathsto(self):
     from comoonics.cdsl import subpathsto
     _resultpairs=[ [ "etc", "etc/sysconfig/network/ifcfg-eth0", [ "etc/sysconfig/network/ifcfg-eth0", "etc/sysconfig/network", "etc/sysconfig" ] ],
                    [ "etc/1", "var/1", [] ],
                    [ "", "etc/1", [ "etc/1", "etc" ] ],
                    [ "", "etc/1/2", [ "etc/1/2", "etc/1", "etc" ] ],
                    [ "etc/1", "etc/2", [ "etc/2" ] ] ,
                    [ "etc/1", "etc/2/3", [ "etc/2/3", "etc/2" ] ],
                    [ "etc/1", "etc/2/3/4", [ "etc/2/3/4", "etc/2/3", "etc/2" ] ] ]
     for _tmp in _resultpairs:
         result=subpathsto(_tmp[0], _tmp[1])
         self.assertEquals(result, _tmp[2], "subpathsto(%s, %s) == %s but not %s" %(_tmp[0], _tmp[1], result, _tmp[2]))
 def testsubpathsto(self):
     from comoonics.cdsl import subpathsto
     _resultpairs = [[
         "etc", "etc/sysconfig/network/ifcfg-eth0",
         [
             "etc/sysconfig/network/ifcfg-eth0", "etc/sysconfig/network",
             "etc/sysconfig"
         ]
     ], ["etc/1", "var/1", []], ["", "etc/1", ["etc/1", "etc"]],
                     ["", "etc/1/2", ["etc/1/2", "etc/1", "etc"]],
                     ["etc/1", "etc/2", ["etc/2"]],
                     ["etc/1", "etc/2/3", ["etc/2/3", "etc/2"]],
                     [
                         "etc/1", "etc/2/3/4",
                         ["etc/2/3/4", "etc/2/3", "etc/2"]
                     ]]
     for _tmp in _resultpairs:
         result = subpathsto(_tmp[0], _tmp[1])
         self.assertEquals(
             result, _tmp[2], "subpathsto(%s, %s) == %s but not %s" %
             (_tmp[0], _tmp[1], result, _tmp[2]))
    def delete(self,recursive=True, symbolic=True, force=False):
        """
        Deletes cdsl from filesystem and inventoryfile
        @param recursive: if set delete subcdsls (e.g. /host/shared/host when removing /host/shared)
        @type  recursive: Boolean
        @param symbolic: If set remove only symbolic links, if not set also remove content, too. Default: True (means only symbolic)
        @type  symbolic: Boolean
        @param force: Removes the cdsl independent from if all contents can be found or not. Default: False
        @type  force: Boolean
        """
        from comoonics.ComPath import Path
        from comoonics.cdsl import getNodeFromPath, isSubPath, commonpath, subpathsto

        if not force and not self.exists():
            raise CdslDoesNotExistException("Cdsl with source " + self.src + " does not exist, cannot delete.")
        if not force and not self.isShared() and not self.isHostdependent():
            raise CdslUnsupportedTypeException(self.type + " is not a supported cdsl type.")         
        #verify if cdsl contains other cdsl, if true delete these first
        #assume completeness of inventoryfile
        if recursive == True:
            _tmp = self.getChilds()
            for cdsl in _tmp:
                cdsl.delete(recursive=recursive, force=force, symbolic=symbolic)
        
        if self.getChilds():
            raise CdslHasChildren("Cdsl %s has children but no recursive option specified." %self.src)
        _cwd=Path()
        _cwd.pushd(self.getBasePath())
        
        #delete or move cdsl from filesystem first is from second to if second=None it is removed
        _delpaths=list()
        _movepairs=dict()
        _delpaths2=list()
        for _path in self.getSourcePaths():
            # This one is always a link to be removed
            if os.path.lexists(_path):
                _delpaths.append(_path)

        for _path in self.getDestPaths():
            if not symbolic:
                _delpaths.append(_path)
            else:            
                self.logger.debug(".delete(%s): Skipping path %s" %(self.src, _path))
                if self.isShared():
                    _movepairs[_path]=self.src
                else:
                    _nodeid=getNodeFromPath(_path, self.cdslRepository, not force)
                    _movepairs[_path]="%s.%s" %(self.src, _nodeid)
#                _delpaths[_path]=self.
                
        # tidy up the rest
        # Means:
        # * if we have siblings: clean up to longest common path with all siblings
        prefixes=list()
        if self.isHostdependent():
            for nodename in self.getNodenames():
                prefixes.append(os.path.join(self.cdslRepository.getTreePath(), nodename))
        elif self.isShared():
            prefixes.append(self.cdslRepository.getSharedTreepath())
        
        # Let's find our parent of same type
#        parent2nd=None
#        if self.getParent()!=None and self.getParent().getParent() != None:
#            parent2nd=self.getParent().getParent()        parent2nd=None
        parent=self.getParent()
            
        subpaths=list()
        siblings=self.getSiblings()
        if len(siblings) > 0:
            longestcommon=""
            for sibling in siblings:
                common=commonpath(self.src, sibling.src)
#                while common and common != longestcommon:
                if isSubPath(common, longestcommon):
                    longestcommon=common
            for _path in subpathsto(longestcommon, self.src):
                subpaths.append(_path)
        # * if we have a parent of same type and no siblings:  clean up to parent
#        elif parent2nd != None:
#            for _path in subpathsto(parent2nd.src, self.src):
#                subpaths.append(_path)
        # * if we don't have a parent and no siblings:  clean up to root+mountpoint
        # * if we have a parent of same type and no siblings:  clean up to parent
        elif parent != None and parent.getParent() != None:
            for _path in subpathsto(parent.src, self.src):
                subpaths.append(_path)
        else:
            for _path in subpathsto("", self.src):
                subpaths.append(_path)
                
        for path in subpaths:
            if str(path) != str(self.src):
                for prefix in prefixes:
                    if os.path.lexists(os.path.join(prefix, path)):
                        _delpaths2.append(os.path.join(prefix, path))
            
        self.logger.debug("delpaths2: %s" %_delpaths2)
                        
        self.logger.debug("delete: cwd: %s" %_cwd)
        self._removePath(_delpaths)
        for _from, _to in _movepairs.items():
            if not _to:
                self._removePath(_from)
            else:
#                for _delpath in _delpaths:
#                    if os.path.samefile(_delpath, _to):
#                        _delpaths.remove(_delpath)
                if os.path.islink(_to):
                    self._removePath(_to)
                shutil.move(_from, _to)
                # We must remove paths from the delpaths that have been moved to as they are
#                for _delpath in _delpaths:
#                    if os.path.samefile(_to, _delpath):
#                        _delpaths.remove(_delpath)
        self._removePath(_delpaths2)
        _deleted=ComSystem.execMethod(self.cdslRepository.delete, self)
        _cwd.popd()
        self.logger.debug("Delete CDSL from Inventoryfile")
        #delete cdsl also from xml inventory file
        #self.cdslRepository.delete(self)
        return _deleted
    def delete(self, recursive=True, force=True):
        """
        Deletes cdsl from filesystem and inventoryfile
        @param force: If not set remove only symbolic links, if set remove content, too
        @type force: Boolean
        @param recursive: if set delete subcdsls (e.g. /host/shared/host when removing /host/shared)
        @type recursive: Boolean
        """
        from comoonics.ComPath import Path
        from comoonics.cdsl import getNodeFromPath, isSubPath, commonpath, subpathsto

        if not self.exists():
            raise CdslDoesNotExistException("Cdsl with source " + self.src +
                                            " does not exist, cannot delete.")
        if not self.isShared() and not self.isHostdependent():
            raise CdslUnsupportedTypeException(
                self.type + " is not a supported cdsl type.")
        #verify if cdsl contains other cdsl, if true delete these first
        #assume completeness of inventoryfile
        if recursive == True:
            _tmp = self.getChilds()
            for cdsl in _tmp:
                cdsl.delete(recursive, force)

        _cwd = Path()
        _cwd.pushd(self.getBasePath())

        #delete or move cdsl from filesystem first is from second to if second=None it is removed
        _delpaths = list()
        _movepairs = dict()
        _delpaths2 = list()
        for _path in self.getSourcePaths():
            # This one is always a link to be removed
            _delpaths.append(_path)

        for _path in self.getDestPaths():
            if force:
                _delpaths.append(_path)
            else:
                self.logger.debug(".delete(%s): Skipping path %s" %
                                  (self.src, _path))
                if self.isShared():
                    _movepairs[_path] = self.src
                else:
                    _nodeid = getNodeFromPath(_path, self.cdslRepository)
                    _movepairs[_path] = "%s.%s" % (self.src, _nodeid)
#                _delpaths[_path]=self.

# tidy up the rest
# Means:
# * if we have siblings: clean up to longest common path with all siblings
        prefixes = list()
        if self.isHostdependent():
            for nodename in self.getNodenames():
                prefixes.append(
                    os.path.join(self.cdslRepository.getTreePath(), nodename))
        elif self.isShared():
            prefixes.append(self.cdslRepository.getSharedTreepath())

        # Let's find our parent of same type
        parent2nd = None
        if self.getParent() != None and self.getParent().getParent() != None:
            parent2nd = self.getParent().getParent()

        subpaths = list()
        if len(self.getSiblings()) > 0:
            longestcommon = ""
            for sibling in self.getSiblings():
                common = commonpath(self.src, sibling.src)
                #                while common and common != longestcommon:
                if isSubPath(common, longestcommon):
                    longestcommon = common
            for _path in subpathsto(longestcommon, self.src):
                subpaths.append(_path)
        # * if we have a parent of same type and no siblings:  clean up to parent
        elif parent2nd != None:
            for _path in subpathsto(parent2nd.src, self.src):
                subpaths.append(_path)
        # * if we don't have a parent and no siblings:  clean up to root+mountpoint
        else:
            for _path in subpathsto("", self.src):
                subpaths.append(_path)

        for path in subpaths:
            if str(path) != str(self.src):
                for prefix in prefixes:
                    _delpaths2.append(os.path.join(prefix, path))

        self.logger.debug("delpaths2: %s" % _delpaths2)

        self.logger.debug("delete: cwd: %s" % _cwd)
        self._removePath(_delpaths)
        for _from, _to in _movepairs.items():
            if not _to:
                self._removePath(_from)
            else:
                #                for _delpath in _delpaths:
                #                    if os.path.samefile(_delpath, _to):
                #                        _delpaths.remove(_delpath)
                if os.path.islink(_to):
                    self._removePath(_to)
                shutil.move(_from, _to)
                # We must remove paths from the delpaths that have been moved to as they are
#                for _delpath in _delpaths:
#                    if os.path.samefile(_to, _delpath):
#                        _delpaths.remove(_delpath)
        self._removePath(_delpaths2)
        _deleted = ComSystem.execMethod(self.cdslRepository.delete, self)
        _cwd.popd()
        self.logger.debug("Delete CDSL from Inventoryfile")
        #delete cdsl also from xml inventory file
        #self.cdslRepository.delete(self)
        return _deleted