-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring to improve API and allow configless .NET Core loading
- Drop `Runtime` wrapper in favour of an abstract base class - Use `pathlib` throughout and allow it as parameter - Expose a `shutdown` method (that is a noop in most cases, but at least it exists) - Add functions to find all installed .NET Core runtimes - If no runtime configuration is given, generate a simple one in a temporary directory
- Loading branch information
Showing
13 changed files
with
457 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,106 @@ | ||
from pathlib import Path | ||
from tempfile import TemporaryDirectory | ||
from typing import Dict, Optional, Sequence | ||
|
||
from .wrappers import Runtime | ||
from .util.find import find_libmono, find_dotnet_root | ||
from .types import Assembly, Runtime, RuntimeInfo | ||
from .util import StrOrPath | ||
from .util.find import find_dotnet_root, find_libmono, find_runtimes | ||
from .util.runtime_spec import DotnetCoreRuntimeSpec | ||
|
||
__all__ = ["get_mono", "get_netfx", "get_coreclr"] | ||
__all__ = [ | ||
"get_mono", | ||
"get_netfx", | ||
"get_coreclr", | ||
"find_dotnet_root", | ||
"find_libmono", | ||
"find_runtimes", | ||
"Runtime", | ||
"Assembly", | ||
"RuntimeInfo", | ||
] | ||
|
||
|
||
def get_mono( | ||
*, | ||
domain: Optional[str] = None, | ||
config_file: Optional[str] = None, | ||
global_config_file: Optional[str] = None, | ||
libmono: Optional[str] = None, | ||
config_file: Optional[StrOrPath] = None, | ||
global_config_file: Optional[StrOrPath] = None, | ||
libmono: Optional[StrOrPath] = None, | ||
sgen: bool = True, | ||
debug: bool = False, | ||
jit_options: Optional[Sequence[str]] = None, | ||
) -> Runtime: | ||
from .mono import Mono | ||
|
||
libmono = _maybe_path(libmono) | ||
if libmono is None: | ||
libmono = find_libmono(sgen) | ||
|
||
impl = Mono( | ||
domain=domain, | ||
debug=debug, | ||
jit_options=jit_options, | ||
config_file=config_file, | ||
global_config_file=global_config_file, | ||
config_file=_maybe_path(config_file), | ||
global_config_file=_maybe_path(global_config_file), | ||
libmono=libmono, | ||
) | ||
return Runtime(impl) | ||
return impl | ||
|
||
|
||
def get_coreclr( | ||
runtime_config: str, | ||
dotnet_root: Optional[str] = None, | ||
*, | ||
runtime_config: Optional[StrOrPath] = None, | ||
dotnet_root: Optional[StrOrPath] = None, | ||
properties: Optional[Dict[str, str]] = None, | ||
runtime_spec: Optional[DotnetCoreRuntimeSpec] = None, | ||
) -> Runtime: | ||
from .hostfxr import DotnetCoreRuntime | ||
|
||
dotnet_root = _maybe_path(dotnet_root) | ||
if dotnet_root is None: | ||
dotnet_root = find_dotnet_root() | ||
|
||
temp_dir = None | ||
runtime_config = _maybe_path(runtime_config) | ||
if runtime_config is None: | ||
if runtime_spec is None: | ||
candidates = [ | ||
rt for rt in find_runtimes() if rt.name == "Microsoft.NETCore.App" | ||
] | ||
candidates.sort(key=lambda spec: spec.version, reverse=True) | ||
if not candidates: | ||
raise RuntimeError("Failed to find a suitable runtime") | ||
|
||
runtime_spec = candidates[0] | ||
|
||
temp_dir = TemporaryDirectory() | ||
runtime_config = Path(temp_dir.name) / "runtimeconfig.json" | ||
|
||
with open(runtime_config, "w") as f: | ||
runtime_spec.write_config(f) | ||
|
||
impl = DotnetCoreRuntime(runtime_config=runtime_config, dotnet_root=dotnet_root) | ||
if properties: | ||
for key, value in properties.items(): | ||
impl[key] = value | ||
|
||
return Runtime(impl) | ||
if temp_dir: | ||
temp_dir.cleanup() | ||
|
||
return impl | ||
|
||
def get_netfx(name: Optional[str] = None, config_file: Optional[str] = None) -> Runtime: | ||
|
||
def get_netfx( | ||
*, name: Optional[str] = None, config_file: Optional[StrOrPath] = None | ||
) -> Runtime: | ||
from .netfx import NetFx | ||
|
||
impl = NetFx(name=name, config_file=config_file) | ||
return Runtime(impl) | ||
impl = NetFx(name=name, config_file=_maybe_path(config_file)) | ||
return impl | ||
|
||
|
||
def _maybe_path(p: Optional[StrOrPath]) -> Optional[Path]: | ||
if p is None: | ||
return None | ||
else: | ||
return Path(p) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import sys | ||
|
||
|
||
cdef = [] | ||
|
||
if sys.platform == "win32": | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.