Skip to content

Commit 594417f

Browse files
committed
changes per code review
1 parent c340838 commit 594417f

3 files changed

Lines changed: 71 additions & 52 deletions

File tree

cmdstanpy/utils.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -182,74 +182,74 @@ def cmdstan_path() -> str:
182182
return cmdstan
183183

184184

185-
def cmdstan_version_before(major: int, minor: int) -> bool:
185+
def cmdstan_version() -> Optional[Tuple[int, ...]]:
186186
"""
187-
Check that CmdStan version is less than Major.minor version.
188-
Parses version string out of CmdStan makefile variable CMDSTAN_VERSION.
187+
Parses version string out of CmdStan makefile variable CMDSTAN_VERSION,
188+
returns Tuple(Major, minor).
189189
190-
If CmdStan installation is found but cannot parse version from makefile
191-
logs warning and returns False. Lenient behavoir required for CI tests,
192-
per comment here:
190+
If CmdStan installation is not found or cannot parse version from makefile
191+
logs warning and returns None. Lenient behavoir required for CI tests,
192+
per comment:
193193
https://github.com/stan-dev/cmdstanpy/pull/321#issuecomment-733817554
194-
195-
:param major: Major version number
196-
:param minor: Minor version number
197-
198-
:return: True if version at or above major.minor, else False.
199194
"""
200195
try:
201196
makefile = os.path.join(cmdstan_path(), 'makefile')
202197
except ValueError:
203-
get_logger().info(
204-
'No CmdStan installation found, '
205-
'cannot assert version is less than %d.%d.',
206-
major,
207-
minor,
208-
)
209-
return False
198+
get_logger().info('No CmdStan installation found.')
199+
return None
210200

211201
if not os.path.exists(makefile):
212202
get_logger().info(
213-
'CmdStan installation %s missing makefile, '
214-
'cannot assert version is less than %d.%d.',
203+
'CmdStan installation %s missing makefile, cannot get version.',
215204
cmdstan_path(),
216-
major,
217-
minor,
218205
)
219-
return False
206+
return None
220207

221208
with open(makefile, 'r') as fd:
222209
contents = fd.read()
223210

224211
start_idx = contents.find('CMDSTAN_VERSION := ')
225212
if start_idx < 0:
226213
get_logger().info(
227-
'Cannot parse version from makefile: %s,'
228-
'cannot assert version is less than %d.%d.',
214+
'Cannot parse version from makefile: %s.',
229215
makefile,
230-
major,
231-
minor,
232216
)
233-
return False
217+
return None
234218

235219
start_idx += len('CMDSTAN_VERSION := ')
236220
end_idx = contents.find('\n', start_idx)
237221

238222
version = contents[start_idx:end_idx]
239-
if version is None or len(version) < 3 or len(version.split('.')) < 2:
223+
splits = version.split('.')
224+
if len(splits) != 3:
240225
get_logger().info(
241-
'Cannot parse version from makefile: %s,'
242-
'cannot assert version is less than %d.%d.',
243-
makefile,
244-
major,
245-
minor,
226+
'Cannot parse version, expected "<major>.<minor>.<patch>", '
227+
'found: "%s".',
228+
version,
246229
)
247-
return False
230+
return None
231+
return tuple(int(x) for x in splits[0:2])
248232

249-
splits = version.split('.')
250-
cur_major = int(splits[0])
251-
cur_minor = int(splits[1])
252-
if cur_major < major or (cur_major == major and cur_minor < minor):
233+
234+
def cmdstan_version_before(major: int, minor: int) -> bool:
235+
"""
236+
Check that CmdStan version is less than Major.minor version.
237+
238+
:param major: Major version number
239+
:param minor: Minor version number
240+
241+
242+
:return: True if version at or above major.minor, else False.
243+
"""
244+
cur_version = cmdstan_version()
245+
if cur_version is None:
246+
get_logger().info(
247+
'Cannot determine whether version is before %d.%d.', major, minor
248+
)
249+
return False
250+
if cur_version[0] < major or (
251+
cur_version[0] == major and cur_version[1] < minor
252+
):
253253
return True
254254
return False
255255

