From f861328bc49f9921d5e32de650a69ce319067743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar?= Date: Thu, 16 Jun 2022 12:16:10 +0200 Subject: [PATCH] More reliable way to get Blender's Python interpreter path (#70) * More reliable way to get Python Blender interpreter path * Test against latest versions in CI * Blender 3.3.0 hangs download * Comment job --- .bumpversion.cfg | 2 +- .github/workflows/ci.yml | 13 +++++++++++-- pytest_blender/__init__.py | 2 +- pytest_blender/__main__.py | 18 +++++++++++++----- pytest_blender/utils.py | 37 ++++++++++++++++++++++++++++--------- setup.cfg | 2 +- 6 files changed, 55 insertions(+), 19 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 17e184b..6a7dcdb 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 3.0.2 +current_version = 3.0.3 [bumpversion:file:pytest_blender/__init__.py] diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f9108bb..5b078c1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,7 +27,7 @@ jobs: # Test previous Blender versions - platform: ubuntu-latest pytest-version: '7.1.2' - blender-version: '2.83.9' + blender-version: '2.83.20' python-version: '3.8' - platform: ubuntu-latest pytest-version: '7.1.2' @@ -41,8 +41,17 @@ jobs: # Test against Pytest v6 - platform: ubuntu-latest pytest-version: '6.2.5' - blender-version: '2.93.8' + blender-version: '2.93.9' python-version: '3.9' + # Latest Blender (Py311) + # + # FIXME: Blender uses for 3.2.0 Python3.10, + # but the pytest-cov test fails because + # it does not show coverage: + #- platform: ubuntu-latest + # pytest-version: '7.1.2' + # blender-version: '3.2.0' + # python-version: '3.11-dev' # Test in MacOS (Py310) - platform: macos-latest pytest-version: '7.1.2' diff --git a/pytest_blender/__init__.py b/pytest_blender/__init__.py index efd99b1..a9964b4 100644 --- a/pytest_blender/__init__.py +++ b/pytest_blender/__init__.py @@ -1,4 +1,4 @@ -__version__ = "3.0.2" +__version__ = "3.0.3" from pytest_blender.utils import ( get_addons_dir, diff --git a/pytest_blender/__main__.py b/pytest_blender/__main__.py index 8509589..f6508b3 100644 --- a/pytest_blender/__main__.py +++ b/pytest_blender/__main__.py @@ -2,7 +2,11 @@ import sys from pytest_blender import __version__ -from pytest_blender.utils import get_blender_binary_path_python, which_blender +from pytest_blender.utils import ( + GetPythonBlenderPathError, + get_blender_binary_path_python, + which_blender, +) __description__ = "Show a Blender's builtin Python interpreter location." @@ -44,10 +48,14 @@ def parse_args(args): def run(args): opts = parse_args(args) - blender_python = get_blender_binary_path_python(opts.blender_executable) - sys.stdout.write(f"{blender_python}\n") - - return 0 + try: + blender_python = get_blender_binary_path_python(opts.blender_executable) + except GetPythonBlenderPathError as exc: + sys.stderr.write(f"{exc.args[0]}\n") + return 1 + else: + sys.stdout.write(f"{blender_python}\n") + return 0 def main(): diff --git a/pytest_blender/utils.py b/pytest_blender/utils.py index 9a73c5c..fc32733 100644 --- a/pytest_blender/utils.py +++ b/pytest_blender/utils.py @@ -1,13 +1,25 @@ import os +import pprint import shutil import subprocess import sys import zipfile +PYTEST_BLENDER_NEW_ISSUE_URL = ( + "https://github.com/mondeja/pytest-blender/issues/new/choose" +) ZIP_ROOTS_IGNORE = [".DStore", ".git", ".gitignore", "__pycache__"] +class PytestBlenderException(Exception): + pass + + +class GetPythonBlenderPathError(PytestBlenderException): + pass + + def zipify_addon_package(in_dirpath, out_dirpath): zipped_path = os.path.join( out_dirpath, @@ -81,10 +93,11 @@ def get_blender_binary_path_python(blender_executable, blend_version=None): """ if blend_version is None: blend_version = get_blender_version(blender_executable) + id_string = "BLENDER-PYTHON-PATH:" python_expr = ( - "import sys;print(sys.executable);" + f"import sys;print('{id_string}',sys.executable);" if parse_version(blend_version) >= (2, 91) - else "import bpy;print(bpy.app.binary_path_python)" + else f"import bpy;print('{id_string}',bpy.app.binary_path_python)" ) stdout = subprocess.check_output( @@ -99,14 +112,20 @@ def get_blender_binary_path_python(blender_executable, blend_version=None): blender_python_path = None for line in stdout.decode("utf-8").splitlines(): - # this should be enough to support Windows and Unix based systems - if ( - os.path.exists(line) - and not os.path.isdir(line) - and "py" in os.path.basename(line.lower()) - ): - blender_python_path = line + if line.startswith(id_string): + blender_python_path = line.split(" ", maxsplit=1)[1] break + if blender_python_path is None: + debugging_data = { + "platform": sys.platform, + "blender_executable": blender_executable, + "stdout": stdout, + } + raise GetPythonBlenderPathError( + "Failed to retrieve the Blender's Python interpreter path." + f" Please, submit a report at {PYTEST_BLENDER_NEW_ISSUE_URL}" + f" including the next data:\n{pprint.pformat(debugging_data)}" + ) return blender_python_path diff --git a/setup.cfg b/setup.cfg index cb67426..2ab988d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = pytest_blender -version = 3.0.2 +version = 3.0.3 description = Blender Pytest plugin. long_description = file: README.md long_description_content_type = text/markdown