craft_parts.utils.os_utils module

Utilities related to the operating system.

class craft_parts.utils.os_utils.OsRelease(*, os_release_file='/etc/os-release')[source]

Bases: object

A class to intelligently determine the OS on which we’re running.

Parameters:

os_release_file (str)

id()[source]

Return the OS ID.

Raises:

OsReleaseIdError – If no ID can be determined.

Return type:

str

name()[source]

Return the OS name.

Raises:

OsReleaseNameError – If no name can be determined.

Return type:

str

version_id()[source]

Return the OS version ID.

Raises:

OsReleaseVersionIdError – If no version ID can be determined.

Return type:

str

class craft_parts.utils.os_utils.TimedWriter[source]

Bases: object

Enforce minimum times between writes.

Ensure subsequent writes happen at least at the specified minimum interval apart from each other, otherwise hosts with low tick resolution may generate files with identical timestamps.

classmethod write_text(filepath, text, encoding=None, errors=None)[source]

Write text to the specified file.

Parameters:
  • filepath (Path) – The path to the file to write to.

  • text (str) – The text to write.

  • encoding (Optional[str]) – The name of the encoding used to encode and decode the file. See the corresponding parameter in os.open for details.

  • errors (Optional[str]) – How encoding/decoding errors are handled, in the same format used in os.open.

Return type:

None

craft_parts.utils.os_utils.get_bin_paths(*, root, existing_only=True)[source]

List common system executable paths.

Parameters:
  • root (Path) – A path to prepend to each entry in the list.

  • existing_only (bool) – Only list paths that are present in the system.

Return type:

list[str]

Returns:

The list of executable paths.

craft_parts.utils.os_utils.get_include_paths(*, root, arch_triplet)[source]

List common include paths.

Parameters:
  • root (Path) – A path to prepend to each entry in the list.

  • arch_triplet (str)

Arch_triplet:

The machine-vendor-os platform triplet definition.

Return type:

list[str]

Returns:

The list of include paths.

craft_parts.utils.os_utils.get_library_paths(*, root, arch_triplet, existing_only=True)[source]

List common library paths.

Parameters:
  • root (Path) – A path to prepend to each entry in the list.

  • existing_only (bool) – Only list paths that are present in the system.

  • arch_triplet (str)

Arch_triplet:

The machine-vendor-os platform triplet definition.

Return type:

list[str]

Returns:

The list of library paths.

craft_parts.utils.os_utils.get_pkg_config_paths(*, root, arch_triplet)[source]

List common pkg-config paths.

Parameters:
  • root (Path) – A path to prepend to each entry in the list.

  • arch_triplet (str)

Arch_triplet:

The machine-vendor-os platform triplet definition.

Return type:

list[str]

Returns:

The list of pkg-config paths.

craft_parts.utils.os_utils.get_system_info()[source]

Obtain running system information.

Return type:

str

craft_parts.utils.os_utils.is_dumb_terminal()[source]

Verify whether the caller is running on a dumb terminal.

Return type:

bool

Returns:

True if on a dumb terminal.

craft_parts.utils.os_utils.is_inside_container()[source]

Determine if the application is in a container.

Return type:

bool

Returns:

Whether the process is running inside a container.

craft_parts.utils.os_utils.is_snap(application_name)[source]

Verify whether we’re running as a snap.

Application_name:

The snap application name. If provided, check if it matches the snap name.

Parameters:

application_name (str)

Return type:

bool

craft_parts.utils.os_utils.mount(device, mountpoint, *args)[source]

Mount a filesystem.

Parameters:
  • device (str) – The device to mount.

  • mountpoint (str) – Where the device will be mounted.

  • args (str) – Additional arguments to mount(8).

Raises:

subprocess.CalledProcessError – on error.

Return type:

None

craft_parts.utils.os_utils.mount_overlayfs(mountpoint, *args)[source]

Mount an overlay filesystem using fuse-overlayfs.

Parameters:
  • mountpoint (str) – Where the device will be mounted.

  • args (str) – Additional arguments to mount(8).

Raises:

subprocess.CalledProcessError – on error.

Return type:

None

craft_parts.utils.os_utils.process_run(command, log_func, **kwargs)[source]

Run a command and handle its output.

Parameters:
  • command (list[str])

  • log_func (Callable[[str], None])

  • kwargs (Any)

Return type:

None

craft_parts.utils.os_utils.umount(mountpoint, *args)[source]

Unmount a filesystem.

Parameters:
  • mountpoint (str) – The mount point or device to unmount.

  • args (str)

Raises:

subprocess.CalledProcessError – on error.

Return type:

None