def test0isSubPath(self): from comoonics.cdsl import isSubPath _resulttriples=[ [ "../../../../.cluster/shared/hostdependent/shared", "shared", True ], [ "../../../../.cluster/shared/hostdependent/shared", ".cluster", True ], [ "../../../../.cluster/shared/hostdependent/shared", "..", True ], [ "../../../../.cluster/shared/hostdependent/shared", ".cluster/shared", True ], [ "../../../../.cluster/shared/hostdependent/shared", "xyz", False ], [ "/../../../../.cluster/shared/hostdependent/shared", "shared", True ], [ "/../../../../.cluster/shared/hostdependent/shared", ".cluster", True ], [ "/../../../../.cluster/shared/hostdependent/shared", "..", True ], [ "/../../../../.cluster/shared/hostdependent/shared", ".cluster/shared", True ], [ "/../../../../.cluster/shared/hostdependent/shared", "xyz", False ], [ ".cdsl.local/hostdependent/shared", ".cdsl.local", True] ] for _tmp in _resulttriples: self.assertEquals(isSubPath(_tmp[0], _tmp[1]), _tmp[2], "isSubPath(%s, %s) == %s" %(_tmp[0], _tmp[1], _tmp[2]))
def test0isSubPath(self): from comoonics.cdsl import isSubPath _resulttriples = [ [ "../../../../.cluster/shared/hostdependent/shared", "shared", True ], [ "../../../../.cluster/shared/hostdependent/shared", ".cluster", True ], ["../../../../.cluster/shared/hostdependent/shared", "..", True], [ "../../../../.cluster/shared/hostdependent/shared", ".cluster/shared", True ], ["../../../../.cluster/shared/hostdependent/shared", "xyz", False], [ "/../../../../.cluster/shared/hostdependent/shared", "shared", True ], [ "/../../../../.cluster/shared/hostdependent/shared", ".cluster", True ], ["/../../../../.cluster/shared/hostdependent/shared", "..", True], [ "/../../../../.cluster/shared/hostdependent/shared", ".cluster/shared", True ], [ "/../../../../.cluster/shared/hostdependent/shared", "xyz", False ], [".cdsl.local/hostdependent/shared", ".cdsl.local", True] ] for _tmp in _resulttriples: self.assertEquals( isSubPath(_tmp[0], _tmp[1]), _tmp[2], "isSubPath(%s, %s) == %s" % (_tmp[0], _tmp[1], _tmp[2]))
def getChilds(self): """ Returns child CDSLs @rtype: list of ComoonicsCdsl """ from comoonics.cdsl import isSubPath _tmpcdsls = [] #_parent=self.getParent() #compare which other cdsl sources contain the whole source of this cdsl for cdsl in self.cdslRepository.getCdsls(): cdslparent=cdsl.getParent() if cdsl.src != self.src and cdslparent and self.src == cdslparent.src and isSubPath(cdsl.src, self.src): self.logger.debug("getChilds: + %s" %cdsl) _tmpcdsls.append(cdsl) return _tmpcdsls
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 commit(self,force=False): """ Commit new or changed cdsl to filesystem and inventoryfile @param force: skip Backup when set and overwrite existing files/directories/links @type force: Boolean """ from comoonics.ComPath import Path from comoonics.cdsl import isSubPath #important pathes to work with cdsls ##### # Chancel creation of cdsl if it already exists # or a cdsl of another type with same src exists # or creation is insane because underlying structure # is of same type as cdsl which should be created ##### if self.exists(): self.logger.debug("CDSL already exists, chancel commit") raise CdslAlreadyExists("Cdsl %s is already existant." %self.src) if self.isShared() and (self.getParent() == None or self.getParent().isShared()): self.logger.debug("The cdsl %s to be shared back seems to recide already in a shared area." %self.src) raise CdslOfSameType("The cdsl %s to be shared seems to recide already in a shared area." %self.src) if self.isHostdependent() and self.getParent() != None and self.getParent().isHostdependent(): self.logger.debug("The cdsl %s to be hostdependent seems to recide alreay in an hostdependent area." %self.src) raise CdslOfSameType("The cdsl %s to be hostdependent seems to recide alreay in an hostdependent area." %self.src) elif self.isShared(): if isSubPath(self.src, self.cdsltree_shared): self.logger.debug("Given source is already part of a hostdependent CDSL") raise CdslAlreadyExists("Cdsl %s is already a hostdependent cdsl." %self.src) elif self.isHostdependent(): if isSubPath(self.src, self.getCDSLLinkPath()): self.logger.debug("Given source is already part of a hostdependent CDSL") raise CdslAlreadyExists("Cdsl %s is already a shared cdsl." %self.src) _path=Path() _path.pushd(self.getBasePath()) if not os.path.exists(self.src): raise CdslDoesNotExistException("File %s for cdsl does not exist (cwd: %s). Cannot create." %(self.src, self.getBasePath())) self.cdslRepository.updateInfrastructure(nodes=self.getNodenames()) # _expanded=self.cdslRepository.expandCdsl(self) # parent=self.getParent() # if parent: # _tail=strippath(self.src, parent.src) # _expandedforparent=os.path.join(parent.cdslRepository.expandCdsl(parent), _tail) # else: # _expandedforparent=_expanded _depth = self._pathdepth(self.src) -1 if _depth > 0: if self.isShared(): _depth=_depth+self._pathdepth(self.cdsltree)+1 # 1 because we need to add the node elif self.isHostdependent() and self.getParent() != None: _depth=_depth+self._pathdepth(self.cdsltree_shared) _relativepath = _depth * "../" else: _relativepath = "" # First copy or move the files to the destpaths... for destpath in self.getDestPaths(): # Create the parentdir if it does not already exist parentdir=os.path.dirname(destpath) if not os.path.exists(parentdir): self.logger.debug("Create Directory " + parentdir) os.makedirs(parentdir) # if symlink and relative adapt to the relativness. if os.path.islink(self.src) and not self.src.startswith(os.sep): self.logger.debug("Creating link from %s => %s" %(os.path.join(_relativepath, self.src), destpath)) ComSystem.execMethod(os.symlink, os.path.join(self._pathdepth(self.getCDSLLinkPath())*"../", os.path.realpath(self.src)[1:]), destpath) else: # Let's copy the data self.logger.debug("Copy Files: " + self.src + " => " + destpath) ComSystem.execLocalStatusOutput("cp -a " + self.src + " " + destpath) # if cdsl is shared we need to copy only once. if self.isShared(): break # createdefault destination # if self.isHostdependent(): # self.logger.debug("Copy Files: " + self.src + " => " + os.path.join(self.cdslRepository.getTree(), self.cdslRepository.getDefaultPath())) # ComSystem.execLocalStatusOutput("cp -a " + self.src + " " + self.cdslRepository.getDefaultPath()) if self.isHostdependent(): if force: self.logger.debug("Removing oldfile %s" %self.src) ComSystem.execLocalStatusOutput("rm -rf %s" %self.src) elif not force: self.logger.debug("Moving %s => %s.orig" %(self.src, self.src)) ComSystem.execLocalStatusOutput("mv %s %s.orig" %(self.src, self.src)) # Now create the symlinks for sourcepath in self.getSourcePaths(): if self.isShared(): # Either backup or remove! if not force: self.logger.debug("Backup Files: " + sourcepath + " => " + sourcepath + ".orig") ComSystem.execLocalStatusOutput("mv " + sourcepath + " " + sourcepath + ".orig") else: self.logger.debug("Remove Files: " + sourcepath) if os.path.isdir(sourcepath): ComSystem.execMethod(shutil.rmtree, sourcepath) if os.path.exists(sourcepath): ComSystem.execMethod(os.remove, sourcepath) src=os.path.join(_relativepath, self.cdslRepository.getSharedTreepath(), self.cdslRepository.expandCdsl(self)) dest=sourcepath elif self.isHostdependent(): src=os.path.join(_relativepath, self.cdslRepository.getLinkPath(), self.cdslRepository.expandCdsl(self)) dest=sourcepath self.logger.debug("Creating Link: %s => %s, currentpath: %s" %(src, dest, _path)) ComSystem.execMethod(os.symlink, src, dest) _path.popd() return ComSystem.execMethod(self.cdslRepository.commit,self)
def getChilds(self): """ Returns child CDSLs @rtype: list of ComoonicsCdsl """ from comoonics.cdsl import isSubPath _tmpcdsls = [] #_parent=self.getParent() #compare which other cdsl sources contain the whole source of this cdsl for cdsl in self.cdslRepository.getCdsls(): cdslparent = cdsl.getParent() if cdsl.src != self.src and cdslparent and self.src == cdslparent.src and isSubPath( cdsl.src, self.src): self.logger.debug("getChilds: + %s" % cdsl) _tmpcdsls.append(cdsl) return _tmpcdsls
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
def commit(self, force=False): """ Commit new or changed cdsl to filesystem and inventoryfile @param force: skip Backup when set and overwrite existing files/directories/links @type force: Boolean """ from comoonics.ComPath import Path from comoonics.cdsl import isSubPath #important pathes to work with cdsls ##### # Chancel creation of cdsl if it already exists # or a cdsl of another type with same src exists # or creation is insane because underlying structure # is of same type as cdsl which should be created ##### if self.exists(): self.logger.debug("CDSL already exists, chancel commit") raise CdslAlreadyExists("Cdsl %s is already existant." % self.src) if self.isShared() and (self.getParent() == None or self.getParent().isShared()): self.logger.debug( "The cdsl %s to be shared back seems to recide already in a shared area." % self.src) raise CdslOfSameType( "The cdsl %s to be shared seems to recide already in a shared area." % self.src) if self.isHostdependent( ) and self.getParent() != None and self.getParent().isHostdependent(): self.logger.debug( "The cdsl %s to be hostdependent seems to recide alreay in an hostdependent area." % self.src) raise CdslOfSameType( "The cdsl %s to be hostdependent seems to recide alreay in an hostdependent area." % self.src) elif self.isShared(): if isSubPath(self.src, self.cdsltree_shared): self.logger.debug( "Given source is already part of a hostdependent CDSL") raise CdslAlreadyExists( "Cdsl %s is already a hostdependent cdsl." % self.src) elif self.isHostdependent(): if isSubPath(self.src, self.getCDSLLinkPath()): self.logger.debug( "Given source is already part of a hostdependent CDSL") raise CdslAlreadyExists("Cdsl %s is already a shared cdsl." % self.src) _path = Path() _path.pushd(self.getBasePath()) # _expanded=self.cdslRepository.expandCdsl(self) # parent=self.getParent() # if parent: # _tail=strippath(self.src, parent.src) # _expandedforparent=os.path.join(parent.cdslRepository.expandCdsl(parent), _tail) # else: # _expandedforparent=_expanded _depth = self._pathdepth(self.src) - 1 if _depth > 0: if self.isShared(): _depth = _depth + self._pathdepth( self.cdsltree) + 1 # 1 because we need to add the node elif self.isHostdependent() and self.getParent() != None: _depth = _depth + self._pathdepth(self.cdsltree_shared) _relativepath = _depth * "../" else: _relativepath = "" # flag to indicate if data has been backuped ignore if force is True once = False # First copy or move the files to the destpaths... for destpath in self.getDestPaths(): # Create the parentdir if it does not already exist parentdir = os.path.dirname(destpath) if not os.path.exists(parentdir): self.logger.debug("Create Directory " + parentdir) os.makedirs(parentdir) # Let's copy the data if self.isHostdependent(): # In case of hd we need to copy for each node self.logger.debug("Copy Files: " + self.src + " => " + destpath) ComSystem.execLocalStatusOutput("cp -a " + self.src + " " + destpath) else: # in case of shared we need to move or remove the files which are changed from hd to shared. if not once: self.logger.debug("Copying hd files once from " + self.src + " =>" + destpath) ComSystem.execLocalStatusOutput("cp -a %s %s" % (self.src, destpath)) once = True # createdefault destination # if self.isHostdependent(): # self.logger.debug("Copy Files: " + self.src + " => " + os.path.join(self.cdslRepository.getTree(), self.cdslRepository.getDefaultPath())) # ComSystem.execLocalStatusOutput("cp -a " + self.src + " " + self.cdslRepository.getDefaultPath()) if self.isHostdependent(): if force: self.logger.debug("Removing oldfile %s" % self.src) ComSystem.execLocalStatusOutput("rm -rf %s" % self.src) elif not force: self.logger.debug("Moving %s => %s.orig" % (self.src, self.src)) ComSystem.execLocalStatusOutput("mv %s %s.orig" % (self.src, self.src)) # Now create the symlinks for sourcepath in self.getSourcePaths(): if self.isShared(): # Either backup or remove! if not force: self.logger.debug("Backup Files: " + sourcepath + " => " + sourcepath + ".orig") ComSystem.execLocalStatusOutput("mv " + sourcepath + " " + sourcepath + ".orig") else: self.logger.debug("Remove Files: " + sourcepath) if os.path.isfile(sourcepath): ComSystem.execMethod(os.remove, sourcepath) else: ComSystem.execMethod(shutil.rmtree, sourcepath) src = os.path.join(_relativepath, self.cdslRepository.getSharedTreepath(), self.cdslRepository.expandCdsl(self)) dest = sourcepath elif self.isHostdependent(): src = os.path.join(_relativepath, self.cdslRepository.getLinkPath(), self.cdslRepository.expandCdsl(self)) dest = sourcepath self.logger.debug("Creating Link: %s => %s, currentpath: %s" % (src, dest, _path)) ComSystem.execMethod(os.symlink, src, dest) _path.popd() return ComSystem.execMethod(self.cdslRepository.commit, self)