Skip to content

Commit

Permalink
Merge pull request #7 from microsoft/bradarm/dev
Browse files Browse the repository at this point in the history
updates to spacefx/_sdk_client.py
  • Loading branch information
alexlianides authored Jul 11, 2024
2 parents fa9948f + 883e08e commit a2c80a6
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions spacefx/_sdk_client.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
import os
import pythonnet
import shutil
from pathlib import Path

import pythonnet


def search_file(filename, search_path):
"""Search for a file recursively in a directory tree."""
for dirpath, _, filenames in os.walk(search_path):
for file in filenames:
if file == filename:
return os.path.join(dirpath, file)
raise FileNotFoundError(f"{filename} not found in {search_path}")
"""Search for a file recursively in a directory tree using rglob."""
search_path = Path(search_path)
for file_path in search_path.rglob(filename):
return str(file_path)
return None


# Find the dotnet directory
DOTNET_DIR = shutil.which("dotnet")
if not DOTNET_DIR:
DOTNET_BIN = shutil.which("dotnet")
if not DOTNET_BIN:
raise ValueError(f"Unable to find an installation of dotnet. Please install dotnet so it's found within the system PATH")

DOTNET_DIR = Path(DOTNET_DIR).resolve(strict=True).parent
DOTNET_DIR = os.path.join(DOTNET_DIR, "shared")
# Resolve the path to the dotnet binary
DOTNET_BIN = str(Path(DOTNET_BIN).resolve(strict=True))

# Find the shared directory
DOTNET_DIR = os.path.join(os.path.dirname(DOTNET_BIN), "shared")
if not os.path.exists(DOTNET_DIR):
raise ValueError(f"dotnet was found, but unable to find the shared directory '${DOTNET_DIR}'. Please check your dotnet installation and make sure the shared folder is present")
raise ValueError(f"dotnet was found at {DOTNET_BIN}, but unable to find the shared directory '{DOTNET_DIR}'. Please check your dotnet installation and make sure the shared folder is present")


# Recursively search and load the runtimeconfig.json - this allows for dotnet minor version changes
runtimeconfig_file = search_file("Microsoft.AspNetCore.App.runtimeconfig.json", DOTNET_DIR)
runtime_config_file = search_file("Microsoft.AspNetCore.App.runtimeconfig.json", DOTNET_DIR)
if runtime_config_file is None:
raise ValueError(f"Unable to find the runtimeconfig.json file for Microsoft.AspNetCore.App in the dotnet shared directory '{DOTNET_DIR}'")

pythonnet.load("coreclr", runtime_config=runtimeconfig_file)
# Load the runtimeconfig.json file
pythonnet.load("coreclr", runtime_config=runtime_config_file)
import clr
from System.Reflection import Assembly

# Load the dotnet dlls by walking down the tree
for dirpath, dirnames, filenames in os.walk(os.path.join(DOTNET_DIR, 'Microsoft.AspNetCore.App')):
if "runtimes" in dirpath:

# Load all dotnet dlls by walking down the Microsoft.AspNetCore.App tree
for dll_path in Path(os.path.join(DOTNET_DIR, 'Microsoft.AspNetCore.App')).rglob("*.dll"):
if "runtimes" in dll_path.parts:
continue
for file in filenames:
if file.endswith(".dll"):
Assembly.LoadFile(os.path.join(dirpath, file))
Assembly.LoadFile(str(dll_path))


# Load the client adapter library which is in the parent directory / spacefxClient
base_dir = Path(__file__).parent / 'spacefxClient'
# Load the client adapter library
SPACEFX_CLIENT_DIR = os.path.join(os.path.dirname(__file__), 'spacefxClient')
spacesdk_client_dll = search_file("spacesdk-client.dll", SPACEFX_CLIENT_DIR)

# Find "spacesdk-client.dll" in any subdirectory
spacesdk_client_dll = next(base_dir.rglob('spacesdk-client.dll'), None)
if spacesdk_client_dll is None:
raise ValueError(f"The DLL 'spacesdk-client.dll' was not found in '{SPACEFX_CLIENT_DIR}'. Please check that the client library was built and deployed to '{SPACEFX_CLIENT_DIR}'")

# Found it. Add it
if spacesdk_client_dll:
assembly = clr.AddReference(str(spacesdk_client_dll))
else:
raise ValueError(f"The DLL 'spacesdk-client.dll' was not found in '{str(base_dir)}'. Please check that the client library was built and deployed to '{str(base_dir)}'")
clr.AddReference(spacesdk_client_dll)


# Import the .NET SpaceFx namespace
Expand Down

0 comments on commit a2c80a6

Please sign in to comment.