def build_ruledag(job, key=(lambda job: job.rule.name)):
    if (job in visited):
        return
    visited.add(job)
    deps = sorted(self.dependencies[job], key=key)
    deps = [(group[0] if preselect.isdisjoint(group) else preselect.intersection(group).pop()) for group in (list(g) for (_, g) in groupby(deps, key))]
    dag[job].extend(deps)
    preselect_parents(job)
    for dep in deps:
        build_ruledag(dep)
def build_ruledag(job, key=(lambda job: job.rule.name)):
    if (job in visited):
        return
    visited.add(job)
    deps = sorted(self.dependencies[job], key=key)
    deps = [(group[0] if preselect.isdisjoint(group) else preselect.intersection(group).pop()) for group in (list(g) for (_, g) in groupby(deps, key))]
    dag[job].extend(deps)
    preselect_parents(job)
    for dep in deps:
        build_ruledag(dep)