Skip to content

Commit 6228bd1

Browse files
committed
Add rebuild function
1 parent 5afe9cf commit 6228bd1

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

cmdstanpy/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def _cleanup_tmpdir() -> None:
3636

3737

3838
from ._version import __version__ # noqa
39+
from .install_cmdstan import rebuild_cmdstan
3940
from .model import CmdStanModel # noqa
4041
from .stanfit import ( # noqa
4142
CmdStanGQ,
@@ -68,4 +69,5 @@ def _cleanup_tmpdir() -> None:
6869
'from_csv',
6970
'write_stan_json',
7071
'show_versions',
72+
'rebuild_cmdstan',
7173
]

cmdstanpy/install_cmdstan.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@
3131
from typing import Callable, Dict, Optional
3232

3333
from cmdstanpy import _DOT_CMDSTAN, _DOT_CMDSTANPY
34-
from cmdstanpy.utils import get_logger, pushd, validate_dir, wrap_progress_hook
34+
from cmdstanpy.utils import (
35+
cmdstan_path,
36+
get_logger,
37+
pushd,
38+
validate_dir,
39+
wrap_progress_hook,
40+
)
3541

3642
MAKE = os.getenv(
3743
'MAKE', 'make' if platform.system() != 'Windows' else 'mingw32-make'
@@ -174,6 +180,21 @@ def compile_example() -> None:
174180
raise CmdStanInstallError('\n'.join(msgs))
175181

176182

183+
def rebuild_cmdstan(verbose: bool = True) -> None:
184+
"""
185+
Rebuilds the existing cmdstan installation
186+
"""
187+
try:
188+
with pushd(cmdstan_path()):
189+
clean_all(verbose)
190+
build(verbose)
191+
compile_example()
192+
except ValueError as e:
193+
raise CmdStanInstallError(
194+
"Failed to rebuild CmdStan. Are you sure it is installed?"
195+
) from e
196+
197+
177198
def install_version(
178199
cmdstan_version: str, overwrite: bool = False, verbose: bool = False
179200
) -> None:

test/test_install_cmdstan.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""install_cmdstan test"""
22

3+
import os
34
import unittest
45

56
from cmdstanpy.install_cmdstan import (
7+
CmdStanInstallError,
68
CmdStanRetrieveError,
79
is_version_available,
810
latest_version,
11+
rebuild_cmdstan,
912
retrieve_version,
1013
)
1114

@@ -34,6 +37,13 @@ def test_retrieve_version(self):
3437
with self.assertRaises(ValueError):
3538
retrieve_version('')
3639

40+
@unittest.mock.patch.dict(os.environ, {"CMDSTAN": "~/some/fake/path"})
41+
def test_rebuild_bad_path(self):
42+
with self.assertRaisesRegex(
43+
CmdStanInstallError, "you sure it is installed"
44+
):
45+
rebuild_cmdstan()
46+
3747

3848
if __name__ == '__main__':
3949
unittest.main()

0 commit comments

Comments
 (0)