Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added pythonnet check with warning output #235

Merged
merged 24 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b9caa98
added pythonnet check with warning output
May 4, 2023
33849b4
try except for cases of older versions of pip - src/ansys/mechanical/…
klmcadams May 4, 2023
8b3880e
added comment describing code block - src/ansys/mechanical/core/embed…
klmcadams May 4, 2023
1c58a27
adjusted warning output & pip comment spacing
klmcadams May 4, 2023
9ffcca4
moved pythonnet check to initializer.py
klmcadams May 7, 2023
bf63fbe
created unit test for pythonnet embedded instance
klmcadams May 7, 2023
c07c28b
updated changelog
klmcadams May 7, 2023
01ad078
Merge branch 'main' into fix/pythonnetconflict
klmcadams May 7, 2023
75c5860
adjusted syntax & added print lines
klmcadams May 7, 2023
8886aad
added remote session unit tests
klmcadams May 7, 2023
86a32a2
fixed style issues
klmcadams May 7, 2023
c32cee4
dynamic python path & used importlib.metadata to detect pythonnet
klmcadams May 7, 2023
80d957a
added apt install python venv to ci_cd yml file
klmcadams May 7, 2023
3b72fc7
added -y flag to apt install python venv
klmcadams May 7, 2023
c6da2f4
remove whitespace - src/ansys/mechanical/core/embedding/app.py
klmcadams May 8, 2023
cf4be53
pass instead of print in try/except - src/ansys/mechanical/core/embed…
klmcadams May 8, 2023
547c818
condensing stderr_output if statement - tests/embedding/test_app.py
klmcadams May 8, 2023
54ea106
assert not warning statement - tests/test_mechanical.py
klmcadams May 8, 2023
17a8976
assert not warning - tests/test_mechanical.py
klmcadams May 8, 2023
35fdee4
Updated comments for warning checking - tests/test_mechanical.py
klmcadams May 8, 2023
b9abfaf
Updated comment for warning check - tests/test_mechanical.py
klmcadams May 8, 2023
e7d2d9b
Updated comment for warning check - tests/embedding/test_app.py
klmcadams May 8, 2023
cd79873
changed assert warning statement - tests/embedding/test_app.py
klmcadams May 8, 2023
b129cd8
removed print statements
klmcadams May 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ jobs:
apt update
apt install -y lsb-release
apt install -y python3-pip
apt install -y python3.8-venv
pip3 install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org pip setuptools
pip3 install --upgrade pip flit
- name: Install packages for testing
Expand Down Expand Up @@ -221,6 +222,7 @@ jobs:
apt update
apt install -y lsb-release
apt install -y python3-pip
apt install -y python3.8-venv
pip3 install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org pip setuptools
pip3 install --upgrade pip flit
- name: Install packages for testing
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG](https://kee
- changelog (#222)
- add link to embedding examples (#228)
- Add `close()` method to `Ansys.Mechanical.Embedding.Application`. See (#229)
- Add check if pythonnet exists in the user environment (#235)

### Changed

Expand Down
14 changes: 14 additions & 0 deletions src/ansys/mechanical/core/embedding/initializer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Initializer for Mechanical embedding. Sets up paths and resolvers."""
from importlib.metadata import distribution
import os
from pathlib import Path
import sys
import warnings

from ansys.mechanical.core.embedding.loader import load_clr
from ansys.mechanical.core.embedding.resolver import resolve
Expand Down Expand Up @@ -38,6 +40,18 @@ def initialize(version):
# need to add system path in order to import the assembly with the resolver
__add_sys_path(version)

# Check if 'pythonnet' is installed... and if so, throw warning
try:
distribution("pythonnet")
warnings.warn(
"The pythonnet package was found in your environment "
"which interferes with the ansys-pythonnet package. "
"Some APIs may not work due to pythonnet being installed.",
stacklevel=2,
)
except ModuleNotFoundError:
pass

# load the CLR with mono that is shipped with the unified ansys installer
load_clr(os.environ[f"AWP_ROOT{version}"])

Expand Down
59 changes: 59 additions & 0 deletions tests/embedding/test_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Miscellaneous embedding tests"""
import os
import shutil
import subprocess
import sys
import tempfile

import pytest
Expand Down Expand Up @@ -52,3 +54,60 @@ def test_app_version(embedded_app):
version = embedded_app.version
assert type(version) is int
assert version >= 231


@pytest.mark.embedding
def test_warning_message():
"""Test pythonnet warning of the embedded instance."""
venv_name = "pythonnetvenv"
base = os.getcwd()

if "win" in sys.platform:
exe_dir = "Scripts"
else:
exe_dir = "bin"

venv_bin = os.path.join(base, "." + venv_name, exe_dir)

# Set up path to use the virtual environment
original_path = os.environ["PATH"]
os.environ["PATH"] = venv_bin + os.pathsep + os.environ.get("PATH", "")

# Create virtual environment
subprocess.run([sys.executable, "-m", "venv", "." + venv_name])

# Upgrade pip
upgrade_pip = subprocess.Popen(
[os.path.join(venv_bin, "python"), "-m", "pip", "install", "-U", "pip"]
)
upgrade_pip.wait()

# Install tests
install_tests = subprocess.Popen([os.path.join(venv_bin, "pip"), "install", "-e", ".[tests]"])
install_tests.wait()

# Install pythonnet
install_pythonnet = subprocess.Popen([os.path.join(venv_bin, "pip"), "install", "pythonnet"])
install_pythonnet.wait()

# Run embedded instance in virtual env with pythonnet installed
embedded_py = os.path.join(base, "tests", "scripts", "run_embedded_app.py")
check_warning = subprocess.Popen(
[os.path.join(venv_bin, "python"), embedded_py], stderr=subprocess.PIPE
)
check_warning.wait()
stderr_output = check_warning.stderr.read().decode()

# If UserWarning & pythonnet are in the stderr output, set warning to True.
# Otherwise, set warning to False
warning = True if "UserWarning" and "pythonnet" in stderr_output else False

# Assert warning message appears for embedded app
assert warning

# Remove virtual environment
venv_dir = os.path.join(base, "." + venv_name)
shutil.rmtree(venv_dir)

# Set PATH back to what it was originally
os.environ["PATH"] = original_path
5 changes: 5 additions & 0 deletions tests/scripts/run_embedded_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Launch embedded instance."""
import ansys.mechanical.core as pymechanical

# Launch embedded instance of app
app = pymechanical.App()
5 changes: 5 additions & 0 deletions tests/scripts/run_remote_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Launch remote session."""
import ansys.mechanical.core as pymechanical

# Launch remote session
pymechanical.launch_mechanical()
79 changes: 79 additions & 0 deletions tests/test_mechanical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import os
import pathlib
import re
import shutil
import subprocess
import sys

import grpc
import pytest
Expand Down Expand Up @@ -510,6 +513,82 @@ def test_launch_grpc_not_supported_version():
pymechanical.mechanical.launch_grpc(exec_file=exec_file)


@pytest.mark.remote_session_launch
def test_warning_message_pythonnet():
"""Test pythonnet warning of the remote session in virtual env."""
venv_name = "pythonnetvenv"
base = os.getcwd()

if "win" in sys.platform:
exe_dir = "Scripts"
else:
exe_dir = "bin"

venv_bin = os.path.join(base, "." + venv_name, exe_dir)

# Set up path to use the virtual environment
original_path = os.environ["PATH"]
os.environ["PATH"] = venv_bin + os.pathsep + os.environ.get("PATH", "")

# Create virtual environment
subprocess.run([sys.executable, "-m", "venv", "." + venv_name])

# Upgrade pip
upgrade_pip = subprocess.Popen(
[os.path.join(venv_bin, "python"), "-m", "pip", "install", "-U", "pip"]
)
upgrade_pip.wait()

# Install tests
install_tests = subprocess.Popen([os.path.join(venv_bin, "pip"), "install", "-e", ".[tests]"])
install_tests.wait()

# Install pythonnet
install_pythonnet = subprocess.Popen([os.path.join(venv_bin, "pip"), "install", "pythonnet"])
install_pythonnet.wait()

# Run remote session in virtual env with pythonnet installed
remote_py = os.path.join(base, "tests", "scripts", "run_remote_session.py")
check_warning = subprocess.Popen(
[os.path.join(venv_bin, "python"), remote_py], stderr=subprocess.PIPE
)
check_warning.wait()
stderr_output = check_warning.stderr.read().decode()

# If UserWarning & pythonnet are in the stderr output, set warning to True.
# Otherwise, set warning to False
warning = True if "UserWarning" and "pythonnet" in stderr_output else False

# Assert the warning message did not appear for the remote session
assert not warning

# Remove virtual environment
venv_dir = os.path.join(base, "." + venv_name)
shutil.rmtree(venv_dir)

# Set PATH back to what it was originally
os.environ["PATH"] = original_path


@pytest.mark.remote_session_launch
def test_warning_message_default():
"""Test pythonnet warning of the remote session in default env."""
base = os.getcwd()

# Run remote session
remote_py = os.path.join(base, "tests", "scripts", "run_remote_session.py")
check_warning = subprocess.Popen([sys.executable, remote_py], stderr=subprocess.PIPE)
check_warning.wait()
stderr_output = check_warning.stderr.read().decode()

# If UserWarning & pythonnet are in the stderr output, set warning to True.
# Otherwise, set warning to False
warning = True if "UserWarning" and "pythonnet" in stderr_output else False

# Assert the warning message did not appear for the remote session
assert not warning


# def test_call_before_launch_or_connect():
# import ansys.mechanical.core as pymechanical
# from ansys.mechanical.core.errors import MechanicalExitedError
Expand Down