Skip to content

Commit

Permalink
Addresses a few changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lionel Vital authored and Chris Livingston committed Jan 30, 2018
1 parent 022b1e7 commit be2583a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 43 deletions.
8 changes: 2 additions & 6 deletions src/python/pants/backend/python/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
from pants.backend.python.targets.python_library import PythonLibrary
from pants.backend.python.targets.python_requirement_library import PythonRequirementLibrary
from pants.backend.python.targets.python_tests import PythonTests
from pants.backend.python.tasks.gather_sources import GatherSources
from pants.backend.python.tasks.pytest_prep import PytestPrep
from pants.backend.python.tasks.pytest_run import PytestRun
from pants.backend.python.tasks.python_binary_create import PythonBinaryCreate
from pants.backend.python.tasks2.build_local_python_distributions import BuildLocalPythonDistributions
from pants.backend.python.tasks2.gather_sources import GatherSources
from pants.backend.python.tasks2.pytest_prep import PytestPrep
from pants.backend.python.tasks2.pytest_run import PytestRun
from pants.backend.python.tasks2.python_binary_create import PythonBinaryCreate
from pants.backend.python.tasks2.python_create_distributions import PythonCreateDistributions
from pants.backend.python.tasks2.python_repl import PythonRepl
from pants.backend.python.tasks2.python_run import PythonRun
from pants.backend.python.tasks2.resolve_requirements import ResolveRequirements
Expand Down Expand Up @@ -63,7 +59,7 @@ def build_file_aliases():

def register_goals():
task(name='interpreter', action=SelectInterpreter).install('pyprep')
task(name='build-local-dists', action=PythonCreateDistributions).install('pyprep')
task(name='build-local-dists', action=BuildLocalPythonDistributions).install('pyprep')
task(name='requirements', action=ResolveRequirements).install('pyprep')
task(name='sources', action=GatherSources).install('pyprep')
task(name='py', action=PythonRun).install('run')
Expand Down
20 changes: 10 additions & 10 deletions src/python/pants/backend/python/targets/python_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def __init__(self,
format, e.g. ``'CPython>=3', or just ['>=2.7','<3']`` for requirements
agnostic to interpreter class.
"""
self.address = address
payload = payload or Payload()
payload.add_fields({
'sources': self.create_sources_field(sources, address.spec_path, key_arg='sources'),
Expand All @@ -54,15 +53,16 @@ def __init__(self,
super(PythonDistribution, self).__init__(address=address, payload=payload, **kwargs)
self.add_labels('python')

count_of_setup_py = [os.path.basename(s) for s in sources].count('setup.py')
if count_of_setup_py == 0:
raise TargetDefinitionException(self,
'A setup.py is required to create a python_dist. '
'You must include a setup.py file in your sources field.')
if count_of_setup_py > 1:
raise TargetDefinitionException(self,
'Multiple setup.py files detected. You can only specify one '
'setup.py in a python_dist target.')
has_setup_py_in_top_level = False
for source in sources:
raise ValueError(self.address.rel_path)
if source == os.path.join(self.address.rel_path, 'setup.py'):
has_setup_py_in_top_level = True

if not has_setup_py_in_top_level:
raise TargetDefinitionException(
self, 'A setup.py in the top-level directory relative to the target definition is required.'
)

# Check that the compatibility requirements are well-formed.
for req in self.payload.compatibility:
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/backend/python/tasks/pex_build_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ def has_python_requirements(tgt):
def targets_are_invalid(targets, invalid_targets):
"""Return whether `invalid_targets` contains at least one target from `targets`.
:param invalid_targets: A list of targets that have been invalidated by a cache manager.
:param targets: A list of targets to check for membership in `invalid_targets`.
:param invalid_targets: A list of targets that have been invalidated by a cache manager.
:return: A boolean indicating if any target in `targets` exists in `invalid_targets`.
"""
return any([target in invalid_targets for target in targets])
return set(targets).isdisjoint(set(invalid_targets))


def _create_source_dumper(builder, tgt):
Expand Down
6 changes: 3 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 @@ -15,7 +15,7 @@
from pants.backend.python.tasks2.pex_build_util import (dump_requirements, dump_sources,
has_python_requirements, has_python_sources,
has_resources)
from pants.backend.python.tasks2.python_create_distributions import PythonCreateDistributions
from pants.backend.python.tasks2.build_local_python_distributions import BuildLocalPythonDistributions
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 @@ -44,7 +44,7 @@ def cache_target_dirs(self):
def prepare(cls, options, round_manager):
round_manager.require_data(PythonInterpreter)
round_manager.require_data('python') # For codegen.
round_manager.require_data(PythonCreateDistributions.PYTHON_DISTS)
round_manager.require_data(BuildLocalPythonDistributions.PYTHON_DISTS)

@staticmethod
def is_binary(target):
Expand Down Expand Up @@ -131,7 +131,7 @@ def _create_binary(self, binary_tgt, results_dir):
dump_requirements(builder, interpreter, req_tgts, self.context.log, binary_tgt.platforms)

# Dump built python distributions, if any, into builder's chroot.
built_dists = self.context.products.get_data(PythonCreateDistributions.PYTHON_DISTS)
built_dists = self.context.products.get_data(BuildLocalPythonDistributions.PYTHON_DISTS)
if built_dists:
for dist in built_dists:
builder.add_dist_location(dist)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,13 @@ def create_pex(self, pex_info=None):

# Note that we check for the existence of the directory, instead of for invalid_vts,
# to cover the empty case.
<<<<<<< 365e70892e9ebcafb7e376fbe4f68694e6efba5e
local_python_dist_targets = self.context.targets(is_local_python_dist)
invalid_target_objs = [v.target for v in invalidation_check.invalid_vts]
context_has_invalid_python_dists = any([lpdt in invalid_target_objs for lpdt in local_python_dist_targets])
if not os.path.isdir(path) or context_has_invalid_python_dists:
source_pexes = self.context.products.get_data(GatherSources.PythonSources).all()
requirements_pex = self.context.products.get_data(ResolveRequirements.REQUIREMENTS_PEX)
pexes = [requirements_pex] + source_pexes
=======
if not os.path.isdir(path) or targets_are_invalid(python_dist_targets, invalid_targets):
pexes = [
self.context.products.get_data(ResolveRequirements.REQUIREMENTS_PEX),
self.context.products.get_data(GatherSources.PYTHON_SOURCES)
]
>>>>>>> Fix merge conflict

if self.extra_requirements():
extra_reqs = [PythonRequirement(req_str) for req_str in self.extra_requirements()]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from pex.pex import PEX
from pex.pex_builder import PEXBuilder

from pants.backend.python.tasks2.build_local_python_distributions import BuildLocalPythonDistributions
from pants.backend.python.tasks2.pex_build_util import dump_requirements, targets_are_invalid
from pants.backend.python.tasks2.python_create_distributions import PythonCreateDistributions
from pants.invalidation.cache_manager import VersionedTargetSet
from pants.task.task import Task
from pants.util.dirutil import safe_concurrent_creation
Expand All @@ -29,7 +29,7 @@ class ResolveRequirementsTaskBase(Task):
@classmethod
def prepare(cls, options, round_manager):
round_manager.require_data(PythonInterpreter)
round_manager.require_data(PythonCreateDistributions.PYTHON_DISTS)
round_manager.require_data(BuildLocalPythonDistributions.PYTHON_DISTS)

def resolve_requirements(self, req_libs, python_dist_targets=()):
"""Requirements resolution for PEX files.
Expand Down Expand Up @@ -69,7 +69,7 @@ def _build_requirements_pex(self, interpreter, path, req_libs):
# NB: If a python_dist depends on a requirement X and another target in play requires
# an incompatible version of X, this will cause the pex resolver to throw an incompatiblity
# error and Pants will abort the build.
built_dists = self.context.products.get_data(PythonCreateDistributions.PYTHON_DISTS)
built_dists = self.context.products.get_data(BuildLocalPythonDistributions.PYTHON_DISTS)
if built_dists:
for dist in built_dists:
builder.add_dist_location(dist)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

import glob
import os
import shutil

Expand All @@ -19,7 +20,7 @@
from pants.util.dirutil import safe_mkdir


class PythonCreateDistributions(Task):
class BuildLocalPythonDistributions(Task):
"""Create python distributions (.whl) from python_dist targets."""

options_scope = 'python-create-distributions'
Expand Down Expand Up @@ -72,16 +73,16 @@ def _create_dist(self, dist_tgt, dist_target_dir):
src_relative_to_target_base)
shutil.copyfile(abs_src_path, src_rel_to_results_dir)
# Build a whl using SetupPyRunner and return its absolute path.
install_dir = os.path.join(dist_target_dir, 'dist')
install_dir = os.path.join(dist_target_dir, '.dist')
safe_mkdir(install_dir)
setup_runner = SetupPyRunner(dist_target_dir, 'bdist_wheel', interpreter=interpreter, install_dir=install_dir)
setup_runner.run()
return self._get_whl_from_dir(install_dir)

