def testSort(self): a = MockTarget("a", []) b = MockTarget("b", [a]) c = MockTarget("c", [b]) d = MockTarget("d", [c, a]) e = MockTarget("e", [d]) self.assertEquals(InternalTarget.sort_targets([a, b, c, d, e]), [e, d, c, b, a]) self.assertEquals(InternalTarget.sort_targets([b, d, a, e, c]), [e, d, c, b, a]) self.assertEquals(InternalTarget.sort_targets([e, d, c, b, a]), [e, d, c, b, a])
def test_detect_cycle_direct(self): a = MockTarget("a") # no cycles yet InternalTarget.sort_targets([a]) a.update_dependencies([a]) try: InternalTarget.sort_targets([a]) self.fail("Expected a cycle to be detected") except InternalTarget.CycleException: # expected pass
def exported_targets(self): candidates = set() if self.transitive: candidates.update(self.context.targets()) else: candidates.update(self.context.target_roots) def get_synthetic(lang, target): mappings = self.context.products.get(lang).get(target) if mappings: for key, generated in mappings.items(): for synthetic in generated: yield synthetic # Handle the case where a code gen target is in the listed roots and the thus the publishable # target is a synthetic twin generated by a code gen task upstream. for candidate in self.context.target_roots: candidates.update(get_synthetic('java', candidate)) candidates.update(get_synthetic('scala', candidate)) def exportable(tgt): return tgt in candidates and tgt.is_exported return OrderedSet(filter(exportable, reversed(InternalTarget.sort_targets(filter(exportable, candidates)))))
def _compute_transitive_deps_by_target(self): """Map from target to all the targets it depends on, transitively.""" # Sort from least to most dependent. sorted_targets = reversed(InternalTarget.sort_targets(self._context.targets())) transitive_deps_by_target = defaultdict(set) # Iterate in dep order, to accumulate the transitive deps for each target. for target in sorted_targets: transitive_deps = set() if hasattr(target, 'dependencies'): for dep in target.dependencies: transitive_deps.update(transitive_deps_by_target.get(dep, [])) transitive_deps.add(dep) transitive_deps_by_target[target] = transitive_deps return transitive_deps_by_target
def _order_target_list(self, targets): """Orders the targets topologically, from least to most dependent.""" targets = set(t for t in targets if isinstance(t, Target)) return filter(targets.__contains__, reversed(InternalTarget.sort_targets(targets)))