sos.utilities — Utilites Interface¶
- class sos.utilities.ImporterHelper(package)[source]¶
Bases:
objectProvides a list of modules that can be imported in a package. Importable modules are located along the module __path__ list and modules are files that end in .py.
- sos.utilities.bold(text)[source]¶
Helper to make text bold in console output, without pulling in dependencies to the project unneccessarily.
- Parameters:
text (
str) – The text to make bold- Returns:
The text wrapped in the ASCII codes to display as bold
- Return type:
str
- sos.utilities.file_is_binary(fname)[source]¶
Helper to determine if a given file contains binary content or not.
This is especially helpful for sos clean, which cannot obfuscate binary data and instead, by default, will remove binary files.
- Parameters:
fname (
str) – The full path of the file to check binaryness of- Returns:
True if binary, else False
- Return type:
bool
- sos.utilities.fileobj(path_or_file, mode='r')[source]¶
Returns a file-like object that can be used as a context manager
- sos.utilities.find(file_pattern, top_dir, max_depth=None, path_pattern=None)[source]¶
Generator function to find files recursively. Usage:
for filename in find("*.properties", "/var/log/foobar"): print filename
- sos.utilities.grep(pattern, *files_or_paths)[source]¶
Returns lines matched in fnames, where fnames can either be pathnames to files to grep through or open file objects to grep through line by line
- sos.utilities.import_module(module_fqname, superclasses=None)[source]¶
Imports the module module_fqname and returns a list of defined classes from that module. If superclasses is defined then the classes returned will be subclasses of the specified superclass or superclasses. If superclasses is plural it must be a tuple of classes.
- sos.utilities.is_executable(command, sysroot=None)[source]¶
Returns if a command matches an executable on the PATH
- sos.utilities.parse_version(version: str) Version¶
Parse the given version string.
>>> parse('1.0.dev1') <Version('1.0.dev1')>
- Parameters:
version – The version string to parse.
- Raises:
InvalidVersion – When the version string is not a valid version.
- sos.utilities.recursive_dict_values_by_key(dobj, keys=[])[source]¶
Recursively compile all elements of a potentially nested dict by a set of keys. If a given key is a dict within
dobj, then _all_ elements within that dict, regardless of child keys, will be returned.For example, if a Plugin searches the devices dict for the ‘storage’ key, then all storage devices under the that dict (e.g. block, fibre, etc…) will be returned. However, if the Plugin specifies ‘block’ via
keys, then only the block devices within the devices[‘storage’] dict will be returned.Any elements passed here that are _not_ keys within the dict or any nested dicts will also be returned.
- Parameters:
dobj (
dict) – The ‘top-level’ dict to intially search bykeys – Which keys to compile elements from within
dobj. If no keys are given, all nested elements are returnedkeys –
listofstr
- Returns:
All elements within the dict and any nested dicts
- Return type:
list
- sos.utilities.shell_out(cmd, timeout=30, chroot=None, runat=None)[source]¶
Shell out to an external command and return the output or the empty string in case of error.
- sos.utilities.sos_get_command_output(command, timeout=300, stderr=False, chroot=None, chdir=None, env=None, foreground=False, binary=False, sizelimit=None, poller=None, to_file=False, tac=False, runas=None)[source]¶
Execute a command and return a dictionary of status and output, optionally changing root or current working directory before executing command.