devops.files.files

Module for file operations in mstd checks.

Functions

determine_file_type(filename)

Determine the file type based on the filename extension.

file_exist(file, *[, throwing, throw_msg])

Check if a file exists at the given path.

filter_cpp_files(files)

Filter and return only C++ related files from the given list.

get_dirs_in_dir([directory])

Get all directories in the specified directory.

get_files_in_dirs(paths[, exclude_dirs, ...])

Get all files in the specified directories.

get_staged_files()

Get the list of staged files in the git repository.

open_file(file[, mode])

Read the content of a file.

write_text(file, content)

Write text content to a file.

Classes

FileType(*values)

Enumeration of file types for mstd checks.

Exceptions

DevOpsFileNotFoundError(filepath[, message])

Exception raised when a specified file is not found.

exception devops.files.files.DevOpsFileNotFoundError(filepath, message=None)[source]

Bases: Exception

Exception raised when a specified file is not found.

Parameters:
  • filepath (Path)

  • message (str | None)

Return type:

None

class devops.files.files.FileType(*values)[source]

Bases: Enum

Enumeration of file types for mstd checks.

UNKNOWN = 0
CPPHeader = 1
CPPSource = 2
CMakeLists = 3
classmethod all_types()[source]

Get a set of all defined file types.

Returns:

set[FileType] – A set containing all file types.

Return type:

set[FileType]

classmethod cpp_types()[source]

Get a set of all CPP related file types.

Return type:

set[FileType]

classmethod is_cpp_type(file_type)[source]

Check if the given file type is a C++ related type.

Parameters:

file_type (FileType)

Return type:

bool

devops.files.files.determine_file_type(filename)[source]

Determine the file type based on the filename extension.

Parameters:

filename (str | Path) – The name of the file to check.

Returns:

FileType – The determined file type.

Return type:

FileType

devops.files.files.get_files_in_dirs(paths, exclude_dirs=None, exclude_files=None, max_recursion=20)[source]

Get all files in the specified directories.

Parameters:
  • paths (Iterable[Path]) – List of directory paths to search for files.

  • exclude_dirs (list[str] | None) – List of directory names to exclude from the search. Defaults to None.

  • exclude_files (list[str] | None) – List of file names to exclude from the search. Defaults to None.

  • max_recursion (int) – Number of maximum recursion through dirs to avoid infinite recursion, default 20

Returns:

list[Path] – List of file paths found in the specified directories.

Return type:

list[Path]

devops.files.files.get_dirs_in_dir(directory='.')[source]

Get all directories in the specified directory.

Parameters:

directory (str | Path) – The path to the directory to search. Defaults to the current directory.

Returns:

list[Path] – List of directory paths found in the specified directory.

Return type:

list[Path]

devops.files.files.get_staged_files()[source]

Get the list of staged files in the git repository.

Returns:

list[Path] – List of staged file paths.

Raises:

subprocess.CalledProcessError – If the git command fails.

Return type:

list[Path]

devops.files.files.file_exist(file, *, throwing=False, throw_msg=None)[source]

Check if a file exists at the given path.

Parameters:
  • file (str | Path) – The path to the file to check.

  • throwing (bool) – Whether to raise an exception if the file does not exist.

  • throw_msg (str | None) – Custom message to use when raising an exception if the file does not exist.

Returns:

bool – True if the file exists, False otherwise.

Raises:

DevOpsFileNotFoundError – If the file does not exist and throwing is True.

Return type:

bool

devops.files.files.open_file(file, mode='r')[source]

Read the content of a file.

Parameters:
  • file (str | Path) – The path to the file to read.

  • mode (str)

Yields:

typing.IO[str] – The opened file object.

Raises:

DevOpsFileNotFoundError – If the file does not exist.

Return type:

Generator[IO[str], None, None]

devops.files.files.write_text(file, content)[source]

Write text content to a file.

Parameters:
  • file (str | Path) – The path to the file to write.

  • content (str) – The text content to write to the file.

Return type:

None

devops.files.files.filter_cpp_files(files)[source]

Filter and return only C++ related files from the given list.

Parameters:

files (list[Path]) – The list of file paths to filter.

Returns:

list[Path] – The filtered list containing only C++ related files.

Return type:

list[Path]