Skip to content

Commit

Permalink
Fix #37 . Modify choosenim auto-discovery to work with choosenim_install
Browse files Browse the repository at this point in the history
  • Loading branch information
Pebaz committed Feb 14, 2021
1 parent e4c83b4 commit adbd240
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
34 changes: 27 additions & 7 deletions nimporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class NimCompiler:
'-d:strip',
'-d:lto',
'-d:ssl'
] + (['--cc:vcc'] if sys.platform == 'win32' else [])
] + (['--cc:vcc'] if shutil.which('vccexe') else [])
EXT_DIR = 'nim-extensions'

@classmethod
Expand Down Expand Up @@ -313,6 +313,27 @@ def find_nim_std_lib(cls):
The Path to the Nim stdlib 'lib' directory if it exists and None
otherwise.
"""
# If Nim is not installed there's nothing to be done
nimexe = shutil.which('nim')
if not nimexe:
return None

# Installed via choosenim_install Pypi package
choosenim_dir = Path('~/.choosenim/toolchains').expanduser().absolute()
if choosenim_dir.exists:
try:
nim_ver = (subprocess.check_output(['nim', '-v'])
.decode(errors='ignore')
.splitlines()[0]
)

version_string = nim_ver.split()[3]
stdlib = choosenim_dir / f'nim-{version_string}/lib'

if (stdlib / 'system.nim').exists():
return stdlib.resolve().absolute()
except:
"Keep trying other methods"

# Installed via ChooseNim
if shutil.which('choosenim'):
Expand All @@ -322,20 +343,19 @@ def find_nim_std_lib(cls):
(choosenim,) = [i for i in o.splitlines() if 'Path:' in i]
toolchain = Path(choosenim.split('Path:').pop().strip())
stdlib = toolchain / 'lib'
if not (stdlib / 'system.nim').exists():
return None
return stdlib.resolve().absolute()

if (stdlib / 'system.nim').exists():
return stdlib.resolve().absolute()

# Installed manually
nimexe = shutil.which('nim')
if not nimexe:
return None
nimexe = Path(nimexe)
result = nimexe.parent / '../lib'
if not (result / 'system.nim').exists():
result = nimexe.resolve().parent / '../lib'

if not (result / 'system.nim').exists():
return None

return result.resolve().absolute()

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setup(
name='nimporter',
version='1.0.3',
version='1.0.4',
license="MIT",
description='Compile Nim extensions for Python when imported!',
long_description=open('README.md').read(),
Expand Down
14 changes: 10 additions & 4 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,20 @@ def test_invoke_compiler_success():
assert any(
"Warning: imported and not used: 'asyncstreams'" in i for i in war
)
assert any('Hint: system [Processing]' in i for i in hin)
assert any('Hint' in i for i in hin)

finally:
if out_file.exists():
out_file.unlink()
if sys.platform == 'win32':
Path('tests/pkg1/warn.exp').unlink()
Path('tests/pkg1/warn.lib').unlink()
warn_exp = Path('tests/pkg1/warn.exp')
warn_lib = Path('tests/pkg1/warn.lib')

if warn_exp.exists():
warn_exp.unlink()

if warn_lib.exists():
warn_lib.unlink()


def test_invoke_compiler_failure():
Expand All @@ -112,7 +118,7 @@ def test_invoke_compiler_failure():

assert not out_file.exists(), err
assert any('Error: cannot open file: fallacy' in i for i in err)
assert any('Hint: system [Processing]' in i for i in hin)
assert any('Hint' in i for i in hin)

finally:
if out_file.exists(): out_file.unlink()
Expand Down

0 comments on commit adbd240

Please sign in to comment.