Node

Node: filesystem structure, contains lists of nodes

  1. Each file/folder is represented by exactly one node.
  2. Some potential class properties are stored on waflib.Build.BuildContext : nodes to depend on, etc. Unused class members can increase the .wafpickle file size sensibly.
  3. Node objects should never be created directly, use the methods Node.make_node() or Node.find_node()
  4. The methods Node.find_resource(), Node.find_dir() Node.find_or_declare() should be used when a build context is present
  5. Each instance of waflib.Context.Context has a unique Node subclass. (waflib.Node.Nod3, see the waflib.Context.Context initializer). A reference to the context owning a node is held as self.ctx
waflib.Node.exclude_regs = '\n**/*~\n**/#*#\n**/.#*\n**/%*%\n**/._*\n**/CVS\n**/CVS/**\n**/.cvsignore\n**/SCCS\n**/SCCS/**\n**/vssver.scc\n**/.svn\n**/.svn/**\n**/BitKeeper\n**/.git\n**/.git/**\n**/.gitignore\n**/.bzr\n**/.bzrignore\n**/.bzr/**\n**/.hg\n**/.hg/**\n**/_MTN\n**/_MTN/**\n**/.arch-ids\n**/{arch}\n**/_darcs\n**/_darcs/**\n**/.intlcache\n**/.DS_Store'

Ant patterns for files and folders to exclude while doing the recursive traversal in waflib.Node.Node.ant_glob()

class waflib.Node.Node(name, parent)[source]

Bases: object

This class is organized in two parts

  • The basic methods meant for filesystem access (compute paths, create folders, etc)
  • The methods bound to a waflib.Build.BuildContext (require bld.srcnode and bld.bldnode)

The Node objects are not thread safe in any way.

dict_class

alias of dict

__slots__ = ('name', 'sig', 'children', 'parent', 'cache_abspath', 'cache_isdir', 'cache_sig')
__setstate__(data)[source]

Deserializes from data

__getstate__()[source]

Serialize the node info

__str__()[source]

String representation (name), for debugging purposes

__repr__()[source]

String representation (abspath), for debugging purposes

__hash__()[source]

Node hash, used for storage in dicts. This hash is not persistent.

__eq__(node)[source]

Node comparison, based on the IDs

__copy__()[source]

Implemented to prevent nodes from being copied (raises an exception)

read(flags='r', encoding='ISO8859-1')[source]

Return the contents of the file represented by this node:

def build(bld):
        bld.path.find_node('wscript').read()
Parameters:
  • fname (string) – Path to file
  • m (string) – Open mode
Return type:

string

Returns:

File contents

write(data, flags='w', encoding='ISO8859-1')[source]

Write some text to the physical file represented by this node:

def build(bld):
        bld.path.make_node('foo.txt').write('Hello, world!')
Parameters:
  • data (string) – data to write
  • flags (string) – Write mode
read_json(convert=True, encoding='utf-8')[source]

Read and parse the contents of this node as JSON:

def build(bld):
        bld.path.find_node('abc.json').read_json()

Note that this by default automatically decodes unicode strings on Python2, unlike what the Python JSON module does.

Parameters:
  • convert (boolean) – Prevents decoding of unicode strings on Python2
  • encoding (string) – The encoding of the file to read. This default to UTF8 as per the JSON standard
Return type:

object

Returns:

Parsed file contents

write_json(data, pretty=True)[source]

Writes a python object as JSON to disk. Files are always written as UTF8 as per the JSON standard:

def build(bld):
        bld.path.find_node('xyz.json').write_json(199)
Parameters:
  • data (object) – The data to write to disk
  • pretty (boolean) – Determines if the JSON will be nicely space separated
chmod(val)[source]

Change file/dir permissions:

def build(bld):
        bld.path.chmod(493) # 0755
delete()[source]

Delete the file/folder, and remove this node from the tree. Do not use this object after calling this method.

evict()[source]

Internal - called when a node is removed

suffix()[source]

Return the file extension

height()[source]

Depth in the folder hierarchy from the filesystem root or from all the file drives

listdir()[source]

List the folder contents

mkdir()[source]

Create a folder represented by this node, creating intermediate nodes as needed An exception will be raised only when the folder cannot possibly exist there

find_node(lst)[source]

Find a node on the file system (files or folders), create intermediate nodes as needed

