Skip to content

Commit

Permalink
Update for python wheel generation and debugging (#5)
Browse files Browse the repository at this point in the history
* removing errant proto compiling

* updating for logger and new proto paths

* updating to enable python wheel build

* updating integration tests

* Updating to run integration test for python

* updating path calculation
  • Loading branch information
bigtallcampbell authored Jul 9, 2024
1 parent ba092eb commit 843e6db
Show file tree
Hide file tree
Showing 24 changed files with 182 additions and 752 deletions.
7 changes: 4 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
"runArgs": [
"--name=spacesdk-client"
],
"workspaceFolder": "/workspaces/spacesdk-client",
"workspaceMount": "source=${localWorkspaceFolder},target=/workspaces/spacesdk-client,type=bind,consistency=cached",
"workspaceFolder": "/workspace/spacesdk-client",
"workspaceMount": "source=${localWorkspaceFolder},target=/workspace/spacesdk-client,type=bind,consistency=cached",
"features": {
"ghcr.io/microsoft/azure-orbital-space-sdk/spacefx-dev:0.11.0": {
"app_name": "spacesdk-client",
"app_type": "spacesdk-client"
"app_type": "spacesdk-client",
"dev_language": "python"
}
},
"hostRequirements": {
Expand Down
48 changes: 47 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@
},
"requireExactSource": true
},
{
"name": "DebugClient (Python)",
"type": "debugpy",
"request": "attach",
"preLaunchTask": "deploy-debugshim-client-python",
"connect": {
"host": "localhost",
"port": 5678,
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "${workspaceFolder}"
}
],
"justMyCode": true,
"postDebugTask": "reset-debugshim-client",
"presentation": {
"hidden": false,
"group": "",
"order": 1
}
},
{
"name": "Integration Tests - Debug (DotNet)",
"type": "coreclr",
Expand Down Expand Up @@ -95,6 +118,29 @@
"group": "integrationtests",
"order": 2
},
}
},
{
"name": "Integration Tests (python) - Run",
"type": "debugpy",
"request": "attach",
"preLaunchTask": "deploy-debugshim-client-integrationtest-python",
"connect": {
"host": "localhost",
"port": 5678,
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "${workspaceFolder}"
}
],
"justMyCode": true,
"postDebugTask": "reset-debugshim-client",
"presentation": {
"hidden": false,
"group": "integrationtests",
"order": 3
},
},
]
}
7 changes: 4 additions & 3 deletions .vscode/projectPostBuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@
#-------------------------------------------------------------------------------------------------------------
source "${SPACEFX_DIR:?}/modules/load_modules.sh" $@ --log_dir "${SPACEFX_DIR:?}/logs/${APP_NAME:?}"

cd /workspaces/spacesdk-client
cd ${CONTAINER_WORKING_DIR}

############################################################
# Script variables
############################################################
DEBUG=false
RELEASE=false
TARGET_DIR=""
PYTHON_DLL_DIR="/workspaces/spacesdk-client/spacefx/spacefxClient"
DOTNET_DLL_BUILD_DIR_PREFIX="/workspaces/spacesdk-client/src/bin"

