"""scons.Node.FS File system nodes. This initializes a "default_fs" Node with an FS at the current directory for its own purposes, and for use by scripts or modules looking for the canonical default. """ # # Copyright (c) 2001, 2002 Steven Knight # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" import string import os import os.path import types import SCons.Node from UserDict import UserDict import sys from SCons.Errors import UserError import SCons.Warnings try: import os _link = os.link except AttributeError: import shutil import stat def _link(src, dest): shutil.copy2(src, dest) st=os.stat(src) os.chmod(dest, stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE) def file_link(src, dest): dir, file = os.path.split(dest) if dir and not os.path.isdir(dir): os.makedirs(dir) _link(src, dest) class ParentOfRoot: """ An instance of this class is used as the parent of the root of a filesystem (POSIX) or drive (Win32). This isn't actually a node, but it looks enough like one so that we don't have to have special purpose code everywhere to deal with dir being None. This class is an instance of the Null object pattern. """ def __init__(self): self.abspath = '' self.path = '' self.abspath_ = '' self.path_ = '' self.name='' self.duplicate=0 self.srcdir=None def is_under(self, dir): return 0 def up(self): return None def getRepositories(self): return [] def get_dir(self): return None if os.path.normcase("TeSt") == os.path.normpath("TeSt"): def _my_normcase(x): return x else: def _my_normcase(x): return string.upper(x) class Entry(SCons.Node.Node): """A generic class for file system entries. This class is for when we don't know yet whether the entry being looked up is a file or a directory. Instances of this class can morph into either Dir or File objects by a later, more precise lookup. Note: this class does not define __cmp__ and __hash__ for efficiency reasons. SCons does a lot of comparing of Entry objects, and so that operation must be as fast as possible, which means we want to use Python's built-in object identity comparison. """ def __init__(self, name, directory, fs): """Initialize a generic file system Entry. Call the superclass initialization, take care of setting up our relative and absolute paths, identify our parent directory, and indicate that this node should use signatures.""" SCons.Node.Node.__init__(self) self.name = name self.fs = fs assert directory, "A directory must be provided" self.abspath = directory.abspath_ + name if directory.path == '.': self.path = name else: self.path = directory.path_ + name self.path_ = self.path self.abspath_ = self.abspath self.dir = directory self.cwd = None # will hold the SConscript directory for target nodes self.duplicate = directory.duplicate def get_dir(self): return self.dir def __str__(self): """A FS node's string representation is its path name.""" if self.duplicate or self.builder: return self.path return self.srcnode().path def get_contents(self): """Fetch the contents of the entry. Since this should return the real contents from the file system, we check to see into what sort of subclass we should morph this Entry.""" if os.path.isfile(self.abspath): self.__class__ = File self._morph() return File.get_contents(self) if os.path.isdir(self.abspath): self.__class__ = Dir self._morph() return Dir.get_contents(self) raise AttributeError def exists(self): try: return self._exists except AttributeError: self._exists = os.path.exists(self.abspath) return self._exists def rexists(self): if not hasattr(self, '_rexists'): self._rexists = self.rfile().exists() return self._rexists def get_parents(self): parents = SCons.Node.Node.get_parents(self) if self.dir and not isinstance(self.dir, ParentOfRoot): parents.append(self.dir) return parents def current(self, calc): """If the underlying path doesn't exist, we know the node is not current without even checking the signature, so return 0. Otherwise, return None to indicate that signature calculation should proceed as normal to find out if the node is current.""" bsig = calc.bsig(self) if not self.exists(): return 0 return calc.current(self, bsig) def is_under(self, dir): if self is dir: return 1 else: return self.dir.is_under(dir) def set_local(self): self._local = 1 def srcnode(self): """If this node is in a build path, return the node corresponding to its source file. Otherwise, return ourself.""" try: return self._srcnode except AttributeError: dir=self.dir name=self.name while dir: if dir.srcdir: self._srcnode = self.fs.Entry(name, dir.srcdir, klass=self.__class__) return self._srcnode name = dir.name + os.sep + name dir=dir.get_dir() self._srcnode = self return self._srcnode # This is for later so we can differentiate between Entry the class and Entry # the method of the FS class. _classEntry = Entry class FS: def __init__(self, path = None): """Initialize the Node.FS subsystem. The supplied path is the top of the source tree, where we expect to find the top-level build file. If no path is supplied, the current directory is the default. The path argument must be a valid absolute path. """ if path == None: self.pathTop = os.getcwd() else: self.pathTop = path self.Root = {} self.Top = None def set_toplevel_dir(self, path): assert not self.Top, "You can only set the top-level path on an FS object that has not had its File, Dir, or Entry methods called yet." self.pathTop = path def __setTopLevelDir(self): if not self.Top: self.Top = self.__doLookup(Dir, os.path.normpath(self.pathTop)) self.Top.path = '.' self.Top.path_ = '.' + os.sep self._cwd = self.Top def getcwd(self): self.__setTopLevelDir() return self._cwd def __checkClass(self, node, klass): if klass == Entry: return node if node.__class__ == Entry: node.__class__ = klass node._morph() return node if not isinstance(node, klass): raise TypeError, "Tried to lookup %s '%s' as a %s." % \ (node.__class__.__name__, node.path, klass.__name__) return node def __doLookup(self, fsclass, name, directory = None, create = 1): """This method differs from the File and Dir factory methods in one important way: the meaning of the directory parameter. In this method, if directory is None or not supplied, the supplied name is expected to be an absolute path. If you try to look up a relative path with directory=None, then an AssertionError will be raised.""" if not name: # This is a stupid hack to compensate for the fact # that the POSIX and Win32 versions of os.path.normpath() # behave differently. In particular, in POSIX: # os.path.normpath('./') == '.' # in Win32 # os.path.normpath('./') == '' # os.path.normpath('.\\') == '' # # This is a definite bug in the Python library, but we have # to live with it. name = '.' path_comp = string.split(name, os.sep) drive, path_first = os.path.splitdrive(path_comp[0]) if not path_first: # Absolute path drive = _my_normcase(drive) try: directory = self.Root[drive] except KeyError: if not create: raise UserError dir = Dir(drive, ParentOfRoot(), self) dir.path = dir.path + os.sep dir.abspath = dir.abspath + os.sep self.Root[drive] = dir directory = dir path_comp = path_comp[1:] else: path_comp = [ path_first, ] + path_comp[1:] # Lookup the directory for path_name in path_comp[:-1]: path_norm = _my_normcase(path_name) try: directory = self.__checkClass(directory.entries[path_norm], Dir) except KeyError: if not create: raise UserError # look at the actual filesystem and make sure there isn't # a file already there path = directory.path_ + path_name if os.path.isfile(path): raise TypeError, \ "File %s found where directory expected." % path dir_temp = Dir(path_name, directory, self) directory.entries[path_norm] = dir_temp directory.add_wkid(dir_temp) directory = dir_temp file_name = _my_normcase(path_comp[-1]) try: ret = self.__checkClass(directory.entries[file_name], fsclass) except KeyError: if not create: raise UserError # make sure we don't create File nodes when there is actually # a directory at that path on the disk, and vice versa path = directory.path_ + path_comp[-1] if fsclass == File: if os.path.isdir(path): raise TypeError, \ "Directory %s found where file expected." % path elif fsclass == Dir: if os.path.isfile(path): raise TypeError, \ "File %s found where directory expected." % path ret = fsclass(path_comp[-1], directory, self) directory.entries[file_name] = ret directory.add_wkid(ret) return ret def __transformPath(self, name, directory): """Take care of setting up the correct top-level directory, usually in preparation for a call to doLookup(). If the path name is prepended with a '#', then it is unconditionally interpreted as relative to the top-level directory of this FS. If directory is None, and name is a relative path, then the same applies. """ self.__setTopLevelDir() if name[0] == '#': directory = self.Top name = name[1:] if name and (name[0] == os.sep or name[0] == '/'): # Correct such that '#/foo' is equivalent # to '#foo'. name = name[1:] name = os.path.join('.', os.path.normpath(name)) elif not directory: directory = self._cwd return (os.path.normpath(name), directory) def chdir(self, dir): """Change the current working directory for lookups. """ self.__setTopLevelDir() if not dir is None: self._cwd = dir def Entry(self, name, directory = None, create = 1, klass=None): """Lookup or create a generic Entry node with the specified name. If the name is a relative path (begins with ./, ../, or a file name), then it is looked up relative to the supplied directory node, or to the top level directory of the FS (supplied at construction time) if no directory is supplied. """ if not klass: klass = Entry if isinstance(name, Entry): return self.__checkClass(name, klass) else: name, directory = self.__transformPath(name, directory) return self.__doLookup(klass, name, directory, create) def File(self, name, directory = None, create = 1): """Lookup or create a File node with the specified name. If the name is a relative path (begins with ./, ../, or a file name), then it is looked up relative to the supplied directory node, or to the top level directory of the FS (supplied at construction time) if no directory is supplied. This method will raise TypeError if a directory is found at the specified path. """ return self.Entry(name, directory, create, File) def Dir(self, name, directory = None, create = 1): """Lookup or create a Dir node with the specified name. If the name is a relative path (begins with ./, ../, or a file name), then it is looked up relative to the supplied directory node, or to the top level directory of the FS (supplied at construction time) if no directory is supplied. This method will raise TypeError if a normal file is found at the specified path. """ return self.Entry(name, directory, create, Dir) def BuildDir(self, build_dir, src_dir, duplicate=1): """Link the supplied build directory to the source directory for purposes of building files.""" self.__setTopLevelDir() if not isinstance(src_dir, SCons.Node.Node): src_dir = self.Dir(src_dir) if not isinstance(build_dir, SCons.Node.Node): build_dir = self.Dir(build_dir) if not src_dir.is_under(self.Top): raise UserError, "Source directory must be under top of build tree." if src_dir.is_under(build_dir): raise UserError, "Source directory cannot be under build directory." build_dir.link(src_dir, duplicate) def Repository(self, *dirs): """Specify Repository directories to search.""" for d in dirs: if not isinstance(d, SCons.Node.Node): d = self.Dir(d) self.__setTopLevelDir() self.Top.addRepository(d) def Rsearch(self, path, clazz=_classEntry, cwd=None): """Search for something in a Repository. Returns the first one found in the list, or None if there isn't one.""" if isinstance(path, SCons.Node.Node): return path else: name, d = self.__transformPath(path, cwd) n = self.__doLookup(clazz, name, d) if n.exists(): return n d = n.get_dir() name = n.name # Search repositories of all directories that this file is under. while d: for rep in d.getRepositories(): try: rnode = self.__doLookup(clazz, name, rep) # Only find the node if it exists and it is not # a derived file. If for some reason, we are # explicitly building a file IN a Repository, we # don't want it to show up in the build tree. # This is usually the case with BuildDir(). # We only want to find pre-existing files. if rnode.exists() and \ (isinstance(rnode, Dir) or not rnode.builder): return rnode except TypeError: pass # Wrong type of node. # Prepend directory name name = d.name + os.sep + name # Go up one directory d = d.get_dir() return None def Rsearchall(self, pathlist, must_exist=1, clazz=_classEntry, cwd=None): """Search for a list of somethings in the Repository list.""" ret = [] if SCons.Util.is_String(pathlist): pathlist = string.split(pathlist, os.pathsep) if not SCons.Util.is_List(pathlist): pathlist = [pathlist] for path in pathlist: if isinstance(path, SCons.Node.Node): ret.append(path) else: name, d = self.__transformPath(path, cwd) n = self.__doLookup(clazz, name, d) if not must_exist or n.exists(): ret.append(n) if isinstance(n, Dir): # If this node is a directory, then any repositories # attached to this node can be repository paths. ret.extend(filter(lambda x, me=must_exist, clazz=clazz: isinstance(x, clazz) and (not me or x.exists()), n.getRepositories())) d = n.get_dir() name = n.name # Search repositories of all directories that this file is under. while d: for rep in d.getRepositories(): try: rnode = self.__doLookup(clazz, name, rep) # Only find the node if it exists (or must_exist is zero) # and it is not a derived file. If for some reason, we # are explicitly building a file IN a Repository, we don't # want it to show up in the build tree. This is usually the # case with BuildDir(). We only want to find pre-existing files. if (not must_exist or rnode.exists()) and \ (not rnode.builder or isinstance(rnode, Dir)): ret.append(rnode) except TypeError: pass # Wrong type of node. # Prepend directory name name = d.name + os.sep + name # Go up one directory d = d.get_dir() return ret # XXX TODO? # Annotate with the creator # rel_path # linked_targets # is_accessible class Dir(Entry): """A class for directories in a file system. """ def __init__(self, name, directory, fs): Entry.__init__(self, name, directory, fs) self._morph() def _morph(self): """Turn a file system node (either a freshly initialized directory object or a separate Entry object) into a proper directory object. Modify our paths to add the trailing slash that indicates a directory. Set up this directory's entries and hook it into the file system tree. Specify that directories (this node) don't use signatures for currency calculation.""" self.path_ = self.path + os.sep self.abspath_ = self.abspath + os.sep self.repositories = [] self.srcdir = None self.entries = {} self.entries['.'] = self self.entries['..'] = self.dir self.cwd = self self.builder = 1 self._sconsign = None def __clearRepositoryCache(self, duplicate=None): """Called when we change the repository(ies) for a directory. This clears any cached information that is invalidated by changing the repository.""" for node in self.entries.values(): if node != self.dir: if node != self and isinstance(node, Dir): node.__clearRepositoryCache(duplicate) else: try: del node._srcreps except AttributeError: pass try: del node._rfile except AttributeError: pass try: del node._rexists except AttributeError: pass try: del node._exists except AttributeError: pass try: del node._srcnode except AttributeError: pass if duplicate != None: node.duplicate=duplicate def __resetDuplicate(self, node): if node != self: node.duplicate = node.get_dir().duplicate def Dir(self, name): """Create a directory node named 'name' relative to this directory.""" return self.fs.Dir(name, self) def File(self, name): """Create file node named 'name' relatove to this directory.""" return self.fs.File(name, self) def link(self, srcdir, duplicate): """Set this directory as the build directory for the supplied source directory.""" self.srcdir = srcdir self.duplicate = duplicate self.__clearRepositoryCache(duplicate) def getRepositories(self): """Returns a list of repositories for this directory.""" if self.srcdir and not self.duplicate: try: return self._srcreps except AttributeError: self._srcreps = self.fs.Rsearchall(self.srcdir.path, clazz=Dir, must_exist=0, cwd=self.fs.Top) \ + self.repositories return self._srcreps return self.repositories def addRepository(self, dir): if not dir in self.repositories and dir != self: self.repositories.append(dir) self.__clearRepositoryCache() def up(self): return self.entries['..'] def root(self): if not self.entries['..']: return self else: return self.entries['..'].root() def all_children(self, scanner): #XXX --random: randomize "dependencies?" keys = filter(lambda k: k != '.' and k != '..', self.entries.keys()) kids = map(lambda x, s=self: s.entries[x], keys) def c(one, two): if one.abspath < two.abspath: return -1 if one.abspath > two.abspath: return 1 return 0 kids.sort(c) return kids def build(self): """A null "builder" for directories.""" pass def calc_signature(self, calc): """A directory has no signature.""" return None def set_bsig(self, bsig): """A directory has no signature.""" bsig = None def set_csig(self, csig): """A directory has no signature.""" csig = None def get_contents(self): """Return a fixed "contents" value of a directory.""" return '' def current(self, calc): """If all of our children were up-to-date, then this directory was up-to-date, too.""" state = 0 for kid in self.children(None): s = kid.get_state() if s and (not state or s > state): state = s import SCons.Node if state == 0 or state == SCons.Node.up_to_date: return 1 else: return 0 def sconsign(self): """Return the .sconsign file info for this directory, creating it first if necessary.""" if not self._sconsign: import SCons.Sig self._sconsign = SCons.Sig.SConsignFile(self) return self._sconsign def srcnode(self): """Dir has a special need for srcnode()...if we have a srcdir attribute set, then that *is* our srcnode.""" if self.srcdir: return self.srcdir return Entry.srcnode(self) # XXX TODO? # base_suf # suffix # addsuffix # accessible # relpath class File(Entry): """A class for files in a file system. """ def __init__(self, name, directory, fs): Entry.__init__(self, name, directory, fs) self._morph() def Dir(self, name): """Create a directory node named 'name' relative to the SConscript directory of this file.""" return self.fs.Dir(name, self.cwd) def File(self, name): """Create a file node named 'name' relative to the SConscript directory of this file.""" return self.fs.File(name, self.cwd) def RDirs(self, pathlist): """Search for a list of directories in the Repository list.""" return self.fs.Rsearchall(pathlist, clazz=Dir, must_exist=0, cwd=self.cwd) def generate_build_env(self): env = SCons.Node.Node.generate_build_env(self) return env.Override({'Dir' : self.Dir, 'File' : self.File, 'RDirs' : self.RDirs}) def _morph(self): """Turn a file system node into a File object.""" self.created = 0 if not hasattr(self, '_local'): self._local = 0 def root(self): return self.dir.root() def get_contents(self): if not self.rexists(): return '' return open(self.rstr(), "rb").read() def get_timestamp(self): if self.rexists(): return os.path.getmtime(self.rstr()) else: return 0 def calc_signature(self, calc): """ Select and calculate the appropriate build signature for a File. self - the File node calc - the signature calculation module returns - the signature This method does not store the signature in the node or in the .sconsign file. """ if self.builder: if SCons.Sig.build_signature: if not hasattr(self, 'bsig'): self.set_bsig(calc.bsig(self.rfile())) return self.get_bsig() else: if not hasattr(self, 'csig'): self.set_csig(calc.csig(self.rfile())) return self.get_csig() elif not self.rexists(): return None else: if not hasattr(self, 'csig'): self.set_csig(calc.csig(self.rfile())) return self.get_csig() def store_csig(self): self.dir.sconsign().set_csig(self.name, self.get_csig()) def store_bsig(self): self.dir.sconsign().set_bsig(self.name, self.get_bsig()) def store_implicit(self): self.dir.sconsign().set_implicit(self.name, self.implicit) def store_timestamp(self): self.dir.sconsign().set_timestamp(self.name, self.get_timestamp()) def get_prevsiginfo(self): """Fetch the previous signature information from the .sconsign entry.""" return self.dir.sconsign().get(self.name) def get_stored_implicit(self): return self.dir.sconsign().get_implicit(self.name) def get_implicit_deps(self, env, scanner, target): if scanner: return scanner.scan(self, env, target) else: return [] def scanner_key(self): return os.path.splitext(self.name)[1] def __createDir(self): # ensure that the directories for this node are # created. listDirs = [] parent=self.dir while parent: if parent.exists(): break listDirs.append(parent) parent = parent.up() listDirs.reverse() for dirnode in listDirs: try: os.mkdir(dirnode.abspath) dirnode._exists = 1 except OSError: pass def built(self): SCons.Node.Node.built(self) if hasattr(self, '_exists'): delattr(self, '_exists') if hasattr(self, '_rexists'): delattr(self, '_rexists') def prepare(self): """Prepare for this file to be created.""" if self.exists(): if self.builder and not self.precious: os.unlink(self.path) if hasattr(self, '_exists'): delattr(self, '_exists') else: self.__createDir() def remove(self): """Remove this file.""" if os.path.exists(self.path): os.unlink(self.path) return 1 return None def exists(self): # Duplicate from source path if we are set up to do this. if self.duplicate and not self.builder and not self.created: src=self.srcnode().rfile() if src.exists() and src.abspath != self.abspath: try: os.unlink(self.abspath) except OSError: pass self.__createDir() file_link(src.abspath, self.abspath) self.created = 1 # Set our exists cache accordingly self._exists=1 self._rexists=1 return 1 return Entry.exists(self) def current(self, calc): bsig = calc.bsig(self) if not self.exists(): # The file doesn't exist locally... r = self.rfile() if r != self: # ...but there is one in a Repository... if calc.current(r, bsig): # ...and it's even up-to-date... if self._local: # ...and they'd like a local copy. print "Local copy of %s from %s" % (self.path, r.path) file_link(r.path, self.path) self.set_bsig(bsig) self.store_bsig() return 1 self._rfile = self return None else: return calc.current(self, bsig) def rfile(self): if not hasattr(self, '_rfile'): self._rfile = self if not self.exists(): n = self.fs.Rsearch(self.path, clazz=File, cwd=self.fs.Top) if n: self._rfile = n return self._rfile def rstr(self): return str(self.rfile()) default_fs = FS() def find_file(filename, paths, node_factory = default_fs.File): """ find_file(str, [Dir()]) -> [nodes] filename - a filename to find paths - a list of directory path *nodes* to search in returns - the node created from the found file. Find a node corresponding to either a derived file or a file that exists already. Only the first file found is returned, and none is returned if no file is found. """ retval = None for dir in paths: try: node = node_factory(filename, dir) # Return true of the node exists or is a derived node. if node.builder or \ (isinstance(node, SCons.Node.FS.Entry) and node.exists()): retval = node break except TypeError: # If we find a directory instead of a file, we don't care pass return retval def find_files(filenames, paths, node_factory = default_fs.File): """ find_files([str], [Dir()]) -> [nodes] filenames - a list of filenames to find paths - a list of directory path *nodes* to search in returns - the nodes created from the found files. Finds nodes corresponding to either derived files or files that exist already. Only the first file found is returned for each filename, and any files that aren't found are ignored. """ nodes = map(lambda x, paths=paths, node_factory=node_factory: find_file(x, paths, node_factory), filenames) return filter(lambda x: x != None, nodes)