bazel-skylib/docs/paths_doc.md

7.9 KiB
Executable File

Skylib module containing file path manipulation functions.

NOTE: The functions in this module currently only support paths with Unix-style path separators (forward slash, "/"); they do not handle Windows-style paths with backslash separators or drive letters.

paths.basename

paths.basename(p)

Returns the basename (i.e., the file portion) of a path.

Note that if p ends with a slash, this function returns an empty string. This matches the behavior of Python's os.path.basename, but differs from the Unix basename command (which would return the path segment preceding the final slash).

PARAMETERS

Name Description Default Value
p The path whose basename should be returned. none

RETURNS

The basename of the path, which includes the extension.

paths.dirname

paths.dirname(p)

Returns the dirname of a path.

The dirname is the portion of p up to but not including the file portion (i.e., the basename). Any slashes immediately preceding the basename are not included, unless omitting them would make the dirname empty.

PARAMETERS

Name Description Default Value
p The path whose dirname should be returned. none

RETURNS

The dirname of the path.

paths.is_absolute

paths.is_absolute(path)

Returns True if path is an absolute path.

PARAMETERS

Name Description Default Value
path A path (which is a string). none

RETURNS

True if path is an absolute path.

paths.is_normalized

paths.is_normalized(str, look_for_same_level_references)

Returns true if the passed path doesn't contain uplevel references "..".

Also checks for single-dot references "." if look_for_same_level_references is True.

PARAMETERS

Name Description Default Value
str The path string to check. none
look_for_same_level_references If True checks if path doesn't contain uplevel references ".." or single-dot references ".". True

RETURNS

True if the path is normalized, False otherwise.

paths.join

paths.join(path, others)

Joins one or more path components intelligently.

This function mimics the behavior of Python's os.path.join function on POSIX platform. It returns the concatenation of path and any members of others, inserting directory separators before each component except the first. The separator is not inserted if the path up until that point is either empty or already ends in a separator.

If any component is an absolute path, all previous components are discarded.

PARAMETERS

Name Description Default Value
path A path segment. none
others Additional path segments. none

RETURNS

A string containing the joined paths.

paths.normalize

paths.normalize(path)

Normalizes a path, eliminating double slashes and other redundant segments.

This function mimics the behavior of Python's os.path.normpath function on POSIX platforms; specifically:

  • If the entire path is empty, "." is returned.
  • All "." segments are removed, unless the path consists solely of a single "." segment.
  • Trailing slashes are removed, unless the path consists solely of slashes.
  • ".." segments are removed as long as there are corresponding segments earlier in the path to remove; otherwise, they are retained as leading ".." segments.
  • Single and double leading slashes are preserved, but three or more leading slashes are collapsed into a single leading slash.
  • Multiple adjacent internal slashes are collapsed into a single slash.

PARAMETERS

Name Description Default Value
path A path. none

RETURNS

The normalized path.

paths.relativize

paths.relativize(path, start)

Returns the portion of path that is relative to start.

Because we do not have access to the underlying file system, this implementation differs slightly from Python's os.path.relpath in that it will fail if path is not beneath start (rather than use parent segments to walk up to the common file system root).

Relativizing paths that start with parent directory references only works if the path both start with the same initial parent references.

PARAMETERS

Name Description Default Value
path The path to relativize. none
start The ancestor path against which to relativize. none

RETURNS

The portion of path that is relative to start.

paths.replace_extension

paths.replace_extension(p, new_extension)

Replaces the extension of the file at the end of a path.

If the path has no extension, the new extension is added to it.

PARAMETERS

Name Description Default Value
p The path whose extension should be replaced. none
new_extension The new extension for the file. The new extension should begin with a dot if you want the new filename to have one. none

RETURNS

The path with the extension replaced (or added, if it did not have one).

paths.split_extension

paths.split_extension(p)

Splits the path p into a tuple containing the root and extension.

Leading periods on the basename are ignored, so path.split_extension(".bashrc") returns (".bashrc", "").

PARAMETERS

Name Description Default Value
p The path whose root and extension should be split. none

RETURNS

A tuple (root, ext) such that the root is the path without the file extension, and ext is the file extension (which, if non-empty, contains the leading dot). The returned tuple always satisfies the relationship root + ext == p.

paths.starts_with

paths.starts_with(path_a, path_b)

Returns True if and only if path_b is an ancestor of path_a.

Does not handle OS dependent case-insensitivity.

PARAMETERS

Name Description Default Value
path_a

-

none
path_b

-

none