libioc.helpers module

Collection of iocage helper functions.

libioc.helpers.exec(command, logger=None, ignore_error=False, **subprocess_args)[source]

Execute a shell command.

Return type

Tuple[Optional[str], Optional[str], int]

libioc.helpers.exec_generator(command, buffer_lines=True, summarize=True, encoding='UTF-8', universal_newlines=True, stdin=None, stdout=None, env=None, logger=None)[source]

Execute a command in an interactive shell.

Return type

Generator[bytes, None, Tuple[Optional[str], Optional[str], int]]

libioc.helpers.exec_passthru(command, logger=None, **subprocess_args)[source]

Execute a command in an interactive shell.

Return type

Tuple[Optional[str], Optional[str], int]

libioc.helpers.get_basedir_list(distribution_name='FreeBSD')[source]

Return the list of basedirs according to the host distribution.

Return type

List[str]

libioc.helpers.get_os_version(version_file='/bin/freebsd-version')[source]

Get the hosts userland version.

Return type

Dict[str, Union[str, int, float]]

libioc.helpers.get_random_uuid()[source]

Generate a random UUID.

Return type

str

libioc.helpers.is_uuid(text)[source]

Return True if the input string is an UUID.

Return type

bool

libioc.helpers.makedirs_safe(target, mode=448, logger=None)[source]

Create a directory without following symlinks.

Return type

None

libioc.helpers.mount(destination, source=None, fstype='nullfs', opts=[], logger=None, **iov_data)[source]

Mount a filesystem using libc.

Return type

None

libioc.helpers.parse_bool(data)[source]

Try to parse booleans from strings.

On success, it returns the parsed boolean on failure it raises a TypeError.

Usage:
>>> parse_bool("YES")
True
>>> parse_bool("false")
False
>>> parse_bool("/etc/passwd")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Not a boolean value
Return type

bool

libioc.helpers.parse_int(data)[source]

Try to parse integer from strings.

On success, it returns the parsed integer on failure it raises a TypeError.

Usage:
>>> parse_int("-1")
-1
>>> parse_int(3)
3
>>> parse_int(None)
TypeError: None is not a number
>>> parse_int("invalid")
TypeError: Value is not an integer: invalid
>>> parse_int(5.0)
5
>>> parse_int(5.1)
TypeError: Value is not an integer: 5.1
Return type

int

libioc.helpers.parse_list(data)[source]

Transform a comma separated string into a list.

Always returns a list of strings. This list is empty when an empty string is provided or the value is None. In any other case the string is split by comma and returned as list.

Return type

List[str]

libioc.helpers.parse_none(data, none_matches=['none', '-', ''])[source]

Raise if the input does not translate to None.

Return type

None

libioc.helpers.parse_user_input(data)[source]

Parse user input.

uses parse_bool() to partially return Boolean and None values All other types as returned as-is

>>> parse_user_input("YES")
True
>>> parse_user_input("false")
False
>>> parse_user_input("notfalse")
'notfalse'
>>> parse_user_input(8.4)
8.4
>>> parse_user_input(dict(ioc=123))
{'ioc': 123}
Return type

Union[str, bool, None]

Raise when the path contains a symlink.

Return type

None

libioc.helpers.split_list_string(data, separator=', ')[source]

Split by separator but keep escaped ones.

Parameters
  • data (str) – Input data that will be split by the separator.

  • separator (str) –

    (optional, default=”,”)

    Input data is split by this separator.

Example

>> split_strlist(“foo,bar,baz”) [“foo”, “bar,baz”]

Returns a list of strings from the input data where escaped separators are ignored and unescaped.

Return type

List[str]

libioc.helpers.to_humanreadable_name(name)[source]

Return a shorted UUID or the original name.

Return type

str

libioc.helpers.to_json(data)[source]

Create a JSON string from the input data.

Return type

str

libioc.helpers.to_string(data, true='yes', false='no', none='-', delimiter=', ')[source]

Translate complex types into a string.

Parameters
  • true (string) – The expected return value when data is True

  • false (string) – The expected return value when data is False

  • none (string) – The expected return value when data is None

Returns

Map input according to arguments or stringified input

Return type

string

Usage:
>>> to_string(True)
"yes"
>>> to_string(False)
"no"
>>> to_string(True, true="yip", false="nope")
"yip"
>>> to_string(False, true="yip", false="nope")
"nope"
>>> to_string(None)
"-"
libioc.helpers.to_ucl(data)[source]

Create UCL content from the input data.

Return type

str

libioc.helpers.umount(mountpoint=None, options=None, force=False, ignore_error=False, logger=None)[source]

Unmount a mountpoint using libc.

Return type

None

libioc.helpers.validate_name(name)[source]

Return True if the name matches the naming convention.

Return type

bool