diff --git a/colcon_powershell/shell/powershell.py b/colcon_powershell/shell/powershell.py index b65ba11..bf24f7b 100644 --- a/colcon_powershell/shell/powershell.py +++ b/colcon_powershell/shell/powershell.py @@ -15,6 +15,7 @@ from colcon_core.shell import logger from colcon_core.shell import ShellExtensionPoint from colcon_core.shell.template import expand_template +import psutil """Environment variable to override the CMake executable""" POWERSHELL_COMMAND_ENVIRONMENT_VARIABLE = EnvironmentVariable( @@ -44,6 +45,20 @@ def which_executable(environment_variable, executable_name): POWERSHELL_COMMAND_ENVIRONMENT_VARIABLE.name, powershell_executable_name) +def parent_process_is_powershell(): + """Determine if the parent process is PowerShell. + + In case the executable name is not found, + it is assumed that the parent process is not PowerShell. + """ + parent = psutil.Process().parent() + if parent is None: + return False + + parent_name = parent.name() + return parent_name == powershell_executable_name + + class PowerShellExtension(ShellExtensionPoint): """Generate `.ps1` scripts to extend the environment.""" @@ -66,7 +81,7 @@ def __init__(self): # noqa: D107 pathexts = os.environ.get('PATHEXT', '').lower().split(os.pathsep) self._is_primary = '.cpl' in pathexts else: - self._is_primary = bool(os.environ.get('PSModulePath')) + self._is_primary = parent_process_is_powershell() if not POWERSHELL_EXECUTABLE: self._is_primary = False diff --git a/setup.cfg b/setup.cfg index 630c8de..b5ab345 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,6 +27,7 @@ keywords = colcon python_requires = >=3.6 install_requires = colcon-core>=0.12.0 + psutil>=5.4.3 packages = find: zip_safe = false diff --git a/stdeb.cfg b/stdeb.cfg index 19167c2..ce61098 100644 --- a/stdeb.cfg +++ b/stdeb.cfg @@ -1,6 +1,6 @@ [colcon-powershell] No-Python2: -Depends3: python3-colcon-core (>= 0.12.0) +Depends3: python3-colcon-core (>= 0.12.0), python3-psutil (>= 5.4.3) Suite: focal jammy noble resolute bookworm trixie X-Python3-Version: >= 3.6 Upstream-Version-Suffix: +upstream diff --git a/test/spell_check.words b/test/spell_check.words index e535729..9cd1237 100644 --- a/test/spell_check.words +++ b/test/spell_check.words @@ -9,6 +9,7 @@ pathlib plugin powershell prepend +psutil pwsh pydocstyle pytest