Skip to content

Commit

Permalink
Deprecate Context.build_file_parser. (pantsbuild#7295)
Browse files Browse the repository at this point in the history
Some tasks rely on it for access to target aliases.
These can be retrieved from the BuildConfiguration instead.

However since Context is public API, we go through a deprecation
cycle before removing the build_file_parser property.

Once this deprecation is complete, we can get rid of BuildFileParser
and its tests entirely.
  • Loading branch information
benjyw authored Mar 3, 2019
1 parent c6743ec commit a286268
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _registered_language_aliases(self):
@memoized_method
def _target_type_for_language(self, language):
alias_for_lang = self._registered_language_aliases()[language]
registered_aliases = self.context.build_file_parser.registered_aliases()
registered_aliases = self.context.build_configuration.registered_aliases()
target_types = registered_aliases.target_types_by_alias.get(alias_for_lang, None)
if not target_types:
raise TaskError('Registered target type `{0}` for language `{1}` does not exist!'.format(alias_for_lang, language))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def get_scope_data(scope):
self.context.products.register_data(self.PANTS_REFERENCE_PRODUCT, ref_page)

def _gen_build_dictionary(self):
buildfile_aliases = self.context.build_file_parser.registered_aliases()
buildfile_aliases = self.context.build_configuration.registered_aliases()
extracter = BuildDictionaryInfoExtracter(buildfile_aliases)
target_type_infos = extracter.get_target_type_info()
other_infos = sorted(extracter.get_object_info() + extracter.get_object_factory_info())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def target_types_for_alias(self, alias):
:raises :class:`TargetFilterTaskMixin.InvalidTargetType`: when no target types correspond to
the given `alias`.
"""
registered_aliases = self.context.build_file_parser.registered_aliases()
registered_aliases = self.context.build_configuration.registered_aliases()
target_types = registered_aliases.target_types_by_alias.get(alias, None)
if not target_types:
raise self.InvalidTargetType('Not a target type: {}'.format(alias))
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/jvm/tasks/rewrite_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def source_extension(cls):
@memoized_property
def _formatted_target_types(self):
aliases = set(self.get_options().target_types)
registered_aliases = self.context.build_file_parser.registered_aliases()
registered_aliases = self.context.build_configuration.registered_aliases()
return tuple({target_type
for alias in aliases
for target_type in registered_aliases.target_types_by_alias[alias]})
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/project_info/tasks/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def _resolve_jars_info(self, targets, classpath_products):

@memoized_property
def target_aliases_map(self):
registered_aliases = self.context.build_file_parser.registered_aliases()
registered_aliases = self.context.build_configuration.registered_aliases()
mapping = {}
for alias, target_types in registered_aliases.target_types_by_alias.items():
# If a target class is registered under multiple aliases returns the last one.
Expand Down
1 change: 1 addition & 0 deletions src/python/pants/bin/goal_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def _setup_context(self):
requested_goals=self._options.goals,
build_graph=build_graph,
build_file_parser=build_file_parser,
build_configuration=self._build_config,
address_mapper=address_mapper,
invalidation_report=invalidation_report,
scheduler=self._graph_session.scheduler_session)
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/core_tasks/targets_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def register_options(cls, register):
register('--details', help='Show details about this target type.')

def console_output(self, targets):
buildfile_aliases = self.context.build_file_parser.registered_aliases()
buildfile_aliases = self.context.build_configuration.registered_aliases()
extracter = BuildDictionaryInfoExtracter(buildfile_aliases)

alias = self.get_options().details
Expand Down
1 change: 1 addition & 0 deletions src/python/pants/goal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ python_library(
':workspace',
'src/python/pants/base:build_environment',
'src/python/pants/build_graph', # XXX(fixme)
'src/python/pants/base:deprecated',
'src/python/pants/base:worker_pool',
'src/python/pants/base:workunit',
'src/python/pants/java/distribution:distribution',
Expand Down
12 changes: 10 additions & 2 deletions src/python/pants/goal/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from twitter.common.collections import OrderedSet

from pants.base.build_environment import get_buildroot, get_scm
from pants.base.deprecated import deprecated
from pants.base.worker_pool import SubprocPool
from pants.base.workunit import WorkUnit, WorkUnitLabel
from pants.build_graph.target import Target
Expand Down Expand Up @@ -41,11 +42,13 @@ class Context(object):
# repository of attributes?
def __init__(self, options, run_tracker, target_roots,
requested_goals=None, target_base=None, build_graph=None,
build_file_parser=None, address_mapper=None, console_outstream=None, scm=None,
build_file_parser=None, build_configuration=None,
address_mapper=None, console_outstream=None, scm=None,
workspace=None, invalidation_report=None, scheduler=None):
self._options = options
self.build_graph = build_graph
self.build_file_parser = build_file_parser
self._build_file_parser = build_file_parser
self.build_configuration = build_configuration
self.address_mapper = address_mapper
self.run_tracker = run_tracker
self._log = run_tracker.logger
Expand All @@ -63,6 +66,11 @@ def __init__(self, options, run_tracker, target_roots,
self._invalidation_report = invalidation_report
self._scheduler = scheduler

@property
@deprecated('1.17.0.dev2', hint_message='Use the build_configuration property.')
def build_file_parser(self):
return self._build_file_parser

@property
def options(self):
"""Returns the new-style options.
Expand Down
6 changes: 3 additions & 3 deletions tests/python/pants_test/base/context_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ def subproc_map(self, f, items):


def create_context_from_options(options, target_roots=None, build_graph=None,
build_file_parser=None, address_mapper=None, console_outstream=None,
workspace=None, scheduler=None):
build_configuration=None, address_mapper=None,
console_outstream=None, workspace=None, scheduler=None):
"""Creates a ``Context`` with the given options and no targets by default.
:param options: An :class:`pants.option.options.Option`-alike object that supports read methods.
Expand All @@ -130,6 +130,6 @@ def create_context_from_options(options, target_roots=None, build_graph=None,
run_tracker = TestContext.DummyRunTracker()
target_roots = maybe_list(target_roots, Target) if target_roots else []
return TestContext(options=options, run_tracker=run_tracker, target_roots=target_roots,
build_graph=build_graph, build_file_parser=build_file_parser,
build_graph=build_graph, build_configuration=build_configuration,
address_mapper=address_mapper, console_outstream=console_outstream,
workspace=workspace, scheduler=scheduler)
14 changes: 9 additions & 5 deletions tests/python/pants_test/build_graph/test_build_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ def __init__(self, *args, **kwargs):
assert False, "This fake target should never be initialized in this test!"


class BuildFileParserBasicsTest(TestBase):
class TestWithBuildFileParser(TestBase):
def setUp(self):
super(TestWithBuildFileParser, self).setUp()
self.build_file_parser = BuildFileParser(self._build_configuration, self.build_root)


class BuildFileParserBasicsTest(TestWithBuildFileParser):
@classmethod
def alias_groups(cls):
return BuildFileAliases(targets={'jvm_binary': ErrorTarget,
Expand Down Expand Up @@ -115,7 +120,7 @@ def test_unicode_string_in_build_file(self):
self.build_file_parser.parse_build_file(build_file)


class BuildFileParserTargetTest(TestBase):
class BuildFileParserTargetTest(TestWithBuildFileParser):
@classmethod
def alias_groups(cls):
return BuildFileAliases(targets={'fake': ErrorTarget})
Expand Down Expand Up @@ -205,8 +210,7 @@ def test_sibling_build_files_duplicates(self):
BuildFile.get_build_files_family(FileSystemProjectTree(self.build_root), '.'))


class BuildFileParserExposedObjectTest(TestBase):

class BuildFileParserExposedObjectTest(TestWithBuildFileParser):
@classmethod
def alias_groups(cls):
return BuildFileAliases(objects={'fake_object': object()})
Expand All @@ -218,7 +222,7 @@ def test_exposed_object(self):
self.assertEqual(len(address_map), 0)


class BuildFileParserExposedContextAwareObjectFactoryTest(TestBase):
class BuildFileParserExposedContextAwareObjectFactoryTest(TestWithBuildFileParser):

Jar = namedtuple('Jar', ['org', 'name', 'rev'])
Repository = namedtuple('Repository', ['name', 'url', 'push_db_basedir'])
Expand Down
9 changes: 1 addition & 8 deletions tests/python/pants_test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from pants.build_graph.address import Address
from pants.build_graph.build_configuration import BuildConfiguration
from pants.build_graph.build_file_aliases import BuildFileAliases
from pants.build_graph.build_file_parser import BuildFileParser
from pants.build_graph.target import Target
from pants.engine.fs import PathGlobs, PathGlobsAndRoot
from pants.engine.legacy.graph import HydratedField
Expand Down Expand Up @@ -320,8 +319,6 @@ def setUp(self):
}

self._build_configuration = self.build_config()
self._build_file_parser = BuildFileParser(self._build_configuration, self.build_root)

self._inited_target = False

def buildroot_files(self, relpath=None):
Expand Down Expand Up @@ -364,10 +361,6 @@ def isolated_local_store(cls):
def build_root(self):
return self._build_root()

@property
def build_file_parser(self):
return self._build_file_parser

@property
def pants_workdir(self):
return self._pants_workdir()
Expand Down Expand Up @@ -499,7 +492,7 @@ def context(self, for_task_types=None, for_subsystems=None, options=None,
context = create_context_from_options(fake_options,
target_roots=target_roots,
build_graph=self.build_graph,
build_file_parser=self._build_file_parser,
build_configuration=self._build_configuration,
address_mapper=address_mapper,
console_outstream=console_outstream,
workspace=workspace,
Expand Down

0 comments on commit a286268

Please sign in to comment.