Lichen

Change of common.py

724:3d89317842b9
common.py
     1.1 --- a/common.py	Mon Mar 13 01:49:11 2017 +0100
     1.2 +++ b/common.py	Mon Mar 13 14:47:44 2017 +0100
     1.3 @@ -987,6 +987,56 @@
     1.4  def same(s1, s2):
     1.5      return set(s1) == set(s2)
     1.6  
     1.7 +def order_dependencies(all_depends):
     1.8 +
     1.9 +    """
    1.10 +    Produce a dependency ordering for the 'all_depends' mapping. This mapping
    1.11 +    has the form "A depends on B, C...". The result will order A, B, C, and so
    1.12 +    on.
    1.13 +    """
    1.14 +
    1.15 +    # Record the reverse dependencies, making a mapping of the form
    1.16 +    # "B is needed by A", "C is needed by A", and so on.
    1.17 +
    1.18 +    usage = {}
    1.19 +
    1.20 +    # Record path-based dependencies.
    1.21 +
    1.22 +    for key in all_depends.keys():
    1.23 +        usage[key] = set()
    1.24 +
    1.25 +    for key, depends in all_depends.items():
    1.26 +        for depend in depends:
    1.27 +            init_item(usage, depend, set)
    1.28 +            usage[depend].add(key)
    1.29 +
    1.30 +    # Produce an ordering by obtaining exposed items (required by items already
    1.31 +    # processed) and putting them at the start of the list.
    1.32 +
    1.33 +    ordered = []
    1.34 +
    1.35 +    while usage:
    1.36 +        have_next = False
    1.37 +
    1.38 +        for path, n in usage.items():
    1.39 +            if not n:
    1.40 +                ordered.insert(0, path)
    1.41 +                depends = all_depends.get(path)
    1.42 +
    1.43 +                # Reduce usage of the referenced items.
    1.44 +
    1.45 +                if depends:
    1.46 +                    for depend in depends:
    1.47 +                        usage[depend].remove(path)
    1.48 +
    1.49 +                del usage[path]
    1.50 +                have_next = True
    1.51 +
    1.52 +        if not have_next:
    1.53 +            raise ValueError, usage
    1.54 +
    1.55 +    return ordered
    1.56 +
    1.57  # General input/output.
    1.58  
    1.59  def readfile(filename):