Skip to content

Commit

Permalink
start switching CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Jul 23, 2024
1 parent 17a04d4 commit 765888c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 55 deletions.
47 changes: 1 addition & 46 deletions src/bidspm/bidspm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@
from __future__ import annotations

import json
import subprocess
from pathlib import Path
from typing import Any

from rich import print

from .matlab import matlab
from .parsers import bidspm_log, sub_command_parser

with open(Path(__file__).parent / "data" / "exit_codes.json") as f:
Expand Down Expand Up @@ -469,48 +465,7 @@ def bidspm(

cmd = f" bidspm('init'); try; {cmd} catch; exit; end;"

return run_command(cmd)


def run_command(cmd: str, platform: str | None = None) -> int:
print("\nRunning the following command:\n")
print(cmd.replace(";", ";\n"))
print()

# TODO exit matlab / octave on crash

if platform is None:
if Path(matlab()).exists():
platform = matlab()
cmd = cmd.replace(new_line, ", ")
else:
platform = "octave"

if platform == "octave":
completed_process = subprocess.run(
[
"octave",
"--no-gui",
"--no-window-system",
"--silent",
"--eval",
f"{cmd}",
]
)

else:
completed_process = subprocess.run(
[
platform,
"-nodisplay",
"-nosplash",
"-nodesktop",
"-r",
f"{cmd}",
]
)

return completed_process.returncode
return cmd


def generate_command_default_model(argv: Any) -> str:
Expand Down
68 changes: 59 additions & 9 deletions src/bidspm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
from __future__ import annotations

import json
import subprocess
import sys
from pathlib import Path
from typing import Any

from .bidspm import bidspm
from rich import print

from .bidspm import bidspm, new_line
from .matlab import matlab
from .parsers import ALLOWED_ACTIONS, bidspm_log, sub_command_parser

log = bidspm_log(name="bidspm")
Expand Down Expand Up @@ -87,7 +91,7 @@ def cli(argv: Any = sys.argv) -> None:

model_file: Path | None = None
if model_file := getattr(args, "model_file", None):
model_file: Path | None = (
model_file = (
Path(args.model_file[0]).absolute() if args.model_file is not None else None
)

Expand All @@ -96,7 +100,7 @@ def cli(argv: Any = sys.argv) -> None:
exit(EXIT_CODES["NOINPUT"]["Value"])

if command == "default_model":
return_code = bidspm(
cmd = bidspm(
bids_dir=bids_dir,
output_dir=output_dir,
analysis_level=analysis_level,
Expand All @@ -110,7 +114,7 @@ def cli(argv: Any = sys.argv) -> None:
)

elif command == "create_roi":
return_code = bidspm(
cmd = bidspm(
bids_dir=bids_dir,
output_dir=output_dir,
analysis_level=analysis_level,
Expand All @@ -127,7 +131,7 @@ def cli(argv: Any = sys.argv) -> None:
)

elif command == "smooth":
return_code = bidspm(
cmd = bidspm(
bids_dir=bids_dir,
output_dir=output_dir,
analysis_level=analysis_level,
Expand All @@ -148,7 +152,7 @@ def cli(argv: Any = sys.argv) -> None:
log.error("Only one task allowed for preprocessing.\n" f"Got: {args.task}")
raise SystemExit(EXIT_CODES["USAGE"]["Value"])

return_code = bidspm(
cmd = bidspm(
bids_dir=bids_dir,
output_dir=output_dir,
analysis_level=analysis_level,
Expand All @@ -168,7 +172,7 @@ def cli(argv: Any = sys.argv) -> None:
)

elif command == "stats":
return_code = bidspm(
cmd = bidspm(
bids_dir=bids_dir,
output_dir=output_dir,
analysis_level=analysis_level,
Expand All @@ -193,7 +197,7 @@ def cli(argv: Any = sys.argv) -> None:
)

elif command == "contrasts":
return_code = bidspm(
cmd = bidspm(
bids_dir=bids_dir,
output_dir=output_dir,
analysis_level=analysis_level,
Expand All @@ -214,7 +218,7 @@ def cli(argv: Any = sys.argv) -> None:
)

elif command == "results":
return_code = bidspm(
cmd = bidspm(
bids_dir=bids_dir,
output_dir=output_dir,
analysis_level=analysis_level,
Expand All @@ -233,7 +237,53 @@ def cli(argv: Any = sys.argv) -> None:
roi_atlas=args.roi_atlas[0],
)

if isinstance(cmd, int):
sys.exit(cmd)

return_code = run_command(cmd)

if return_code == 1:
raise SystemExit(EXIT_CODES["FAILURE"]["Value"])
else:
sys.exit(EXIT_CODES["SUCCESS"]["Value"])


def run_command(cmd: str, platform: str | None = None) -> int:
print("\nRunning the following command:\n")
print(cmd.replace(";", ";\n"))
print()

# TODO exit matlab / octave on crash

if platform is None:
if Path(matlab()).exists():
platform = matlab()
cmd = cmd.replace(new_line, ", ")
else:
platform = "octave"

if platform == "octave":
completed_process = subprocess.run(
[
"octave",
"--no-gui",
"--no-window-system",
"--silent",
"--eval",
f"{cmd}",
]
)

else:
completed_process = subprocess.run(
[
platform,
"-nodisplay",
"-nosplash",
"-nodesktop",
"-r",
f"{cmd}",
]
)

return completed_process.returncode

0 comments on commit 765888c

Please sign in to comment.