def __init__(self,
                 src,
                 _type,
                 cdslRepository,
                 clusterinfo=None,
                 nodes=None,
                 timestamp=None,
                 ignoreerrors=False):
        """
        Constructs a new com.oonics cdsl from given L{ComoonicsCdslRepository} and nodes. 
        The constructor gets the needed nodes either from list (nodes) or from a given 
        L{ComoonicsClusterInfo} but never from both! You can optional assign the creation 
        timestamp manually.
        
        Additional the constructor is setting the default values from
        L{ComoonicsCdslRepository} to variables which can easily be accessed during cdsl 
        processing.
        
        @param src: source of cdsl to create
        @type src: string
        @param type: type of cdsl to create, could be hostdependent or shared
        @type type: string
        @param cdslRepository: cdslRepository to use
        @type cdslRepository: L{ComoonicsCdslRepository}
        @param clusterinfo: clusterinfo with information about used nodes (Default: None)
        @type clusterinfo: L{ComoonicsClusterInfo}
        @param nodes: Array of nodes to use for cdsl (Default: None)
        @type nodes: Array of strings
        @param timestamp: Timestamp to set to cdsl (Default: None), if not set create timestamp from systemtime
        @type timestamp: string
        """

        #set reldir to current path
        self.reldir = os.getcwd()

        self.logger = ComLog.getLogger("comoonics.cdsl.ComCdsl.ComoonicsCdsl")
        src = cdslRepository.stripsrc(src)
        cdslRepository = cdslRepository.getRepositoryForCdsl(src)
        #add default node to a special nodelist
        super(ComoonicsCdsl, self).__init__(src,
                                            _type,
                                            cdslRepository,
                                            clusterinfo,
                                            nodes,
                                            timestamp,
                                            ignoreerrors=ignoreerrors)
        self.nodesWithDefaultdir = self.nodes[:]
        self.nodesWithDefaultdir.append(self.default_node)
        src = cdslRepository.stripsrc(src, False)
        self.setSource(src)

        #get needed pathes from cdslrepository and normalize them
        self.cdsltree = dirtrim(cdslRepository.getTreePath())
        self.cdsltree_shared = dirtrim(cdslRepository.getSharedTreepath())
        self.default_dir = dirtrim(cdslRepository.getDefaultDir())
        self.cdsl_link = dirtrim(cdslRepository.getLinkPath())
    def __init__(self, src, _type, cdslRepository, clusterinfo=None, nodes=None, timestamp=None, ignoreerrors=False, realpath=True, stripsource=True):
        """
        Constructs a new com.oonics cdsl from given L{ComoonicsCdslRepository} and nodes. 
        The constructor gets the needed nodes either from list (nodes) or from a given 
        L{ComoonicsClusterInfo} but never from both! You can optional assign the creation 
        timestamp manually.
        
        Additional the constructor is setting the default values from
        L{ComoonicsCdslRepository} to variables which can easily be accessed during cdsl 
        processing.
        
        @param src: source of cdsl to create
        @type src: string
        @param type: type of cdsl to create, could be hostdependent or shared
        @type type: string
        @param cdslRepository: cdslRepository to use
        @type cdslRepository: L{ComoonicsCdslRepository}
        @param clusterinfo: clusterinfo with information about used nodes (Default: None)
        @type clusterinfo: L{ComoonicsClusterInfo}
        @param nodes: Array of nodes to use for cdsl (Default: None)
        @type nodes: Array of strings
        @param timestamp: Timestamp to set to cdsl (Default: None), if not set create timestamp from systemtime
        @type timestamp: string
        @param realpath: Should this src path be resolved to its realpath. Default: True.
        @type  realpath: L{Boolean}
        @param stripsource: Should this src path be stripped and checked or taken as is. 
                            This can be switched of if read from repo (speed up). Default: True.
        @type  stripsource: L{Boolean}
        """

        #set reldir to current path
        self.reldir = os.getcwd()
        # private attribute to store stripped state.
        self._stripped=False

        self.logger = ComLog.getLogger("comoonics.cdsl.ComCdsl.ComoonicsCdsl")
        
        if stripsource and not self._stripped:
            src=cdslRepository.stripsrc(src, realpath=realpath)
            cdslRepository=cdslRepository.getRepositoryForCdsl(src)
        #add default node to a special nodelist
        super(ComoonicsCdsl,self).__init__(src, _type, cdslRepository, clusterinfo, nodes, timestamp, ignoreerrors=ignoreerrors)
        self.nodesWithDefaultdir = self.nodes[:]
        self.nodesWithDefaultdir.append(self.default_node)
        if stripsource and not self._stripped:
            src=cdslRepository.stripsrc(src, join=False, realpath=realpath)
            self._stripped=True
        self.setSource(src)

        #get needed pathes from cdslrepository and normalize them
        self.cdsltree = dirtrim(cdslRepository.getTreePath())
        self.cdsltree_shared = dirtrim(cdslRepository.getSharedTreepath())    
        self.default_dir = dirtrim(cdslRepository.getDefaultDir())
        self.cdsl_link = dirtrim(cdslRepository.getLinkPath())
    def walkdir(self, top=".", names=None, recursive=True, onerror=None, filter=None):
        """Directory tree generator.
    
        Modified version of os.walk(). See L{os} for details of usage.
        In contrast to os.path.walk it does not stop when hitting a symbolic 
        link, but does stop when hitting a underlying mountpoint (replaced 
        i{if not islink(path):} with i{if not ismount(path):}). Needs import 
        from error, listdir of Module L{os} to get needed globals (added i{from 
        os import error, listdir}). Removed import from method islink(). Uses method 
        ismount which is defined in this module.
        """
        from comoonics.cdsl.ComCdslRepository import CdslNotFoundException
        from comoonics.cdsl import ismount, dirtrim
        from comoonics.cdsl.ComCdsl import Cdsl    
        if not filter:
            filter=os.path.islink    

        # We may not have read permission for top, in which case we can't
        # get a list of the files the directory contains.  os.path.walk
        # always suppressed the exception then, rather than blow up for a
        # minor reason when (say) a thousand readable directories are still
        # left to visit.  That logic is copied here.
        try:
            if isinstance(top, Cdsl):
                top=top.src
            if not names:
                names = os.listdir(top)
            if isinstance(names, basestring):
                names = [ names, ]

            for name in names:
                # Only links are cdsls so far
                cdsllink=os.path.normpath(os.path.join(self.cdslrepository.workingdir, top, name))
                if not filter(cdsllink) and not os.path.isdir(cdsllink):
                    continue
                cdsl=None
                if self.cdslrepository.isExpandedDir(name):
                    continue
                cdslsrc=os.path.join(dirtrim(top), name)
                try:
                    cdsl=self.cdslrepository.getCdsl(cdslsrc)
                except CdslNotFoundException:
                    try:
                        cdsl=self.cdslrepository.guessonerror(cdslsrc, self.clusterinfo)
                    except CdslNotFoundException:
                        self.logger.debug("Path %s does not exists and cannot be guessed. Skipping it." %cdslsrc)
                if cdsl:
                    yield cdsl
                if recursive and os.path.isdir(cdslsrc) and not ismount(cdslsrc):
                    for cdsl2 in self.walkdir(cdslsrc, onerror):
                        yield cdsl2
        except os.error, err:
            if onerror is not None:
                onerror(err)
            return
 def _pathdepth(self,path):
     """
     Method to calculate the depth of a given path.
     @param path: Path
     @type path: string
     @return: Depth of given path
     @rtype: int
     """
     _path=dirtrim(os.path.normpath(path))
     if not _path or _path == "":
         return 0
     else:
         return _path.count(os.sep)+1
 def _pathdepth(self, path):
     """
     Method to calculate the depth of a given path.
     @param path: Path
     @type path: string
     @return: Depth of given path
     @rtype: int
     """
     _path = dirtrim(os.path.normpath(path))
     if not _path or _path == "":
         return 0
     else:
         return _path.count(os.sep) + 1
    def __init__(self, src, _type, cdslRepository, clusterinfo=None, nodes=None, timestamp=None, ignoreerrors=False):
        """
        Constructs a new cdsl-xml-object from given L{CdslRepository} and nodes. The constructor 
        gets the needed nodes either from list (nodes) or from a given L{ClusterInfo} but never 
        from both! You can optional assign the creation timestamp manually.
        @param src: source of cdsl to create
        @type src: string
        @param type: type of cdsl to create, could be hostdependent or shared
        @type type: string
        @param cdslRepository: cdslRepository to use
        @type cdslRepository: L{CdslRepository}
        @param clusterinfo: clusterinfo with information about used nodes (Default: None)
        @type clusterinfo: L{ClusterInfo}
        @param nodes: Array of nodes to use for cdsl (Default: None)
        @type nodes: Array of strings
        @param timestamp: Timestamp to set to cdsl (Default: None), if not set create timestamp from systemtime
        @type timestamp: string
        """
        import xml.dom
        self.nodes=None
        self.src = dirtrim(src)
        self.type = _type
        self.cdslRepository = cdslRepository
        self.clusterinfo = clusterinfo
        self.logger = ComLog.getLogger("comoonics.cdsl.ComCdsl.Cdsl")
        
        if timestamp != None:
            self.timestamp = timestamp
        else:
            self.timestamp = time.time()
        
        #get nodes from clusterinfo if nodelist is not setgiven
        if (nodes == None or len(nodes)==0) and clusterinfo != None:
            self.node_prefix = cdslRepository.getNodePrefix()
            self.use_nodeids = cdslRepository.getUseNodeids()
            self.maxnodeidnum = cdslRepository.getMaxnodeidnum()
        
            if ((self.use_nodeids == "True") and (self.maxnodeidnum == "0")): #maxnodidnum is not set but use_nodeid is set
                self.nodes = clusterinfo.getNodeIdentifiers("id")
            elif (self.maxnodeidnum == "0"): #maxnodidnum is set but use_nodeid is not set
                self.nodes = clusterinfo.getNodeIdentifiers("name")
                
            else: #use_nodeids and maxnodeidnum are set
                self.nodes = range(1,(int(self.maxnodeidnum)+1))
            
            #value of node_prefix matters if use_nodeids is set
            if ((self.node_prefix) and (self.use_nodeids == "True")):
                for i in range(len(self.nodes)):
                    self.nodes[i] = str(self.node_prefix) + str(self.nodes[i])
            elif (self.node_prefix) and not (self.use_nodeids == "True"):
                raise CdslPrefixWithoutNodeidsException("Prefix could only be used together with use_nodeids")
                
        #set given nodelist
        elif (nodes != None):
            self.nodes = nodes
            
        #no nodelist or clusterinfo is given OR both is given
        elif cdslRepository and not self.nodes:
            self.nodes=[]
            for node in range(1, int(cdslRepository.getMaxnodeidnum())+1): self.nodes.append(str(node))
        else:
            raise TypeError("Cdsl constructor called with wrong parameters. Propably no cdslrepository given.")
        
        if not self.nodes or len(self.nodes) == 0:
            raise CdslNodeidsRequired("""No node identities specified for this cdsl %s. 
At least on node identity is specified. Either specify a default with with the maxnodeidnum option or add nodes at will.""" %self.src)
        
        #create DOM-Element
        doc = cdslRepository.getDocument()
        topelement=None
        child = doc.documentElement.firstChild
        while child:
            # Do we care about the timestamp?? I think no!
            if child.nodeType == xml.dom.Node.ELEMENT_NODE and child.getAttribute("src") == self.src and child.getAttribute("type") == self.type:
                topelement=child
                break
            child = child.nextSibling
        if not topelement:
            topelement=cdslRepository.getDocument().createElement("cdsl")
            topelement.setAttribute("src",self.src)
            topelement.setAttribute("type",self.type)
            topelement.setAttribute("timestamp",str(self.timestamp))
        
            nodes=doc.createElement("nodes")
            topelement.appendChild(nodes)
        
            for node in self.nodes:
                node1=self._createNoderefElement(node, document=doc)
                nodes.appendChild(node1)

        
        #self.XmlElement = doc
        #element = xpath.Evaluate('/cdsl', self.XmlElement)[0]
        
        #super(Cdsl,self).__init__(element,self.XmlElement)
        super(Cdsl,self).__init__(topelement,doc)
        self.parent=None
        self.parent=self.getParent()
        if self.parent and self.parent.type == self.type and not ignoreerrors:
            raise CdslOfSameType("Cannot create the cdsl %s of the same type then the already existing cdsl %s." %(self.src, self.parent.src))
    def __init__(self,
                 src,
                 _type,
                 cdslRepository,
                 clusterinfo=None,
                 nodes=None,
                 timestamp=None,
                 ignoreerrors=False):
        """
        Constructs a new cdsl-xml-object from given L{CdslRepository} and nodes. The constructor 
        gets the needed nodes either from list (nodes) or from a given L{ClusterInfo} but never 
        from both! You can optional assign the creation timestamp manually.
        @param src: source of cdsl to create
        @type src: string
        @param type: type of cdsl to create, could be hostdependent or shared
        @type type: string
        @param cdslRepository: cdslRepository to use
        @type cdslRepository: L{CdslRepository}
        @param clusterinfo: clusterinfo with information about used nodes (Default: None)
        @type clusterinfo: L{ClusterInfo}
        @param nodes: Array of nodes to use for cdsl (Default: None)
        @type nodes: Array of strings
        @param timestamp: Timestamp to set to cdsl (Default: None), if not set create timestamp from systemtime
        @type timestamp: string
        """
        import xml.dom
        self.src = dirtrim(src)
        self.type = _type
        self.cdslRepository = cdslRepository
        self.clusterinfo = clusterinfo
        self.logger = ComLog.getLogger("comoonics.cdsl.ComCdsl.Cdsl")

        if timestamp != None:
            self.timestamp = timestamp
        else:
            self.timestamp = time.time()

        #get nodes from clusterinfo if nodelist is not setgiven
        if (nodes == None) and (clusterinfo != None):
            self.node_prefix = cdslRepository.getNodePrefix()
            self.use_nodeids = cdslRepository.getUseNodeids()
            self.maxnodeidnum = cdslRepository.getMaxnodeidnum()

            if ((self.use_nodeids == "True") and
                (self.maxnodeidnum
                 == "0")):  #maxnodidnum is not set but use_nodeid is set
                self.nodes = clusterinfo.getNodeIdentifiers("id")
            elif (self.maxnodeidnum == "0"
                  ):  #maxnodidnum is set but use_nodeid is not set
                self.nodes = clusterinfo.getNodeIdentifiers("name")

            else:  #use_nodeids and maxnodeidnum are set
                self.nodes = range(1, (int(self.maxnodeidnum) + 1))

            #value of node_prefix matters if use_nodeids is set
            if ((self.node_prefix) and (self.use_nodeids == "True")):
                for i in range(len(self.nodes)):
                    self.nodes[i] = str(self.node_prefix) + str(self.nodes[i])
            elif (self.node_prefix) and not (self.use_nodeids == "True"):
                raise CdslPrefixWithoutNodeidsException(
                    "Prefix could only be used together with use_nodeids")

        #set given nodelist
        elif (nodes != None) and (clusterinfo == None):
            self.nodes = nodes

        #no nodelist or clusterinfo is given OR both is given
        elif cdslRepository:
            self.nodes = []
            for node in range(1, int(cdslRepository.getMaxnodeidnum()) + 1):
                self.nodes.append(str(node))
        else:
            raise TypeError(
                "Cdsl constructor called with wrong parameters. Propably no cdslrepository given."
            )

        #create DOM-Element
        doc = cdslRepository.getDocument()
        topelement = None
        child = doc.documentElement.firstChild
        while child:
            # Do we care about the timestamp?? I think no!
            if child.nodeType == xml.dom.Node.ELEMENT_NODE and child.getAttribute(
                    "src") == self.src and child.getAttribute(
                        "type") == self.type:
                topelement = child
                break
            child = child.nextSibling
        if not topelement:
            topelement = cdslRepository.getDocument().createElement("cdsl")
            topelement.setAttribute("src", self.src)
            topelement.setAttribute("type", self.type)
            topelement.setAttribute("timestamp", str(self.timestamp))

            nodes = doc.createElement("nodes")
            topelement.appendChild(nodes)

            for node in self.nodes:
                node1 = doc.createElement("noderef")
                #If nodeids without prefix are used, use prefix id_ to get a valid xml-file
                #            _isDigit=re.match('^[0-9]*$',str(node))
                #            if _isDigit != None:
                #                node = "id_" + str(node)
                node1.setAttribute("ref", str(node))
                nodes.appendChild(node1)

        #self.XmlElement = doc
        #element = xpath.Evaluate('/cdsl', self.XmlElement)[0]

        #super(Cdsl,self).__init__(element,self.XmlElement)
        super(Cdsl, self).__init__(topelement, doc)
        parent = self.getParent()
        if parent and parent.type == self.type and not ignoreerrors:
            raise CdslOfSameType(
                "Cannot create the cdsl %s of the same type then the already existing cdsl %s."
                % (self.src, parent.src))
    def walkdir(self,
                top=".",
                names=None,
                recursive=True,
                onerror=None,
                filter=None):
        """Directory tree generator.
    
        Modified version of os.walk(). See L{os} for details of usage.
        In contrast to os.path.walk it does not stop when hitting a symbolic 
        link, but does stop when hitting a underlying mountpoint (replaced 
        i{if not islink(path):} with i{if not ismount(path):}). Needs import 
        from error, listdir of Module L{os} to get needed globals (added i{from 
        os import error, listdir}). Removed import from method islink(). Uses method 
        ismount which is defined in this module.
        """
        from comoonics.cdsl.ComCdslRepository import CdslNotFoundException
        from comoonics.cdsl import ismount, dirtrim
        from comoonics.cdsl.ComCdsl import Cdsl
        if not filter:
            filter = os.path.islink

        # We may not have read permission for top, in which case we can't
        # get a list of the files the directory contains.  os.path.walk
        # always suppressed the exception then, rather than blow up for a
        # minor reason when (say) a thousand readable directories are still
        # left to visit.  That logic is copied here.
        try:
            if isinstance(top, Cdsl):
                top = top.src
            if not names:
                names = os.listdir(top)
            if isinstance(names, basestring):
                names = [
                    names,
                ]

            for name in names:
                # Only links are cdsls so far
                cdsllink = os.path.normpath(
                    os.path.join(self.cdslrepository.workingdir, top, name))
                if not filter(cdsllink) and not os.path.isdir(cdsllink):
                    continue
                cdsl = None
                if self.cdslrepository.isExpandedDir(name):
                    continue
                cdslsrc = os.path.join(dirtrim(top), name)
                try:
                    cdsl = self.cdslrepository.getCdsl(cdslsrc)
                except CdslNotFoundException:
                    try:
                        cdsl = self.cdslrepository.guessonerror(
                            cdslsrc, self.clusterinfo)
                    except CdslNotFoundException:
                        self.logger.debug(
                            "Path %s does not exists and cannot be guessed. Skipping it."
                            % cdslsrc)
                if cdsl:
                    yield cdsl
                if recursive and os.path.isdir(
                        cdslsrc) and not ismount(cdslsrc):
                    for cdsl2 in self.walkdir(cdslsrc, onerror):
                        yield cdsl2
        except os.error, err:
            if onerror is not None:
                onerror(err)
            return