Resolved an issue with "pio pkg exec" command on WIndows while executing Python scripts from a package

This commit is contained in:
Ivan Kravets
2023-07-11 14:23:05 +03:00
parent 68e62c7137
commit be4d016f61
3 changed files with 15 additions and 10 deletions

View File

@ -19,6 +19,7 @@ PlatformIO Core 6
~~~~~~~~~~~~~~~~~~~
* Resolved an issue that caused generated projects for `PlatformIO IDE for VSCode <https://docs.platformio.org/en/latest/integration/ide/vscode.html>`__ to break when the ``-iprefix`` compiler flag was used
* Resolved an issue encountered while utilizing the `pio pkg exec <https://docs.platformio.org/en/latest/core/userguide/pkg/cmd_exec.html>`__ command on the Windows platform to execute Python scripts from a package
6.1.9 (2023-07-06)
~~~~~~~~~~~~~~~~~~

View File

@ -20,7 +20,7 @@ import click
from platformio.compat import IS_MACOS, IS_WINDOWS
from platformio.exception import ReturnErrorCode, UserSideException
from platformio.package.manager.tool import ToolPackageManager
from platformio.proc import get_pythonexe_path
from platformio.proc import get_pythonexe_path, where_is_program
@click.command("exec", short_help="Run command from package tool")
@ -52,9 +52,13 @@ def package_exec_cmd(obj, package, call, args):
inject_pkg_to_environ(pkg)
os.environ["PIO_PYTHON_EXE"] = get_pythonexe_path()
# inject current python interpreter on Windows
if IS_WINDOWS and args and args[0].endswith(".py"):
if args[0].endswith(".py"):
args = [os.environ["PIO_PYTHON_EXE"]] + list(args)
if not os.path.exists(args[1]):
args[1] = where_is_program(args[1])
result = None
try:
run_options = dict(shell=call is not None, env=os.environ)

View File

@ -185,10 +185,17 @@ def copy_pythonpath_to_osenv():
def where_is_program(program, envpath=None):
env = os.environ
env = os.environ.copy()
if envpath:
env["PATH"] = envpath
# look up in $PATH
for bin_dir in env.get("PATH", "").split(os.pathsep):
if os.path.isfile(os.path.join(bin_dir, program)):
return os.path.join(bin_dir, program)
if IS_WINDOWS and os.path.isfile(os.path.join(bin_dir, "%s.exe" % program)):
return os.path.join(bin_dir, "%s.exe" % program)
# try OS's built-in commands
try:
result = exec_command(["where" if IS_WINDOWS else "which", program], env=env)
@ -197,13 +204,6 @@ def where_is_program(program, envpath=None):
except OSError:
pass
# look up in $PATH
for bin_dir in env.get("PATH", "").split(os.pathsep):
if os.path.isfile(os.path.join(bin_dir, program)):
return os.path.join(bin_dir, program)
if os.path.isfile(os.path.join(bin_dir, "%s.exe" % program)):
return os.path.join(bin_dir, "%s.exe" % program)
return program