Skip to content

Commit

Permalink
Fix parsing of R package versions
Browse files Browse the repository at this point in the history
  • Loading branch information
rrwick committed Apr 5, 2023
1 parent 9073d03 commit ee60e93
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
27 changes: 18 additions & 9 deletions test/test_software.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,34 @@ def test_parse_r_version_2():
assert trycycler.software.parse_r_version(output) == '?'


def test_parse_ape_version_1():
def test_parse_r_package_version_1():
output = '> packageVersion("ape")\n' \
'[1] ‘5.3’\n' \
'>'
assert trycycler.software.parse_ape_version(output) == '5.3'
assert trycycler.software.parse_r_package_version(output) == '5.3'


def test_parse_ape_version_2():
output = 'Not the correct output'
assert trycycler.software.parse_ape_version(output) == '?'
def test_parse_r_package_version_2():
output = '> packageVersion("ape")\n' \
"[1] '5.3'\n" \
'>'
assert trycycler.software.parse_r_package_version(output) == '5.3'


def test_parse_phangorn_version_1():
def test_parse_r_package_version_3():
output = '> packageVersion("phangorn")\n' \
'[1] ‘2.5.5’\n' \
'>'
assert trycycler.software.parse_ape_version(output) == '2.5.5'
assert trycycler.software.parse_r_package_version(output) == '2.5.5'


def test_parse_r_package_version_4():
output = '> packageVersion("phangorn")\n' \
"[1] '2.5.5'\n" \
'>'
assert trycycler.software.parse_r_package_version(output) == '2.5.5'


def test_parse_phangorn_version_2():
def test_parse_r_package_version_5():
output = 'Not the correct output'
assert trycycler.software.parse_ape_version(output) == '?'
assert trycycler.software.parse_r_package_version(output) == '?'
19 changes: 7 additions & 12 deletions trycycler/software.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,10 @@ def check_ape():
if 'there is no package' in output or 'not found' in output:
sys.exit('\nError: unable to find ape - make sure that the "ape" package is installed '
'for your R installation, then try again.')
version = parse_ape_version(output)
version = parse_r_package_version(output)
log(f' ape: v{version}')


def parse_ape_version(output):
if '[1] ‘' in output:
output = output.split('[1] ‘')[1]
output = output.split('’')[0]
return output.strip()
else:
return '?'


def check_phangorn():
try:
output = subprocess.check_output(['R', '--quiet', '-e', 'packageVersion("phangorn")'],
Expand All @@ -155,14 +146,18 @@ def check_phangorn():
if 'there is no package' in output or 'not found' in output:
sys.exit('\nError: unable to find phangorn - make sure that the "phangorn" package is '
'installed for your R installation, then try again.')
version = parse_phangorn_version(output)
version = parse_r_package_version(output)
log(f' phangorn: v{version}')


def parse_phangorn_version(output):
def parse_r_package_version(output):
if '[1] ‘' in output:
output = output.split('[1] ‘')[1]
output = output.split('’')[0]
return output.strip()
elif "[1] '" in output:
output = output.split("[1] '")[1]
output = output.split("'")[0]
return output.strip()
else:
return '?'

0 comments on commit ee60e93

Please sign in to comment.