test/test_cmdstan_args.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,11 +588,12 @@ def test_args_sig_figs(self):
588588
)
589589
expect = (
590590
'Argument "sig_figs" invalid for CmdStan versions < 2.25, '
591-
'using verson {} in directory {}'
591+
'using version {} in directory {}'
592592
).format(
593593
os.path.basename(cmdstan_path()),
594594
os.path.dirname(cmdstan_path()),
595595
)
596+
print(log)
596597
log.check_present(('cmdstanpy', 'WARNING', expect))
597598
else:
598599
cmdstan_args = CmdStanArgs(

test/test_utils.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import string
1414
import tempfile
1515
import unittest
16+
from pathlib import Path
1617

1718
import numpy as np
1819
import pandas as pd
@@ -28,6 +29,7 @@
2829
TemporaryCopiedFile,
2930
check_sampler_csv,
3031
cmdstan_path,
32+
cmdstan_version,
3133
cmdstan_version_before,
3234
do_command,
3335
flatten_chains,
@@ -205,27 +207,43 @@ def test_munge_cmdstan_versions(self):
205207
def test_cmdstan_version_before(self):
206208
cmdstan_path() # sets os.environ['CMDSTAN']
207209
self.assertTrue(cmdstan_version_before(99, 99))
210+
self.assertFalse(cmdstan_version_before(1, 1))
208211

212+
def test_cmdstan_version(self):
209213
with tempfile.TemporaryDirectory(
210214
prefix="cmdstan_tests", dir=_TMPDIR
211215
) as tmpdir:
212216
tdir = os.path.join(tmpdir, 'tmpdir_xxx')
213217
os.makedirs(tdir)
214-
os.makedirs(os.path.join(tdir, 'cmdstan-2.22.0'))
215-
os.environ['CMDSTAN'] = os.path.join(tdir, 'cmdstan-2.22.0')
218+
fake_path = os.path.join(tdir, 'cmdstan-2.22.0')
219+
os.makedirs(os.path.join(fake_path))
220+
fake_bin = os.path.join(fake_path, 'bin')
221+
os.makedirs(fake_bin)
222+
Path(os.path.join(fake_bin, 'stanc' + EXTENSION)).touch()
223+
os.environ['CMDSTAN'] = fake_path
224+
self.assertTrue(fake_path == cmdstan_path())
225+
expect = (
226+
'CmdStan installation {} missing makefile, '
227+
'cannot get version.'.format(fake_path)
228+
)
216229
with LogCapture() as log:
217230
logging.getLogger()
218-
self.assertFalse(cmdstan_version_before(99, 99))
219-
del os.environ['CMDSTAN']
220-
cmdstan_path()
221-
log.check_present(
222-
(
223-
'cmdstanpy',
224-
'INFO',
225-
'No CmdStan installation found, '
226-
'cannot assert version is less than 99.99.',
227-
)
231+
cmdstan_version()
232+
log.check_present(('cmdstanpy', 'INFO', expect))
233+
fake_makefile = os.path.join(fake_path, 'makefile')
234+
with open(fake_makefile, 'w') as fd:
235+
fd.write('... CMDSTAN_VERSION := dont_need_no_mmp\n\n')
236+
expect = (
237+
'Cannot parse version, expected "<major>.<minor>.<patch>", '
238+
'found: "dont_need_no_mmp".'
228239
)
240+
with LogCapture() as log:
241+
logging.getLogger()
242+
cmdstan_version()
243+
log.check_present(('cmdstanpy', 'INFO', expect))
244+
# cleanup
245+
del os.environ['CMDSTAN']
246+
cmdstan_path()
229247

230248

231249
class DataFilesTest(unittest.TestCase):

0 commit comments

Comments
 (0)