PYTHON_DLL_DIR="${CONTAINER_WORKING_DIR}/spacefx/spacefxClient"
DOTNET_DLL_BUILD_DIR_PREFIX="${CONTAINER_WORKING_DIR}/src/bin"
DOTNET_DLL_BUILD_DIR_SUFFIX="/net6.0"
DOTNET_DLL_BUILD_DIR=""
############################################################
Expand Down
54 changes: 54 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,60 @@
"clear": false
}
},
{
"label": "deploy-debugshim-client-integrationtest-python",
"isBackground": false,
"dependsOn": [
"build-client"
],
"command": "/bin/bash",
"type": "shell",
"args": [
"/spacefx-dev/debugShim-deploy.sh",
"--debug_shim",
"${DEBUG_SHIM_CLIENT}",
"--python_file",
"${workspaceFolder}/test/integrationTests_python/integrationTest.py",
"--port",
"5678",
"--disable_plugin_configs"
],
"presentation": {
"echo": true,
"reveal": "never",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": false
}
},
{
"label": "deploy-debugshim-client-python",
"isBackground": false,
"dependsOn": [
"build-client"
],
"command": "/bin/bash",
"type": "shell",
"args": [
"/spacefx-dev/debugShim-deploy.sh",
"--debug_shim",
"${DEBUG_SHIM_CLIENT}",
"--python_file",
"${workspaceFolder}/test/debugClient_python/debugClient.py",
"--port",
"5678",
"--disable_plugin_configs"
],
"presentation": {
"echo": true,
"reveal": "never",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": false
}
},
{
"label": "reset-debugshim-client",
"isBackground": true,
Expand Down
9 changes: 9 additions & 0 deletions spacefx/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from spacefx import protos
from spacefx import _sdk_client
from spacefx import client
from spacefx import logging
from spacefx import position
from spacefx import link
from spacefx import sensor

logger = logging.__SpaceFxLogger
70 changes: 50 additions & 20 deletions spacefx/_sdk_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import os

import pythonnet


def find_git_root(starting_directory):
"""
Search for a .git directory by moving up the directory tree from the starting directory.
If found, return the path to the directory containing the .git directory.
If not found and the root of the directory tree is reached, return None.
"""
current_directory = starting_directory
while True:
# Check if .git directory exists in the current directory
if os.path.isdir(os.path.join(current_directory, ".git")):
return os.path.join(current_directory, ".git")
# Move up one directory level
parent_directory = os.path.dirname(current_directory)
if parent_directory == current_directory:
# Root of the directory tree is reached without finding .git
return None
current_directory = parent_directory


def search_file(filename, search_path):
"""Search for a file recursively in a directory tree."""
for dirpath, _, filenames in os.walk(search_path):
Expand All @@ -11,42 +29,54 @@ def search_file(filename, search_path):
return os.path.join(dirpath, file)
raise FileNotFoundError(f"{filename} not found in {search_path}")

# Find the dotnet directory
PROD_DOTNET_DIR = "/usr/share/dotnet/shared"
DOTNET_DIR=""

prod_dotnet_dir = "/usr/share/dotnet/shared"
dev_dotnet_dir = "/root/.dotnet/shared"
dotnet_dir = ""
# SpaceFX-Dev installed dotnet to the .git directory so it can be used by the
# debugShim and the devcontainer. This will locate the .git directory by
# walking up the tree
DEV_DOTNET_DIR = find_git_root(os.path.dirname(os.path.abspath(__file__)))

if DEV_DOTNET_DIR:
DEV_DOTNET_DIR = os.path.join(DEV_DOTNET_DIR, "spacefx-dev", "dotnet", "shared")
DOTNET_DIR = DEV_DOTNET_DIR if os.path.exists(DEV_DOTNET_DIR) else None

if not DOTNET_DIR:
DOTNET_DIR = PROD_DOTNET_DIR if os.path.exists(PROD_DOTNET_DIR) else None

if not DOTNET_DIR:
raise ValueError(f"Was not able to find dotnet directory as '{PROD_DOTNET_DIR}' nor within a '{DEV_DOTNET_DIR}'. Please check that dotnet is installed")

if os.path.exists(prod_dotnet_dir):
dotnet_dir = prod_dotnet_dir
elif os.path.exists(dev_dotnet_dir):
dotnet_dir = dev_dotnet_dir
else:
raise ValueError(f"Was not able to find dotnet directory as '{prod_dotnet_dir}' nor '{dev_dotnet_dir}'. Please check that dotnet is installed")

# 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)
runtimeconfig_file = search_file("Microsoft.AspNetCore.App.runtimeconfig.json", DOTNET_DIR)

pythonnet.load("coreclr", runtime_config=runtimeconfig_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')):
for dirpath, dirnames, filenames in os.walk(os.path.join(DOTNET_DIR, 'Microsoft.AspNetCore.App')):
if "runtimes" in dirpath:
continue
for file in filenames:
if file.endswith(".dll"):
Assembly.LoadFile(os.path.join(dirpath, file))


# Load the client adapter library which is in the parent directory / spacefxClient
for dirpath, dirnames, filenames in os.walk(os.path.join(os.path.dirname(__file__), 'spacefxClient')):
if "runtimes" in dirpath:
continue
for file in filenames:
if file.endswith(".dll"):
dll_lib = Assembly.LoadFile(os.path.join(dirpath, file))
if file == "sdk-dotnet.dll":
assembly = clr.AddReference(os.path.join(dirpath, file))
base_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'spacefxClient')

# Find "spacesdk-client.dll" in any subdirectory
spacesdk_client_dll = next(base_dir.rglob('spacesdk-client.dll'), None)

# 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)}'")


# Import the .NET SpaceFx namespace
import Microsoft.Azure.SpaceFx
Expand Down
Loading

0 comments on commit 843e6db

Please sign in to comment.