forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the ability to pull in a Maven artifact and extract its conten…
…ts for use to feeding into another task. UnpackedJars (unpacked_jars) is a target that extracts files from a jar artifact. The UnpackJars task does the extraction work. DeferredSourcesMapper is a task that feeds some unpacked files into another task's sources= attribute. Initially supported by java_protobuf_library targets. Adds FromTarget (from_target) and which is used to re-direct the sources attribute to one of these UnpackedJars target Also adds DeferredSourcesField which is a SourcesField that doesn't know the exact list of sources when the graph is initially built. Initial Design doc: https://docs.google.com/a/squareup.com/document/d/1CeKmYBRDq_Agn_YO-Nn6Ek4amGbQnJpV3PceB0_tSsU/edit Testing Done: Added some unit tests, an example and an integration test. CI running at https://travis-ci.org/pantsbuild/pants/builds/47172787 Bugs closed: 720 Reviewed at https://rbcommons.com/s/twitter/r/1210/
- Loading branch information
1 parent
19f5f19
commit 0dba5f5
Showing
36 changed files
with
883 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
examples/src/java/com/pants/examples/protobuf/unpacked_jars/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
jvm_binary(name='unpacked_jars', | ||
basename='protobuf-unpacked-jars-example', | ||
source='ExampleProtobufExternalArchive.java', | ||
main='com.pants.examples.protobuf.unpacked_jars.ExampleProtobufExternalArchive', | ||
dependencies=[ | ||
'examples/src/protobuf/com/pants/examples/unpacked_jars' | ||
], | ||
) |
17 changes: 17 additions & 0 deletions
17
...es/src/java/com/pants/examples/protobuf/unpacked_jars/ExampleProtobufExternalArchive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package com.pants.examples.protobuf.unpacked_jars; | ||
|
||
import com.squareup.testing.protolib.External; | ||
|
||
class ExampleProtobufExternalArchive { | ||
private ExampleProtobufExternalArchive() { | ||
} | ||
|
||
public static void main(String[] args) { | ||
External.ExternalMessage message = External.ExternalMessage.newBuilder().setMessageType(1) | ||
.setMessageContent("Hello World!").build(); | ||
System.out.println("Message is: " + message.getMessageContent()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
examples/src/protobuf/com/pants/examples/unpacked_jars/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
java_protobuf_library(name='unpacked_jars', | ||
sources=from_target(':external-source'), | ||
) | ||
|
||
unpacked_jars(name='external-source', | ||
libraries=[':external-source-jars'], | ||
include_patterns=[ | ||
'com/squareup/testing/**/*.proto', | ||
], | ||
) | ||
|
||
jar_library(name='external-source-jars', | ||
jars=[ | ||
jar(org='com.squareup.testing.protolib', name='protolib-external-test', rev='0.0.2'), | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# coding=utf-8 | ||
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import (nested_scopes, generators, division, absolute_import, with_statement, | ||
print_function, unicode_literals) | ||
|
||
import six | ||
|
||
from pants.base.address import Addresses | ||
|
||
|
||
class FromTarget(object): | ||
"""Used in a BUILD file to redirect the value of the sources= attribute to another target. | ||
""" | ||
class ExpectedAddressError(Exception): | ||
"""Thrown if an object that is not an address is added to an import attribute. | ||
""" | ||
|
||
def __init__(self, parse_context): | ||
""" | ||
:param ParseContext parse_context: build file context | ||
""" | ||
self._parse_context = parse_context | ||
|
||
def __call__(self, address): | ||
"""Expects a string representing an address.""" | ||
if not isinstance(address, six.string_types): | ||
raise self.ExpectedAddressError("Expected string address argument, got type {type}" | ||
.format(type(address))) | ||
return Addresses(addresses=[address], rel_path=self._parse_context.rel_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/python/pants/backend/core/tasks/deferred_sources_mapper.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# coding=utf-8 | ||
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
|
||
from __future__ import (nested_scopes, generators, division, absolute_import, with_statement, | ||
print_function, unicode_literals) | ||
|
||
import logging | ||
|
||
from pants.backend.core.tasks.task import Task | ||
from pants.base.address_lookup_error import AddressLookupError | ||
from pants.base.payload_field import DeferredSourcesField | ||
from pants.base.source_root import SourceRoot | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class DeferredSourcesMapper(Task): | ||
"""Map DeferredSorucesFields to files that produce product 'unpacked_archives', like UnpackJars | ||
If you want a task to be able to map sources like this, make it require the 'deferred_sources' | ||
product. | ||
""" | ||
|
||
class SourcesTargetLookupError(AddressLookupError): | ||
"""Raised when the referenced target cannot be found in the build graph""" | ||
pass | ||
|
||
class NoUnpackedSourcesError(AddressLookupError): | ||
"""Raised when there are no files found unpacked from the archive""" | ||
pass | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(DeferredSourcesMapper, self).__init__(*args, **kwargs) | ||
|
||
def prepare(self, round_manager): | ||
super(DeferredSourcesMapper, self).prepare(round_manager) | ||
round_manager.require_data('unpacked_archives') | ||
|
||
@classmethod | ||
def product_types(cls): | ||
""" | ||
Declare product produced by this task | ||
deferred_sources does not have any data associated with it. Downstream tasks can | ||
depend on it just make sure that this task completes first. | ||
:return: | ||
""" | ||
return ['deferred_sources'] | ||
|
||
def execute(self): | ||
deferred_sources_fields = [] | ||
def find_deferred_sources_fields(target): | ||
for name, payload_field in target.payload.fields: | ||
if isinstance(payload_field, DeferredSourcesField): | ||
deferred_sources_fields.append((target, name, payload_field)) | ||
addresses = [target.address for target in self.context.targets()] | ||
self.context.build_graph.walk_transitive_dependency_graph(addresses, | ||
find_deferred_sources_fields) | ||
|
||
unpacked_sources = self.context.products.get_data('unpacked_archives') | ||
for (target, name, payload_field) in deferred_sources_fields: | ||
sources_target = self.context.build_graph.get_target(payload_field.address) | ||
if not sources_target: | ||
raise self.SourcesTargetLookupError( | ||
"Couldn't find {sources_spec} referenced from {target} field {name} in build graph" | ||
.format(sources_spec=payload_field.address.spec, target=target.address.spec, name=name)) | ||
if not sources_target in unpacked_sources: | ||
raise self.NoUnpackedSourcesError( | ||
"Target {sources_spec} referenced from {target} field {name} did not unpack any sources" | ||
.format(spec=sources_target.address.spec, target=target.address.spec, name=name)) | ||
sources, rel_unpack_dir = unpacked_sources[sources_target] | ||
SourceRoot.register_mutable(rel_unpack_dir) | ||
payload_field.populate(sources, rel_unpack_dir) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.