Skip to content

Commit

Permalink
cli:run: add more error checking to running single files, add dry-run…
Browse files Browse the repository at this point in the history
… option, small fixes
  • Loading branch information
Arun Persaud committed Nov 1, 2024
1 parent c59b47f commit 9a01e32
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions src/pymcnp/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
-p --parallel=<threads> Run files in parallel on <threads> threads.
-c --command=<command> Command to run.
-P --path=<path> Path to use.
-n --dry-run Don't run or create directories, just print what would happen
"""

import os
import inspect
from pathlib import Path
import shutil
import subprocess
import sys
from typing import Final
from pathlib import Path

from docopt import docopt
from rich import print

from ..files.inp import Inp, read_input
from . import _io
Expand All @@ -41,18 +45,20 @@ class Run:
def __init__(
self,
inp: Inp,
path: str | Path = '.',
path: str | Path = Path('.'),
command: str = 'mcnp6',
dry_run: bool = False,
):
"""
Parameters:
path: Path to directory to store run inputs and outputs.
command: Terminal command to execute.
"""

self.path: Final[str] = Path(path)
self.path: Final[Path] = Path(path)
self.command: Final[str] = command
self.inp: Final[Inp] = inp
self.dry_run: bool = dry_run

@staticmethod
def prehook():
Expand All @@ -70,9 +76,14 @@ def parallel_prehook():
def parallel_posthook():
pass

def check_prog(self, name: str) -> None:
if shutil.which(name) is None:
print(f'[red]ERROR[/] Cannot find {name} program.')
sys.exit(3)

def run_single(self) -> Path:
"""
Runs MCNP INP files.
Runs a MCNP INP files.
``run_single`` creates a directory to store a copy of the input file
passed to MCNP and the outputfiles generated by MCNP.
Expand All @@ -83,14 +94,27 @@ def run_single(self) -> Path:

timestamp = _io.get_timestamp()

directory_path = self.path / f'pymcnp-run-{timestamp}'
directory_path.mkdir(exist_ok=False, parents=True)
self.check_prog(self.command)

directory_path = self.path / f'pymcnp-run-{timestamp}'
inp_path = directory_path / f'pymcnp-inp-{timestamp}.inp'

command_to_run = f'{self.command} i={inp_path.name}'

if self.dry_run:
print('[yellow]INFO[/] Would run:')
print(f' create {directory_path}')
print(f' save {inp_path}')
print(' executing prehook')
print(f' {command_to_run}')
print(' executing posthook')
return directory_path

directory_path.mkdir(exist_ok=False, parents=True)
self.inp.to_mcnp_file(inp_path)

self.prehook()
os.system(f'{self.command} i={inp_path}')
subprocess.run(command_to_run, cwd=directory_path, shell=True)
self.posthook()

return directory_path
Expand All @@ -114,6 +138,9 @@ def run_parallel(self, count: int) -> Path:
if count <= 0:
_io.error('Invalid Count.')

self.check_prog(self.command)
self.check_prog('parallel')

timestamp = _io.get_timestamp()

directory_path = self.path / f'pymcnp-runs-{timestamp}'
Expand Down Expand Up @@ -177,8 +204,9 @@ def main() -> None:
inp = read_input(args['<file>'])
command = args['--command'] if args['--command'] else 'mcnp6'
path = args['--path'] if args['--path'] else Path.cwd()
dry_run = args['--dry-run']

run = Run(inp, path=path, command=command)
run = Run(inp, path=path, command=command, dry_run=dry_run)

if args['--parallel'] is not None:
run.run_parallel(int(args['--parallel']))
Expand Down

0 comments on commit 9a01e32

Please sign in to comment.