Skip to content

Commit

Permalink
Merge pull request #16 from pfmoore/pipfix
Browse files Browse the repository at this point in the history
Allow callers to specify how to run subprocesses
  • Loading branch information
pfmoore committed Oct 9, 2018
2 parents 5d25cfd + 9fafdea commit 450a96a
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions pep517/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class BackendUnavailable(Exception):
class UnsupportedOperation(Exception):
"""May be raised by build_sdist if the backend indicates that it can't."""

def default_subprocess_runner(cmd, cwd=None, extra_environ=None):
"""The default method of calling the wrapper subprocess."""
env = os.environ.copy()
if extra_environ:
env.update(extra_environ)

check_call(cmd, cwd=cwd, env=env)

class Pep517HookCaller(object):
"""A wrapper around a source directory to be built with a PEP 517 backend.
Expand All @@ -33,6 +41,16 @@ class Pep517HookCaller(object):
def __init__(self, source_dir, build_backend):
self.source_dir = abspath(source_dir)
self.build_backend = build_backend
self._subprocess_runner = default_subprocess_runner

# TODO: Is this over-engineered? Maybe frontends only need to
# set this when creating the wrapper, not on every call.
@contextmanager
def subprocess_runner(self, runner):
prev = self._subprocess_runner
self._subprocess_runner = runner
yield
self._subprocess_runner = prev

def get_requires_for_build_wheel(self, config_settings=None):
"""Identify packages required for building a wheel
Expand Down Expand Up @@ -108,8 +126,6 @@ def build_sdist(self, sdist_directory, config_settings=None):


def _call_hook(self, hook_name, kwargs):
env = os.environ.copy()

# On Python 2, pytoml returns Unicode values (which is correct) but the
# environment passed to check_call needs to contain string values. We
# convert here by encoding using ASCII (the backend can only contain
Expand All @@ -121,14 +137,16 @@ def _call_hook(self, hook_name, kwargs):
else:
build_backend = self.build_backend

env['PEP517_BUILD_BACKEND'] = build_backend
with tempdir() as td:
compat.write_json({'kwargs': kwargs}, pjoin(td, 'input.json'),
indent=2)

# Run the hook in a subprocess
check_call([sys.executable, _in_proc_script, hook_name, td],
cwd=self.source_dir, env=env)
self._subprocess_runner(
[sys.executable, _in_proc_script, hook_name, td],
cwd=self.source_dir,
extra_environ={'PEP517_BUILD_BACKEND': build_backend}
)

data = compat.read_json(pjoin(td, 'output.json'))
if data.get('unsupported'):
Expand Down

0 comments on commit 450a96a

Please sign in to comment.