From 61b40e68e833fa88f70067fd99ee556dcb3d3fa7 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Thu, 4 Aug 2022 08:54:33 -0600 Subject: Let test framework still run if no psutil Some odd environments - the one I'm running into it is inside an msys2 POSIX shell on Windows, using msys Python - don't have a psutil module to install. Let the test framework continue to work even if this is the case. fixes #4199 Signed-off-by: Mats Wichmann --- testing/framework/TestCmd.py | 65 +++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/testing/framework/TestCmd.py b/testing/framework/TestCmd.py index 039f9e0..8d258ac 100644 --- a/testing/framework/TestCmd.py +++ b/testing/framework/TestCmd.py @@ -302,7 +302,12 @@ import errno import hashlib import os import re -import psutil +try: + import psutil +except ImportError: + HAVE_PSUTIL = False +else: + HAVE_PSUTIL = True import shutil import signal import stat @@ -387,35 +392,39 @@ def _caller(tblist, skip): return string -def clean_up_ninja_daemon(self, result_type): +def clean_up_ninja_daemon(self, result_type) -> None: """ - Kill any running scons daemon started by ninja and clean up it's working dir and - temp files. + Kill any running scons daemon started by ninja and clean up + + Working directory and temp files are removed. + Skipped if this platform doesn't have psutil (e.g. msys2 on Windows) """ - if self: - for path in Path(self.workdir).rglob('.ninja'): - daemon_dir = Path(tempfile.gettempdir()) / ( - "scons_daemon_" + str(hashlib.md5(str(path.resolve()).encode()).hexdigest()) - ) - pidfiles = [daemon_dir / 'pidfile', path / 'scons_daemon_dirty'] - for pidfile in pidfiles: - if pidfile.exists(): - with open(pidfile) as f: - try: - pid = int(f.read()) - os.kill(pid, signal.SIGINT) - except OSError: - pass - - while True: - if pid not in [proc.pid for proc in psutil.process_iter()]: - break - else: - time.sleep(0.1) - - if not self._preserve[result_type]: - if daemon_dir.exists(): - shutil.rmtree(daemon_dir) + if not self or not HAVE_PSUTIL: + return + + for path in Path(self.workdir).rglob('.ninja'): + daemon_dir = Path(tempfile.gettempdir()) / ( + "scons_daemon_" + str(hashlib.md5(str(path.resolve()).encode()).hexdigest()) + ) + pidfiles = [daemon_dir / 'pidfile', path / 'scons_daemon_dirty'] + for pidfile in pidfiles: + if pidfile.exists(): + with open(pidfile) as f: + try: + pid = int(f.read()) + os.kill(pid, signal.SIGINT) + except OSError: + pass + + while True: + if pid not in [proc.pid for proc in psutil.process_iter()]: + break + else: + time.sleep(0.1) + + if not self._preserve[result_type]: + if daemon_dir.exists(): + shutil.rmtree(daemon_dir) def fail_test(self=None, condition=True, function=None, skip=0, message=None): -- cgit v0.12 From b6e4f71baae45899e43a65a5f899f2043ac1e3f5 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Thu, 4 Aug 2022 09:40:22 -0600 Subject: framework psutil usage: let the kill proceed Instead of skipping the whole kill/check loop if psutil module is not found, instead just skip the check part - should be okay to issue the kill. Signed-off-by: Mats Wichmann --- testing/framework/TestCmd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testing/framework/TestCmd.py b/testing/framework/TestCmd.py index 8d258ac..aabd1dc 100644 --- a/testing/framework/TestCmd.py +++ b/testing/framework/TestCmd.py @@ -399,7 +399,7 @@ def clean_up_ninja_daemon(self, result_type) -> None: Working directory and temp files are removed. Skipped if this platform doesn't have psutil (e.g. msys2 on Windows) """ - if not self or not HAVE_PSUTIL: + if not self: return for path in Path(self.workdir).rglob('.ninja'): @@ -416,7 +416,7 @@ def clean_up_ninja_daemon(self, result_type) -> None: except OSError: pass - while True: + while HAVE_PSUTIL: if pid not in [proc.pid for proc in psutil.process_iter()]: break else: -- cgit v0.12