diff --git a/build.cmd b/build.cmd index 3c42514d..42c07695 100644 --- a/build.cmd +++ b/build.cmd @@ -40,8 +40,7 @@ if /i [%1] == [--buildDotNetBridgeOnly] ( if /i [%1] == [--skipDotNetBridge] ( set SkipDotNetBridge=True shift && goto :Arg_Loop -) -else goto :Usage +) else goto :Usage :Usage echo "Usage: build.cmd [--configuration ] [--runTests] [--buildDotNetBridgeOnly] [--skipDotNetBridge]" @@ -187,8 +186,7 @@ if "%VisualStudioVersion%"=="15.0" ( goto :VS2017 ) else if "%VisualStudioVersion%"=="14.0" ( goto :VS2015 -) -else goto :MissingVersion +) else goto :MissingVersion :MissingVersion :: Can't find VS 2015 or 2017 @@ -261,8 +259,7 @@ copy "%BuildOutputDir%%Configuration%\pybridge.pyd" "%__currentScriptDir%src\py if %PythonVersion% == 2.7 ( copy "%BuildOutputDir%%Configuration%\Platform\win-x64\publish\*.dll" "%__currentScriptDir%src\python\nimbusml\internal\libs\" -) -else ( +) else ( for /F "tokens=*" %%A in (build/libs_win.txt) do copy "%BuildOutputDir%%Configuration%\Platform\win-x64\publish\%%A" "%__currentScriptDir%src\python\nimbusml\internal\libs\" ) diff --git a/src/python/nimbusml/internal/utils/entrypoints.py b/src/python/nimbusml/internal/utils/entrypoints.py index 1fc0a440..1f030443 100644 --- a/src/python/nimbusml/internal/utils/entrypoints.py +++ b/src/python/nimbusml/internal/utils/entrypoints.py @@ -8,7 +8,6 @@ import functools import json import os -import pkg_resources import tempfile from collections import OrderedDict from enum import Enum @@ -23,7 +22,7 @@ from .data_stream import FileDataStream from .dataframes import resolve_dataframe, resolve_csr_matrix, pd_concat, \ resolve_output -from .utils import try_set +from .utils import try_set, set_clr_environment_vars, get_clr_path from ..libs.pybridge import px_call @@ -453,21 +452,8 @@ def remove_multi_level_index(c): call_parameters['dotnetClrPath'] = try_set(nimbusml_path, True, str) # dotnetcore2 package is available only for python 3.x if six.PY3: - from dotnetcore2 import runtime as clr_runtime - dependencies_path = None - try: - # try to resolve dependencies, for ex. libunwind - dependencies_path = clr_runtime.ensure_dependencies() - except: - pass - os.environ['DOTNET_SYSTEM_GLOBALIZATION_INVARIANT'] = 'true' - if dependencies_path is not None: - os.environ['LD_LIBRARY_PATH'] = dependencies_path - dotnet_module = pkg_resources.get_distribution('dotnetcore2') - dotnet_path = os.path.join( - dotnet_module.module_path, 'dotnetcore2', 'bin', 'shared', - 'Microsoft.NETCore.App', dotnet_module.version) - call_parameters['dotnetClrPath'] = try_set(dotnet_path, True, str) + set_clr_environment_vars() + call_parameters['dotnetClrPath'] = try_set(get_clr_path(), True, str) if random_state: call_parameters['seed'] = try_set(random_state, False, int) ret = self._try_call_bridge( diff --git a/src/python/nimbusml/internal/utils/utils.py b/src/python/nimbusml/internal/utils/utils.py index 27a57497..62def151 100644 --- a/src/python/nimbusml/internal/utils/utils.py +++ b/src/python/nimbusml/internal/utils/utils.py @@ -8,6 +8,7 @@ import logging import os +import pkg_resources import tempfile from datetime import datetime @@ -276,3 +277,44 @@ def set_shape(pred, X): pred.input_shape_ = (len(X), len(X[0])) else: pred.input_shape_ = (len(X), 1) + +def set_clr_environment_vars(): + """ + Set system environment variables required by the .NET CLR. + Python 3.x only, as dotnetcore2 is not available for Python 2.x. + """ + from dotnetcore2 import runtime as clr_runtime + dependencies_path = None + try: + # try to resolve dependencies, for ex. libunwind + dependencies_path = clr_runtime.ensure_dependencies() + except: + pass + os.environ['DOTNET_SYSTEM_GLOBALIZATION_INVARIANT'] = 'true' + if dependencies_path is not None: + os.environ['LD_LIBRARY_PATH'] = dependencies_path + +def get_clr_path(): + """ + Return path to .NET CLR binaries. + Python 3.x only, as dotnetcore2 is not available for Python 2.x. + """ + from dotnetcore2 import runtime as clr_runtime + clr_version = pkg_resources.get_distribution('dotnetcore2').version + partial_path = os.path.join(clr_runtime._get_bin_folder(), 'shared', 'Microsoft.NETCore.App') + clr_path = os.path.join(partial_path, clr_version) + if not os.path.exists(clr_path): + # If folder name does not match published version, use the folder that + # exists + try: + version_folder = os.listdir(partial_path)[0] + except IndexError: + raise ImportError("Trouble importing dotnetcore2: " + "{} had no version folder.".format(partial_path)) + clr_path = os.path.join(partial_path, version_folder) + # Verify binaries are present + if not os.path.exists(os.path.join(clr_path, 'Microsoft.CSharp.dll')): + raise ImportError( + "Trouble importing dotnetcore2: Microsoft.CSharp.dll was not " + "found in {}.".format(clr_path)) + return clr_path \ No newline at end of file