Skip to content

Commit

Permalink
First pass at kwlzn change suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Livingston committed Jan 30, 2018
1 parent e2f84cc commit 7297096
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
14 changes: 2 additions & 12 deletions src/python/pants/backend/python/targets/python_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,15 @@
from pants.base.payload_field import PrimitiveField

class PythonDistribution(PythonTarget):
"""A Python distribution containing c/cpp extensions.
"""A Python distribution.
:API: public
"""

@classmethod
def alias(cls):
return 'python_distribution'


def __init__(self,
platforms=(),
**kwargs):
def __init__(self, **kwargs):
payload = Payload()
payload.add_fields({
'platforms': PrimitiveField(tuple(maybe_list(platforms or [])))
})
super(PythonDistribution, self).__init__(sources=[], payload=payload, **kwargs)

@property
def platforms(self):
return self.payload.platforms
9 changes: 6 additions & 3 deletions src/python/pants/backend/python/tasks/pex_build_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def has_python_sources(tgt):
return is_python_target(tgt) and tgt.has_sources()


def has_python_and_c_sources(tgt):
def is_local_python_dist(tgt):
return isinstance(tgt, PythonDistribution)


Expand Down Expand Up @@ -165,14 +165,15 @@ def build_python_distribution_from_target(target, workdir):
whl_dist = chroot_deps_contents[0] # TODO: find better way to grab .whl from chroot
whl_location = os.path.join(pydist_workdir, fingerprint, '.deps', whl_dist)
return whl_location
return None
else:
raise TaskError('Failed to package python distribution for target: %s', target.name)


def dump_python_distributions(builder, dist_targets, workdir, log):
"""Dump python distribution targets into a given builder
:param builder: Dump the python_distributions into this builder.
:param dist_targets: PythonDistribution targets to dump
:param dist_targets: A list of `PythonDistribution` targets to build.
:param workdir: Working directory for python targets (./pantsd/python)
:param log: Use this logger.
"""
Expand All @@ -181,6 +182,8 @@ def dump_python_distributions(builder, dist_targets, workdir, log):
locations = set()
for tgt in dist_targets:
whl_location = build_python_distribution_from_target(tgt, workdir)
if whl_location in locations:
raise TaskError('Two wheels of the same name have been created: %s.', whl_location)
if whl_location:
locations.add(whl_location)

Expand Down
5 changes: 2 additions & 3 deletions src/python/pants/backend/python/tasks/python_binary_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
from pants.backend.python.targets.python_binary import PythonBinary
from pants.backend.python.tasks2.pex_build_util import (dump_python_distributions,
dump_requirements, dump_sources,
has_python_and_c_sources,
has_python_requirements, has_python_sources,
has_resources)
has_resources, is_local_python_dist)
from pants.base.build_environment import get_buildroot
from pants.base.exceptions import TaskError
from pants.build_graph.target_scopes import Scopes
Expand Down Expand Up @@ -125,7 +124,7 @@ def _create_binary(self, binary_tgt, results_dir):
builder.add_interpreter_constraint(constraint)
elif has_python_requirements(tgt):
req_tgts.append(tgt)
elif has_python_and_c_sources(tgt):
elif is_local_python_dist(tgt):
python_dist_targets.append(tgt)

# Dump everything into the builder's chroot.
Expand Down
5 changes: 2 additions & 3 deletions src/python/pants/backend/python/tasks/resolve_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

from pants.backend.python.tasks2.pex_build_util import (has_python_and_c_sources,
has_python_requirements)
from pants.backend.python.tasks2.pex_build_util import has_python_requirements, is_local_python_dist
from pants.backend.python.tasks2.resolve_requirements_task_base import ResolveRequirementsTaskBase


Expand All @@ -20,6 +19,6 @@ def product_types(cls):

def execute(self):
req_libs = self.context.targets(has_python_requirements)
python_dist_targets = self.context.targets(has_python_and_c_sources)
python_dist_targets = self.context.targets(is_local_python_dist)
pex = self.resolve_requirements(req_libs, python_dist_targets=python_dist_targets)
self.context.products.register_data(self.REQUIREMENTS_PEX, pex)
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ class ResolveRequirementsTaskBase(Task):
def prepare(cls, options, round_manager):
round_manager.require_data(PythonInterpreter)

def resolve_requirements(self, req_libs, python_dist_targets=None):
def resolve_requirements(self, req_libs, local_python_dist_targets=None):
"""Requirements resolution for PEX files.
:param req_libs: A list of :class:`PythonRequirementLibrary` targets to resolve.
:param local_python_dist_targets: A list of :class:`PythonDistribution` targets to resolve.
:returns a PEX containing target requirements and any specified python dist targets.
"""
with self.invalidated(req_libs) as invalidation_check:
# If there are no relevant targets, we still go through the motions of resolving
# an empty set of requirements, to prevent downstream tasks from having to check
Expand All @@ -48,12 +54,12 @@ def resolve_requirements(self, req_libs, python_dist_targets=None):
if not os.path.isdir(path):
with safe_concurrent_creation(path) as safe_path:
self._build_requirements_pex(interpreter, safe_path, req_libs,
python_dist_targets=python_dist_targets)
local_python_dist_targets=local_python_dist_targets)
return PEX(path, interpreter=interpreter)

def _build_requirements_pex(self, interpreter, path, req_libs, python_dist_targets=None):
def _build_requirements_pex(self, interpreter, path, req_libs, local_python_dist_targets=None):
builder = PEXBuilder(path=path, interpreter=interpreter, copy=True)
dump_requirements(builder, interpreter, req_libs, self.context.log)
if python_dist_targets:
dump_python_distributions(builder, python_dist_targets, self.workdir, self.context.log)
if local_python_dist_targets:
dump_python_distributions(builder, local_python_dist_targets, self.workdir, self.context.log)
builder.freeze()

0 comments on commit 7297096

Please sign in to comment.