ocebuild.filesystem#

Shared filesystem utilities.

Submodules#

Package Contents#

Functions#

extract_archive(...)

Extracts a file from a URL and yields a temporary extraction directory.

clear_cache(cache_dirs)

Clears all cache directories

copy(→ None)

Copies a file or directory.

remove(→ None)

Removes a file or directory.

rename(→ ocebuild.third_party.cpython.pathlib.Path)

Renames a file or directory.

move(→ ocebuild.third_party.cpython.pathlib.Path)

Moves a file or directory to a new location.

glob(...)

Returns a list of paths matching the given pattern.

Attributes#

CACHE_DIR

Global cache directory for storing and re-using files between builds.

UNPACK_DIR

Directory for unpacking and handling remote or cached archives.

ocebuild.filesystem.extract_archive(url: str | urllib.request.Request, persist: bool = False) Generator[ocebuild.third_party.cpython.pathlib.Path, str, None][source]#

Extracts a file from a URL and yields a temporary extraction directory.

Parameters:
  • url – URL of the archive file.

  • persist – Flag to disable cleanup of the temporary directory.

Yields:

tmp_dir (str) – Path to the temporary directory.

Example

>>> with extract_archive('https://example.com/foo.zip') as tmp_dir:
print(tmp_dir)
# -> "/tmp/xxxxxx"
ocebuild.filesystem.CACHE_DIR[source]#

Global cache directory for storing and re-using files between builds.

ocebuild.filesystem.UNPACK_DIR[source]#

Directory for unpacking and handling remote or cached archives.

ocebuild.filesystem.clear_cache(cache_dirs: List[ocebuild.third_party.cpython.pathlib.Path])[source]#

Clears all cache directories

ocebuild.filesystem.copy(src: str | PathLike[str], dest: str | PathLike[str], **kwargs) None[source]#

Copies a file or directory.

Parameters:

path – Path to the file or directory.

Raises:

ValueError – If the path is not a file or directory.

ocebuild.filesystem.remove(path: str | PathLike[str]) None[source]#

Removes a file or directory.

Parameters:

path – Path to the file or directory.

Raises:

ValueError – If the path is not a file or directory.

ocebuild.filesystem.rename(path: str | PathLike[str], name: str) ocebuild.third_party.cpython.pathlib.Path[source]#

Renames a file or directory.

Parameters:
  • path – Path to the file or directory.

  • name – New name for the file or directory.

Returns:

The renamed path.

Raises:
  • FileNotFoundError – If the file or directory does not exist.

  • OSError – If the file or directory cannot be renamed.

ocebuild.filesystem.move(src: str | PathLike[str], target: str | PathLike[str], name: str | None = None, **kwargs) ocebuild.third_party.cpython.pathlib.Path[source]#

Moves a file or directory to a new location.

This is a simple wrapper over shutil’s move method that recursively creates missing directories in the target path.

Parameters:
  • src – Source path.

  • target – Destination path.

  • name (Optional) – Destination file or directory name.

Returns:

The destination path.

ocebuild.filesystem.glob(directory: str | PathLike[str], pattern: str, include: str | List[str] | None = None, exclude: str | List[str] | None = None, first: bool | None = False) Generator[ocebuild.third_party.cpython.pathlib.Path, None, None] | ocebuild.third_party.cpython.pathlib.Path | None[source]#

Returns a list of paths matching the given pattern.

Parameters:
  • directory – Directory to search.

  • pattern – Glob pattern.

  • include – A glob pattern or list of glob patterns to include.

  • exclude – A glob pattern or list of glob patterns to exclude.

  • first (Optional) – Whether to return only the first match.

Returns:

A list of matching paths. Instead returns the first matching path if first is True.