Skip to content

Commit

Permalink
Add --absolute to V2 filedeps goal
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Arellano committed Aug 18, 2019
1 parent ea20d48 commit 6e6f3d0
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 291 deletions.
29 changes: 21 additions & 8 deletions src/python/pants/rules/core/filedeps.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from pex.orderedset import OrderedSet
from pathlib import Path

from pants.base.build_environment import get_buildroot
from pants.engine.console import Console
from pants.engine.goal import Goal, LineOriented
from pants.engine.legacy.graph import TransitiveHydratedTargets
Expand All @@ -16,24 +17,36 @@ class Filedeps(LineOriented, Goal):
closure of targets are also included.
"""

# TODO: Until this implements more of the options of `filedeps`, it can't claim the name!
# TODO: Until this implements `--globs`, this can't claim the name `filedeps`!
name = 'fast-filedeps'

@classmethod
def register_options(cls, register):
super().register_options(register)
register(
'--absolute', type=bool, default=True,
help='If True, output with absolute path; else, output with path relative to the build root.'
)


@console_rule(Filedeps, [Console, Filedeps.Options, TransitiveHydratedTargets])
def file_deps(console, filedeps_options, transitive_hydrated_targets):

uniq_set = OrderedSet()
absolute = filedeps_options.values.absolute

unique_rel_paths = set()
build_root = get_buildroot()

for hydrated_target in transitive_hydrated_targets.closure:
if hydrated_target.address.rel_path:
uniq_set.add(hydrated_target.address.rel_path)
unique_rel_paths.add(hydrated_target.address.rel_path)
if hasattr(hydrated_target.adaptor, "sources"):
uniq_set.update(hydrated_target.adaptor.sources.snapshot.files)
unique_rel_paths.update(hydrated_target.adaptor.sources.snapshot.files)

with Filedeps.line_oriented(filedeps_options, console) as (print_stdout, print_stderr):
for f_path in uniq_set:
print_stdout(f_path)
with Filedeps.line_oriented(filedeps_options, console) as (print_stdout, _):
for rel_path in sorted(unique_rel_paths):
final_path = f"{Path(build_root) / rel_path}" if absolute else rel_path
print_stdout(final_path)

return Filedeps(exit_code=0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,25 @@ def alias_groups(cls):
def rules(cls):
return super().rules() + list_targets.rules()

def setUp(self):
def setUp(self) -> None:
super().setUp()

# Setup a BUILD tree for various list tests
class Lib:

def __init__(self, name, provides=False):
def __init__(self, name: str, provides: bool = False) -> None:
self.name = name
self.provides = dedent("""
self.provides = dedent(f"""
artifact(
org='com.example',
name='{0}',
name='{name}',
repo=public
)
""".format(name)).strip() if provides else 'None'
""").strip() if provides else 'None'

def create_library(path, *libs):
def create_library(path: str, *libs: Lib) -> None:
libs = libs or [Lib(os.path.basename(os.path.dirname(self.build_path(path))))]
for lib in libs:
target = "java_library(name='{name}', provides={provides}, sources=[])\n".format(
name=lib.name, provides=lib.provides)
target = f"java_library(name='{lib.name}', provides={lib.provides}, sources=[])\n"
self.add_to_build_file(path, target)

create_library('a')
Expand Down
5 changes: 2 additions & 3 deletions tests/python/pants_test/rules/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ python_tests(
name='filedeps',
source='test_filedeps.py',
dependencies=[
'src/python/pants/backend/python/targets',
'src/python/pants/build_graph',
'src/python/pants/engine/legacy:graph',
'src/python/pants/rules/core',
'tests/python/pants_test:test_base',
'tests/python/pants_test/engine:util',
'tests/python/pants_test:console_rule_test_base',
]
)

Expand Down
Loading

0 comments on commit 6e6f3d0

Please sign in to comment.