taucmdr.util module¶
Utility functions.
Handles system manipulation and status tasks, e.g. subprocess management or file creation.
- taucmdr.util._ARCHIVE_ERRORS = (<class 'tarfile.TarError'>, <class 'EOFError'>, <class 'zlib.error'>, <class '_lzma.LZMAError'>)¶
Exceptions raised while reading a corrupt or truncated archive, such as an interrupted download.
- Type:
- taucmdr.util._cleanup_dtemp()[source]¶
At exit, keep copies of failed builds’ temporary directories, then delete all temporary directories.
- taucmdr.util._extract_all(archive, dest, members=None)[source]¶
Extract members of the open tar file archive into dest.
- Parameters:
archive (tarfile.TarFile) – Open tar file.
dest (str) – Destination folder.
members – Members to extract, or None for all of them.
- taucmdr.util._rmtree(path, ignore_errors, onexc)[source]¶
Call
shutil.rmtreewith an onexc handler on any Python version.
- taucmdr.util.archive_toplevel(archive)[source]¶
Returns the name of the top-level directory in an archive.
- Assumes that the archive file is rooted in a single top-level directory::
- foo
/bar /baz
The top-level directory here is “foo” This routine will return stupid results for archives with multiple top-level elements.
- taucmdr.util.cleanup_dtemp(path)[source]¶
Delete a temporary directory now instead of when the program exits.
- Parameters:
path (tempfile.TemporaryDirectory) – A directory from
mkdtemportempfile.TemporaryDirectory.
- taucmdr.util.copy_file(src, dest, show_progress=True)[source]¶
Works just like
shutil.copyexcept with progress bars.
- taucmdr.util.create_archive(fmt, dest, items, cwd=None, show_progress=True)[source]¶
Creates a new archive file in the specified format.
- taucmdr.util.create_subprocess(cmd, cwd=None, env=None, stdout=True, log=True, show_progress=False, error_buf=50, record_output=False)[source]¶
Create a subprocess.
See
subprocess.Popen.- Parameters:
cmd (list) – Command and its command line arguments.
cwd (str) – If not None, change directory to cwd before creating the subprocess.
env (dict) – Environment variables to set or unset before launching cmd.
stdout (bool) – If True send subprocess stdout and stderr to this processes’ stdout.
log (bool) – If True send subprocess stdout and stderr to the debug log.
error_buf (int) – If non-zero, stdout is not already being sent, and return value is non-zero then send last error_buf lines of subprocess stdout and stderr to this processes’ stdout.
record_output (bool) – If True return output.
- Returns:
Subprocess return code.
- Return type:
- taucmdr.util.download(src, dest, timeout=8)[source]¶
Downloads or copies files.
src may be a file path or URL. The destination folder will be created if it doesn’t exist. Download is via curl, wget, or Python’s urllib as appropriate.
- taucmdr.util.extract_archive(archive, dest, show_progress=True)[source]¶
Extracts archive file to dest.
Supports compressed and uncompressed tar archives. Destination folder will be created if it doesn’t exist.
- taucmdr.util.get_command_output(cmd)[source]¶
Return the possibly cached output of a command.
Just
subprocess.check_outputwith a cache. Subprocess stderr is always sent to subprocess stdout.- Parameters:
cmd (list) – Command and its command line arguments.
- Raises:
subprocess.CalledProcessError – return code was non-zero.
- Returns:
Subprocess output.
- Return type:
- taucmdr.util.hline(title, *args, **kwargs)[source]¶
Build a colorful horizontal rule for console output.
Uses
logger.LINE_WIDTHto generate a string of ‘=’ characters as wide as the terminal. title is included in the string near the left of the horizontal line.
- taucmdr.util.is_clean_container(obj)[source]¶
Recursively checks a container for bytes, bytearray and memory view objects in keys and values. Care must be taken to avoid infinite loops caused by cycles in a dictionary with cycles.
- Parameters:
obj (any) – Container to be checked for disallowed binary data
- Returns:
Whether or not bad binary entries were found
- Return type:
- taucmdr.util.mkdirp(*args)[source]¶
Creates a directory and all its parents.
Works just like
mkdir -p.- Parameters:
*args – Paths to create.
- taucmdr.util.mkdtemp(*args, **kwargs)[source]¶
Like tempfile.mkdtemp but directory will be recursively deleted when program exits.
- Returns:
The new directory. Pass it to
cleanup_dtempto delete it sooner.- Return type:
- taucmdr.util.page_output(output_string: str)[source]¶
Pipe string to a pager.
If PAGER is an environment then use that as pager, otherwise use less.
- Parameters:
output_string (str) – String to put output.
- taucmdr.util.parse_bool(value, additional_true=None, additional_false=None)[source]¶
Parses a value to a boolean value.
If value is a string try to interpret it as a bool: * [‘1’, ‘t’, ‘y’, ‘true’, ‘yes’, ‘on’] ==> True * [‘0’, ‘f’, ‘n’, ‘false’, ‘no’, ‘off’] ==> False Otherwise raise TypeError.
- Parameters:
- Returns:
True if value is true, False if value is false.
- Return type:
- Raises:
ValueError – value does not parse.
- taucmdr.util.path_accessible(path, mode='r')[source]¶
Check if a file or directory exists and is accessible.
Files are checked by attempting to open them with the given mode. Directories are checked by testing their access bits only, which may fail for some filesystems which may have permissions semantics beyond the usual POSIX permission-bit model. We’ll fix this if it becomes a problem.
- taucmdr.util.rmtree(path, ignore_errors=False, onexc=None, attempts=5)[source]¶
Wrapper around shutil.rmtree to work around stale or slow NFS directories.
Tries repeatedly to recursively remove path and sleeps between attempts.
- Parameters:
path (str) – A directory but not a symbolic link to a directory.
ignore_errors (bool) – If True then errors resulting from failed removals will be ignored. If False or omitted, such errors are handled by calling a handler specified by onexc or, if that is omitted, they raise an exception.
onexc – Callable that accepts function, path, and the exception. See
shutil.rmtree.attempts (int) – Number of times to repeat shutil.rmtree before giving up.
- taucmdr.util.tmpfs_prefix()[source]¶
Path to a uniquely named directory in a temporary filesystem, ideally a ramdisk.
/dev/shm is the preferred tmpfs, but if it’s unavailable or mounted with noexec then fall back to tempfile.gettempdir(), which is usually /tmp. If that filesystem is also unavailable then use the filesystem prefix of the highest writable storage container.
An attempt is made to ensure that there is at least 2GiB of free space in the selected filesystem.
- Returns:
- A uniquely-named directory in the temporary filesystem. The directory
and all its contents will be deleted when the program exits if it installs correctly.
- Return type:
- taucmdr.util.umask(new_mask)[source]¶
Context manager to temporarily set the process umask.
- Parameters:
new_mask – The argument to
os.umask.
- taucmdr.util.verify_archive(archive)[source]¶
Check that an archive can be read to its end.
Reading only an archive’s first members doesn’t catch an archive that was cut short, such as by an interrupted copy, so this reads the whole file.