Parameters:lst (string or list of string) – path
make_node(lst)[source]

Find or create a node without looking on the filesystem

Parameters:lst (string or list of string) – path
search_node(lst)[source]

Search for a node without looking on the filesystem

Parameters:lst (string or list of string) – path
path_from(node)[source]

Path of this node seen from the other:

def build(bld):
        n1 = bld.path.find_node('foo/bar/xyz.txt')
        n2 = bld.path.find_node('foo/stuff/')
        n1.path_from(n2) # '../bar/xyz.txt'
Parameters:node (waflib.Node.Node) – path to use as a reference
abspath()[source]

Absolute path. A cache is kept in the context as cache_node_abspath

is_child_of(node)[source]

Does this node belong to the subtree node?:

def build(bld):
        node = bld.path.find_node('wscript')
        node.is_child_of(bld.path) # True
Parameters:node (waflib.Node.Node) – path to use as a reference
ant_iter(accept=None, maxdepth=25, pats=[], dir=False, src=True, remove=True)[source]

Semi-private and recursive method used by ant_glob.

Parameters:
  • accept (function) – function used for accepting/rejecting a node, returns the patterns that can be still accepted in recursion
  • maxdepth (int) – maximum depth in the filesystem (25)
  • pats (tuple) – list of patterns to accept and list of patterns to exclude
  • dir (bool) – return folders too (False by default)
  • src (bool) – return files (True by default)
  • remove (bool) – remove files/folders that do not exist (True by default)
ant_glob(*k, **kw)[source]

This method is used for finding files across folders. It behaves like ant patterns:

  • **/* find all files recursively
  • **/*.class find all files ending by .class
  • .. find files having two dot characters

For example:

def configure(cfg):
        cfg.path.ant_glob('**/*.cpp') # find all .cpp files
        cfg.root.ant_glob('etc/*.txt') # using the filesystem root can be slow
        cfg.path.ant_glob('*.cpp', excl=['*.c'], src=True, dir=False)

For more information see http://ant.apache.org/manual/dirtasks.html

The nodes that correspond to files and folders that do not exist will be removed. To prevent this behaviour, pass ‘remove=False’

Parameters:
  • incl (string or list of strings) – ant patterns or list of patterns to include
  • excl (string or list of strings) – ant patterns or list of patterns to exclude
  • dir (bool) – return folders too (False by default)
  • src (bool) – return files (True by default)
  • remove (bool) – remove files/folders that do not exist (True by default)
  • maxdepth (int) – maximum depth of recursion
  • ignorecase (bool) – ignore case while matching (False by default)
is_src()[source]

True if the node is below the source directory note: !is_src does not imply is_bld()

Return type:bool
is_bld()[source]

True if the node is below the build directory note: !is_bld does not imply is_src

Return type:bool
get_src()[source]

Return the equivalent src node (or self if not possible)

Return type:waflib.Node.Node
get_bld()[source]

Return the equivalent bld node (or self if not possible)

Return type:waflib.Node.Node
find_resource(lst)[source]

Try to find a declared build node or a source file

Parameters:lst (string or list of string) – path
find_or_declare(lst)[source]

if ‘self’ is in build directory, try to return an existing node if no node is found, go to the source directory try to find an existing node in the source directory if no node is found, create it in the build directory

Parameters:lst (string or list of string) – path
find_dir(lst)[source]

Search for a folder in the filesystem

Parameters:lst (string or list of string) – path
change_ext(ext, ext_in=None)[source]
Returns:A build node of the same path, but with a different extension
Return type:waflib.Node.Node
bldpath()[source]

Path seen from the build directory default/src/foo.cpp

srcpath()[source]

Path seen from the source directory ../src/foo.cpp

relpath()[source]

If a file in the build directory, bldpath, else srcpath

bld_dir()[source]

Build path without the file name

get_bld_sig()[source]

Node signature, assuming the file is in the build directory

__doc__ = '\n\tThis class is organized in two parts\n\n\t* The basic methods meant for filesystem access (compute paths, create folders, etc)\n\t* The methods bound to a :py:class:`waflib.Build.BuildContext` (require ``bld.srcnode`` and ``bld.bldnode``)\n\n\tThe Node objects are not thread safe in any way.\n\t'
__module__ = 'waflib.Node'
waflib.Node.pickle_lock = <thread.lock object>

Lock mandatory for thread-safe node serialization