Skip to content

Commit

Permalink
Merge pull request #9 from mabu-github/feature/macos
Browse files Browse the repository at this point in the history
Feature/macos
  • Loading branch information
mathiasburger committed Feb 8, 2019
2 parents e907b18 + fa76ef5 commit 9957e86
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
42 changes: 39 additions & 3 deletions pgimp/GimpScriptRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

CHILD_PROCESS_START_TIMEOUT = 10

PYTHON2_PYTHONPATH = None

JsonType = Union[None, bool, int, float, str, list, dict]


Expand Down Expand Up @@ -61,18 +63,26 @@ class GimpUnsupportedOSException(GimpException):
"""


def is_linux():
return sys.platform in ['linux', 'linux2']


def is_mac_os():
return sys.platform == 'darwin'


def path_to_gimp_executable():
global EXECUTABLE_GIMP_PATH

if EXECUTABLE_GIMP_PATH is not None:
return EXECUTABLE_GIMP_PATH

if sys.platform in ['linux', 'linux2', 'darwin']:
if is_linux() or is_mac_os():
EXECUTABLE_GIMP_PATH = shutil.which(EXECUTABLE_GIMP)
else:
raise GimpUnsupportedOSException('Your operating system "{:s}" is not supported.'.format(sys.platform))

if sys.platform == 'darwin':
if is_mac_os():
locations = [
'/Applications/GIMP*.app/Contents/MacOS/gimp',
'/Applications/Gimp*.app/Contents/MacOS/gimp',
Expand All @@ -96,7 +106,7 @@ def path_to_xvfb_run():


def is_xvfb_present():
return path_to_xvfb_run() is not None
return path_to_xvfb_run() is not None


def strip_gimp_warnings(input):
Expand Down Expand Up @@ -128,6 +138,28 @@ def strip_initialization_warnings(error_lines):
return error_lines


def python2_pythonpath():
global PYTHON2_PYTHONPATH
if PYTHON2_PYTHONPATH is None:
python = shutil.which('python2')
if python is None:
raise GimpScriptException('Could not find a python2 installation.')
proc = subprocess.Popen(
[python],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, stderr = proc.communicate(
'import sys; print(":".join(set([p for p in sys.path if "site-packages" in p or "dist-packages" in p])));'.encode(),
timeout=5
)
if stderr.decode() != '':
raise GimpScriptException('Could not determine python2 site-packages location.')
PYTHON2_PYTHONPATH = stdout.decode().rstrip('\n')
return PYTHON2_PYTHONPATH


class GimpScriptRunner:
"""
Executes python2 scripts within gimp's python interpreter and is used to create
Expand Down Expand Up @@ -343,6 +375,10 @@ def _send_to_gimp(

gimp_environment = {'__working_directory__': self._working_directory}
gimp_environment.update(os.environ.copy())
if 'PYTHONPATH' not in gimp_environment:
gimp_environment['PYTHONPATH'] = python2_pythonpath()
else:
gimp_environment['PYTHONPATH'] = python2_pythonpath() + ':' + gimp_environment['PYTHONPATH']
gimp_environment.update({k: v for k, v in self._environment.items() if self._environment[k] is not None})

parameters = parameters or {}
Expand Down
6 changes: 5 additions & 1 deletion pgimp/GimpScriptRunnerTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pytest

from pgimp.GimpScriptRunner import GimpScriptRunner, GimpScriptException, GimpScriptExecutionTimeoutException, \
strip_gimp_warnings, strip_initialization_warnings
strip_gimp_warnings, strip_initialization_warnings, python2_pythonpath
from pgimp.util import file
from pgimp.util.TempFile import TempFile

Expand Down Expand Up @@ -217,3 +217,7 @@ def test_strip_initialization_warnings():
error_lines = strip_initialization_warnings(error_lines)

assert ['abc'] == error_lines


def test_python2_pythonpath():
assert 'site-packages' in python2_pythonpath() or 'dist-packages' in python2_pythonpath()
2 changes: 2 additions & 0 deletions requirements-python2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy>=1.10
typing
33 changes: 33 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import re
import shutil
import subprocess

from setuptools import setup, find_packages
from setuptools.command.build_py import build_py
Expand All @@ -8,6 +10,37 @@
from pgimp import __version__, PROJECT, AUTHOR
from pgimp.util import file


class GimpInstallationException(Exception):
pass


def check_python2_installation():
python = shutil.which('python2')
if python is None:
raise GimpInstallationException('Could not find a python2 installation.')
proc = subprocess.Popen(
[python],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)

requirements = open(os.path.join(os.path.dirname(__file__), 'requirements-python2.txt'), 'r').readlines()
imports = list(map(lambda r: re.split('[<=>\\s]', r)[0], filter(lambda x: x != '\n', requirements)))
import_statements = list(map(lambda r: 'import ' + r, imports))
stdout, stderr = proc.communicate(
'\n'.join(import_statements).encode(),
timeout=5
)
if stderr.decode() != '':
raise GimpInstallationException(
'At least one of the following packages is missing in the python2 installation: ' + ', '.join(imports)
)


check_python2_installation()

pgimp.execute_scripts_with_process_check = False # because psutil might not yet be installed


Expand Down

0 comments on commit 9957e86

Please sign in to comment.