Node

Node: filesystem structure

  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() for the low-level operations
  4. The methods Node.find_resource(), Node.find_dir() Node.find_or_declare() must be used when a build context is present
  5. Each instance of waflib.Context.Context has a unique Node subclass required for serialization. (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**/*.swp\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)
dict_class

Subclasses can provide a dict class to enable case insensitivity for example.

alias of dict

__slots__ = ('name', 'parent', 'children', 'cache_abspath', 'cache_isdir')
__init__(name, parent)[source]

Note

Use Node.make_node() or Node.find_node() instead of calling this constructor

__setstate__(data)[source]

Deserializes node information, used for persistence

__getstate__()[source]

Serializes node information, used for persistence

__str__()[source]

String representation (abspath), for debugging purposes

Return type:string
__repr__()[source]

String representation (abspath), for debugging purposes

Return type:string
__copy__()[source]

Provided to prevent nodes from being copied

Raises :waflib.Errors.WafError
read(flags='r', encoding='latin-1')[source]

Reads and returns the contents of the file represented by this node, see waflib.Utils.readf():

def build(bld):
        bld.path.find_node('wscript').read()
Parameters:
  • flags (string) – Open mode
  • encoding (string) – encoding value for Python3
Return type:

string or bytes

Returns:

File contents

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

Writes data to the file represented by this node, see waflib.Utils.writef():

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

Reads and parses the contents of this node as JSON (Python ≥ 2.6):

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 (Python ≥ 2.6) as UTF-8 data (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
exists()[source]

Returns whether the Node is present on the filesystem

Return type:bool
isdir()[source]

Returns whether the Node represents a folder

Return type:bool
chmod(val)[source]

Changes the file/dir permissions:

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

Removes the file/folder from the filesystem (equivalent to rm -rf), and remove this object from the Node tree. Do not use this object after calling this method.

evict()[source]

Removes this node from the Node tree

suffix()[source]

Returns the file rightmost extension, for example a.b.c.d → .d

Return type:string
height()[source]

Returns the depth in the folder hierarchy from the filesystem root or from all the file drives

Returns:filesystem depth
Return type:integer
listdir()[source]

Lists the folder contents

Returns:list of file/folder names ordered alphabetically
Return type:list of string
mkdir()[source]

Creates a folder represented by this node. Intermediate folders are created as needed.

Raises :waflib.Errors.WafError when the folder is missing
find_node(lst)[source]

Finds a node on the file system (files or folders), and creates the corresponding Node objects if it exists

Parameters:lst (string or list of string) – relative path
Returns:The corresponding Node object or None if no entry was found on the filesystem
Return type::py:class:´waflib.Node.Node´
make_node(lst)[source]

Returns or creates a Node object corresponding to the input path without considering the filesystem.

Parameters:lst (string or list of string) – relative path
Return type::py:class:´waflib.Node.Node´
search_node(lst)[source]

Returns a Node previously defined in the data structure. The filesystem is not considered.

Parameters:lst (string or list of string) – relative path
Return type::py:class:´waflib.Node.Node´ or None if there is no entry in the Node datastructure
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
Returns:a relative path or an absolute one if that is better
Return type:string
abspath()[source]

Returns the absolute path. A cache is kept in the context as cache_node_abspath

Return type:string
is_child_of(node)[source]

Returns whether the object belongs to a subtree of the input 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
Return type:bool
ant_iter(accept=None, maxdepth=25, pats=[], dir=False, src=True, remove=True)[source]

Recursive method used by waflib.Node.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)
Returns:

A generator object to iterate from

Return type:

iterator

ant_glob(*k, **kw)[source]

Finds files across folders:

  • **/* 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') # finds all .cpp files
        cfg.root.ant_glob('etc/*.txt') # matching from 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 are garbage-collected. To prevent this behaviour in particular when running over the build directory, 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)
Returns:

The corresponding Nodes

Returns:

Whether to evaluate the Nodes lazily, alters the type of the returned value

Return type:

by default, list of waflib.Node.Node instances

is_src()[source]

Returns True if the node is below the source directory. Note that !is_src() is_bld()

Return type:bool
is_bld()[source]

Returns True if the node is below the build directory. Note that !is_bld() is_src()

Return type:bool
get_src()[source]

Returns the corresponding Node object in the source directory (or self if already under the source directory). Use this method only if the purpose is to create a Node object (this is common with folders but not with files, see ticket 1937)

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

Return the corresponding Node object in the build directory (or self if already under the build directory). Use this method only if the purpose is to create a Node object (this is common with folders but not with files, see ticket 1937)

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

Use this method in the build phase to find source files corresponding to the relative path given.

First it looks up the Node data structure to find any declared Node object in the build directory. If None is found, it then considers the filesystem in the source directory.

Parameters:lst (string or list of string) – relative path
Returns:the corresponding Node object or None
Return type:waflib.Node.Node
find_or_declare(lst)[source]

Use this method in the build phase to declare output files which are meant to be written in the build directory.

This method creates the Node object and its parent folder as needed.

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

Searches for a folder on the filesystem (see waflib.Node.Node.find_node())

Parameters:lst (string or list of string) – relative path
Returns:The corresponding Node object or None if there is no such folder
Return type:waflib.Node.Node
change_ext(ext, ext_in=None)[source]

Declares a build node with a distinct extension; this is uses waflib.Node.Node.find_or_declare()

Returns:A build node of the same path, but with a different extension
Return type:waflib.Node.Node
bldpath()[source]

Returns the relative path seen from the build directory src/foo.cpp

Return type:string
srcpath()[source]

Returns the relative path seen from the source directory ../src/foo.cpp

Return type:string
relpath()[source]

If a file in the build directory, returns waflib.Node.Node.bldpath(), else returns waflib.Node.Node.srcpath()

Return type:string
bld_dir()[source]

Equivalent to self.parent.bldpath()

Return type:string
h_file()[source]

See waflib.Utils.h_file()

Returns:a hash representing the file contents
Return type:string or bytes
get_bld_sig()[source]

Returns a signature (see waflib.Node.Node.h_file()) for the purpose of build dependency calculation. This method uses a per-context cache.

Returns:a hash representing the object contents
Return type:string or bytes
waflib.Node.pickle_lock = <thread.lock object at 0x271a530>

Lock mandatory for thread-safe node serialization

Previous topic

Logs

Next topic

Options

This Page