Skip to content

Commit de0c141

Browse files
committed
Allow interactive mode from cmdstanpy.install_cmdstan function
1 parent 0cc04ea commit de0c141

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

cmdstanpy/install_cmdstan.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ def cores(self) -> int:
229229
answer = input("Enter a number or hit enter to continue: ")
230230
try:
231231
return min(max_cpus, max(int(answer), 1))
232-
# pylint: disable=broad-except
233-
except Exception:
232+
except ValueError:
234233
return 1
235234

236235

@@ -614,12 +613,13 @@ def run_install(args: Union[InteractiveSettings, InstallationSettings]) -> None:
614613

615614

616615
def parse_cmdline_args() -> Dict[str, Any]:
617-
parser = argparse.ArgumentParser()
616+
parser = argparse.ArgumentParser("install_cmdstan")
618617
parser.add_argument(
619618
'--interactive',
620619
'-i',
621620
action='store_true',
622-
help="Run the installation in interactive mode",
621+
help="Ignore other arguments and run the installation in "
622+
+ "interactive mode",
623623
)
624624
parser.add_argument(
625625
'--version', '-v', help="version, defaults to latest release version"

cmdstanpy/utils.py

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,8 @@ def install_cmdstan(
12521252
progress: bool = False,
12531253
verbose: bool = False,
12541254
cores: int = 1,
1255+
*,
1256+
interactive: bool = False,
12551257
) -> bool:
12561258
"""
12571259
Download and install a CmdStan release from GitHub. Downloads the release
@@ -1283,35 +1285,60 @@ def install_cmdstan(
12831285
:param cores: Integer, number of cores to use in the ``make`` command.
12841286
Default is 1 core.
12851287
1288+
:param interactive: Boolean value; if true, ignore all other arguments
1289+
to this function and run in an interactive mode, prompting the user
1290+
to provide the other information manually through the standard input.
1291+
1292+
This flag should only be used in interactive environments,
1293+
e.g. on the command line.
1294+
12861295
:return: Boolean value; ``True`` for success.
12871296
"""
1297+
logger = get_logger()
12881298
try:
1289-
from .install_cmdstan import InstallationSettings, run_install
1290-
1291-
args = InstallationSettings(
1292-
version=version,
1293-
overwrite=overwrite,
1294-
verbose=verbose,
1295-
compiler=compiler,
1296-
progress=progress,
1297-
dir=dir,
1298-
cores=cores,
1299+
from .install_cmdstan import (
1300+
InstallationSettings,
1301+
InteractiveSettings,
1302+
run_install,
12991303
)
1304+
1305+
args: Union[InstallationSettings, InteractiveSettings]
1306+
1307+
if interactive:
1308+
if any(
1309+
[
1310+
version,
1311+
dir,
1312+
overwrite,
1313+
compiler,
1314+
progress,
1315+
verbose,
1316+
cores != 1,
1317+
]
1318+
):
1319+
logger.warning(
1320+
"Interactive installation requested but other arguments"
1321+
" were used.\n\tThese values will be ignored!"
1322+
)
1323+
args = InteractiveSettings()
1324+
else:
1325+
args = InstallationSettings(
1326+
version=version,
1327+
overwrite=overwrite,
1328+
verbose=verbose,
1329+
compiler=compiler,
1330+
progress=progress,
1331+
dir=dir,
1332+
cores=cores,
1333+
)
13001334
run_install(args)
13011335
# pylint: disable=broad-except
13021336
except Exception as e:
1303-
logger = get_logger()
1304-
logger.warning('CmdStan installation failed.')
1305-
logger.warning(str(e))
1337+
logger.warning('CmdStan installation failed.\n%s', str(e))
13061338
return False
13071339

1308-
if dir is not None:
1309-
if version is not None:
1310-
set_cmdstan_path(os.path.join(dir, 'cmdstan-' + version))
1311-
else:
1312-
set_cmdstan_path(
1313-
os.path.join(dir, get_latest_cmdstan(dir)) # type: ignore
1314-
)
1340+
set_cmdstan_path(args.dir)
1341+
13151342
return True
13161343

13171344

0 commit comments

Comments
 (0)