Skip to content

Commit 501021c

Browse files
authored
feat: allow the installation of targets on PyAnsys packages (#464)
1 parent 4d9995d commit 501021c

File tree

2 files changed

+111
-7
lines changed

2 files changed

+111
-7
lines changed

src/ansys/tools/installer/common.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,65 @@ def get_pkg_versions(pkg_name):
127127
session.verify = True
128128

129129
return all_versions
130+
131+
132+
def get_targets(pkg_name, version, exclude_tests_and_docs=True):
133+
"""
134+
Get the available targets for a package version.
135+
136+
Parameters
137+
----------
138+
pkg_name : str
139+
Name of the package for which to fetch the available targets.
140+
version : str
141+
Version of the package.
142+
exclude_tests_and_docs : bool, optional
143+
If True, exclude test and documentation targets from the list.
144+
Default is True.
145+
146+
Returns
147+
-------
148+
list
149+
A sorted list of available targets for the specified package version.
150+
The first element is an empty string, mimicking the behavior of
151+
no target.
152+
153+
Examples
154+
--------
155+
>>> get_targets("pyansys", "0.1.0")
156+
['target1', 'target2', ...]
157+
"""
158+
session = requests.Session()
159+
session.verify = False
160+
urls = [
161+
f"https://pypi.python.org/pypi/{pkg_name}/{version}/json",
162+
f"https://pypi.org/pypi/{pkg_name}/{version}/json",
163+
]
164+
all_targets = []
165+
166+
for url in urls:
167+
try:
168+
targets = json.loads(requests.get(url, verify=certifi.where()).content)[
169+
"info"
170+
]
171+
# Check if targets are available
172+
if targets.get("provides_extra"):
173+
all_targets = targets["provides_extra"]
174+
break
175+
except (requests.exceptions.SSLError, requests.exceptions.ConnectionError):
176+
LOG.warning(f"Cannot connect to {url}... No target listed.")
177+
178+
session.verify = True
179+
180+
# Ensure the first element is an empty string
181+
all_targets.insert(0, "")
182+
183+
# Exclude test and documentation targets if specified
184+
if exclude_tests_and_docs:
185+
all_targets = [
186+
target
187+
for target in all_targets
188+
if "tests" not in target.lower() and "doc" not in target.lower()
189+
]
190+
191+
return all_targets

src/ansys/tools/installer/installed_table.py

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from PySide6.QtGui import QStandardItem, QStandardItemModel
3333
from PySide6.QtWidgets import QComboBox
3434

35-
from ansys.tools.installer.common import get_pkg_versions
35+
from ansys.tools.installer.common import get_pkg_versions, get_targets
3636
from ansys.tools.installer.configure_json import ConfigureJson
3737
from ansys.tools.installer.constants import PYANSYS_LIBS, SELECT_VENV_MANAGE_TAB
3838
from ansys.tools.installer.find_python import (
@@ -300,23 +300,45 @@ def __init__(self, parent=None):
300300
hbox_install_pyansys = QtWidgets.QHBoxLayout()
301301
pyansys_pkg_manage_box_layout.addLayout(hbox_install_pyansys)
302302

303+
package_layout = QtWidgets.QVBoxLayout()
304+
package_label = QtWidgets.QLabel("Package")
303305
self.model = QStandardItemModel()
304306
self.packages_combo = QComboBox()
305307
self.packages_combo.setModel(self.model)
308+
package_layout.addWidget(package_label)
309+
package_layout.addWidget(self.packages_combo)
306310

311+
version_layout = QtWidgets.QVBoxLayout()
312+
version_label = QtWidgets.QLabel("Version")
307313
self.versions_combo = QComboBox()
314+
version_layout.addWidget(version_label)
315+
version_layout.addWidget(self.versions_combo)
316+
317+
target_layout = QtWidgets.QVBoxLayout()
318+
target_label = QtWidgets.QLabel("Target (optional)")
319+
self.version_target_combo = QComboBox()
320+
target_layout.addWidget(target_label)
321+
target_layout.addWidget(self.version_target_combo)
322+
323+
install_button_layout = QtWidgets.QVBoxLayout()
324+
install_button_label = QtWidgets.QLabel(" ")
308325
self.button_launch_cmd = QtWidgets.QPushButton("Install")
309-
self.button_launch_cmd.clicked.connect(self.install_pyansys_packages)
326+
install_button_layout.addWidget(install_button_label)
327+
install_button_layout.addWidget(self.button_launch_cmd)
310328

329+
self.button_launch_cmd.clicked.connect(self.install_pyansys_packages)
311330
for library in PYANSYS_LIBS:
312331
self.model.appendRow(QStandardItem(library))
313-
314332
self.packages_combo.currentIndexChanged.connect(self.update_package_combo)
333+
self.versions_combo.currentIndexChanged.connect(
334+
self.update_package_target_combo
335+
)
315336
self.update_package_combo(0)
316337

317-
hbox_install_pyansys.addWidget(self.packages_combo)
318-
hbox_install_pyansys.addWidget(self.versions_combo)
319-
hbox_install_pyansys.addWidget(self.button_launch_cmd)
338+
hbox_install_pyansys.addLayout(package_layout)
339+
hbox_install_pyansys.addLayout(version_layout)
340+
hbox_install_pyansys.addLayout(target_layout)
341+
hbox_install_pyansys.addLayout(install_button_layout)
320342
layout.addWidget(pyansys_pkg_manage_box)
321343

322344
# ensure the table is always in focus
@@ -391,8 +413,10 @@ def install_pyansys_packages(self):
391413
"""Install PyAnsys - chosen packages."""
392414
chosen_pkg = self.packages_combo.currentText()
393415
chosen_ver = self.versions_combo.currentText()
416+
chosen_target = self.version_target_combo.currentText()
417+
fmt_target = "" if not chosen_target else f"[{chosen_target}]"
394418
pck_ver = (
395-
f"{PYANSYS_LIBS[chosen_pkg]}=={chosen_ver}"
419+
f"{PYANSYS_LIBS[chosen_pkg]}{fmt_target}=={chosen_ver}"
396420
if chosen_ver
397421
else f"{PYANSYS_LIBS[chosen_pkg]}"
398422
)
@@ -415,6 +439,24 @@ def update_package_combo(self, index):
415439
self.versions_combo.setModel(versions_model)
416440
self.versions_combo.setCurrentIndex(0)
417441

442+
def update_package_target_combo(self, index):
443+
"""Depending on the version chosen for a package, update the available list of targets."""
444+
package_name = PYANSYS_LIBS[self.packages_combo.currentText()]
445+
version = self.versions_combo.currentText()
446+
447+
# Clear the previous targets
448+
self.version_target_combo.clear()
449+
450+
# Get the available targets for the selected package version
451+
targets = get_targets(package_name, version)
452+
453+
# Populate the target combo box with the available targets
454+
for target in targets:
455+
self.version_target_combo.addItem(target)
456+
457+
# Set the first target as the current selection
458+
self.version_target_combo.setCurrentIndex(0)
459+
418460
def list_packages(self):
419461
"""List installed Python packages."""
420462
self.launch_cmd("uv pip list")

0 commit comments

Comments
 (0)