Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
62 changes: 62 additions & 0 deletions src/ansys/tools/installer/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,65 @@ def get_pkg_versions(pkg_name):
session.verify = True

return all_versions


def get_targets(pkg_name, version, exclude_tests_and_docs=True):
"""
Get the available targets for a package version.

Parameters
----------
pkg_name : str
Name of the package for which to fetch the available targets.
version : str
Version of the package.
exclude_tests_and_docs : bool, optional
If True, exclude test and documentation targets from the list.
Default is True.

Returns
-------
list
A sorted list of available targets for the specified package version.
The first element is an empty string, mimicking the behavior of
no target.

Examples
--------
>>> get_targets("pyansys", "0.1.0")
['target1', 'target2', ...]
"""
session = requests.Session()
session.verify = False
urls = [
f"https://pypi.python.org/pypi/{pkg_name}/{version}/json",
f"https://pypi.org/pypi/{pkg_name}/{version}/json",
]
all_targets = []

for url in urls:
try:
targets = json.loads(requests.get(url, verify=certifi.where()).content)[
"info"
]
# Check if targets are available
if targets.get("provides_extra"):
all_targets = targets["provides_extra"]
break
except (requests.exceptions.SSLError, requests.exceptions.ConnectionError):
LOG.warning(f"Cannot connect to {url}... No target listed.")

session.verify = True

# Ensure the first element is an empty string
all_targets.insert(0, "")

# Exclude test and documentation targets if specified
if exclude_tests_and_docs:
all_targets = [
target
for target in all_targets
if "tests" not in target.lower() and "doc" not in target.lower()
]

return all_targets
56 changes: 49 additions & 7 deletions src/ansys/tools/installer/installed_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from PySide6.QtGui import QStandardItem, QStandardItemModel
from PySide6.QtWidgets import QComboBox

from ansys.tools.installer.common import get_pkg_versions
from ansys.tools.installer.common import get_pkg_versions, get_targets
from ansys.tools.installer.configure_json import ConfigureJson
from ansys.tools.installer.constants import PYANSYS_LIBS, SELECT_VENV_MANAGE_TAB
from ansys.tools.installer.find_python import (
Expand Down Expand Up @@ -300,23 +300,45 @@ def __init__(self, parent=None):
hbox_install_pyansys = QtWidgets.QHBoxLayout()
pyansys_pkg_manage_box_layout.addLayout(hbox_install_pyansys)

package_layout = QtWidgets.QVBoxLayout()
package_label = QtWidgets.QLabel("Package")
self.model = QStandardItemModel()
self.packages_combo = QComboBox()
self.packages_combo.setModel(self.model)
package_layout.addWidget(package_label)
package_layout.addWidget(self.packages_combo)

version_layout = QtWidgets.QVBoxLayout()
version_label = QtWidgets.QLabel("Version")
self.versions_combo = QComboBox()
version_layout.addWidget(version_label)
version_layout.addWidget(self.versions_combo)

target_layout = QtWidgets.QVBoxLayout()
target_label = QtWidgets.QLabel("Target (optional)")
self.version_target_combo = QComboBox()
target_layout.addWidget(target_label)
target_layout.addWidget(self.version_target_combo)

install_button_layout = QtWidgets.QVBoxLayout()
install_button_label = QtWidgets.QLabel(" ")
self.button_launch_cmd = QtWidgets.QPushButton("Install")
self.button_launch_cmd.clicked.connect(self.install_pyansys_packages)
install_button_layout.addWidget(install_button_label)
install_button_layout.addWidget(self.button_launch_cmd)

self.button_launch_cmd.clicked.connect(self.install_pyansys_packages)
for library in PYANSYS_LIBS:
self.model.appendRow(QStandardItem(library))

self.packages_combo.currentIndexChanged.connect(self.update_package_combo)
self.versions_combo.currentIndexChanged.connect(
self.update_package_target_combo
)
self.update_package_combo(0)

hbox_install_pyansys.addWidget(self.packages_combo)
hbox_install_pyansys.addWidget(self.versions_combo)
hbox_install_pyansys.addWidget(self.button_launch_cmd)
hbox_install_pyansys.addLayout(package_layout)
hbox_install_pyansys.addLayout(version_layout)
hbox_install_pyansys.addLayout(target_layout)
hbox_install_pyansys.addLayout(install_button_layout)
layout.addWidget(pyansys_pkg_manage_box)

# ensure the table is always in focus
Expand Down Expand Up @@ -391,8 +413,10 @@ def install_pyansys_packages(self):
"""Install PyAnsys - chosen packages."""
chosen_pkg = self.packages_combo.currentText()
chosen_ver = self.versions_combo.currentText()
chosen_target = self.version_target_combo.currentText()
fmt_target = "" if not chosen_target else f"[{chosen_target}]"
pck_ver = (
f"{PYANSYS_LIBS[chosen_pkg]}=={chosen_ver}"
f"{PYANSYS_LIBS[chosen_pkg]}{fmt_target}=={chosen_ver}"
if chosen_ver
else f"{PYANSYS_LIBS[chosen_pkg]}"
)
Expand All @@ -415,6 +439,24 @@ def update_package_combo(self, index):
self.versions_combo.setModel(versions_model)
self.versions_combo.setCurrentIndex(0)

def update_package_target_combo(self, index):
"""Depending on the version chosen for a package, update the available list of targets."""
package_name = PYANSYS_LIBS[self.packages_combo.currentText()]
version = self.versions_combo.currentText()

# Clear the previous targets
self.version_target_combo.clear()

# Get the available targets for the selected package version
targets = get_targets(package_name, version)

# Populate the target combo box with the available targets
for target in targets:
self.version_target_combo.addItem(target)

# Set the first target as the current selection
self.version_target_combo.setCurrentIndex(0)

def list_packages(self):
"""List installed Python packages."""
self.launch_cmd("uv pip list")
Expand Down