def test_Y_CdslsDeleteNoForce(self): import shutil from comoonics.cdsl import cmpbysubdirs from comoonics.ComPath import Path wpath=Path() _cdslsrev=repository.getCdsls() _cdslsrev.sort(cmpbysubdirs) _cdslsrev.reverse() for _cdsl in _cdslsrev: # print "- %s\n" %_cdsl.src _cdsl.delete(True, False) _files2remove=list() if _cdsl.isHostdependent(): for nodeid in setupCluster.clusterinfo.getNodeIdentifiers('id'): _file="%s.%s" %(_cdsl.src, nodeid) _files2remove.append(_file) _files2remove.append("%s.%s" %(_cdsl.src, "orig")) wpath.pushd(setupCdsls.repository.workingdir) if _cdsl.isHostdependent(): shutil.move("%s.%s" %(_cdsl.src, "default"), _cdsl.src) wpath.popd() _cdsl.commit() wpath.pushd(setupCdsls.repository.workingdir) for _file in _files2remove: # print "- %s" %_file if os.path.isdir(_file): shutil.rmtree(_file) # os.removedirs(_file) else: os.remove(_file) wpath.popd() self.assertTrue(_cdsl.exists(), "%s CDSL %s does not exist although it was created before." %(_cdsl.type, _cdsl)) for __cdsl in setupCdsls.repository.getCdsls(): self.assertTrue(__cdsl.exists(), "The still existant %s cdsl %s does not exist any more." %(__cdsl.type, __cdsl))
def setupCDSLInfrastructure(self, path, cdslRepository, clusterinfo): from comoonics.ComPath import Path self.repository.buildInfrastructure(clusterinfo) self._createRepository(clusterinfo) wpath=Path() wpath.pushd(self.repository.workingdir) if os.path.isdir(cdslRepository.getLinkPath()): os.rmdir(cdslRepository.getLinkPath()) os.symlink(os.path.join(cdslRepository.getTreePath(), self.mynodeid), cdslRepository.getLinkPath()) self._createCDSLFiles(path) wpath.popd()
def cleanUpInfrastructure(self, path, cdslRepository, clusterinfo): from comoonics.ComPath import Path for cdsl in self.repository.getCdsls(): if cdsl.exists(): cdsl.delete(True, True) wpath=Path() wpath.pushd(self.repository.workingdir) if os.path.islink(cdslRepository.getLinkPath()): os.remove(cdslRepository.getLinkPath()) os.mkdir(cdslRepository.getLinkPath()) cdslRepository.removeInfrastructure(clusterinfo) wpath.popd()
def __init__(self): super(SetupCDSLRepository, self).__init__() from comoonics.cdsl.ComCdslRepository import ComoonicsCdslRepository from comoonics.cdsl import getCdslRepository from comoonics.ComPath import Path import shutil os.mkdir(os.path.join(tmppath, "repo1")) os.mkdir(os.path.join(tmppath, "repo1/repo2")) os.mkdir(os.path.join(tmppath, "repo1/repo2/repo3")) os.mkdir(os.path.join(tmppath, "repo4")) os.mkdir(os.path.join(tmppath, "repo4/repo5")) os.mkdir(os.path.join(tmppath, "repo4/repo5/repo6")) os.mkdir(os.path.join(tmppath, "repo7")) os.mkdir(os.path.join(tmppath, "repo8")) os.mkdir(os.path.join(tmppath, "repo8/repo9")) os.mkdir(os.path.join(tmppath, "repo8/repo9/repo10")) # Need for testing migration!! # Will be created inside test not here!! os.makedirs(os.path.join(tmppath, "repo7", "var/lib/cdsl")) shutil.copyfile(os.path.join(testpath, "cdsl4.xml"), os.path.join(tmppath, "repo7", ComoonicsCdslRepository.default_resources[1])) wpath=Path() wpath.pushd(tmppath) self.cdslRepositories= [] repopath=os.path.join(tmppath, "repo1") cdslRepository1 = getCdslRepository(root=repopath, usenodeids="True") cdslRepository2 = getCdslRepository(root=repopath, mountpoint="repo2", usenodeids="True") cdslRepository1.addRepository(cdslRepository2) cdslRepository3 = getCdslRepository(root=os.path.join(repopath,"repo2"), mountpoint="repo3", usenodeids="True") cdslRepository2.addRepository(cdslRepository3) self.cdslRepositories.append(cdslRepository1) repopath=os.path.join(tmppath, "repo4") cdslRepository1 = getCdslRepository(root=repopath, mountpoint="", usenodeids="True", maxnodeidnum="4") cdslRepository2 = getCdslRepository(root=repopath, mountpoint="repo5", usenodeids="True", maxnodeidnum="4") cdslRepository1.addRepository(cdslRepository2) cdslRepository3 = getCdslRepository(root=os.path.join(repopath, "repo5"), mountpoint="repo6", usenodeids="True", maxnodeidnum="4") cdslRepository2.addRepository(cdslRepository3) self.cdslRepositories.append(cdslRepository1) repopath=os.path.join(tmppath, "repo8") cdslRepository1 = getCdslRepository(root=repopath, usenodeids="True") cdslRepository2 = getCdslRepository(root=repopath, mountpoint="repo9", usenodeids="True") cdslRepository1.addRepository(cdslRepository2) cdslRepository3 = getCdslRepository(root=os.path.join(repopath,"repo9"), mountpoint="repo10", usenodeids="True") cdslRepository2.addRepository(cdslRepository3) self.cdslRepositories.append(cdslRepository1) wpath.popd()
def getParent(self, _path=None): """ This method calls itself recursively on the dirpart of the cdsl until it finds a cdsl in the repo. This is returned and the "directest" parent. @param _path: if path is given the path is taken instead of this cdsl itself. @type _path: string @return: eReturns the parent CDSL @rtype: ComoonicsCdsl """ import os.path from comoonics.ComPath import Path from comoonics.cdsl import stripleadingsep from comoonics.cdsl.ComCdslRepository import CdslNotFoundException if _path == None: _path = os.path.dirname(self.src) self.logger.debug("getParent(path: %s)" % _path) if not _path or _path.strip() == "" or os.path.normpath( stripleadingsep( os.path.join( self.cdslRepository.root, self.cdslRepository.getMountpoint()))) == _path: return None try: cwd = Path() cwd.pushd(self.cdslRepository.workingdir) _cdsl = self.cdslRepository.getCdsl(_path) cwd.popd() return _cdsl except CdslNotFoundException: cwd.popd() return self.getParent(os.path.dirname(_path))
def __init__(self, element, doc): """ default constructor: __init__(element, doc) """ super(PathModificationset, self).__init__(element, doc) try: __path = element.getElementsByTagName('path')[0] except Exception: raise ComException("Path for modificationset \"%s\" not defined" % self.getAttribute("name", "unknown")) self.path = Path(__path, doc) self.createModificationsList( self.path.getElement().getElementsByTagName("modification"), doc) self.addToUndoMap(self.path.__class__.__name__, "pushd", "popd")
def testPushPopD(self): oldcwd = os.getcwd() path = Path() self.assertEquals( path.getPath(), oldcwd, "Currentpath %s does not equals Path currentpath %s." % (path, oldcwd)) path.pushd(self.tmpdir) self.assertEquals( os.path.realpath(path.getPath()), self.tmpdir, "Currentpath %s does not equals Path currentpath %s." % (path, self.tmpdir)) for _dir in self.dirs: olddir = path.getPath() print "%s <==> %s" % (olddir, _dir) path.pushd(_dir) self.assertEquals( path.getPath(), _dir, "Currentpath %s does not equals Path currentpath %s." % (path, _dir)) _dir2 = path.popd() self.assertEquals( _dir2, olddir, "Currentpath %s does not equals Path currentpath %s." % (_dir2, olddir)) _dir2 = path.popd() self.assertEquals( _dir2, oldcwd, "Currentpath %s does not equals Path currentpath %s." % (_dir2, oldcwd))
def _get_mkisofs_command(self): _knownopts = ["bootcd", "cdlabel"] _opts = ["-o " + self.isoname, "-J", "-R", "-D"] if self.getProperties() and self.getProperties().has_key("bootcd"): if self.getProperties().getProperty( "bootcd").getValue() == "livecd": _opts.append("-no-emul-boot") _opts.append("-boot-info-table") _opts.append("-boot-load-size 4") _opts.append("-b boot/isolinux.bin") _opts.append("-c boot/isolinux.boot") if self.getProperties() and self.getProperties().has_key("cdlabel"): key = self.getProperties().getProperty("cdlabel") _opts.append("-A '%s'" % key.getValue()) _opts.append("-V '%s'" % key.getValue()) for _property in self.getProperties().keys(): if _property in _knownopts: continue _value = self.getProperties()[_property].getValue() if _value == "": _opts.append("-%s" % _property) else: _opts.append("-%s %s" % (_property, _value)) for _element in self.pathlist: _opts.append(Path(_element, self.getDocument()).getPath()) return CMD_MKISOFS + " " + " ".join(_opts)
def getParent(self, _path=None): """ This method calls itself recursively on the dirpart of the cdsl until it finds a cdsl in the repo. This is returned and the "directest" parent. @param _path: if path is given the path is taken instead of this cdsl itself. @type _path: string @return: eReturns the parent CDSL @rtype: ComoonicsCdsl """ import os.path from comoonics.ComPath import Path from comoonics.cdsl import stripleadingsep from comoonics.cdsl.ComCdslRepository import CdslNotFoundException if _path == None: _path=os.path.dirname(self.src) self.logger.debug("getParent(path: %s)" %_path) if not _path or _path.strip() == "" or os.path.normpath(stripleadingsep(os.path.join(self.cdslRepository.root, self.cdslRepository.getMountpoint()))) == _path: return None try: cwd=Path() cwd.pushd(self.cdslRepository.workingdir) _cdsl=self.cdslRepository.getCdsl(_path) cwd.popd() return _cdsl except CdslNotFoundException: cwd.popd() return self.getParent(os.path.dirname(_path))
def findParent(self, path=None): """ This method calls itself recursively on the dirpart of the cdsl until it finds a cdsl in the repo. This is returned and the "directest" parent. @param path: if path is given the path is taken instead of this cdsl itself. @type path: string @return: eReturns the parent CDSL @rtype: ComoonicsCdsl """ import os.path from comoonics.ComPath import Path from comoonics.cdsl import stripleadingsep from comoonics.cdsl.ComCdslRepository import CdslNotFoundException # getParent is always called from a already stripped cdsl!! stripsource=False if path == None: path=os.path.dirname(self.src) # self.logger.debug("getParent(path: %s)" %_path) if not path or path.strip() == "" or os.path.normpath(stripleadingsep(os.path.join(self.cdslRepository.root, self.cdslRepository.getMountpoint()))) == path: return None try: cwd=Path() cwd.pushd(self.cdslRepository.workingdir) cdsl=self.cdslRepository.getCdsl(src=path, repository=self.cdslRepository, stripsource=stripsource) cwd.popd() return cdsl except CdslNotFoundException: cwd.popd() return self.getParent(os.path.dirname(path))
def walk(self, **keys): from comoonics.ComPath import Path from comoonics.cdsl.ComCdslRepository import ComoonicsCdslRepository # Note that listdir and error are globals in this module due # to earlier import-*. cwd=Path() cwd.pushd(os.path.join(self.cdslrepository.workingdir, self.cdslrepository.getLinkPath())) if not keys.get("onfilesystem", False): for cdsl in self.cdslrepository.walkCdsls(keys.get("clusterinfo"), keys.get("cdsls", []), ComoonicsCdslRepository.guessonerror): yield cdsl else: _pathhead, _pathtail=os.path.split(keys.get("path", ".")) if not _pathhead and not _pathtail or _pathtail==".": _pathhead=_pathtail _pathtail=None for cdsl in self.walkdir(_pathhead, _pathtail, keys.get("recursive", True)): yield cdsl # self.cdslrepository.workingdir.popd() cwd.popd()
def __init__(self, *params, **kwds): """ Supported constructors __init__(element, doc) __init__(path, source=True|False) __init__(path=path, source=True, dest=True) """ PathCopyObject.logger.debug("__init__()") if (len(params) == 2 and not isinstance(params[0], xml.dom.Node)) or ( kwds and kwds.has_key("path") and (kwds.has_key("source") or kwds.has_key("dest"))): _path = None _source = (len(params) == 2 and params[1] == True) or ( kwds and kwds.has_key("source") and kwds["source"] == True) if len(params) >= 1: _path = params[0] else: _path = kwds["path"] _tagname = "destination" if _source: _tagname = "source" doc = xml.dom.getDOMImplementation().createDocument( None, _tagname, None) element = doc.documentElement element.setAttribute("type", "path") if isinstance(_path, basestring): _path = Path(_path) element.appendChild(_path.getElement()) self.path = _path CopyObjectJournaled.__init__(self, element, doc) else: element = params[0] doc = params[1] CopyObjectJournaled.__init__(self, element, doc) try: __path = element.getElementsByTagName('path')[0] self.path = Path(__path, doc) except Exception: raise ComException("Path for copyobject \"%s\" not defined" % self.getAttribute("name", "unknown")) PathCopyObject.logger.debug("__init__(%s)" % self.path) self.addToUndoMap(self.path.__class__.__name__, "pushd", "popd")
def testMigration(self): import comoonics.cdsl.migration from comoonics.cdsl.ComCdslRepository import ComoonicsCdslRepository from comoonics import XmlTools from comoonics.cdsl import stripleadingsep fromsource=os.path.join(baseSetup.testpath, "cdsl4.xml") cwd=Path() cwd.pushd(baseSetup.tmppath) repository=comoonics.cdsl.migration.migrate(None, ComoonicsCdslRepository.version, fromresource=fromsource, root=baseSetup.tmppath, mountpoint="repo8", ignoreerrors=True) oldelement=XmlTools.parseXMLFile(fromsource) wanttocdsls=oldelement.documentElement.getElementsByTagName("cdsl") for i in range(len(wanttocdsls)): wanttocdsl=wanttocdsls[i] src=stripleadingsep(wanttocdsl.getAttribute("src")) iscdsl=repository.getCdsl(src) self.assertTrue(wanttocdsl.getAttribute("timestamp") == iscdsl.getAttribute("timestamp") and \ wanttocdsl.getAttribute("type") == iscdsl.getAttribute("type"), \ "Cdsl %s has different timestamp or type after migration" %iscdsl) os.remove(os.path.join(repository.root, repository.getMountpoint(), repository.resource)) cwd.popd()
def test_Y_CdslsDeleteNoForce(self): import shutil from comoonics.cdsl import cmpbysubdirs from comoonics.ComPath import Path wpath = Path() _cdslsrev = repository.getCdsls() _cdslsrev.sort(cmpbysubdirs) _cdslsrev.reverse() for _cdsl in _cdslsrev: # print "- %s\n" %_cdsl.src _cdsl.delete(recursive=True, force=False, symbolic=True) _files2remove = list() if _cdsl.isHostdependent(): for nodeid in setupCluster.clusterInfos[0].getNodeIdentifiers("id"): _file = "%s.%s" % (_cdsl.src, nodeid) _files2remove.append(_file) _files2remove.append("%s.%s" % (_cdsl.src, "default")) wpath.pushd(setupCdsls.repository.workingdir) for _file in _files2remove: # print "- %s" %_file if os.path.isdir(_file): shutil.rmtree(_file) # os.removedirs(_file) else: os.remove(_file) wpath.popd() self.assertFalse( _cdsl.exists(), "%s CDSL %s does exist although it was removed before." % (_cdsl.type, _cdsl) ) for __cdsl in setupCdsls.repository.getCdsls(): self.assertTrue( __cdsl.exists(), "The still existent %s cdsl %s does not exist any more. Deleted %s" % (__cdsl.type, __cdsl, _cdsl), ) setupCdsls._createRepository(setupCluster.clusterInfos[0]) setupCdsls._createCDSLFiles(setup.tmppath) _cdsls = repository.getCdsls() _cdsls.sort(cmpbysubdirs) for _cdsl in _cdsls: _cdsl.commit(force=True)
def __init__(self, *params, **kwds): """ Supported constructors __init__(element, doc) __init__(path, source=True|False) __init__(path=path, source=True, dest=True) """ PathCopyObject.logger.debug("__init__()") if (len(params) == 2 and not isinstance(params[0], xml.dom.Node)) or ( kwds and kwds.has_key("path") and (kwds.has_key("source") or kwds.has_key("dest")) ): _path = None _source = (len(params) == 2 and params[1] == True) or ( kwds and kwds.has_key("source") and kwds["source"] == True ) if len(params) >= 1: _path = params[0] else: _path = kwds["path"] _tagname = "destination" if _source: _tagname = "source" doc = xml.dom.getDOMImplementation().createDocument(None, _tagname, None) element = doc.documentElement element.setAttribute("type", "path") if isinstance(_path, basestring): _path = Path(_path) element.appendChild(_path.getElement()) self.path = _path CopyObjectJournaled.__init__(self, element, doc) else: element = params[0] doc = params[1] CopyObjectJournaled.__init__(self, element, doc) try: __path = element.getElementsByTagName("path")[0] self.path = Path(__path, doc) except Exception: raise ComException('Path for copyobject "%s" not defined' % self.getAttribute("name", "unknown")) PathCopyObject.logger.debug("__init__(%s)" % self.path) self.addToUndoMap(self.path.__class__.__name__, "pushd", "popd")
def testMigration(self): import comoonics.cdsl.migration from comoonics.cdsl.ComCdslRepository import ComoonicsCdslRepository from comoonics import XmlTools from comoonics.cdsl import stripleadingsep fromsource = os.path.join(baseSetup.testpath, "cdsl4.xml") cwd = Path() cwd.pushd(baseSetup.tmppath) repository = comoonics.cdsl.migration.migrate( None, ComoonicsCdslRepository.version, fromresource=fromsource, root=baseSetup.tmppath, mountpoint="repo8", ignoreerrors=True) oldelement = XmlTools.parseXMLFile(fromsource) wanttocdsls = oldelement.documentElement.getElementsByTagName("cdsl") for i in range(len(wanttocdsls)): wanttocdsl = wanttocdsls[i] src = stripleadingsep(wanttocdsl.getAttribute("src")) iscdsl = repository.getCdsl(src) self.assertTrue(wanttocdsl.getAttribute("timestamp") == iscdsl.getAttribute("timestamp") and \ wanttocdsl.getAttribute("type") == iscdsl.getAttribute("type"), \ "Cdsl %s has different timestamp or type after migration" %iscdsl) os.remove( os.path.join(repository.root, repository.getMountpoint(), repository.resource)) cwd.popd()
def setupCDSLInfrastructure(self, path, cdslRepository, clusterinfo): from comoonics.cdsl.ComCdsl import Cdsl from comoonics.cdsl.ComCdslRepository import CdslNotFoundException from comoonics.ComPath import Path self.repository.buildInfrastructure(clusterinfo) _dirs=self.results.keys() _dirs.sort() for _path in _dirs: _cdsl=None try: _cdsl=self.repository.getCdsl(_path) except CdslNotFoundException: if self.results[_path][1] == True: _cdsl=Cdsl(_path, Cdsl.HOSTDEPENDENT_TYPE, self.repository, clusterinfo) elif self.results[_path][1] == False: _cdsl=Cdsl(_path, Cdsl.SHARED_TYPE, self.repository, clusterinfo) if _cdsl: self.repository.commit(_cdsl) wpath=Path() wpath.pushd(self.repository.workingdir) if os.path.isdir(cdslRepository.getLinkPath()): os.rmdir(cdslRepository.getLinkPath()) os.symlink(os.path.join(cdslRepository.getTreePath(), self.mynodeid), cdslRepository.getLinkPath()) self._createCDSLFiles(path) wpath.popd()
def __init__(self, clusterinfo): super(SetupCDSLRepository, self).__init__() from comoonics.cdsl.ComCdslRepository import ComoonicsCdslRepository from comoonics.ComPath import Path import shutil os.mkdir(os.path.join(tmppath, "repo2")) os.mkdir(os.path.join(tmppath, "repo2/repo3")) os.mkdir(os.path.join(tmppath, "repo4")) os.mkdir(os.path.join(tmppath, "repo4/repo5")) os.mkdir(os.path.join(tmppath, "repo4/repo5/repo6")) os.mkdir(os.path.join(tmppath, "repo7")) os.mkdir(os.path.join(tmppath, "repo8")) # Need for testing migration!! os.makedirs(os.path.join(tmppath, "repo7", "var/lib/cdsl")) shutil.copyfile(os.path.join(testpath, "cdsl4.xml"), os.path.join(tmppath, "repo7", ComoonicsCdslRepository.default_resources[1])) wpath=Path() wpath.pushd(tmppath) self.cdslRepository1 = ComoonicsCdslRepository(clusterinfo=clusterinfo, root=tmppath, usenodeids="True") self.cdslRepository2 = ComoonicsCdslRepository(clusterinfo=clusterinfo, root=tmppath, mountpoint="repo2", usenodeids="True") self.cdslRepository1.addRepository(self.cdslRepository2) self.cdslRepository3 = ComoonicsCdslRepository(clusterinfo=clusterinfo, root=os.path.join(tmppath,"repo2"), mountpoint="repo3", usenodeids="True") self.cdslRepository2.addRepository(self.cdslRepository3) self.cdslRepository4 = ComoonicsCdslRepository(root=tmppath, mountpoint="repo4", usenodeids="True", maxnodeidnum="4") self.cdslRepository5 = ComoonicsCdslRepository(root=os.path.join(tmppath, "repo4"), mountpoint="repo5", usenodeids="True", maxnodeidnum="4") self.cdslRepository4.addRepository(self.cdslRepository5) self.cdslRepository6 = ComoonicsCdslRepository(root=os.path.join(tmppath, "repo4", "repo5"), mountpoint="repo6", usenodeids="True", maxnodeidnum="4") self.cdslRepository5.addRepository(self.cdslRepository6) wpath.popd()
def __init__(self, element, doc): """ default constructor: __init__(element, doc) """ super(PathModificationset, self).__init__(element, doc) try: __path=element.getElementsByTagName('path')[0] except Exception: raise ComException("Path for modificationset \"%s\" not defined" %self.getAttribute("name", "unknown")) self.path=Path(__path, doc) self.createModificationsList(self.path.getElement().getElementsByTagName("modification"), doc) self.addToUndoMap(self.path.__class__.__name__, "pushd", "popd")
def _testXML(self, _xml): import comoonics.XmlTools from comoonics.ComPath import Path from comoonics.enterprisecopy import ComModification _doc = comoonics.XmlTools.parseXMLString(_xml) _path=Path(_doc.documentElement, _doc) _path.mkdir() _path.pushd(_path.getPath()) for _modification in _doc.documentElement.getElementsByTagName("modification"): try: _modification=ComModification.getModification(_modification, _doc) _modification.doModification() except Exception, e: self.assert_("Caught exception %s during Catif modification %s" %(e, _modification))
def _testXML(self, _xml): import comoonics.XmlTools from comoonics.ComPath import Path from comoonics.enterprisecopy import ComModification _doc = comoonics.XmlTools.parseXMLString(_xml) _path = Path(_doc.documentElement, _doc) _path.mkdir() _path.pushd(_path.getPath()) for _modification in _doc.documentElement.getElementsByTagName( "modification"): try: _modification = ComModification.getModification( _modification, _doc) _modification.doModification() except Exception, e: self.assert_( "Caught exception %s during Catif modification %s" % (e, _modification))
class PathModificationset(ModificationsetJournaled): """ implementation class for this modificationset """ __logStrLevel__ = "comoonics.enterprisecopy.ComPathModificationset.PathModificationset" logger = ComLog.getLogger(__logStrLevel__) def __init__(self, element, doc): """ default constructor: __init__(element, doc) """ super(PathModificationset, self).__init__(element, doc) try: __path = element.getElementsByTagName('path')[0] except Exception: raise ComException("Path for modificationset \"%s\" not defined" % self.getAttribute("name", "unknown")) self.path = Path(__path, doc) self.createModificationsList( self.path.getElement().getElementsByTagName("modification"), doc) self.addToUndoMap(self.path.__class__.__name__, "pushd", "popd") def doPre(self): import os self.path.mkdir() self.path.pushd(self.path.getPath()) self.journal(self.path, "pushd") PathModificationset.logger.debug("doPre() CWD: " + os.getcwd()) super(PathModificationset, self).doPre() def doPost(self): import os super(PathModificationset, self).doPost() oldpath = self.path.getPath() self.replayJournal() self.commitJournal() #os.chdir(self.cwd) #umount Filesystem #if self.umountfs: # self.filesystem.umountDir(self.mountpoint) PathModificationset.logger.debug("doPost() CWD: " + os.getcwd())
class PathModificationset(ModificationsetJournaled): """ implementation class for this modificationset """ __logStrLevel__="comoonics.enterprisecopy.ComPathModificationset.PathModificationset" logger=ComLog.getLogger(__logStrLevel__) def __init__(self, element, doc): """ default constructor: __init__(element, doc) """ super(PathModificationset, self).__init__(element, doc) try: __path=element.getElementsByTagName('path')[0] except Exception: raise ComException("Path for modificationset \"%s\" not defined" %self.getAttribute("name", "unknown")) self.path=Path(__path, doc) self.createModificationsList(self.path.getElement().getElementsByTagName("modification"), doc) self.addToUndoMap(self.path.__class__.__name__, "pushd", "popd") def doPre(self): import os self.path.mkdir() self.path.pushd(self.path.getPath()) self.journal(self.path, "pushd") PathModificationset.logger.debug("doPre() CWD: " + os.getcwd()) super(PathModificationset, self).doPre() def doPost(self): import os super(PathModificationset, self).doPost() oldpath=self.path.getPath() self.replayJournal() self.commitJournal() #os.chdir(self.cwd) #umount Filesystem #if self.umountfs: # self.filesystem.umountDir(self.mountpoint) PathModificationset.logger.debug("doPost() CWD: " + os.getcwd())
def walk(self, **keys): from comoonics.ComPath import Path from comoonics.cdsl.ComCdslRepository import ComoonicsCdslRepository # Note that listdir and error are globals in this module due # to earlier import-*. cwd = Path() cwd.pushd( os.path.join(self.cdslrepository.workingdir, self.cdslrepository.getLinkPath())) if not keys.get("onfilesystem", False): for cdsl in self.cdslrepository.walkCdsls( keys.get("clusterinfo"), keys.get("cdsls", []), ComoonicsCdslRepository.guessonerror): yield cdsl else: _pathhead, _pathtail = os.path.split(keys.get("path", ".")) if not _pathhead and not _pathtail or _pathtail == ".": _pathhead = _pathtail _pathtail = None for cdsl in self.walkdir(_pathhead, _pathtail, keys.get("recursive", True)): yield cdsl # self.cdslrepository.workingdir.popd() cwd.popd()
def _getTmpdir(self): _tmpdir=self.TMPDIR %time.strftime(self.TIMEFORMAT) return Path(_tmpdir)
class PathCopyObject(CopyObjectJournaled): __logStrLevel__="comoonics.enterprisecopy.ComPathCopyObject.PathCopyObject" logger=ComLog.getLogger(__logStrLevel__) def __init__(self, *params, **kwds): """ Supported constructors __init__(element, doc) __init__(path, source=True|False) __init__(path=path, source=True, dest=True) """ PathCopyObject.logger.debug("__init__()") self.origpath=None if (len(params)==2 and not isinstance(params[0], xml.dom.Node)) or (kwds and kwds.has_key("path") and (kwds.has_key("source") or kwds.has_key("dest"))): _path=None _source=(len(params)==2 and params[1]==True) or (kwds and kwds.has_key("source") and kwds["source"]==True) if len(params)>=1: _path=params[0] else: _path=kwds["path"] _tagname="destination" if _source: _tagname="source" doc=xml.dom.getDOMImplementation().createDocument(None, _tagname, None) element=doc.documentElement element.setAttribute("type", "path") if isinstance(_path, basestring): _path=Path(_path) element.appendChild(_path.getElement()) self.path=_path CopyObjectJournaled.__init__(self, element, doc) else: element=params[0] doc=params[1] CopyObjectJournaled.__init__(self, element, doc) try: __path=element.getElementsByTagName('path')[0] self.path=Path(__path, doc) except Exception: raise ComException("Path for copyobject \"%s\" not defined" %self.getAttribute("name", "unknown")) PathCopyObject.logger.debug("__init__(%s)" %self.path) self.addToUndoMap(self.path.__class__.__name__, "pushd", "popd") def __prepare(self): import os self.origpath=self.path.getPath() ComSystem.execMethod(self.path.mkdir) ComSystem.execMethod(self.path.pushd, self.path.getPath()) if not ComSystem.isSimulate(): self.journal(self.path, "pushd") PathCopyObject.logger.debug("prepareAsSource() CWD: " + os.getcwd()) def __cleanup(self): import os oldpath=self.getPath().getPath() self.replayJournal() self.commitJournal() #os.chdir(self.cwd) #umount Filesystem #if self.umountfs: # self.filesystem.umountDir(self.mountpoint) PathCopyObject.logger.debug("__cleanup: remove: %s" %self.getPath().getAttribute("remove", "false")) self.path.remove(oldpath) PathCopyObject.logger.debug("doPost() CWD: " + os.getcwd()) self.path.setPath(self.origpath) def prepareAsSource(self): ''' prepare CopyObject as source ''' self.__prepare() def cleanupSource(self): ''' do source specific cleanup ''' self.__cleanup() def cleanupDest(self): ''' do destination specific cleanup ''' self.__cleanup() def prepareAsDest(self): ''' prepare CopyObject as destination ''' self.__prepare() def getMetaData(self): ''' returns the metadata element ''' return self.getPath().getElement() def updateMetaData(self, element): ''' updates meta data information ''' # get filesystem element try: __spath=element except Exception: raise ComException("path for metadata not defined") # save dest attributes __attr = self.getPath().getElement().attributes # copy all source fs info to dest self.setPathElement(__spath) # restore saved attibutes from dest self.getPath().setAttributes(__attr) def getPath(self): return self.path def setPath(self, filesystem): self.setPathElement(filesystem.getElement()) def setPathElement(self, element): __parent=self.path.getElement().parentNode __newnode=element.cloneNode(True) __oldnode=self.path.getElement() self.path.setElement(__newnode) # only replace attributes try: __parent.replaceChild(__newnode, __oldnode) except Exception, e: ComLog.getLogger(PathCopyObject.__logStrLevel__).warning(e)
class test_CdslRepository(unittest.TestCase): cwd = Path(os.getcwd()) def setUp(self): pass def tearDown(self): self.cwd.popd() def testResource1(self): self.assertEquals(setupCDSLRepository.cdslRepository1.getResource(), ".cdsl_inventory.xml") def testResource2(self): """ Test without clusterinfo """ self.assertEquals(setupCDSLRepository.cdslRepository4.getResource(), ".cdsl_inventory.xml") def testTreePath(self): self.assertEquals(setupCDSLRepository.cdslRepository1.getTreePath(), ".cluster/cdsl") def testSharedtreePath(self): """ @rtype: string """ self.assertEquals( setupCDSLRepository.cdslRepository1.getSharedTreepath(), ".cluster/shared") def testLinkPath(self): """ @rtype: string """ self.assertEquals(setupCDSLRepository.cdslRepository1.getLinkPath(), ".cdsl.local") def testMountpoint1(self): """ @rtype: string """ self.assertEquals(setupCDSLRepository.cdslRepository1.getMountpoint(), "") def testMountpoint2(self): """ Test without clusterinfo @rtype: string """ self.assertEquals(setupCDSLRepository.cdslRepository4.getMountpoint(), "repo4") def testDefaultDir(self): """ @rtype: string """ self.assertEquals(setupCDSLRepository.cdslRepository1.getDefaultDir(), "default") def testMaxnodeidnum1(self): """ @rtype: string """ self.assertEquals( setupCDSLRepository.cdslRepository1.getMaxnodeidnum(), "0") def testMaxnodeidnum2(self): """ @rtype: string """ self.assertEquals( setupCDSLRepository.cdslRepository4.getMaxnodeidnum(), "4") def testNodePrefix(self): """ @rtype: string """ self.assertEquals(setupCDSLRepository.cdslRepository1.getNodePrefix(), "") def testUseNodeids(self): """ @rtype: string """ self.assertEquals(setupCDSLRepository.cdslRepository1.getUseNodeids(), "True") def testExpandString(self): """ @rtype: string """ self.assertEquals( setupCDSLRepository.cdslRepository1.getExpandString(), ".cdsl") def testExpand1(self): from comoonics.cdsl.ComCdsl import Cdsl from comoonics.cdsl.ComCdslRepository import CdslNotFoundException _dirs = setupCDSLRepository.results.keys() _dirs.sort() setupCDSLRepository.cdslRepository1.buildInfrastructure( setupCluster.clusterinfo) self.cwd.pushd(baseSetup.tmppath) for _path in _dirs: _cdsl = None try: _cdsl = setupCDSLRepository.cdslRepository1.getCdsl( _path, setupCDSLRepository.cdslRepository1) except CdslNotFoundException: if setupCDSLRepository.results[_path][1] == True: _cdsl = Cdsl(_path, Cdsl.HOSTDEPENDENT_TYPE, setupCDSLRepository.cdslRepository1, setupCluster.clusterinfo) elif setupCDSLRepository.results[_path][1] == False: _cdsl = Cdsl(_path, Cdsl.SHARED_TYPE, setupCDSLRepository.cdslRepository1, setupCluster.clusterinfo) if _cdsl: setupCDSLRepository.cdslRepository1.commit(_cdsl) _expanded = setupCDSLRepository.cdslRepository1.expandCdsl( _cdsl) _isexpanded = setupCDSLRepository.cdslRepository1.isExpandedDir( _expanded) _shouldnotbeexpanded = _expanded == _cdsl.src self.assertEquals( _expanded, setupCDSLRepository.results[_path][0], "Expansion of cdsl \"%s\" => \"%s\" != \"%s\"" % (_cdsl.src, _expanded, setupCDSLRepository.results[_path][0])) self.assertTrue( _isexpanded or _shouldnotbeexpanded, "Path %s=>%s should be detected as expanded but is not %s!!!" % (_cdsl.src, _expanded, _isexpanded)) setupCDSLRepository.cdslRepository1.removeInfrastructure( setupCluster.clusterinfo) self.cwd.popd() def testExpand2(self): """ Test without clusterinfo """ from comoonics.cdsl.ComCdsl import Cdsl from comoonics.cdsl.ComCdslRepository import CdslNotFoundException self.cwd.pushd(baseSetup.tmppath) _dirs = setupCDSLRepository.results.keys() _dirs.sort() setupCDSLRepository.cdslRepository4.buildInfrastructure() for _path in _dirs: _cdsl = None try: _cdsl = setupCDSLRepository.cdslRepository4.getCdsl(_path) except CdslNotFoundException: if setupCDSLRepository.results[_path][1] == True: _cdsl = Cdsl(_path, Cdsl.HOSTDEPENDENT_TYPE, setupCDSLRepository.cdslRepository4) elif setupCDSLRepository.results[_path][1] == False: _cdsl = Cdsl(_path, Cdsl.SHARED_TYPE, setupCDSLRepository.cdslRepository4) if _cdsl: setupCDSLRepository.cdslRepository4.commit(_cdsl) _expanded = setupCDSLRepository.cdslRepository4.expandCdsl( _cdsl) _isexpanded = setupCDSLRepository.cdslRepository4.isExpandedDir( _expanded) _shouldnotbeexpanded = _expanded == _cdsl.src self.assertEquals( _expanded, setupCDSLRepository.results[_path][0], "Expansion of cdsl \"%s\" => \"%s\" != \"%s\"" % (_cdsl.src, _expanded, setupCDSLRepository.results[_path][0])) self.assertTrue( _isexpanded or _shouldnotbeexpanded, "Path %s=>%s should be detected as expanded but is not %s!!!" % (_cdsl.src, _expanded, _isexpanded)) setupCDSLRepository.cdslRepository4.removeInfrastructure() self.cwd.popd() def testBuildInfrastructure1(self): setupCDSLRepository.cdslRepository1.buildInfrastructure( setupCluster.clusterinfo) self.cwd.pushd( os.path.join(baseSetup.tmppath, setupCDSLRepository.cdslRepository1.getMountpoint())) self.assertTrue( os.path.isdir(setupCDSLRepository.cdslRepository1.getTreePath()), "Cdsltree %s is no directory!" % setupCDSLRepository.cdslRepository1.getTreePath()) self.assertEquals( setupCDSLRepository.cdslRepository1.getTreePath(True), os.path.realpath( os.path.join( baseSetup.tmppath, setupCDSLRepository.cdslRepository1.getTreePath())), "Cdsltree %s!=%s." % (setupCDSLRepository.cdslRepository1.getTreePath(True), os.path.join(baseSetup.tmppath, setupCDSLRepository.cdslRepository1.getTreePath()))) self.assertTrue( os.path.isdir( setupCDSLRepository.cdslRepository1.getSharedTreepath()), "Cdsl sharedtree %s is no directory!" % setupCDSLRepository.cdslRepository1.getSharedTreepath()) self.assertTrue( os.path.isdir(setupCDSLRepository.cdslRepository1.getLinkPath()), "Cdsl link %s is no directory" % setupCDSLRepository.cdslRepository1.getLinkPath()) for _node in setupCluster.clusterinfo.getNodes(): self.assertTrue( os.path.isdir( os.path.join( setupCDSLRepository.cdslRepository1.getTreePath(), _node.getId())), "Cdsl Nodedir %s is no directory!" % os.path.join(setupCDSLRepository.cdslRepository1.getTreePath(), _node.getId())) setupCDSLRepository.cdslRepository1.removeInfrastructure( setupCluster.clusterinfo) self.assertFalse( os.path.exists(setupCDSLRepository.cdslRepository1.getResource()), "The inventory file %s exists although it shouldn't." % setupCDSLRepository.cdslRepository1.getResource()) self.assertFalse( os.path.exists(setupCDSLRepository.cdslRepository2.getResource()), "The inventory file %s exists although it shouldn't." % setupCDSLRepository.cdslRepository1.getResource()) self.assertFalse( os.path.exists(setupCDSLRepository.cdslRepository3.getResource()), "The inventory file %s exists although it shouldn't." % setupCDSLRepository.cdslRepository1.getResource()) self.assert_("Test assertion") def testBuildInfrastructure2(self): """ Test without clusterinfo """ setupCDSLRepository.cdslRepository4.buildInfrastructure() self.cwd.pushd( os.path.join(baseSetup.tmppath, setupCDSLRepository.cdslRepository4.getMountpoint())) self.assertTrue( os.path.isdir(setupCDSLRepository.cdslRepository4.getTreePath()), "Cdsltree %s is no directory!" % setupCDSLRepository.cdslRepository1.getTreePath()) self.assertEquals( setupCDSLRepository.cdslRepository4.getTreePath(True), os.path.realpath( os.path.join( baseSetup.tmppath, setupCDSLRepository.cdslRepository4.getMountpoint(), setupCDSLRepository.cdslRepository4.getTreePath())), "Cdsltree %s!=%s." % (setupCDSLRepository.cdslRepository1.getTreePath(True), os.path.join(baseSetup.tmppath, setupCDSLRepository.cdslRepository1.getTreePath()))) self.assertTrue( os.path.isdir( setupCDSLRepository.cdslRepository4.getSharedTreepath()), "Cdsl sharedtree %s is no directory!" % setupCDSLRepository.cdslRepository1.getSharedTreepath()) self.assertTrue( os.path.isdir(setupCDSLRepository.cdslRepository4.getLinkPath()), "Cdsl link %s is no directory" % setupCDSLRepository.cdslRepository1.getLinkPath()) for _node in range( 1, int(setupCDSLRepository.cdslRepository4.getMaxnodeidnum()) + 1): self.assertTrue( os.path.isdir( os.path.join( setupCDSLRepository.cdslRepository4.getTreePath(), str(_node))), "Cdsl Nodedir %s is no directory!" % os.path.join(setupCDSLRepository.cdslRepository4.getTreePath(), str(_node))) setupCDSLRepository.cdslRepository4.removeInfrastructure() self.assertFalse( os.path.exists(setupCDSLRepository.cdslRepository4.getResource()), "The inventory file %s exists although it shouldn't." % setupCDSLRepository.cdslRepository1.getResource()) self.assertFalse( os.path.exists(setupCDSLRepository.cdslRepository5.getResource()), "The inventory file %s exists although it shouldn't." % setupCDSLRepository.cdslRepository1.getResource()) self.assertFalse( os.path.exists(setupCDSLRepository.cdslRepository6.getResource()), "The inventory file %s exists although it shouldn't." % setupCDSLRepository.cdslRepository1.getResource()) def testVersionException(self): from comoonics.cdsl.ComCdslRepository import CdslVersionException, ComoonicsCdslRepository try: setupCDSLRepository.cdslRepository7 = ComoonicsCdslRepository( clusterinfo=setupCluster.clusterinfo, root=os.path.join(baseSetup.tmppath, "repo7")) self.assert_("CdslVersionException not risn. Error") except CdslVersionException: pass def testMigration(self): import comoonics.cdsl.migration from comoonics.cdsl.ComCdslRepository import ComoonicsCdslRepository from comoonics import XmlTools from comoonics.cdsl import stripleadingsep fromsource = os.path.join(baseSetup.testpath, "cdsl4.xml") cwd = Path() cwd.pushd(baseSetup.tmppath) repository = comoonics.cdsl.migration.migrate( None, ComoonicsCdslRepository.version, fromresource=fromsource, root=baseSetup.tmppath, mountpoint="repo8", ignoreerrors=True) oldelement = XmlTools.parseXMLFile(fromsource) wanttocdsls = oldelement.documentElement.getElementsByTagName("cdsl") for i in range(len(wanttocdsls)): wanttocdsl = wanttocdsls[i] src = stripleadingsep(wanttocdsl.getAttribute("src")) iscdsl = repository.getCdsl(src) self.assertTrue(wanttocdsl.getAttribute("timestamp") == iscdsl.getAttribute("timestamp") and \ wanttocdsl.getAttribute("type") == iscdsl.getAttribute("type"), \ "Cdsl %s has different timestamp or type after migration" %iscdsl) os.remove( os.path.join(repository.root, repository.getMountpoint(), repository.resource)) cwd.popd()
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 exists(self): """ Checks if the destpath file exists (no dead end) and if destpath, cdslpath and cdsllinkpath are the same file. This seems to be more complicated then expected ;-) . So here the current approach: Case 1 Not nested: If a CDSL is not nested it exists if the sourcepath, the cdsllinkpath and one destpath are exactly the same file and all other destpaths just exist. Case 2 nested and hostdependent: A CDSL exists if the resolved path is nested and the sourcepath is a cdsl shared path and the link's destpath is a hostdependent path and one resolved destpaths is the same file then the source and all other destpaths exist. Case 3 nested and shared: A CDSL exists if the resolved path is nested and the sourcepath is a cdsl hostdependent path and the link's destpath is a cdsl shared path and the resolved destpath exists. Not sure about if also all possible hostdependent sources should exist?. @return: True if this cdsl exists @rtype: Boolean """ from comoonics.ComPath import Path from comoonics.cdsl import ltrimDir, isHostdependentPath, isSharedPath _path=Path() _path.pushd(self.getBasePath()) if not os.path.exists(self.src): return False _exists=False _cdslpath=self.getCDSLPath() _cdsllinkpath=self.getCDSLLinkPath() if not os.path.islink(_cdslpath): _path.popd() return False _cdslreadpath=ltrimDir(os.readlink(_cdslpath)) if self.isNested(): # Now we start with all cases that hold for being nested if self.isShared(): # Case 2 (Hostdependent and nested) if isHostdependentPath(_cdslreadpath, self.cdslRepository) or not isSharedPath(_cdslreadpath, self.cdslRepository): _path.popd() return False elif self.isHostdependent(): # Case 3 (shared and nested) if not isHostdependentPath(_cdslreadpath, self.cdslRepository) or isSharedPath(_cdslreadpath, self.cdslRepository): _path.popd() return False _exists_once=False _exists_everywhere=True for _destpath in self.getDestPaths(): try: _exists=os.path.lexists(_destpath) and \ os.path.samefile(_destpath, _cdslpath) and \ os.path.samefile(_destpath, _cdsllinkpath) if _exists: _exists_once=True if not os.path.lexists(_destpath): _exists_everywhere=False except OSError: _exists_once=False _exists_everywhere=False _path.popd() return _exists_once and _exists_everywhere
class PathCopyObject(CopyObjectJournaled): __logStrLevel__ = "comoonics.enterprisecopy.ComPathCopyObject.PathCopyObject" logger = ComLog.getLogger(__logStrLevel__) def __init__(self, *params, **kwds): """ Supported constructors __init__(element, doc) __init__(path, source=True|False) __init__(path=path, source=True, dest=True) """ PathCopyObject.logger.debug("__init__()") if (len(params) == 2 and not isinstance(params[0], xml.dom.Node)) or ( kwds and kwds.has_key("path") and (kwds.has_key("source") or kwds.has_key("dest"))): _path = None _source = (len(params) == 2 and params[1] == True) or ( kwds and kwds.has_key("source") and kwds["source"] == True) if len(params) >= 1: _path = params[0] else: _path = kwds["path"] _tagname = "destination" if _source: _tagname = "source" doc = xml.dom.getDOMImplementation().createDocument( None, _tagname, None) element = doc.documentElement element.setAttribute("type", "path") if isinstance(_path, basestring): _path = Path(_path) element.appendChild(_path.getElement()) self.path = _path CopyObjectJournaled.__init__(self, element, doc) else: element = params[0] doc = params[1] CopyObjectJournaled.__init__(self, element, doc) try: __path = element.getElementsByTagName('path')[0] self.path = Path(__path, doc) except Exception: raise ComException("Path for copyobject \"%s\" not defined" % self.getAttribute("name", "unknown")) PathCopyObject.logger.debug("__init__(%s)" % self.path) self.addToUndoMap(self.path.__class__.__name__, "pushd", "popd") def __prepare(self): import os self.origpath = self.path.getPath() ComSystem.execMethod(self.path.mkdir) ComSystem.execMethod(self.path.pushd, self.path.getPath()) if not ComSystem.isSimulate(): self.journal(self.path, "pushd", self.path.getPath()) PathCopyObject.logger.debug("prepareAsSource() CWD: " + os.getcwd()) def __cleanup(self): import os oldpath = self.path.getPath() self.replayJournal() self.commitJournal() #os.chdir(self.cwd) #umount Filesystem #if self.umountfs: # self.filesystem.umountDir(self.mountpoint) PathCopyObject.logger.debug("__cleanup: remove: %s" % self.getAttribute("remove", "false")) self.path.remove(oldpath) PathCopyObject.logger.debug("doPost() CWD: " + os.getcwd()) self.path.setPath(self.origpath) def prepareAsSource(self): ''' prepare CopyObject as source ''' self.__prepare() def cleanupSource(self): ''' do source specific cleanup ''' self.__cleanup() def cleanupDest(self): ''' do destination specific cleanup ''' self.__cleanup() def prepareAsDest(self): ''' prepare CopyObject as destination ''' self.__prepare() def getMetaData(self): ''' returns the metadata element ''' return self.getPath().getElement() def updateMetaData(self, element): ''' updates meta data information ''' pass def getPath(self): return self.path def getMountpoint(self): return self.getPath().getElement()
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)
def exists(self): """ Checks if the destpath file exists (no dead end) and if destpath, cdslpath and cdsllinkpath are the same file. This seems to be more complicated then expected ;-) . So here the current approach: Case 1 Not nested: If a CDSL is not nested it exists if the sourcepath, the cdsllinkpath and one destpath are exactly the same file and all other destpaths just exist. Case 2 nested and hostdependent: A CDSL exists if the resolved path is nested and the sourcepath is a cdsl shared path and the link's destpath is a hostdependent path and one resolved destpaths is the same file then the source and all other destpaths exist. Case 3 nested and shared: A CDSL exists if the resolved path is nested and the sourcepath is a cdsl hostdependent path and the link's destpath is a cdsl shared path and the resolved destpath exists. Not sure about if also all possible hostdependent sources should exist?. @return: True if this cdsl exists @rtype: Boolean """ from comoonics.ComPath import Path from comoonics.cdsl import ltrimDir, isHostdependentPath, isSharedPath _path = Path() _path.pushd(self.getBasePath()) _exists = False _cdslpath = self.getCDSLPath() _cdsllinkpath = self.getCDSLLinkPath() if not os.path.islink(_cdslpath): _path.popd() return False _cdslreadpath = ltrimDir(os.readlink(_cdslpath)) if self.isNested(): # Now we start with all cases that hold for being nested if self.isShared(): # Case 2 (Hostdependent and nested) if isHostdependentPath( _cdslreadpath, self.cdslRepository) or not isSharedPath( _cdslreadpath, self.cdslRepository): _path.popd() return False elif self.isHostdependent(): # Case 3 (shared and nested) if not isHostdependentPath( _cdslreadpath, self.cdslRepository) or isSharedPath( _cdslreadpath, self.cdslRepository): _path.popd() return False _exists_once = False _exists_everywhere = True for _destpath in self.getDestPaths(): try: _exists=os.path.exists(_destpath) and \ os.path.samefile(_destpath, _cdslpath) and \ os.path.samefile(_destpath, _cdsllinkpath) if _exists: _exists_once = True if not os.path.exists(_destpath): _exists_everywhere = False except OSError: _exists_once = False _exists_everywhere = False _path.popd() return _exists_once and _exists_everywhere