serena.util.file_system#


class ScanResult(directories, files)[source]#

Bases: NamedTuple

Result of scanning a directory.

Create new instance of ScanResult(directories, files)

Parameters:
  • directories (list[str])

  • files (list[str])

directories: list[str]#

Alias for field number 0

files: list[str]#

Alias for field number 1

scan_directory(
path,
recursive=False,
relative_to=None,
is_ignored_dir=None,
is_ignored_file=None,
)[source]#
Parameters:
  • path (str) – the path to scan

  • recursive (bool) – whether to recursively scan subdirectories

  • relative_to (str | None) – the path to which the results should be relative to; if None, provide absolute paths

  • is_ignored_dir (Callable[[str], bool] | None) – a function with which to determine whether the given directory (abs. path) shall be ignored

  • is_ignored_file (Callable[[str], bool] | None) – a function with which to determine whether the given file (abs. path) shall be ignored

Returns:

the list of directories and files

Return type:

ScanResult

find_all_non_ignored_files(repo_root)[source]#

Find all non-ignored files in the repository, respecting all gitignore files in the repository.

Parameters:

repo_root (str) – The root directory of the repository

Returns:

A list of all non-ignored files in the repository

Return type:

list[str]

class GitignoreSpec(file_path: str, patterns: list[str] = <factory>)[source]#

Bases: object

Parameters:
  • file_path (str)

  • patterns (list[str])

file_path: str#

Path to the gitignore file.

patterns: list[str]#

List of patterns from the gitignore file. The patterns are adjusted based on the gitignore file location.

pathspec: PathSpec#

Compiled PathSpec object for pattern matching.

matches(relative_path)[source]#

Check if the given path matches any pattern in this gitignore spec.

Parameters:

relative_path (str) – Path to check (should be relative to repo root)

Returns:

True if path matches any pattern

Return type:

bool

class GitignoreParser(repo_root)[source]#

Bases: object

Parser for gitignore files in a repository.

This class handles parsing multiple gitignore files throughout a repository and provides methods to check if paths should be ignored.

Initialize the parser for a repository.

Parameters:

repo_root (str) – Root directory of the repository

should_ignore(path)[source]#

Check if a path should be ignored based on the gitignore rules.

Parameters:

path (str) – Path to check (absolute or relative to repo_root)

Returns:

True if the path should be ignored, False otherwise

Return type:

bool

get_ignore_specs()[source]#

Get all loaded gitignore specs.

Returns:

List of GitignoreSpec objects

Return type:

list[GitignoreSpec]

reload()[source]#

Reload all gitignore files from the repository.

Return type:

None

match_path(relative_path, path_spec, root_path='')[source]#

Match a relative path against a given pathspec. Just pathspec.match_file() is not enough, we need to do some massaging to fix issues with pathspec matching.

Parameters:
  • relative_path (str) – relative path to match against the pathspec

  • path_spec (PathSpec) – the pathspec to match against

  • root_path (str) – the root path from which the relative path is derived

Returns:

Return type:

bool