Skip to content

Commit a2f91e6

Browse files
refactor: make cli testing not depending on MAPDL. (#3678)
* refactor: make cli testing not depending on MAPDL. * chore: adding changelog file 3678.added.md [dependabot-skip] * test: improving test coverage --------- Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent b5e2492 commit a2f91e6

File tree

5 files changed

+252
-122
lines changed

5 files changed

+252
-122
lines changed

doc/changelog.d/3678.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
refactor: make cli testing not depending on MAPDL.

src/ansys/mapdl/core/cli/list_instances.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
flag_value=True,
3737
type=bool,
3838
default=False,
39-
help="Print only instances",
39+
help="Do not print the child process, only the main processes (instances).",
4040
)
4141
@click.option(
4242
"--long",
@@ -72,6 +72,15 @@ def list_instances(instances, long, cmd, location) -> None:
7272
# Assuming all ansys processes have -grpc flag
7373
mapdl_instances = []
7474

75+
def is_grpc_based(proc):
76+
cmdline = proc.cmdline()
77+
return "-grpc" in cmdline
78+
79+
def get_port(proc):
80+
cmdline = proc.cmdline()
81+
ind_grpc = cmdline.index("-port")
82+
return cmdline[ind_grpc + 1]
83+
7584
def is_valid_process(proc):
7685
valid_status = proc.status() in [
7786
psutil.STATUS_RUNNING,
@@ -81,12 +90,7 @@ def is_valid_process(proc):
8190
valid_ansys_process = ("ansys" in proc.name().lower()) or (
8291
"mapdl" in proc.name().lower()
8392
)
84-
# Early exit to avoid checking 'cmdline' of a protected process (raises psutil.AccessDenied)
85-
if not valid_ansys_process:
86-
return False
87-
88-
grpc_is_active = "-grpc" in proc.cmdline()
89-
return valid_status and valid_ansys_process and grpc_is_active
93+
return valid_status and valid_ansys_process and is_grpc_based(proc)
9094

9195
for proc in psutil.process_iter():
9296
# Check if the process is running and not suspended
@@ -104,8 +108,6 @@ def is_valid_process(proc):
104108
continue
105109

106110
# printing
107-
table = []
108-
109111
if long:
110112
cmd = True
111113
location = True
@@ -120,18 +122,10 @@ def is_valid_process(proc):
120122
if location:
121123
headers.append("Working directory")
122124

123-
def get_port(proc):
124-
cmdline = proc.cmdline()
125-
ind_grpc = cmdline.index("-port")
126-
return cmdline[ind_grpc + 1]
127-
128-
def is_grpc_based(proc):
129-
cmdline = proc.cmdline()
130-
return "-grpc" in cmdline
131-
132125
table = []
133126
for each_p in mapdl_instances:
134-
if not each_p.ansys_instance or not is_grpc_based(each_p):
127+
if instances and not each_p.ansys_instance:
128+
# Skip child processes if only printing instances
135129
continue
136130

137131
proc_line = []

src/ansys/mapdl/core/cli/start.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ def start(
281281
additional_switches=additional_switches,
282282
start_timeout=start_timeout,
283283
port=port,
284-
license_server_check=license_server_check,
285284
license_type=license_type,
286285
version=version,
287286
)

src/ansys/mapdl/core/cli/stop.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ def stop(port: int, pid: Optional[int], all: bool) -> None:
6868
"""
6969
import psutil
7070

71-
from ansys.mapdl.core.launcher import is_ansys_process
72-
7371
PROCESS_OK_STATUS = [
7472
# List of all process status, comment out the ones that means that
7573
# process is not OK.
@@ -97,15 +95,11 @@ def stop(port: int, pid: Optional[int], all: bool) -> None:
9795
killed_ = False
9896
for proc in psutil.process_iter():
9997
try:
100-
if (
101-
psutil.pid_exists(proc.pid)
102-
and proc.status() in PROCESS_OK_STATUS
103-
and is_ansys_process(proc)
104-
):
98+
if _is_valid_ansys_process(PROCESS_OK_STATUS, proc):
10599
# Killing "all"
106100
if all:
107101
try:
108-
proc.kill()
102+
_kill_process(proc)
109103
killed_ = True
110104
except psutil.NoSuchProcess:
111105
pass
@@ -114,7 +108,7 @@ def stop(port: int, pid: Optional[int], all: bool) -> None:
114108
# Killing by ports
115109
if str(port) in proc.cmdline():
116110
try:
117-
proc.kill()
111+
_kill_process(proc)
118112
killed_ = True
119113
except psutil.NoSuchProcess:
120114
pass
@@ -154,8 +148,9 @@ def stop(port: int, pid: Optional[int], all: bool) -> None:
154148

155149
p = psutil.Process(pid)
156150
for child in p.children(recursive=True):
157-
child.kill()
158-
p.kill()
151+
_kill_process(child)
152+
153+
_kill_process(p)
159154

160155
if p.status == "running":
161156
click.echo(
@@ -168,3 +163,19 @@ def stop(port: int, pid: Optional[int], all: bool) -> None:
168163
+ f"The process with PID {pid} and its children have been stopped."
169164
)
170165
return
166+
167+
168+
def _kill_process(proc):
169+
proc.kill()
170+
171+
172+
def _is_valid_ansys_process(PROCESS_OK_STATUS, proc):
173+
import psutil
174+
175+
from ansys.mapdl.core.launcher import is_ansys_process
176+
177+
return (
178+
psutil.pid_exists(proc.pid)
179+
and proc.status() in PROCESS_OK_STATUS
180+
and is_ansys_process(proc)
181+
)

0 commit comments

Comments
 (0)