datumaro.util.os_util#

Functions

catch_output()

check_instruction_set(instruction)

contains_unsafe_path_component(path)

Check if any "/" or ""-separated component of path is (or, once Win32's trailing dot/space trimming is accounted for, resolves to) the parent directory reference "..".

copytree(src, dst)

dir_items(path, ext[, truncate_ext])

ensure_within_base(path, base_dir)

Validate that an already-constructed path (e.g. one supplied directly by a caller instead of being composed from parts via join_within_base) stays within base_dir.

extract_subset_name_from_parent(url, start)

Extract subset name from the given url.

find_files(dirpath, exts[, recursive, ...])

generate_next_name(names, basename[, sep, ...])

Generates the "next" name by appending a next index to the occurrence of the basename with the highest index in the input collection.

get_all_file_extensions(path, ignore_dirs)

import_foreign_module(name, path)

is_subpath(path, base)

Tests if a path is subpath of another path or the paths are equal.

join_within_base(base_dir, *parts)

Join parts onto base_dir, and reject the result if it escapes base_dir.

make_file_name(s)

Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens.

split_path(path)

suppress_output([stdout, stderr])

walk(path[, max_depth, min_depth])

datumaro.util.os_util.check_instruction_set(instruction)[source]#
datumaro.util.os_util.contains_unsafe_path_component(path: str) → bool[source]#

Check if any “/” or “"-separated component of path is (or, once Win32’s trailing dot/space trimming is accounted for, resolves to) the parent directory reference “..”.

datumaro.util.os_util.join_within_base(base_dir: str, *parts: str) → str[source]#

Join parts onto base_dir, and reject the result if it escapes base_dir.

Untrusted values (e.g. dataset item ids) can contain ‘..’ or be absolute, so a plain osp.join() can traverse outside of base_dir. Raises ValueError in that case (including when the paths don’t share a common root, e.g. different drives on Windows, or when a part would resolve to ‘..’ only after Win32’s trailing dot/space trimming is applied).

datumaro.util.os_util.ensure_within_base(path: str, base_dir: str) → str[source]#

Validate that an already-constructed path (e.g. one supplied directly by a caller instead of being composed from parts via join_within_base) stays within base_dir. Raises ValueError otherwise.

datumaro.util.os_util.import_foreign_module(name, path)[source]#
datumaro.util.os_util.walk(path, max_depth: int | None = None, min_depth: int | None = None)[source]#
datumaro.util.os_util.find_files(dirpath: str, exts: str | Iterable[str], recursive: bool = False, max_depth: int | None = None, min_depth: int | None = None) → Iterator[str][source]#
datumaro.util.os_util.copytree(src, dst)[source]#
datumaro.util.os_util.suppress_output(stdout: bool = True, stderr: bool = False)[source]#
datumaro.util.os_util.catch_output()[source]#
datumaro.util.os_util.dir_items(path, ext, truncate_ext=False)[source]#
datumaro.util.os_util.split_path(path)[source]#
datumaro.util.os_util.is_subpath(path: str, base: str) → bool[source]#

Tests if a path is subpath of another path or the paths are equal.

datumaro.util.os_util.make_file_name(s: str) → str[source]#

Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens.

datumaro.util.os_util.generate_next_name(names: Iterable[str], basename: str, sep: str = '.', suffix: str = '', default: str | None = None) → str[source]#

Generates the “next” name by appending a next index to the occurrence of the basename with the highest index in the input collection.

Returns: next string name

Example:

Inputs:

name_abc

name_base

name_base1

name_base5

Basename: name_base

Output: name_base6

datumaro.util.os_util.extract_subset_name_from_parent(url: str, start: str) → str[source]#

Extract subset name from the given url.

For example, if url = “/a/b/images/train/img.jpg” and start = “/a/b”, it will return “train”. On the other hand, if url = “/a/b/images/img.jpg” and start = “/a/b”, it will return DEFAULT_SUBSET_NAME.

Parameters:
  • url (str) – Given url to extract subset

  • start – The head path of url to obtain the relative path from the url

Returns:

Subset name

Return type:

str

datumaro.util.os_util.get_all_file_extensions(path: str, ignore_dirs: Set[str]) → List[str][source]#