pycloudlib.util module
Helpers for shell string and processing.
- pycloudlib.util.add_key_to_cloud_config(public_key: str, user_data: str | None = None)[source]
Add a public key to the cloud-config.
- pycloudlib.util.chmod(path, mode)[source]
Run chmod on a file or directory.
- Parameters:
path – string of path to run on
mode – int of mode to apply
- pycloudlib.util.get_query_param(uri: str, param: str) list[source]
Extract query query params of param from uri.
>>> url = "https://cloud.com/v1/vpcs?limit=1&start=r134-fe06d70f" >>> get_query_param(url, "start") ['r134-fe06d70f']
- pycloudlib.util.get_query_params(uri: str) Dict[str, list][source]
Extract query params from uri.
>>> url = "https://cloud.com/v1/vpcs?limit=1&start=r134-fe06d70f" >>> get_query_params(url) {'limit': ['1'], 'start': ['r134-fe06d70f']}
- pycloudlib.util.get_timestamped_tag(tag)[source]
Create tag with current timestamp.
- Parameters:
tag – string, Base tag to be used
- Returns
An updated tag with current timestamp
- pycloudlib.util.is_writable_dir(path)[source]
Make sure dir is writable.
- Parameters:
path – path to determine if writable
- Returns:
boolean with result
- pycloudlib.util.log_exception_list(exceptions: List[Exception])[source]
Print a list of exceptions (including traceback) to stderr.
- pycloudlib.util.mkdtemp(prefix='pycloudlib')[source]
Make a temporary directory.
- Parameters:
prefix – optional, temproary dir name prefix (default: pycloudlib)
- Returns:
tempfile object that was created
- pycloudlib.util.shell_pack(cmd)[source]
Return a string that can shuffled through ‘sh’ and execute cmd.
- In Python subprocess terms:
check_output(cmd) == check_output(shell_pack(cmd), shell=True)
- Parameters:
cmd – list or string of command to pack up
- Returns:
base64 encoded string
- pycloudlib.util.shell_quote(cmd)[source]
Quote a shell string.
- Parameters:
cmd – command to quote
- Returns:
quoted string
- pycloudlib.util.shell_safe(cmd)[source]
Produce string safe shell string.
Create a string that can be passed to $(set – <string>) to produce the same array that cmd represents.
Internally we utilize ‘getopt’s ability/knowledge on how to quote strings to be safe for shell. This implementation could be changed to be pure python. It is just a matter of correctly escaping or quoting characters like: ‘ “ ^ & $ ; ( ) …
- Parameters:
cmd – command as a list
- Returns:
shell safe string
- pycloudlib.util.subp(args, data=None, env=None, shell=False, rcs=(0,), shortcircuit_stdin=True)[source]
Subprocess wrapper.
- Parameters:
args – command to run
data – data to pass
env – optional env to use
shell – optional shell to use
rcs – tuple of successful exit codes, default: (0)
shortcircuit_stdin – bind stdin to /dev/null if no data is given
- Returns:
Tuple of out, err, return_code