reviewbot.utils.filesystem¶
Utilities for filesystem operations and path normalization.
Functions
|
Temporarily change directory into the given working directory. |
Clean up all temporary files. |
|
|
Ensure directories exist to an absolute path. |
|
Return the platform most likely used to generate a path. |
Create a temporary directory and return the path. |
|
|
Create a temporary file and return the path. |
|
Normalize a path from a diff, making it suitable for local use. |
Classes
|
The platform used to generate a filesystem path. |
- class PathPlatform(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
Enum
The platform used to generate a filesystem path.
New in version 3.0.
- LOCAL = 'posix'¶
- chdir(path)[source]¶
Temporarily change directory into the given working directory.
- Parameters:
path (
str
) – The directory to operate within.
- cleanup_tempfiles()[source]¶
Clean up all temporary files.
This will delete all the files created by
make_tempfile()
.
- make_tempfile(content=None, extension='')[source]¶
Create a temporary file and return the path.
- Parameters:
content (
bytes
, optional) – Optional content to put in the file.extension (
str
, optional) – An optional file extension to add to the end of the filename.
- Returns:
The name of the new file.
- Return type:
str
- make_tempdir()[source]¶
Create a temporary directory and return the path.
- Returns:
The name of the new directory.
- Return type:
str
- ensure_dirs_exist(path)[source]¶
Ensure directories exist to an absolute path.
- Parameters:
path (
str
) – The absolute path for which directories should be created if they don’t exist.- Raises:
ValueError – If the path is not absolute.
OSError – If making the directory failed.
- get_path_platform(path)[source]¶
Return the platform most likely used to generate a path.
This supports Windows and POSIX filesystem and UNC paths.
New in version 3.0.
- Parameters:
path (
str
) – The Windows or POSIX path.- Returns:
The platform that the path is most likely for.
- Return type:
- normalize_platform_path(path, relative_to=None, target_platform=PathPlatform.POSIX)[source]¶
Normalize a path from a diff, making it suitable for local use.
This will convert either a Windows or POSIX path to the local platform, collapsing any relative components (such as
..
) within, and then making the result relative.The path can optionally be joined to a path specified in
relative_to
.New in version 3.0.
- Parameters:
path (
str
) – The path to normalize.relative_to (
str
, optional) – An optional directory that the normalized path should be joined to.target_platform (
PathPlatform
, optional) – The target platform for the normalized path. This is mostly for unit testing purposes.
- Returns:
The resulting normalized path.
- Return type:
str
- Raises:
reviewbot.errors.SuspiciousFilePath – The resulting normalized file path is suspicious. It would have resulted in access outside the source tree.