Skip to content

Commit

Permalink
now we can declare cpp_sources in python_dist targets!
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Feb 1, 2018
1 parent 2591288 commit 3155138
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

from pants.backend.python.targets.python_target import PythonTarget
from pants.backend.python.targets.python_library import PythonLibrary
from pants.base.payload import Payload
from pants.source.payload_fields import SourcesField
from pants.source.wrapped_globs import FilesetWithSpec
from pants.util.memo import memoized_property


class PythonDistribution(PythonTarget):
class PythonDistribution(PythonLibrary):
"""A Python distribution target that accepts a user-defined setup.py."""

default_native_sources_globs = '*.c'
default_native_sources_exclude_globs = None

@memoized_property
def _cpp_sources_field(self):
cpp_sources_field = self.payload.get_field('cpp_sources')
Expand All @@ -25,8 +28,6 @@ def _cpp_sources_field(self):
def cpp_sources_relative_to_target_base(self):
return self._cpp_sources_field.sources

default_sources_globs = '*.py'

@classmethod
def alias(cls):
return 'python_dist'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ def _create_dist(self, dist_tgt, dist_target_dir):
src_relative_to_target_base)
shutil.copyfile(abs_src_path, src_rel_to_results_dir)
with temporary_dir() as tmpdir:
native_sources =
"'{}'".format(x) for x in dist_tgt.cpp_sources_relative_to_target_base()
native_sources = ("'{}'".format(x) for x in dist_tgt.cpp_sources_relative_to_target_base())
pantssetup_import_contents = PANTSSETUP_IMPORT_BOILERPLATE.format(
setup_target=repr(dist_tgt),
native_sources_joined=','.join(native_sources),
Expand Down
3 changes: 2 additions & 1 deletion src/python/pants/bin/engine_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pants.engine.legacy.parser import LegacyPythonCallbacksParser
from pants.engine.legacy.structs import (GoTargetAdaptor, JavaLibraryAdaptor, JunitTestsAdaptor,
JvmAppAdaptor, PythonLibraryAdaptor, PythonTargetAdaptor,
PythonTestsAdaptor, RemoteSourcesAdaptor,
PythonDistributionAdaptor, PythonTestsAdaptor, RemoteSourcesAdaptor,
ScalaLibraryAdaptor, TargetAdaptor)
from pants.engine.mapper import AddressMapper
from pants.engine.native import Native
Expand Down Expand Up @@ -53,6 +53,7 @@ def __init__(self, build_file_aliases):
self._table[alias] = ScalaLibraryAdaptor
for alias in ['python_library', 'pants_plugin']:
self._table[alias] = PythonLibraryAdaptor
self._table['python_dist'] = PythonDistributionAdaptor
for alias in ['go_library', 'go_binary']:
self._table[alias] = GoTargetAdaptor

Expand Down
23 changes: 23 additions & 0 deletions src/python/pants/engine/legacy/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,29 @@ def default_sources_exclude_globs(self):
return PythonTestsAdaptor.python_test_globs


class PythonDistributionAdaptor(PythonTargetAdaptor):

def _get_cpp_sources(self):
cpp_sources = getattr(self, 'cpp_sources', None)
if cpp_sources is None and self.default_native_sources_globs is not None:
return Globs(*self.default_native_sources_globs,
spec_path=self.address.spec_path,
exclude=self.default_native_sources_exclude_globs)
return cpp_sources

@property
def field_adaptors(self):
with exception_logging(logger, 'Exception in `field_adaptors` property'):
field_adaptors = super(PythonDistributionAdaptor, self).field_adaptors
cpp_sources = self._get_cpp_sources()
if not cpp_sources:
return field_adaptors
base_globs = BaseGlobs.from_sources_field(cpp_sources, self.address.spec_path)
path_globs = base_globs.to_path_globs(self.address.spec_path)
cpp_sources_field = SourcesField(self.address, 'cpp_sources', base_globs.filespecs, path_globs)
return field_adaptors + (cpp_sources_field,)


class PythonTestsAdaptor(PythonTargetAdaptor):
python_test_globs = ('test_*.py', '*_test.py')

Expand Down

0 comments on commit 3155138

Please sign in to comment.