Skip to content

Commit d4e0c60

Browse files
author
Jose Alonso Solis Lemus
committed
carp_runner
1 parent 7745229 commit d4e0c60

1 file changed

Lines changed: 98 additions & 1 deletion

File tree

docs/API_reference.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,104 @@ A custom exception raised by `CommandRunner.run()` on failure. It is a subclass
185185

186186
---
187187

188-
### 5. Command-Line Interface (CLI)
188+
### 5. CARPentry Command Execution
189+
190+
**Entry Point:** `pycemrg.system.CarpRunner`
191+
192+
A specialized runner for executing commands from the CARPentry/openCARP ecosystem. Its primary responsibility is to correctly source the `config.sh` file from a CARPentry installation, setting up the complex environment (`PATH`, `PYTHONPATH`, license variables, etc.) before delegating execution to a generic `CommandRunner`.
193+
194+
**Instantiation:**
195+
196+
There are two primary ways to initialize the `CarpRunner`: by providing an explicit path or by using the auto-discovery class method.
197+
198+
* **1. Explicit Path (Recommended):**
199+
200+
```python
201+
import logging
202+
from pycemrg.system import CommandRunner, CarpRunner
203+
204+
# A generic CommandRunner is required
205+
runner = CommandRunner()
206+
207+
# Instantiate CarpRunner with the path to the installation's config.sh
208+
carp_runner = CarpRunner(
209+
runner=runner,
210+
carp_config_path="/path/to/your/carpentry_bundle/config.sh"
211+
)
212+
```
213+
214+
* **2. Auto-Discovery:**
215+
216+
```python
217+
# Use the classmethod to find the config file in common locations
218+
config_path = CarpRunner.find_installation()
219+
220+
if config_path:
221+
carp_runner = CarpRunner(runner=runner, carp_config_path=config_path)
222+
else:
223+
raise RuntimeError("Could not automatically locate CARPentry installation.")
224+
```
225+
226+
**Methods & Properties:**
227+
228+
#### `.run()`
229+
Execute a command within the fully configured CARPentry environment.
230+
231+
* **Signature:** `(cmd: Sequence[Union[str, Path]], expected_outputs: Optional[Sequence[Path]] = None, cwd: Optional[Path] = None, ignore_errors: Optional[Sequence[str]] = None) -> str`
232+
* **Args:**
233+
* `cmd` (Sequence[str | Path]): Command to execute (e.g., `['openCARP', '+F', 'sim.par']`).
234+
* *Other arguments are passed directly to the underlying `CommandRunner.run()` method.*
235+
* **Returns:**
236+
* `str`: The captured `stdout` from the command.
237+
* **Raises:**
238+
* `pycemrg.system.CommandExecutionError`: If the command fails.
239+
* `pycemrg.system.CarpEnvironmentError`: If the CARPentry environment fails to load.
240+
* `FileNotFoundError`: If expected outputs are missing after a successful run.
241+
242+
#### `.carp_env`
243+
A read-only property that returns the loaded CARPentry environment. The environment is lazy-loaded on first access and cached.
244+
245+
* **Type:** `property`
246+
* **Returns:**
247+
* `Dict[str, str]`: A dictionary of all environment variables sourced from `config.sh`.
248+
249+
#### `.installation_root`
250+
A read-only property that returns the root directory of the CARPentry installation.
251+
252+
* **Type:** `property`
253+
* **Returns:**
254+
* `pathlib.Path`: The absolute path to the CARPentry installation directory.
255+
256+
#### `.validate_command_exists()`
257+
Checks if a specific command (e.g., `openCARP`, `meshtool`) is available in the sourced environment's `PATH`.
258+
259+
* **Signature:** `(command: str) -> bool`
260+
* **Args:**
261+
* `command` (str): The name of the executable to check.
262+
* **Returns:**
263+
* `bool`: `True` if the command is found and executable, `False` otherwise.
264+
265+
#### `.find_installation()`
266+
A class method to search for a `config.sh` file in a list of common installation directories.
267+
268+
* **Signature:** `(search_paths: Optional[Sequence[Path]] = None) -> Optional[Path]`
269+
* **Type:** `classmethod`
270+
* **Args:**
271+
* `search_paths` (Optional[Sequence[Path]]): A list of directories to search. If `None`, a default list of common locations is used (e.g., `~/carpentry_bundle`, `/opt/CARPentry`).
272+
* **Returns:**
273+
* `Optional[pathlib.Path]`: The path to the first `config.sh` file found, or `None`.
274+
275+
**Other Utility Methods:**
276+
The class also provides several helper methods for convenience: `reload_environment()`, `get_carp_path()`, `get_carputils_settings_path()`, and `get_license_path()`.
277+
278+
**Associated Exception:**
279+
280+
#### `pycemrg.system.CarpEnvironmentError`
281+
A custom exception raised by `CarpRunner` if it fails to source or validate the CARPentry environment from the `config.sh` file. This can happen if the file is corrupted, incomplete, or the sourcing command fails. It is a subclass of `RuntimeError`.
282+
283+
---
284+
285+
### 6. Command-Line Interface (CLI)
189286

190287
For interactive use, the library provides a CLI to perform scaffolding.
191288

0 commit comments

Comments
 (0)