Utils

Utilities and platform-specific fixes

The portability fixes try to provide a consistent behavior of the Waf API through Python versions 2.3 to 3.X and across different platforms (win32, linux, etc)

waflib.Utils.SIG_NIL

Arbitrary null value for a md5 hash. This value must be changed when the hash value is replaced (size)

waflib.Utils.O644

Constant representing the permissions for regular files (0644 raises a syntax error on python 3)

waflib.Utils.O755

Constant representing the permissions for executable files (0755 raises a syntax error on python 3)

waflib.Utils.rot_chr

List of characters to use when displaying the throbber (progress bar)

waflib.Utils.rot_idx

Index of the current throbber character (progress bar)

waflib.Utils.readf(fname, m='r')[source]

Read an entire file into a string, in practice the wrapper node.read(..) should be used instead of this method:

def build(ctx):
        from waflib import Utils
        txt = Utils.readf(self.path.find_node('wscript').abspath())
        txt = ctx.path.find_node('wscript').read()
Parameters:
  • fname (string) – Path to file
  • m (string) – Open mode
Return type:

string

Returns:

Content of the file

waflib.Utils.h_file(filename)[source]

Compute a hash value for a file by using md5. This method may be replaced by a faster version if necessary. The following uses the file size and the timestamp value:

import stat
from waflib import Utils
def h_file(filename):
        st = os.stat(filename)
        if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
        m = Utils.md5()
        m.update(str(st.st_mtime))
        m.update(str(st.st_size))
        m.update(filename)
        return m.digest()
Utils.h_file = h_file
Parameters:filename (string) – path to the file to hash
Returns:hash of the file contents
waflib.Utils.to_hex(s)[source]

Return the hexadecimal representation of a string

Parameters:s (string) – string to convert
waflib.Utils.num2ver(ver)[source]

Convert a string, tuple or version number into an integer. The number is supposed to have at most 4 digits:

from waflib.Utils import num2ver
num2ver('1.3.2') == num2ver((1,3,2)) == num2ver((1,3,2,0))
Parameters:ver (string or tuple of numbers) – a version number
waflib.Utils.ex_stack()[source]

Extract the stack to display exceptions

Returns:a string represening the last exception
waflib.Utils.to_list(sth)[source]

Convert a string argument to a list by splitting on spaces, and pass through a list argument unchanged:

from waflib.Utils import to_list
lst = to_list("a b c d")
Parameters:sth – List or a string of items separated by spaces
Return type:list
Returns:Argument converted to list
waflib.Utils.str_to_dict(txt)[source]

Parse a string with key = value pairs into a dictionary:

from waflib import Utils
x = Utils.str_to_dict('''
        a = 1
        b = test
''')
Parameters:s (string) – String to parse
Return type:dict
Returns:Dictionary containing parsed key-value pairs
waflib.Utils.split_path(path)[source]

Split a path by / or . This function is not like os.path.split

Parameters:path (string) – path to split
Returns:list of strings
waflib.Utils.check_dir(path)[source]

Ensure that a directory exists (similar to mkdir -p).

Parameters:dir (string) – Path to directory
waflib.Utils.def_attrs(cls, **kw)[source]

Set default attributes on a class instance

Parameters:
  • cls (class) – the class to update the given attributes in.
  • kw (dict) – dictionary of attributes names and values.
waflib.Utils.quote_define_name(s)[source]

Convert a string to an identifier suitable for C defines.

Parameters:s (string) – String to convert
Return type:string
Returns:Identifier suitable for C defines
waflib.Utils.h_list(lst)[source]

Hash lists. For tuples, using hash(tup) is much more efficient

Parameters:lst (list of strings) – list to hash
Returns:hash of the list
waflib.Utils.h_fun(fun)[source]

Hash functions

Parameters:fun (function) – function to hash
Returns:hash of the function
waflib.Utils.subst_vars(expr, params)[source]

Replace ${VAR} with the value of VAR taken from a dict or a config set:

from waflib import Utils
s = Utils.subst_vars('${PREFIX}/bin', env)
Parameters:
  • expr (string) – String to perform substitution on
  • params – Dictionary or config set to look up variable values.
waflib.Utils.destos_to_binfmt(key)[source]

Return the binary format based on the unversioned platform name.

Parameters:key (string) – platform name
Returns:string representing the binary format
waflib.Utils.unversioned_sys_platform()[source]

Return the unversioned platform name. Some Python platform names contain versions, that depend on the build environment, e.g. linux2, freebsd6, etc. This returns the name without the version number. Exceptions are os2 and win32, which are returned verbatim.

Return type:string
Returns:Unversioned platform name
waflib.Utils.nada(*k, **kw)[source]

A function that does nothing

Returns:None
class waflib.Utils.Timer[source]

Bases: object

Simple object for timing the execution of commands. Its string representation is the current time:

from waflib.Utils import Timer
timer = Timer()
a_few_operations()
s = str(timer)
waflib.Utils.read_la_file(path)[source]

Read property files, used by msvc.py

Parameters:path (string) – file to read
waflib.Utils.nogc(fun)[source]

Decorator: let a function disable the garbage collector during its execution. It is used in the build context when storing/loading the build cache file (pickle)

Parameters:fun (function) – function to execute
Returns:the return value of the function executed
waflib.Utils.run_once(fun)[source]

Decorator: let a function cache its results, use like this:

@run_once
def foo(k):
        return 345*2343
Parameters:fun (function) – function to execute
Returns:the return value of the function executed

Previous topic

TaskGen

Next topic

errcheck

This Page