def _get_whl_from_dir(self, install_dir):
"""Return the absolute path of the whl in a setup.py install directory."""
dists = os.listdir(install_dir)
dists = glob.glob(os.path.join(install_dir, '*.whl'))
if len(dists) == 0:
raise TaskError('No distributions were produced by python_create_distribution task.')
else:
return os.path.join(os.path.abspath(install_dir), dists[0])

return dists[0]
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
from textwrap import dedent

from pants.backend.python.targets.python_distribution import PythonDistribution
from pants.backend.python.tasks2.python_create_distributions import PythonCreateDistributions
from pants.backend.python.tasks2.build_local_python_distributions import BuildLocalPythonDistributions
from pants_test.backend.python.tasks.python_task_test_base import PythonTaskTestBase


class TestPythonCreateDistributions(PythonTaskTestBase):
class TestBuildLocalPythonDistributions(PythonTaskTestBase):
@classmethod
def task_type(cls):
return PythonCreateDistributions
return BuildLocalPythonDistributions

def setUp(self):
super(TestPythonCreateDistributions, self).setUp()
super(BuildLocalPythonDistributions, self).setUp()

# Setup simple python_dist target
sources = ['foo.py', 'bar.py', '__init__.py', 'setup.py']
Expand All @@ -43,9 +43,9 @@ def setUp(self):
sources=sources)

def test_python_create_distributions(self):
context = self.context(target_roots=[self.python_dist_tgt], for_task_types=[PythonCreateDistributions])
context = self.context(target_roots=[self.python_dist_tgt], for_task_types=[BuildLocalPythonDistributions])
python_create_distributions_task = self.create_task(context)
python_create_distributions_task.execute()
built_dists = context.products.get_data(PythonCreateDistributions.PYTHON_DISTS)
built_dists = context.products.get_data(BuildLocalPythonDistributions.PYTHON_DISTS)
self.assertGreater(len(built_dists), 0)
self.assertTrue(any(['my_dist-0.0.0-py2-none-any.whl' in dist for dist in list(built_dists)]))

0 comments on commit be2583a

Please sign in to comment.