trlcdoc.util

Common helpers of the trlcdoc steps.

Attributes

MARKER_FILE

MARKER_TEXT

LOG

Exceptions

TrlcdocError

Error that stops the documentation build with a message.

Functions

find_tool(name[, hint]) → str

Find a console script, preferring the one of the running Python environment.

find_graphviz(purpose) → str

Find the Graphviz dot program, so a missing Graphviz stops the build instead of leaving out diagrams.

run(cmd, cwd[, env, stdin, source]) → int

Run an external command and stop the build if it fails.

run_unchecked(cmd, cwd[, env, stdin, source]) → int

Run an external command and return its exit code.

prepare_output(output) → pathlib.Path

Create the output folder if needed and mark it as owned by trlcdoc.

clean_dir(folder, output) → pathlib.Path

Create an empty folder inside the output folder, removing previous content.

remove_output(output) → bool

Remove the output folder with all its content.

git_output(args, cwd) → str | None

Run a git command and return its output.

git_root(path) → pathlib.Path | None

Get the root folder of the git repository that contains a folder.

repository_url(repository, root) → str

Get the repository URL for links to project files.

Module Contents

trlcdoc.util.MARKER_FILE = '.trlcdoc'
trlcdoc.util.MARKER_TEXT = Multiline-String
Show Value
"""Generated by trlcdoc. The whole folder is removed by 'trlcdoc clean' !
"""
trlcdoc.util.LOG
exception trlcdoc.util.TrlcdocError[source]

Bases: Exception

Inheritance diagram of trlcdoc.util.TrlcdocError

Error that stops the documentation build with a message.

trlcdoc.util.find_tool(name: str, hint: str = 'Install the trlcdoc package dependencies.') → str[source]

Find a console script, preferring the one of the running Python environment.

Tools that are not installed with trlcdoc, e.g. Java, are searched on the PATH.

Parameters:
  • name (str) – Name of the console script, e.g. ‘lobster-trlc’.

  • hint (str) – How to install the tool, added to the error message.

Returns:

Path of the executable.

Return type:

str

Raises:

TrlcdocError – If the tool is not installed.

trlcdoc.util.find_graphviz(purpose: str) → str[source]

Find the Graphviz dot program, so a missing Graphviz stops the build instead of leaving out diagrams.

Parameters:

purpose (str) – What needs Graphviz, added to the error message, e.g. ‘for the tracing policy diagram’.

Returns:

Path of the dot executable.

Return type:

str

Raises:

TrlcdocError – If dot is not on the PATH.

trlcdoc.util.run(cmd: list[str], cwd: pathlib.Path, env: dict[str, str] | None = None, stdin: str | None = None, source: str | None = None) → int[source]

Run an external command and stop the build if it fails.

Parameters:
  • cmd (list[str]) – Command and its arguments.

  • cwd (Path) – Working directory.

  • env (dict[str, str] | None) – Additional environment variables.

  • stdin (str | None) – Text passed to the standard input of the command.

  • source (str | None) – Configuration entry of the command, e.g. ‘tests.command’, named in the error message if the program can’t be started. None for the tools trlcdoc runs itself.

Returns:

The exit code, which is always 0.

Return type:

int

Raises:

TrlcdocError – If the program can’t be started or the command returns a non-zero exit code.

trlcdoc.util.run_unchecked(cmd: list[str], cwd: pathlib.Path, env: dict[str, str] | None = None, stdin: str | None = None, source: str | None = None) → int[source]

Run an external command and return its exit code.

The command is always shown. Verbose output adds the working folder, the environment variables the command gets from trlcdoc, the size of its input, its exit code and duration. The output of the command is not captured, it goes directly to the console.

Parameters:
  • cmd (list[str]) – Command and its arguments.

  • cwd (Path) – Working directory.

  • env (dict[str, str] | None) – Additional environment variables.

  • stdin (str | None) – Text passed to the standard input of the command.

  • source (str | None) – Configuration entry of the command, e.g. ‘tests.command’, named in the error message if the program can’t be started. None for the tools trlcdoc runs itself.

Returns:

The exit code of the command.

Return type:

int

Raises:

TrlcdocError – If the program is not found or not executable.

trlcdoc.util.prepare_output(output: pathlib.Path) → pathlib.Path[source]

Create the output folder if needed and mark it as owned by trlcdoc.

Parameters:

output (Path) – The output folder.

Returns:

The output folder.

Return type:

Path

Raises:

TrlcdocError – If the folder exists with content but without the marker file.

trlcdoc.util.clean_dir(folder: pathlib.Path, output: pathlib.Path) → pathlib.Path[source]

Create an empty folder inside the output folder, removing previous content.

Parameters:
  • folder (Path) – The folder, a subfolder of the output folder.

  • output (Path) – The output folder.

Returns:

The folder.

Return type:

Path

Raises:

TrlcdocError – If the folder is not inside a trlcdoc output folder.

trlcdoc.util.remove_output(output: pathlib.Path) → bool[source]

Remove the output folder with all its content.

Parameters:

output (Path) – The output folder.

Returns:

True if the folder existed and was removed.

Return type:

bool

Raises:

TrlcdocError – If the folder exists with content but without the marker file.

trlcdoc.util.git_output(args: list[str], cwd: pathlib.Path) → str | None[source]

Run a git command and return its output.

Parameters:
  • args (list[str]) – git arguments.

  • cwd (Path) – Working directory.

Returns:

The stripped output, or None if git failed.

Return type:

str | None

trlcdoc.util.git_root(path: pathlib.Path) → pathlib.Path | None[source]

Get the root folder of the git repository that contains a folder.

The project root is a subfolder of the repository if several projects share one repository.

Parameters:

path (Path) – A folder inside the repository, e.g. the project root.

Returns:

The resolved repository root, or None if the folder is not in a git repository.

Return type:

Path | None

trlcdoc.util.repository_url(repository: str, root: pathlib.Path) → str[source]

Get the repository URL for links to project files.

Parameters:
  • repository (str) – Configured repository URL or ‘auto’ for the origin remote.

  • root (Path) – Project root, the git working directory.

Returns:

HTTPS repository URL without ‘.git’ suffix, the base of the file links.

Return type:

str

Raises:

TrlcdocError – If ‘auto’ is configured and git has no origin remote.