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.
This adds the ability to peek a BUILD file for a named target, printing the target name if it is found. Additionally, peek allows a line_number flag which will print the starting line of the named target if it is found in the BUILD file. Also, this adds a suppress warnings feature to Buildozer. Example: $ ./pants peek --line_number=true path/to/directory:target_name To Verify: $ ./pants test contrib/buildrefactor/tests/python/pants_test/contrib/buildrefactor:peek_integration This addresses: pantsbuild#4861 Please enter the commit message for your changes. Lines starting
- Loading branch information
1 parent
c4e1cd6
commit e8b78ab
Showing
6 changed files
with
96 additions
and
6 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
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
46 changes: 46 additions & 0 deletions
46
contrib/buildrefactor/src/python/pants/contrib/buildrefactor/peek.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,46 @@ | ||
# coding=utf-8 | ||
# Copyright 2017 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
#./pants peek --path=src/scala/org/pantsbuild/zinc/analysis --target-name=zinc-analysis | ||
#./pants peek --path=src/scala/org/pantsbuild/zinc/extractor --target-name=zinc-extractor --line_number=True | ||
|
||
from __future__ import (absolute_import, division, generators, nested_scopes, print_function, | ||
unicode_literals, with_statement) | ||
|
||
from pants.task.task import Task | ||
from pants.contrib.buildrefactor.buildozer import Buildozer | ||
|
||
|
||
class Peek(Task): | ||
"""Peek a build file for a specified target | ||
$ ./pants peek --line_number=true path/to/directory:target_name | ||
line_number: optional flag to print the starting line of the target if found | ||
""" | ||
|
||
@classmethod | ||
def register_options(cls, register): | ||
super(Peek, cls).register_options(register) | ||
|
||
register('--line_number', help='Prints the starting line number of the named target.', type=bool, default=False) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(Peek, self).__init__(*args, **kwargs) | ||
|
||
self.options = self.get_options() | ||
|
||
def execute(self): | ||
self.print_name() | ||
|
||
def print_name(self): | ||
for root in self.context.target_roots: | ||
address_spec = root.address.spec | ||
print('\n') | ||
Buildozer.execute_binary('print name', address_spec, supress_warnings=True) | ||
print('Named target found in BUILD file.') | ||
|
||
if self.options.line_number: | ||
print('\n\'{}\' starts on line number:'.format(root.name)) | ||
Buildozer.execute_binary('print startline', address_spec, supress_warnings=True) |
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
27 changes: 27 additions & 0 deletions
27
contrib/buildrefactor/tests/python/pants_test/contrib/buildrefactor/test_peek_integration.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,27 @@ | ||
# coding=utf-8 | ||
# Copyright 2017 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import (absolute_import, division, generators, nested_scopes, print_function, | ||
unicode_literals, with_statement) | ||
|
||
from pants_test.pants_run_integration_test import PantsRunIntegrationTest | ||
|
||
|
||
class PeekIntegrationTest(PantsRunIntegrationTest): | ||
"""Test Peek goal functionality""" | ||
|
||
def test_print_name(self): | ||
peek_print_run = self.run_pants(['peek', | ||
'testprojects/tests/java/org/pantsbuild/testproject/buildrefactor/x:X']) | ||
|
||
self.assertIn('[peek]\n\nX\nNamed target found in BUILD file.', peek_print_run.stdout_data) | ||
self.assertIn('SUCCESS', peek_print_run.stdout_data) | ||
|
||
def test_print_line_number(self): | ||
peek_line_number_run = self.run_pants(['peek', | ||
'--line_number=true', | ||
'testprojects/tests/java/org/pantsbuild/testproject/buildrefactor/x:X']) | ||
|
||
self.assertIn('\n\n\'X\' starts on line number:\n4', peek_line_number_run.stdout_data) | ||
self.assertIn('SUCCESS', peek_line_number_run.stdout_data) |