craft_parts.utils.file_utils module

File-related utilities.

class craft_parts.utils.file_utils.NonBlockingRWFifo(path)[source]

Bases: object

A non-blocking FIFO for reading and writing.

Parameters:

path (str) –

close()[source]

Close the FIFO.

Return type:

None

property path: str

Return the path to the FIFO file.

Return type:

str

read()[source]

Read from the FIFO.

Return type:

str

write(data)[source]

Write to the FIFO.

Parameters:

data (str) – The data to write.

Return type:

int

craft_parts.utils.file_utils.calculate_hash(filename, *, algorithm)[source]

Calculate the hash of the given file.

Parameters:
  • filename (Path) – The path to the file to digest.

  • algorithm (str) – The algorithm to use, as defined by hashlib.

Return type:

str

Returns:

The file hash.

Raises:

ValueError – If the algorithm is unsupported.

craft_parts.utils.file_utils.copy(source, destination, *, follow_symlinks=False, permissions=None)[source]

Copy source and destination files.

This function overwrites the destination if it already exists, and also tries to copy ownership information.

Parameters:
  • source (str) – The source to be copied to destination.

  • destination (str) – Where to put the copy.

  • follow_symlinks (bool) – Whether or not symlinks should be followed.

  • permissions (Optional[List[Permissions]]) – The permissions definitions that should be applied to the new file.

Raises:

CopyFileNotFound – If source doesn’t exist.

Return type:

None

craft_parts.utils.file_utils.create_similar_directory(source, destination, permissions=None)[source]

Create a directory with the same permission bits and owner information.

Parameters:
  • source (str) – Directory from which to copy name, permission bits, and owner information.

  • destination (str) – Directory to create and to which the source information will be copied.

  • permissions (Optional[List[Permissions]]) – The permission definitions to apply to the new directory. If omitted, the new directory will have the same permissions and ownership of source.

Return type:

None

Hard-link source and destination files.

Parameters:
  • source (str) – The source to which destination will be linked.

  • destination (str) – The destination to be linked to source.

  • follow_symlinks (bool) – Whether or not symlinks should be followed.

Raises:

CopyFileNotFound – If source doesn’t exist.

Return type:

None

Hard-link source and destination files. Copy if it fails to link.

Hard-linking may fail (e.g. a cross-device link, or permission denied), so as a backup plan we just copy it. Note that we always copy the file if its permissions will change.

Parameters:
  • source (str) – The source to which destination will be linked.

  • destination (str) – The destination to be linked to source.

  • follow_symlinks (bool) – Whether or not symlinks should be followed.

  • permissions (Optional[List[Permissions]]) – The permissions definitions that should be applied to the new file.

Return type:

None

Copy a source tree into a destination, hard-linking if possible.

Parameters:
  • source_tree (str) – Source directory to be copied.

  • destination_tree (str) – Destination directory. If this directory already exists, the files in source_tree will take precedence.

  • ignore (Optional[Callable[[str, List[str]], List[str]]]) – If given, called with two params, source dir and dir contents, for every dir copied. Should return list of contents to NOT copy.

  • copy_function (Callable[..., None]) – Callable that actually copies.

Return type:

None