Skip to content

Commit 6042648

Browse files
authored
Use the same API for command line and runtimeconfig hosting (#92)
1 parent 718fc48 commit 6042648

File tree

3 files changed

+46
-73
lines changed

3 files changed

+46
-73
lines changed

clr_loader/__init__.py

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"get_mono",
1212
"get_netfx",
1313
"get_coreclr",
14-
"get_coreclr_command_line",
1514
"find_dotnet_root",
1615
"find_libmono",
1716
"find_runtimes",
@@ -94,6 +93,7 @@ def get_mono(
9493
def get_coreclr(
9594
*,
9695
runtime_config: Optional[StrOrPath] = None,
96+
entry_dll: Optional[StrOrPath] = None,
9797
dotnet_root: Optional[StrOrPath] = None,
9898
properties: Optional[Dict[str, str]] = None,
9999
runtime_spec: Optional[DotnetCoreRuntimeSpec] = None,
@@ -106,9 +106,13 @@ def get_coreclr(
106106
the first function object is retrieved.
107107
108108
:param runtime_config:
109-
Pass to a ``runtimeconfig.json`` as generated by
109+
Path to a ``runtimeconfig.json`` as generated by
110110
``dotnet publish``. If this parameter is not given, a temporary runtime
111111
config will be generated.
112+
:param entry_dll:
113+
The path to the entry dll. If this parameter is given, the runtime will
114+
be initialized using the command line hosting API instead of using the
115+
runtime config.
112116
:param dotnet_root:
113117
The root directory of the .NET Core installation. If this is not
114118
specified, we try to discover it using :py:func:`find_dotnet_root`.
@@ -119,6 +123,7 @@ def get_coreclr(
119123
If the ``runtime_config`` is not specified, the concrete runtime to use
120124
can be controlled by passing this parameter. Possible values can be
121125
retrieved using :py:func:`find_runtimes`."""
126+
122127
from .hostfxr import DotnetCoreRuntime
123128

124129
dotnet_root = _maybe_path(dotnet_root)
@@ -127,7 +132,9 @@ def get_coreclr(
127132

128133
temp_dir = None
129134
runtime_config = _maybe_path(runtime_config)
130-
if runtime_config is None:
135+
entry_dll = _maybe_path(entry_dll)
136+
137+
if runtime_config is None and entry_dll is None:
131138
if runtime_spec is None:
132139
candidates = [
133140
rt for rt in find_runtimes() if rt.name == "Microsoft.NETCore.App"
@@ -144,7 +151,12 @@ def get_coreclr(
144151
with open(runtime_config, "w") as f:
145152
runtime_spec.write_config(f)
146153

147-
impl = DotnetCoreRuntime(runtime_config=runtime_config, dotnet_root=dotnet_root)
154+
if entry_dll is not None:
155+
entry_dll = _maybe_path(entry_dll)
156+
impl = DotnetCoreRuntime(entry_dll=entry_dll, dotnet_root=dotnet_root)
157+
else:
158+
impl = DotnetCoreRuntime(runtime_config=runtime_config, dotnet_root=dotnet_root)
159+
148160
if properties:
149161
for key, value in properties.items():
150162
impl[key] = value
@@ -155,41 +167,6 @@ def get_coreclr(
155167
return impl
156168

157169

158-
def get_coreclr_command_line(
159-
*,
160-
entry_dll: StrOrPath,
161-
dotnet_root: Optional[StrOrPath] = None,
162-
properties: Optional[Dict[str, str]] = None,
163-
) -> Runtime:
164-
"""Get a CoreCLR (.NET Core) runtime instance
165-
The returned ``DotnetCoreRuntimeCommandLine`` also acts as a mapping of the config
166-
properties. They can be retrieved using the index operator and can be
167-
written until the runtime is initialized. The runtime is initialized when
168-
the first function object is retrieved.
169-
:param entry_dll:
170-
The path to the entry dll.
171-
:param dotnet_root:
172-
The root directory of the .NET Core installation. If this is not
173-
specified, we try to discover it using :py:func:`find_dotnet_root`.
174-
:param properties:
175-
Additional runtime properties. These can also be passed using the
176-
``configProperties`` section in the runtime config."""
177-
from .hostfxr import DotnetCoreCommandRuntime
178-
179-
dotnet_root = _maybe_path(dotnet_root)
180-
if dotnet_root is None:
181-
dotnet_root = find_dotnet_root()
182-
183-
impl = DotnetCoreCommandRuntime(
184-
entry_dll=_maybe_path(entry_dll), dotnet_root=dotnet_root
185-
)
186-
if properties:
187-
for key, value in properties.items():
188-
impl[key] = value
189-
190-
return impl
191-
192-
193170
def get_netfx(
194171
*, domain: Optional[str] = None, config_file: Optional[StrOrPath] = None
195172
) -> Runtime:

clr_loader/hostfxr.py

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
import sys
22
from pathlib import Path
3-
from typing import Generator, Tuple
3+
from typing import Generator, Tuple, Optional
44

55
from .ffi import ffi, load_hostfxr
66
from .types import Runtime, RuntimeInfo, StrOrPath
77
from .util import check_result
88

9-
__all__ = ["DotnetCoreRuntime", "DotnetCoreCommandRuntime"]
9+
__all__ = ["DotnetCoreRuntime"]
1010

1111
_IS_SHUTDOWN = False
1212

1313

14-
class DotnetCoreRuntimeBase(Runtime):
14+
class DotnetCoreRuntime(Runtime):
1515
_version: str
1616

17-
def __init__(self, dotnet_root: Path):
17+
def __init__(
18+
self,
19+
*,
20+
dotnet_root: Path,
21+
runtime_config: Optional[Path] = None,
22+
entry_dll: Optional[Path] = None,
23+
**params: str,
24+
):
1825
self._handle = None
1926

2027
if _IS_SHUTDOWN:
@@ -24,6 +31,23 @@ def __init__(self, dotnet_root: Path):
2431
self._dll = load_hostfxr(self._dotnet_root)
2532
self._load_func = None
2633

34+
if runtime_config is not None:
35+
self._handle = _get_handle_for_runtime_config(
36+
self._dll, self._dotnet_root, runtime_config
37+
)
38+
elif entry_dll is not None:
39+
self._handle = _get_handle_for_dotnet_command_line(
40+
self._dll, self._dotnet_root, entry_dll
41+
)
42+
else:
43+
raise ValueError("Either runtime_config or entry_dll must be provided")
44+
45+
for key, value in params.items():
46+
self[key] = value
47+
48+
# TODO: Get version
49+
self._version: str = "<undefined>"
50+
2751
@property
2852
def dotnet_root(self) -> Path:
2953
return self._dotnet_root
@@ -117,34 +141,6 @@ def info(self):
117141
)
118142

119143

120-
class DotnetCoreRuntime(DotnetCoreRuntimeBase):
121-
def __init__(self, runtime_config: Path, dotnet_root: Path, **params: str):
122-
super().__init__(dotnet_root)
123-
self._handle = _get_handle_for_runtime_config(
124-
self._dll, self._dotnet_root, runtime_config
125-
)
126-
127-
for key, value in params.items():
128-
self[key] = value
129-
130-
# TODO: Get version
131-
self._version = "<undefined>"
132-
133-
134-
class DotnetCoreCommandRuntime(DotnetCoreRuntimeBase):
135-
def __init__(self, entry_dll: Path, dotnet_root: Path, **params: str):
136-
super().__init__(dotnet_root)
137-
self._handle = _get_handle_for_dotnet_command_line(
138-
self._dll, self._dotnet_root, entry_dll
139-
)
140-
141-
for key, value in params.items():
142-
self[key] = value
143-
144-
# TODO: Get version
145-
self._version = "<undefined>"
146-
147-
148144
def _get_handle_for_runtime_config(
149145
dll, dotnet_root: StrOrPath, runtime_config: StrOrPath
150146
):

tests/test_common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ def test_coreclr_command_line(example_netcore: Path):
9797

9898

9999
def _do_test_coreclr_command_line(example_netcore: Path):
100-
from clr_loader import get_coreclr_command_line
100+
from clr_loader import get_coreclr
101101

102-
coreclr = get_coreclr_command_line(entry_dll=example_netcore / "example.dll")
102+
coreclr = get_coreclr(entry_dll=example_netcore / "example.dll")
103103
asm = coreclr.get_assembly(example_netcore / "example.dll")
104104

105105
run_tests(asm)

0 commit comments

Comments
 (0)