2023-11-30 22:32:06 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
import subprocess
|
|
|
|
import typing
|
|
|
|
|
|
|
|
import allure
|
|
|
|
import psutil
|
|
|
|
|
|
|
|
import configs
|
2024-06-18 06:53:37 +00:00
|
|
|
from configs.system import get_platform
|
2023-11-30 22:32:06 +00:00
|
|
|
|
2023-11-30 10:51:00 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
2023-11-30 22:32:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def find_process_by_port(port: int) -> typing.List[int]:
|
|
|
|
pid_list = []
|
|
|
|
for proc in psutil.process_iter():
|
|
|
|
try:
|
|
|
|
for conns in proc.connections(kind='inet'):
|
|
|
|
if conns.laddr.port == port:
|
|
|
|
pid_list.append(proc.pid)
|
|
|
|
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
|
|
pass
|
|
|
|
return pid_list
|
|
|
|
|
|
|
|
|
|
|
|
def find_free_port(start: int, step: int):
|
|
|
|
while find_process_by_port(start):
|
2024-01-09 07:52:40 +00:00
|
|
|
start += step
|
2023-11-30 22:32:06 +00:00
|
|
|
return start
|
|
|
|
|
2024-01-09 07:52:40 +00:00
|
|
|
|
2023-11-30 22:32:06 +00:00
|
|
|
@allure.step('Kill process')
|
2024-06-17 14:50:40 +00:00
|
|
|
def kill_process(pid):
|
|
|
|
LOG.debug(f'Terminating process {pid}')
|
2024-06-11 14:04:06 +00:00
|
|
|
|
2024-06-17 14:50:40 +00:00
|
|
|
try:
|
2024-06-18 06:53:37 +00:00
|
|
|
if get_platform() == "Windows":
|
2024-06-17 14:50:40 +00:00
|
|
|
subprocess.call(f"taskkill /F /T /PID {str(pid)}")
|
2024-06-18 06:53:37 +00:00
|
|
|
elif get_platform() in ["Linux", "Darwin"]:
|
2024-06-17 14:50:40 +00:00
|
|
|
os.kill(pid, signal.SIGKILL)
|
|
|
|
else:
|
2024-06-18 06:53:37 +00:00
|
|
|
raise NotImplementedError(f"Unsupported platform: {get_platform()}")
|
2024-06-17 14:50:40 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Failed to terminate process {pid}: {e}")
|
2024-01-09 07:52:40 +00:00
|
|
|
|
2024-10-07 11:45:43 +00:00
|
|
|
|
2023-11-30 22:32:06 +00:00
|
|
|
@allure.step('System execute command')
|
|
|
|
def execute(
|
|
|
|
command: list,
|
2023-11-30 10:51:00 +00:00
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
stdout=subprocess.STDOUT,
|
|
|
|
shell=False,
|
2023-11-30 22:32:06 +00:00
|
|
|
):
|
2023-11-30 10:51:00 +00:00
|
|
|
LOG.info('Executing: %s', command)
|
2023-11-30 22:32:06 +00:00
|
|
|
process = subprocess.Popen(command, shell=shell, stderr=stderr, stdout=stdout)
|
|
|
|
return process.pid
|
|
|
|
|
2024-01-09 07:52:40 +00:00
|
|
|
|
2023-11-30 22:32:06 +00:00
|
|
|
@allure.step('System run command')
|
|
|
|
def run(
|
|
|
|
command: list,
|
2023-11-30 10:51:00 +00:00
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
stdout=subprocess.STDOUT,
|
|
|
|
shell=False,
|
|
|
|
timeout_sec=configs.timeouts.PROCESS_TIMEOUT_SEC
|
2023-11-30 22:32:06 +00:00
|
|
|
):
|
2023-11-30 10:51:00 +00:00
|
|
|
LOG.info('Running: %s', command)
|
|
|
|
process = subprocess.run(
|
|
|
|
command,
|
|
|
|
shell=shell,
|
|
|
|
stderr=stderr,
|
|
|
|
stdout=stdout,
|
|
|
|
timeout=timeout_sec,
|
|
|
|
check=True
|
|
|
|
)
|
2024-07-24 10:13:05 +00:00
|
|
|
|
2024-10-07 11:45:43 +00:00
|
|
|
|
2024-07-24 10:13:05 +00:00
|
|
|
@allure.step('Get pid by process name')
|
|
|
|
def get_pid_by_process_name(name):
|
|
|
|
pid_list = []
|
|
|
|
for proc in psutil.process_iter():
|
|
|
|
try:
|
|
|
|
if proc.name() == name and proc.status() != 'zombie':
|
|
|
|
pid_list.append(proc.pid)
|
|
|
|
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
|
|
|
continue
|
|
|
|
return pid_list if len(pid_list) > 0 else None
|