Package mbuild :: Module util
[frames] | no frames]

Module util

source code

Basic useful utilities: file copying, removal, permissions, path-name manipulation, and command execution.

Functions
 
cmkdir(path_to_dir)
Make a directory if it does not exist
source code
 
compute_clang_version(full_path) source code
 
compute_gcc_version(full_path) source code
string
cond_add_quotes(s)
If there are spaces in the input string s, put quotes around the string and return it...
source code
 
copy_file(src, tgt)
Copy src to tgt.
source code
 
copy_tree(src, tgt, ignore_patterns=None, symlinks=False)
Copy the tree at src to tgt.
source code
 
escape_special_characters(s)
Add a backslash before characters that have special meanings in regular expressions.
source code
 
file_hashes_are_valid(list_of_files, fn)
Return true iff the old hashes in the file fn are valid for all of the specified list of files.
source code
 
find_dir(d)
Look upwards for a particular filesystem directory d as a subdirectory of one of the ancestors.
source code
 
find_python(env)
return path to NON cygwin
source code
string or list of strings
flip_slashes(s)
convert to backslashes to _mysep slashes.
source code
 
gcc_version_test(major, minor, rev, gstr)
Return True if the specified gcc version string (gstr) is at or after the specified major,minor,revision args
source code
 
get_clang_version(full_path) source code
string
get_elapsed_time(start_time, end_time=None)
compute the elapsed time in seconds or minutes
source code
 
get_gcc_version(gcc)
Return the compressed version number of gcc
source code
float
get_time()
Returns: current time as float
source code
string
get_time_str()
Returns: current time as string
source code
 
glob(s)
Run the normal glob.glob() on s but make sure all the slashes are flipped forward afterwards.
source code
 
hash_file(fn) source code
 
hash_files(list_of_files, fn)
Hash the files in the list of files and write the hashes to fn
source code
 
hash_list(list_of_strings)
Compute a sha1 hash of a list of strings and return the hex digest
source code
 
hash_string(s)
Compute a sha1 hash of a string and return the hex digest
source code
string
join(*args)
join all the args supplied as arguments using a forward slash as the separator
source code
 
list2string(ls)
Print a list as a string
source code
 
make_documentation_tree_accessible(dir)
Make the directory teree rooted at dir web-accessible.
source code
 
make_executable(fn)
Make the file or directory readable & executable by user/group, writable by user
source code
 
make_read_only(fn)
Make the file fn read-only
source code
 
make_web_accessible(fn)
Make the file readable by all and writable by the current owner
source code
 
make_web_accessible_dir(dir)
Make the directory readable and executable by all and writable by the current owner
source code
 
make_writable(fn)
Make the file or directory readable/writable/executable by me
source code
 
modify_dir_tree(path, dir_fn=None, file_fn=None)
Walk the tree rooted at path and apply the function dir_fn to directories and file_fn to files.
source code
 
move_file(src, tgt)
Move/Rename src to tgt.
source code
string
myjoin(*args)
join all the args supplied as arguments using _mysep as the separator.
source code
 
peel_dir(s, n)
Remove n trailing path components from s by calling os.path.dirname()
source code
string or list of strings
posix_slashes(s)
convert to posix slashes.
source code
string or list of strings
prefix_files(dir, input_files)
Add dir on to the front of the input file or files.
source code
 
print_elapsed_time(start_time, end_time=None, prefix=None, current=False)
print the elapsed time in seconds or minutes.
source code
 
qdip(fn)
Add quotes to a string if there are spaces in the name
source code
 
quote(fn)
Add quotes around the file nameed fn.
source code
 
read_signatures(fn)
Return a dictionary of d[file]=hash from the specified file
source code
 
remove_file(fn, env=None, quiet=True)
Remove a file or link if it exists.
source code
 
remove_files(lst, env=None)
Remove all the files in the list of files, lst.
source code
 
remove_files_from_tree(dir, file_patterns)
Remove files that match the re object compiled pattern provided
source code
 
remove_files_glob(lst, env=None)
Remove all files in the list of wild card expressions.
source code
 
