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
= 'iluvcuteoverload'¶ Arbitrary null value for a md5 hash. This value must be changed when the hash value is replaced (size)
-
waflib.Utils.
O644
= 420¶ Constant representing the permissions for regular files (0644 raises a syntax error on python 3)
-
waflib.Utils.
O755
= 493¶ 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
= 0¶ Index of the current throbber character (progress bar)
-
waflib.Utils.
readf_unix
(fname, m='r', encoding='ISO8859-1')¶ Read an entire file into a string, use this function instead of os.open() whenever possible.
In practice the wrapper node.read(..) should be preferred to this function:
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
- encoding (string) – encoding value, only used for python 3
Return type: string
Returns: Content of the file
-
waflib.Utils.
writef_unix
(fname, data, m='w', encoding='ISO8859-1')¶ Write an entire file from a string, use this function instead of os.open() whenever possible.
In practice the wrapper node.write(..) should be preferred to this function:
def build(ctx): from waflib import Utils txt = Utils.writef(self.path.make_node('i_like_kittens').abspath(), 'some data') self.path.make_node('i_like_kittens').write('some data')
Parameters: - fname (string) – Path to file
- data (string) – The contents to write to the file
- m (string) – Open mode
- encoding (string) – encoding value, only used for python 3
-
waflib.Utils.
h_file_unix
(fname)¶ 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(fname): st = os.stat(fname) 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(fname) return m.digest() Utils.h_file = h_file
Parameters: fname (string) – path to the file to hash Returns: hash of the file contents
-
waflib.Utils.
readf
(fname, m='r', encoding='ISO8859-1')[source]¶ Read an entire file into a string, use this function instead of os.open() whenever possible.
In practice the wrapper node.read(..) should be preferred to this function:
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
- encoding (string) – encoding value, only used for python 3
Return type: string
Returns: Content of the file
-
waflib.Utils.
writef
(fname, data, m='w', encoding='ISO8859-1')[source]¶ Write an entire file from a string, use this function instead of os.open() whenever possible.
In practice the wrapper node.write(..) should be preferred to this function:
def build(ctx): from waflib import Utils txt = Utils.writef(self.path.make_node('i_like_kittens').abspath(), 'some data') self.path.make_node('i_like_kittens').write('some data')
Parameters: - fname (string) – Path to file
- data (string) – The contents to write to the file
- m (string) – Open mode
- encoding (string) – encoding value, only used for python 3
-
waflib.Utils.
h_file
(fname)[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(fname): st = os.stat(fname) 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(fname) return m.digest() Utils.h_file = h_file
Parameters: fname (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.
listdir_win32
(s)[source]¶ List the contents of a folder in a portable manner. On Win32, return the list of drive letters: [‘C:’, ‘X:’, ‘Z:’]
Parameters: s (string) – a string, which can be empty on Windows
-
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.
split_path_unix
(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.
split_path
(path)¶ 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: path (string) – Path to directory
-
waflib.Utils.
check_exe
(name, env=None)[source]¶ Ensure that a program exists
Parameters: name (string) – name or path to program Returns: path of the program or None
-
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, except on python >= 3.3 where hash randomization assumes everybody is running a web application.
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.
h_cmd
(ins)[source]¶ Task command hashes are calculated by calling this function. The inputs can be strings, functions, tuples/lists containing strings/functions
-
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
-
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)
-
__doc__
= '\n\tSimple object for timing the execution of commands.\n\tIts string representation is the current time::\n\n\t\tfrom waflib.Utils import Timer\n\t\ttimer = Timer()\n\t\ta_few_operations()\n\t\ts = str(timer)\n\t'¶
-
__module__
= 'waflib.Utils'¶
-
-
waflib.Utils.
read_la_file
(path)[source]¶ Read property files, used by msvc.py
Parameters: path (string) – file to read