Skip to content

Commit

Permalink
More reliable way to get Blender's Python interpreter path (#70)
Browse files Browse the repository at this point in the history
* More reliable way to get Python Blender interpreter path

* Test against latest versions in CI

* Blender 3.3.0 hangs download

* Comment job
  • Loading branch information
mondeja authored Jun 16, 2022
1 parent 2fcfb14 commit f861328
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 3.0.2
current_version = 3.0.3

[bumpversion:file:pytest_blender/__init__.py]

Expand Down
13 changes: 11 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion pytest_blender/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "3.0.2"
__version__ = "3.0.3"

from pytest_blender.utils import (
get_addons_dir,
Expand Down
18 changes: 13 additions & 5 deletions pytest_blender/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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():
Expand Down
37 changes: 28 additions & 9 deletions pytest_blender/utils.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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


Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit f861328

Please sign in to comment.