Skip to content

Commit

Permalink
Peek Target using Buildozer
Browse files Browse the repository at this point in the history
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
denalimarsh committed Nov 29, 2017
1 parent c4e1cd6 commit e8b78ab
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ python_library(
name='meta_rename',
sources=['meta_rename.py']
)


python_library(
name='peek',
sources=['peek.py']
)
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,18 @@ def _execute_buildozer_script(self, command):
Buildozer.execute_binary(command, address.spec, binary=self._executable)

@classmethod
def execute_binary(cls, command, spec, binary=None, version='0.6.0.dce8b3c287652cbcaf43c8dd076b3f48c92ab44c'):
def execute_binary(cls, command, spec, binary=None, version='0.6.0.dce8b3c287652cbcaf43c8dd076b3f48c92ab44c', supress_warnings=False):
binary = binary if binary else BinaryUtil.Factory.create().select_binary('scripts/buildozer', version, 'buildozer')

Buildozer._execute_buildozer_command([binary, command, spec])
Buildozer._execute_buildozer_command([binary, command, spec], supress_warnings)

@classmethod
def _execute_buildozer_command(cls, buildozer_command):
def _execute_buildozer_command(cls, buildozer_command, supress_warnings):
try:
subprocess.check_call(buildozer_command, cwd=get_buildroot())
except subprocess.CalledProcessError as err:
if err.returncode == 3:
logger.warn('{} ... no changes were made'.format(buildozer_command))
if not supress_warnings:
logger.warn('{} ... no changes were made'.format(buildozer_command))
else:
raise TaskError('{} ... exited non-zero ({}).'.format(buildozer_command, err.returncode))
raise TaskError('{} ... exited non-zero ({}).'.format(buildozer_command, err.returncode))
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)
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
unicode_literals, with_statement)

from pants.goal.task_registrar import TaskRegistrar as task

from pants.contrib.buildrefactor.buildozer import Buildozer
from pants.contrib.buildrefactor.meta_rename import MetaRename
from pants.contrib.buildrefactor.peek import Peek


def register_goals():
task(name='buildozer', action=Buildozer).install('buildozer')
task(name='meta-rename', action=MetaRename).install('meta-rename')
task(name='peek', action=Peek).install('peek')
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ python_tests(
]
)

python_tests(
name='peek_integration',
sources=['test_peek_integration.py'],
dependencies=[
'tests/python/pants_test:int-test',
]
)

python_tests(
name='meta_rename_integration',
Expand All @@ -53,3 +60,5 @@ python_tests(
'tests/python/pants_test:int-test',
]
)


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)

0 comments on commit e8b78ab

Please sign in to comment.