remove_tree(dir_name, env=None, dangerous=False)
Remove a directory if it exists.
source code
integer
run_cmd_io(cmd, fn_i, fn_o, shell_executable=None, directory=None)
Run a command string using the subprocess module.
source code
tuple
run_command(cmd, separate_stderr=False, shell_executable=None, directory=None, osenv=None, input_file_name=None, **kwargs)
Run a command string using the subprocess module.
source code
tuple
run_command_output_file(cmd, output_file_name, shell_executable=None, directory=None, osenv=None, input_file_name=None, **kwargs)
Run a command string using the subprocess module.
source code
tuple return: (return code, list of stdout+stderr lines)
run_command_timed(cmd, shell_executable=None, directory=None, osenv=None, seconds=0, input_file_name=None, **kwargs)
Run a timed command.
source code
tuple
run_command_unbufferred(cmd, prefix_line=None, shell_executable=None, directory=None, osenv=None, input_file_name=None, **kwargs)
Run a command string using the subprocess module.
source code
string
strip_quotes(a)
Conditionally remove leading/trailing quotes from a string
source code
 
symlink(env, src, tgt)
Make a symlink from src to target.
source code
 
touch(fn)
Open a file for append.
source code
 
write_signatures(fn, d)
Write a dictionary of d[file]=hash to the specified file
source code
Variables
  __package__ = 'mbuild'
Function Details

cond_add_quotes(s)

source code 

If there are spaces in the input string s, put quotes around the string and return it... if there are not already quotes in the string.

Parameters:
  • s (string) - path name
Returns: string
string with quotes, if necessary

copy_tree(src, tgt, ignore_patterns=None, symlinks=False)

source code 

Copy the tree at src to tgt. This will first remove tgt if it already exists.

escape_special_characters(s)

source code 

Add a backslash before characters that have special meanings in regular expressions. Python does not handle backslashes in regular expressions or substitution text so they must be escaped before processing.

find_dir(d)

source code 

Look upwards for a particular filesystem directory d as a subdirectory of one of the ancestors. Return None on failure

flip_slashes(s)

source code 

convert to backslashes to _mysep slashes. _mysep slashes are defined to be backslashes on native windows and forward slashes everywhere else.

Parameters:
  • s (string or list of strings) - path name(s)
Returns: string or list of strings
string(s) with _mysep slashes

get_elapsed_time(start_time, end_time=None)

source code 

compute the elapsed time in seconds or minutes

Parameters:
  • start_time (float) - starting time.
  • end_time (float) - ending time.
Returns: string

get_time()

source code 
Returns: float
current time as float

get_time_str()

source code 
Returns: string
current time as string

glob(s)

source code 

Run the normal glob.glob() on s but make sure all the slashes are flipped forward afterwards. This is shorthand for mbuild.posix_slashes(glob.glob(s))

join(*args)

source code 

join all the args supplied as arguments using a forward slash as the separator

Parameters:
  • args (strings) - path component strings
Returns: string
string with forward-slashes

make_documentation_tree_accessible(dir)

source code 

Make the directory teree rooted at dir web-accessible. That is, the directories are readable and executable by anyone and the files are readable by anyone.

modify_dir_tree(path, dir_fn=None, file_fn=None)

source code 

Walk the tree rooted at path and apply the function dir_fn to directories and file_fn to files. This is intended for doing recursive chmods, etc.

myjoin(*args)

source code 

join all the args supplied as arguments using _mysep as the separator. _mysep is a backslash on native windows and a forward slash everywhere else.

Parameters:
  • args (strings) - path component strings
Returns: string
string with _mysep slashes

posix_slashes(s)

source code 

convert to posix slashes. Do not flip slashes immediately before spaces

Parameters:
  • s (string or list of strings) - path name(s)
Returns: string or list of strings
string(s) with forward slashes

prefix_files(dir, input_files)

source code 

Add dir on to the front of the input file or files. Works with strings or lists of strings.

Parameters:
  • dir (string) - prefix directory
  • input_files (string or list of strings) - name(s) of files
Returns: string or list of strings
input file(s) prefixed with dir sp

print_elapsed_time(start_time, end_time=None, prefix=None, current=False)

source code 

print the elapsed time in seconds or minutes.

Parameters:
  • start_time (float) - the starting time
  • end_time (float) - the ending time (optional)
  • prefix (string) - a string to print at the start of the line (optional)

