def console_output(self, _): buildfiles = OrderedSet() if self._dependees_type: base_paths = OrderedSet() for dependees_type in self._dependees_type: # FIXME(pl): This should be a standard function provided by the plugin/BuildFileParser # machinery try: # Try to do a fully qualified import 1st for filtering on custom types. from_list, module, type_name = dependees_type.rsplit('.', 2) module = __import__('%s.%s' % (from_list, module), fromlist=[from_list]) target_type = getattr(module, type_name) except (ImportError, ValueError): # Fall back on pants provided target types. registered_aliases = self.context.build_file_parser.registered_aliases() if dependees_type not in registered_aliases.targets: raise TaskError('Invalid type name: %s' % dependees_type) target_type = registered_aliases.targets[dependees_type] # Try to find the SourceRoot for the given input type try: roots = SourceRoot.roots(target_type) base_paths.update(roots) except KeyError: pass if not base_paths: raise TaskError('No SourceRoot set for any target type in %s.' % self._dependees_type + '\nPlease define a source root in BUILD file as:' + '\n\tsource_root(\'<src-folder>\', %s)' % ', '.join(self._dependees_type)) for base_path in base_paths: buildfiles.update(BuildFile.scan_buildfiles(get_buildroot(), os.path.join(get_buildroot(), base_path))) else: buildfiles = BuildFile.scan_buildfiles(get_buildroot()) build_graph = self.context.build_graph build_file_parser = self.context.build_file_parser dependees_by_target = defaultdict(set) for build_file in buildfiles: build_file_parser.parse_build_file(build_file) for address in build_file_parser.addresses_by_build_file[build_file]: build_file_parser.inject_spec_closure_into_build_graph(address.spec, build_graph) for address in build_file_parser.addresses_by_build_file[build_file]: target = build_graph.get_target(address) # TODO(John Sirois): tighten up the notion of targets written down in a BUILD by a # user vs. targets created by pants at runtime. target = self.get_concrete_target(target) for dependency in target.dependencies: dependency = self.get_concrete_target(dependency) dependees_by_target[dependency].add(target) roots = set(self.context.target_roots) if self._closed: for root in roots: yield root.address.spec for dependant in self.get_dependants(dependees_by_target, roots): yield dependant.address.spec
def console_output(self, _): buildfiles = OrderedSet() address_mapper = self.context.address_mapper if self._dependees_type: base_paths = OrderedSet() for dependees_type in self._dependees_type: target_aliases = self.context.build_file_parser.registered_aliases( ).targets if dependees_type not in target_aliases: raise TaskError( 'Invalid type name: {}'.format(dependees_type)) target_type = target_aliases[dependees_type] # Try to find the SourceRoot for the given input type try: roots = SourceRoot.roots(target_type) base_paths.update(roots) except KeyError: pass if not base_paths: raise TaskError( 'No SourceRoot set for any target type in {}.'.format( self._dependees_type) + '\nPlease define a source root in BUILD file as:' + '\n\tsource_root(\'<src-folder>\', {})'.format(', '.join( self._dependees_type))) for base_path in base_paths: buildfiles.update( address_mapper.scan_buildfiles( get_buildroot(), os.path.join(get_buildroot(), base_path), spec_excludes=self._spec_excludes)) else: buildfiles = address_mapper.scan_buildfiles( get_buildroot(), spec_excludes=self._spec_excludes) build_graph = self.context.build_graph build_file_parser = self.context.build_file_parser dependees_by_target = defaultdict(set) for build_file in buildfiles: address_map = build_file_parser.parse_build_file(build_file) for address in address_map.keys(): build_graph.inject_address_closure(address) for address in address_map.keys(): target = build_graph.get_target(address) # TODO(John Sirois): tighten up the notion of targets written down in a BUILD by a # user vs. targets created by pants at runtime. target = self.get_concrete_target(target) for dependency in target.dependencies: dependency = self.get_concrete_target(dependency) dependees_by_target[dependency].add(target) roots = set(self.context.target_roots) if self._closed: for root in roots: yield root.address.spec for dependant in self.get_dependants(dependees_by_target, roots): yield dependant.address.spec
def test_register(self): self._assert_source_root_empty() SourceRoot.register("tests", TestTarget) self.assertEquals({"tests": OrderedSet([TestTarget])}, SourceRoot.all_roots()) self.assertEquals(OrderedSet([TestTarget]), SourceRoot.types("tests")) self.assertEquals(OrderedSet(["tests"]), SourceRoot.roots(TestTarget))
def check_buildroot(self, buildroot_path): self._assert_source_root_empty() SourceRoot.register(buildroot_path, TestTarget) self.assertEquals({".": OrderedSet([TestTarget])}, SourceRoot.all_roots()) self.assertEquals(OrderedSet([TestTarget]), SourceRoot.types(".")) self.assertEquals(OrderedSet(["."]), SourceRoot.roots(TestTarget)) target = TestTarget("//mock/foo/bar:baz") self.assertEqual("", SourceRoot.find(target))
def console_output(self, _): buildfiles = OrderedSet() address_mapper = self.context.address_mapper if self._dependees_type: base_paths = OrderedSet() for dependees_type in self._dependees_type: target_aliases = self.context.build_file_parser.registered_aliases().targets if dependees_type not in target_aliases: raise TaskError('Invalid type name: {}'.format(dependees_type)) target_type = target_aliases[dependees_type] # Try to find the SourceRoot for the given input type try: roots = SourceRoot.roots(target_type) base_paths.update(roots) except KeyError: pass if not base_paths: raise TaskError('No SourceRoot set for any target type in {}.'.format(self._dependees_type) + '\nPlease define a source root in BUILD file as:' + '\n\tsource_root(\'<src-folder>\', {})'.format(', '.join(self._dependees_type))) for base_path in base_paths: buildfiles.update(address_mapper.scan_buildfiles(get_buildroot(), os.path.join(get_buildroot(), base_path), spec_excludes=self._spec_excludes)) else: buildfiles = address_mapper.scan_buildfiles(get_buildroot(), spec_excludes=self._spec_excludes) build_graph = self.context.build_graph build_file_parser = self.context.build_file_parser dependees_by_target = defaultdict(set) for build_file in buildfiles: address_map = build_file_parser.parse_build_file(build_file) for address in address_map.keys(): build_graph.inject_address_closure(address) for address in address_map.keys(): target = build_graph.get_target(address) # TODO(John Sirois): tighten up the notion of targets written down in a BUILD by a # user vs. targets created by pants at runtime. target = self.get_concrete_target(target) for dependency in target.dependencies: dependency = self.get_concrete_target(dependency) dependees_by_target[dependency].add(target) roots = set(self.context.target_roots) if self._closed: for root in roots: yield root.address.spec for dependant in self.get_dependants(dependees_by_target, roots): yield dependant.address.spec
def _set_up_mocks(self, class_type, src_roots): self.mox.StubOutWithMock(SourceRoot, 'roots') SourceRoot.roots(class_type).AndReturn(src_roots) self.mox.ReplayAll()
def _assert_source_root_empty(self): self.assertEqual({}, SourceRoot.all_roots()) with self.assertRaises(KeyError): self.assertEqual(set(), SourceRoot.types("tests")) with self.assertRaises(KeyError): self.assertEqual(set(), SourceRoot.roots(TestTarget))
def safe_get_source_roots(target_type): return set(SourceRoot.roots( target_type)) if target_type in all_rooted_types else set()
def safe_get_source_roots(target_type): return set(SourceRoot.roots(target_type)) if target_type in all_rooted_types else set()
def console_output(self, _): buildfiles = OrderedSet() address_mapper = self.context.address_mapper if self._dependees_types: base_paths = OrderedSet() for dependees_type in self._dependees_types: target_types = self.target_types_for_alias(dependees_type) # Try to find the SourceRoots for the given input type alias for target_type in target_types: try: roots = SourceRoot.roots(target_type) base_paths.update(roots) except KeyError: pass # TODO(John Sirois): BUG: This should not cause a failure, it should just force a slower full # scan. # TODO(John Sirois): BUG: The --type argument only limited the scn bases, it does no limit the # types of targets found under those bases, ie: we may have just limited our scan to roots # containing java_library, but those same roots likely also contain jvm_binary targets that # we do not wish to have in the results. So the --type filtering needs to apply to the final # dependees_by_target map as well below. if not base_paths: raise TaskError(dedent("""\ No SourceRoot set for any of these target types: {}. Please define a source root in BUILD file as: source_root('<src-folder>', {}) """.format(' '.join(self._dependees_types), ', '.join(self._dependees_types))).strip()) for base_path in base_paths: scanned = address_mapper.scan_buildfiles(get_buildroot(), os.path.join(get_buildroot(), base_path), spec_excludes=self._spec_excludes) buildfiles.update(scanned) else: buildfiles = address_mapper.scan_buildfiles(get_buildroot(), spec_excludes=self._spec_excludes) build_graph = self.context.build_graph build_file_parser = self.context.build_file_parser dependees_by_target = defaultdict(set) for build_file in buildfiles: address_map = build_file_parser.parse_build_file(build_file) for address in address_map.keys(): build_graph.inject_address_closure(address) for address in address_map.keys(): target = build_graph.get_target(address) # TODO(John Sirois): tighten up the notion of targets written down in a BUILD by a # user vs. targets created by pants at runtime. target = self.get_concrete_target(target) for dependency in target.dependencies: dependency = self.get_concrete_target(dependency) dependees_by_target[dependency].add(target) roots = set(self.context.target_roots) if self._closed: for root in roots: yield root.address.spec for dependant in self.get_dependants(dependees_by_target, roots): yield dependant.address.spec
def console_output(self, _): buildfiles = OrderedSet() if self._dependees_type: base_paths = OrderedSet() for dependees_type in self._dependees_type: # FIXME(pl): This should be a standard function provided by the plugin/BuildFileParser # machinery try: # Try to do a fully qualified import 1st for filtering on custom types. from_list, module, type_name = dependees_type.rsplit( '.', 2) module = __import__('%s.%s' % (from_list, module), fromlist=[from_list]) target_type = getattr(module, type_name) except (ImportError, ValueError): # Fall back on pants provided target types. if dependees_type not in pants.base.build_file_aliases.target_aliases: raise TaskError('Invalid type name: %s' % dependees_type) target_type = pants.base.build_file_aliases.target_aliases[ dependees_type] # Find the SourceRoot for the given input type base_paths.update(SourceRoot.roots(target_type)) if not base_paths: raise TaskError( 'No SourceRoot set for any target type in %s.' % self._dependees_type + '\nPlease define a source root in BUILD file as:' + '\n\tsource_root(\'<src-folder>\', %s)' % ', '.join(self._dependees_type)) for base_path in base_paths: buildfiles.update( BuildFile.scan_buildfiles(get_buildroot(), base_path)) else: buildfiles = BuildFile.scan_buildfiles(get_buildroot()) build_graph = self.context.build_graph build_file_parser = self.context.build_file_parser dependees_by_target = defaultdict(set) for build_file in buildfiles: build_file_parser.parse_build_file(build_file) for address in build_file_parser.addresses_by_build_file[ build_file]: build_file_parser.inject_spec_closure_into_build_graph( address.spec, build_graph) for address in build_file_parser.addresses_by_build_file[ build_file]: target = build_graph.get_target(address) # TODO(John Sirois): tighten up the notion of targets written down in a BUILD by a # user vs. targets created by pants at runtime. target = self.get_concrete_target(target) for dependency in target.dependencies: dependency = self.get_concrete_target(dependency) dependees_by_target[dependency].add(target) roots = set(self.context.target_roots) if self._closed: for root in roots: yield root.address.build_file_spec for dependant in self.get_dependants(dependees_by_target, roots): yield dependant.address.build_file_spec