Skip to content

Commit

Permalink
refactor out unnecessary interpreter wrapper to use a contextmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Feb 22, 2018
1 parent 41a1a9a commit 6a68876
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 78 deletions.
6 changes: 0 additions & 6 deletions src/python/pants/backend/python/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ python_library(
':python_chroot',
':python_requirement',
':python_requirements',
':sandboxed_interpreter',
':sdist_builder',
':thrift_builder',
]
Expand Down Expand Up @@ -126,11 +125,6 @@ python_library(
sources = ['python_requirements.py'],
)

python_library(
name = 'sandboxed_interpreter',
sources = ['sandboxed_interpreter.py'],
)

python_library(
name = 'sdist_builder',
sources = ['sdist_builder.py'],
Expand Down
64 changes: 0 additions & 64 deletions src/python/pants/backend/python/sandboxed_interpreter.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/python/pants/backend/python/tasks/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ python_library(
'3rdparty/python:pex',
'3rdparty/python/twitter/commons:twitter.common.collections',
'3rdparty/python/twitter/commons:twitter.common.dirutil',
'src/python/pants/backend/native',
'src/python/pants/backend/native/subsystems',
'src/python/pants/backend/python:python_requirement',
'src/python/pants/backend/python:python_requirements',
'src/python/pants/backend/python:interpreter_cache',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import glob
import os
import shutil
from contextlib import contextmanager

from pex.interpreter import PythonInterpreter

from pants.backend.native.subsystems.llvm import LLVM
from pants.backend.python.python_requirement import PythonRequirement
from pants.backend.python.sandboxed_interpreter import SandboxedInterpreter
from pants.backend.python.targets.python_requirement_library import PythonRequirementLibrary
from pants.backend.python.tasks.pex_build_util import is_local_python_dist
from pants.backend.python.tasks.setup_py import SetupPyRunner
Expand Down Expand Up @@ -67,7 +67,6 @@ def execute(self):
fingerprint_strategy=DefaultFingerprintStrategy(),
invalidate_dependents=True) as invalidation_check:
interpreter = self.context.products.get_data(PythonInterpreter)
sandboxed_interpreter = SandboxedInterpreter(self.llvm_base_dir, interpreter)

for vt in invalidation_check.invalid_vts:
if vt.target.dependencies:
Expand All @@ -76,7 +75,7 @@ def execute(self):
'List any 3rd party requirements in the install_requirements argument '
'of your setup function.'
)
self._create_dist(vt.target, vt.results_dir, sandboxed_interpreter)
self._create_dist(vt.target, vt.results_dir, interpreter)

for vt in invalidation_check.all_vts:
dist = self._get_whl_from_dir(os.path.join(vt.results_dir, 'dist'))
Expand All @@ -100,13 +99,40 @@ def _copy_sources(self, dist_tgt, dist_target_dir):
src_relative_to_target_base)
shutil.copyfile(abs_src_path, src_rel_to_results_dir)

def _create_dist(self, dist_tgt, dist_target_dir, sandboxed_interpreter):
@contextmanager
def _sandboxed_setuppy(self):
sanitized_env = os.environ.copy()

# use our compiler at the front of the path
# TODO: when we provide ld and stdlib headers, don't add the original path
sanitized_env['PATH'] = ':'.join([
os.path.join(self.llvm_base_dir, 'bin'),
sanitized_env.get('PATH'),
])

# TODO: figure out whether we actually should be compiling fat binaries.
# this line tells distutils to only compile for 64-bit archs -- if not, it
# will attempt to build a fat binary for 32- and 64-bit archs, which makes
# clang invoke "lipo", an osx command which does not appear to be open
# source. see Lib/distutils/sysconfig.py and Lib/_osx_support.py in CPython.
sanitized_env['ARCHFLAGS'] = '-arch x86_64'

env_vars_to_scrub = ['CC', 'CXX']
for env_var in env_vars_to_scrub:
sanitized_env.pop(env_var, None)

with environment_as(**sanitized_env):
yield


def _create_dist(self, dist_tgt, dist_target_dir, interpreter):
"""Create a .whl file for the specified python_distribution target."""
self.context.log.debug("dist_target_dir: '{}'".format(dist_target_dir))
self._copy_sources(dist_tgt, dist_target_dir)
# Build a whl using SetupPyRunner and return its absolute path.
setup_runner = SetupPyRunner(dist_target_dir, 'bdist_wheel', interpreter=sandboxed_interpreter)
setup_runner.run()
with self._sandboxed_setuppy():
# Build a whl using SetupPyRunner and return its absolute path.
setup_runner = SetupPyRunner(dist_target_dir, 'bdist_wheel', interpreter=interpreter)
setup_runner.run()

def _inject_synthetic_dist_requirements(self, dist, req_lib_addr):
"""Inject a synthetic requirements library that references a local wheel.
Expand Down

0 comments on commit 6a68876

Please sign in to comment.