defresolve(groups, hosts): # Groups that have already been solved resolved_groups = {} # Group names that are not root non_root = set() # Make dict with resolution of each group result = {} for name in groups: result[name] = _resolve_rec(name, groups, hosts, resolved_groups, non_root) for name in groups: if name in non_root: del result[name] return result
def_resolve_rec(name, groups, hosts, resolved_groups, non_root): # If group has already been resolved, finish if name in resolved_groups: return resolved_groups[name] # If it is a host, finish if name in hosts: return hosts[name] # new group resolution resolved = {} for child in groups[name]: # Resolve each child resolved[child] = _resolve_rec(child, groups, hosts, resolved_groups, non_root) # Mark child as non_root non_root.add(child) # Save to resolved groups resolved_groups[name] = resolved return resolved