quote(fn)

source code 

Add quotes around the file nameed fn. Return a string

remove_file(fn, env=None, quiet=True)

source code 

Remove a file or link if it exists. env parameter is not used.

remove_files(lst, env=None)

source code 

Remove all the files in the list of files, lst. The env parameter is not used

remove_files_glob(lst, env=None)

source code 

Remove all files in the list of wild card expressions. The env parameter is not used

remove_tree(dir_name, env=None, dangerous=False)

source code 

Remove a directory if it exists. env parameter is not used. This will not remove a directory that has a .svn subdirectory indicating it is a source directory. Warning: It does not look recursively for .svn subdirectories.

Parameters:
  • dir_name (string) - a directory name
  • env (env_t) - optional. Not currently used.
  • dangerous (bool) - optional. If True,will delete anything including svn trees!! BE CAREFUL! default False.

run_cmd_io(cmd, fn_i, fn_o, shell_executable=None, directory=None)

source code 

Run a command string using the subprocess module. Read standard input from fn_i and write stdout/stderr to fn_o.

Parameters:
  • cmd (string) - command line to execut with all args.
  • fn_i (string) - input file name
  • fn_o (string) - output file name
  • shell_executable (string) - the shell executable
  • directory (string) - a directory to change to before running the command.
Returns: integer
return code

run_command(cmd, separate_stderr=False, shell_executable=None, directory=None, osenv=None, input_file_name=None, **kwargs)

source code 

Run a command string using the subprocess module.

Parameters:
  • cmd (string) - command line to execut with all args.
  • separate_stderr (bool) - If True, the return tuple has a list of stderr lines as the 3rd element
  • shell_executable (string) - the shell executable
  • directory (string) - a directory to change to before running the command.
  • osenv (dictionary) - dict of environment vars to be passed to the new process
  • input_file_name (string) - file name to read stdin from. Default none
Returns: tuple
(return code, list of stdout lines, list of lines of stderr)

run_command_output_file(cmd, output_file_name, shell_executable=None, directory=None, osenv=None, input_file_name=None, **kwargs)

source code 

Run a command string using the subprocess module.

Parameters:
  • cmd (string) - command line to execut with all args.
  • output_file_name (string) - output file name
  • shell_executable (string) - the shell executable
  • directory (string) - a directory to change to before running the command.
  • osenv (dictionary) - dict of environment vars to be passed to the new process
  • input_file_name (string) - file name to read stdin from. Default none
Returns: tuple
(return code, list of stdout lines)

run_command_timed(cmd, shell_executable=None, directory=None, osenv=None, seconds=0, input_file_name=None, **kwargs)

source code 

Run a timed command. kwargs are keyword args for subprocess.Popen.

Parameters:
  • cmd (string or python function) - command to run
  • shell_executable (string) - the shell executable
  • directory (string) - the directory to run the command in
  • osenv (dictionary) - dict of environment vars to be passed to the new process
  • seconds (number) - maximum execution time in seconds
  • input_file_name (string) - input filename when redirecting stdin.
  • kwargs (keyword args) - keyword args for subprocess.Popen
Returns: tuple return: (return code, list of stdout+stderr lines)

run_command_unbufferred(cmd, prefix_line=None, shell_executable=None, directory=None, osenv=None, input_file_name=None, **kwargs)

source code 

Run a command string using the subprocess module.

Parameters:
  • cmd (string) - command line to execut with all args.
  • prefix_line (string) - a string to prefix each output line. Default None
  • shell_executable (string) - NOT USED BY THIS FUNCTION
  • directory (string) - a directory to change to before running the command.
  • osenv (dictionary) - dict of environment vars to be passed to the new process
  • input_file_name (string) - file name to read stdin from. Default none
Returns: tuple
(return code, list of stdout lines, empty list)

strip_quotes(a)

source code 

Conditionally remove leading/trailing quotes from a string

Parameters:
  • a (string) - a string potentially with quotes
Returns: string
same string without the leading and trailing quotes

symlink(env, src, tgt)

source code 

Make a symlink from src to target. Not available on windows.

touch(fn)

source code 

Open a file for append. Write nothing to it