def test_filtering(self): output = list(topological_sort([(1,1), (1,2)])) assert output.pop(0) == set([1]) assert output.pop(0) == set([2]) output = list(topological_sort({1: 1, 2: set([2,1])})) assert output.pop(0) == set([1]) assert output.pop(0) == set([2])
def test_unspecified_dependencies(self): with pytest.raises(UnderspecifiedDependencies): list(topological_sort([(1,2)], require_fully_specified=True)) with pytest.raises(UnderspecifiedDependencies): list(topological_sort({2:1}, require_fully_specified=True)) assert list(topological_sort({1:None, 2:1}, require_fully_specified=True)) == [ set([1]), set([2])]
def test_types(self): with pytest.raises(TypeError): list(topological_sort([1,2,3])) with pytest.raises(TypeError): list(topological_sort(None)) with pytest.raises(TypeError): list(topological_sort((1,2), (2,3)))
def test_cycles(self): with pytest.raises(DependencyCycle): list(topological_sort([(1, 2), (2, 1)])) with pytest.raises(DependencyCycle): list(topological_sort([(1, 2), (2, 3), (3, 1)])) with pytest.raises(DependencyCycle): list(topological_sort({1: 2, 2: 3, 3: 1}))
def test_types(self): with pytest.raises(TypeError): list(topological_sort([1, 2, 3])) with pytest.raises(TypeError): list(topological_sort(None)) with pytest.raises(TypeError): list(topological_sort((1, 2), (2, 3)))
def test_filtering(self): output = list(topological_sort([(1, 1), (1, 2)])) assert output.pop(0) == set([1]) assert output.pop(0) == set([2]) output = list(topological_sort({1: 1, 2: set([2, 1])})) assert output.pop(0) == set([1]) assert output.pop(0) == set([2])
def test_cycles(self): with pytest.raises(DependencyCycle): list(topological_sort([(1,2), (2,1)])) with pytest.raises(DependencyCycle): list(topological_sort([(1,2), (2,3), (3,1)])) with pytest.raises(DependencyCycle): list(topological_sort({1: 2, 2: 3, 3: 1}))
def test_unspecified_dependencies(self): with pytest.raises(UnderspecifiedDependencies): list(topological_sort([(1, 2)], require_fully_specified=True)) with pytest.raises(UnderspecifiedDependencies): list(topological_sort({2: 1}, require_fully_specified=True)) assert list( topological_sort({ 1: None, 2: 1 }, require_fully_specified=True)) == [set([1]), set([2])]
def test_basic_ordering(self): def run_asserts(output): assert output.pop(0) == set([1]) assert output.pop(0) == set([2]) assert output.pop(0) == set([3]) output = list(topological_sort([(1,2), (2,3)])) run_asserts(output) output = list(topological_sort({2:1, 3:2})) run_asserts(output)
def test_basic_ordering(self): def run_asserts(output): assert output.pop(0) == set([1]) assert output.pop(0) == set([2]) assert output.pop(0) == set([3]) output = list(topological_sort([(1, 2), (2, 3)])) run_asserts(output) output = list(topological_sort({2: 1, 3: 2})) run_asserts(output)
def test_mixed_dict_sets(self): def run_asserts(output): assert output.pop(0) == set([1]) assert output.pop(0) == set([2]) assert output.pop(0) == set([3]) assert output.pop(0) == set([4]) output = list(topological_sort([(1,2), (2,3), (2,4), (3,4)])) run_asserts(output) output = list(topological_sort({2: 1, 3: 2, 4: set([2, 3])})) run_asserts(output)
def test_mixed_dict_sets(self): def run_asserts(output): assert output.pop(0) == set([1]) assert output.pop(0) == set([2]) assert output.pop(0) == set([3]) assert output.pop(0) == set([4]) output = list(topological_sort([(1, 2), (2, 3), (2, 4), (3, 4)])) run_asserts(output) output = list(topological_sort({2: 1, 3: 2, 4: set([2, 3])})) run_asserts(output)
def console_output(self, targets): depmap = defaultdict(set) def map_deps(target): # TODO(John Sirois): rationalize target hierarchies - this is the only 'safe' way to treat # both python and jvm targets today. if hasattr(target, 'dependencies'): deps = depmap[str(target.address)] for dep in target.dependencies: for resolved in filter(self._is_target, dep.resolve()): deps.add(str(resolved.address)) for root in self.context.target_roots: root.walk(map_deps, self._is_target) tsorted = [] for group in topological_sort(depmap): tsorted.extend(group) if self._reverse: tsorted = reversed(tsorted) roots = set(str(root.address) for root in self.context.target_roots) for address in tsorted: if address in roots: yield address
def __init__(self, label, dependencies=None, description=None): """ @label = the label that identifies this module for dependency management @dependencies = a string or list of strings of modules this module depends upon (optional) @description = a one-liner describing this application module, e.g. "Logging module" """ self._label = label self._description = description if isinstance(dependencies, list): self._dependencies = set(dependencies) elif isinstance(dependencies, Compatibility.string): self._dependencies = set([dependencies]) elif dependencies is None: self._dependencies = set() else: raise TypeError( 'Dependencies should be None, string or list of strings, got: %s' % type(dependencies)) AppModule._MODULE_REGISTRY[label] = self AppModule._MODULE_DEPENDENCIES[label] = self._dependencies try: list(topological_sort(AppModule._MODULE_DEPENDENCIES)) except DependencyCycle: raise AppModule.DependencyCycle( "Found a cycle in app module dependencies!")
def test_mixed_types(self): deps = { 1: "bob", 2: "frank", "frank": "esther", "esther": set(["brian", 3]), } iter = topological_sort(deps) assert next(iter) == set(["bob", "brian", 3]) assert next(iter) == set([1, "esther"]) assert next(iter) == set(["frank"]) assert next(iter) == set([2])
def _setup_modules(self): """ Setup all initialized modules. """ module_registry = AppModule.module_registry() for bundle in topological_sort(AppModule.module_dependencies()): for module_label in bundle: assert module_label in module_registry module = module_registry[module_label] self._debug_log('Initializing: %s (%s)' % (module.label(), module.description())) try: module.setup_function() except AppModule.Unimplemented: pass self._init_modules.append(module.label())
def __init__(self, label, dependencies=None, dependents=None, description=None): """ @label = the label that identifies this module for dependency management @dependencies = a string or list of strings of modules this module depends upon (optional) @dependents = a string or list of strings of modules that depend upon this module (optional) @description = a one-liner describing this application module, e.g. "Logging module" """ self._label = label self._description = description self._dependencies = maybe_list(dependencies or []) self._dependents = maybe_list(dependents or []) self._MODULE_REGISTRY[label] = self self._MODULE_DEPENDENCIES[label].update(self._dependencies) for dependent in self._dependents: self._MODULE_DEPENDENCIES[dependent].add(label) try: list(topological_sort(self._MODULE_DEPENDENCIES)) except DependencyCycle: raise AppModule.DependencyCycle("Found a cycle in app module dependencies!")
def console_output(self, targets): depmap = defaultdict(set) def map_deps(target): deps = depmap[target.address.spec] for dep in target.dependencies: deps.add(dep.address.spec) for root in self.context.target_roots: root.walk(map_deps) tsorted = [] for group in topological_sort(depmap): tsorted.extend(group) if self._reverse: tsorted = reversed(tsorted) roots = set(root.address.spec for root in self.context.target_roots) for address in tsorted: if address in roots: yield address
def __init__(self, label, dependencies = None, description = None): """ @label = the label that identifies this module for dependency management @dependencies = a string or list of strings of modules this module depends upon (optional) @description = a one-liner describing this application module, e.g. "Logging module" """ self._label = label self._description = description if isinstance(dependencies, list): self._dependencies = set(dependencies) elif isinstance(dependencies, basestring): self._dependencies = set([dependencies]) elif dependencies is None: self._dependencies = set() else: raise TypeError('Dependencies should be None, string or list of strings, got: %s' % type(dependencies)) AppModule._MODULE_REGISTRY[label] = self AppModule._MODULE_DEPENDENCIES[label] = self._dependencies try: list(topological_sort(AppModule._MODULE_DEPENDENCIES)) except DependencyCycle: raise AppModule.DependencyCycle("Found a cycle in app module dependencies!")
def __init__(self, label, dependencies=None, dependents=None, description=None): """ @label = the label that identifies this module for dependency management @dependencies = a string or list of strings of modules this module depends upon (optional) @dependents = a string or list of strings of modules that depend upon this module (optional) @description = a one-liner describing this application module, e.g. "Logging module" """ self._label = label self._description = description self._dependencies = maybe_list(dependencies or []) self._dependents = maybe_list(dependents or []) self._MODULE_REGISTRY[label] = self self._MODULE_DEPENDENCIES[label].update(self._dependencies) for dependent in self._dependents: self._MODULE_DEPENDENCIES[dependent].add(label) try: list(topological_sort(self._MODULE_DEPENDENCIES)) except DependencyCycle: raise AppModule.DependencyCycle( "Found a cycle in app module dependencies!")
def test_empty(self): assert list(topological_sort([])) == [] assert list(topological_sort({})) == []