|
| 1 | +def create(): |
| 2 | + __self_ref = [None] |
| 3 | + __items_by_id = {} |
| 4 | + |
| 5 | + def __self(): |
| 6 | + return __self_ref[0] |
| 7 | + |
| 8 | + def add(*items): |
| 9 | + for item in items: |
| 10 | + _assert_item(item) |
| 11 | + |
| 12 | + if __items_by_id.get(item.id): |
| 13 | + fail("Failed to add item {}: item with the same ID already exists") |
| 14 | + |
| 15 | + __items_by_id[item.id] = item |
| 16 | + |
| 17 | + return __self() |
| 18 | + |
| 19 | + # This function returns the items in the order they should be launched |
| 20 | + # based on their dependencies. |
| 21 | + # |
| 22 | + # It will try to preserve the order in which the items were added, |
| 23 | + # only reordering them if necessary to satisfy the dependencies. |
| 24 | + # |
| 25 | + # If there are any cycles in the dependencies, it will fail. |
| 26 | + # If there are any missing dependencies, it will also fail. |
| 27 | + def sequence(): |
| 28 | + # First we check whether we have all the items |
| 29 | + all_dependency_ids = [ |
| 30 | + dependency |
| 31 | + for item in __items_by_id.values() |
| 32 | + for dependency in item.dependencies |
| 33 | + ] |
| 34 | + |
| 35 | + # Now check we have all of them |
| 36 | + missing_dependency_ids = [ |
| 37 | + id for id in all_dependency_ids if id not in __items_by_id |
| 38 | + ] |
| 39 | + if missing_dependency_ids: |
| 40 | + fail( |
| 41 | + "Failed to launch: Missing items {}".format( |
| 42 | + ",".join(missing_dependency_ids) |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + # Now we have to order the items based on their dependencies |
| 47 | + # |
| 48 | + # First we start with the default sequence - the order in which the items were added |
| 49 | + ordered_items = __items_by_id.values() |
| 50 | + num_items = len(ordered_items) |
| 51 | + |
| 52 | + for index in range(num_items): |
| 53 | + item = ordered_items[index] |
| 54 | + |
| 55 | + # Since we are not allowed any unbound loops, we'll have to resort to somewhat different strategy |
| 56 | + # |
| 57 | + # We will calculate the lowest index at which this item can be placed |
| 58 | + # based on its dependencies. |
| 59 | + lowest_desired_index = _lowest_desired_index(item, ordered_items) |
| 60 | + |
| 61 | + # If the lowest index is lower or equal to the current index, everything is fine and we can continue |
| 62 | + if lowest_desired_index <= index: |
| 63 | + continue |
| 64 | + |
| 65 | + # If the lowest index is greater than the current index, we need to swap the item with the item at the lowest index |
| 66 | + item_to_swap = ordered_items[lowest_desired_index] |
| 67 | + |
| 68 | + # We cannot just swap thew though - we also need to check that the item being swapped in is not dependent on the item being swapped out |
| 69 | + # |
| 70 | + # We do this by checking the lowest desired index for the item being swapped in, |
| 71 | + # and if it is greater than the current index, we fail |
| 72 | + # |
| 73 | + # In other words, if the item we want to swap with the current item is dependent on the current item, |
| 74 | + # we cannot swap them because we have a cycle |
| 75 | + lowest_desired_index_for_item_to_swap = _lowest_desired_index( |
| 76 | + item_to_swap, ordered_items |
| 77 | + ) |
| 78 | + |
| 79 | + if lowest_desired_index_for_item_to_swap > index: |
| 80 | + fail( |
| 81 | + "Cannot create launch sequence: Item {} <-> {}".format( |
| 82 | + item.id, item_to_swap.id |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + ordered_items[index] = item_to_swap |
| 87 | + ordered_items[lowest_desired_index] = item |
| 88 | + |
| 89 | + return ordered_items |
| 90 | + |
| 91 | + __self_ref[0] = struct( |
| 92 | + add=add, |
| 93 | + sequence=sequence, |
| 94 | + ) |
| 95 | + |
| 96 | + return __self() |
| 97 | + |
| 98 | + |
| 99 | +# Launches a scheule by executing each item in the order determined by the schedule. |
| 100 | +def launch(plan, schedule): |
| 101 | + items = schedule.sequence() |
| 102 | + launched = {} |
| 103 | + |
| 104 | + for item in items: |
| 105 | + missing_dependencies = [id for id in item.dependencies if id not in launched] |
| 106 | + if missing_dependencies: |
| 107 | + fail( |
| 108 | + "schedule: Launch error: Missing dependencies {} for item {}".format( |
| 109 | + ",".join(missing_dependencies), |
| 110 | + item.id, |
| 111 | + ) |
| 112 | + ) |
| 113 | + |
| 114 | + launched[item.id] = item.launch(plan, launched) |
| 115 | + |
| 116 | + return launched |
| 117 | + |
| 118 | + |
| 119 | +def item(id, launch, dependencies=[]): |
| 120 | + return _assert_item( |
| 121 | + struct( |
| 122 | + id=id, |
| 123 | + launch=launch, |
| 124 | + dependencies=dependencies, |
| 125 | + ) |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +def _lowest_desired_index(item, items): |
| 130 | + items_without_item = list(items) |
| 131 | + items_without_item.remove(item) |
| 132 | + |
| 133 | + for index in range(len(items)): |
| 134 | + previous_items = items_without_item[:index] |
| 135 | + previous_ids = [i.id for i in previous_items] |
| 136 | + |
| 137 | + missing_dependencies = [ |
| 138 | + id for id in item.dependencies if id not in previous_ids |
| 139 | + ] |
| 140 | + |
| 141 | + if not missing_dependencies: |
| 142 | + return index |
| 143 | + |
| 144 | + |
| 145 | +def _assert_item(item): |
| 146 | + type_of_item = type(item) |
| 147 | + if type_of_item != "struct": |
| 148 | + fail( |
| 149 | + "schedule: Expected an item to be a struct, got {} of type {}".format( |
| 150 | + item, type_of_item |
| 151 | + ) |
| 152 | + ) |
| 153 | + |
| 154 | + if not hasattr(item, "dependencies"): |
| 155 | + fail( |
| 156 | + "schedule: Expected an item to have a property 'dependencies', got {}".format( |
| 157 | + item, type_of_item |
| 158 | + ) |
| 159 | + ) |
| 160 | + |
| 161 | + type_of_dependencies = type(item.dependencies) |
| 162 | + if type_of_dependencies != "list": |
| 163 | + fail( |
| 164 | + "schedule: Expected an item to have a 'dependencies' property of type list but 'dependencies' is of type".format( |
| 165 | + type_of_dependencies |
| 166 | + ) |
| 167 | + ) |
| 168 | + |
| 169 | + has_self_as_dependency = item.id in item.dependencies |
| 170 | + if has_self_as_dependency: |
| 171 | + fail("schedule: Item {} specifies itself as its dependency".format(item.id)) |
| 172 | + |
| 173 | + if not hasattr(item, "id"): |
| 174 | + fail( |
| 175 | + "schedule: Expected an item to have a property 'id', got {}".format( |
| 176 | + item, type_of_item |
| 177 | + ) |
| 178 | + ) |
| 179 | + |
| 180 | + if not hasattr(item, "launch"): |
| 181 | + fail( |
| 182 | + "schedule: Expected an item to have a property 'launch', got {}".format( |
| 183 | + item, type_of_item |
| 184 | + ) |
| 185 | + ) |
| 186 | + |
| 187 | + type_of_launch = type(item.launch) |
| 188 | + if type_of_launch != "function": |
| 189 | + fail( |
| 190 | + "schedule: Expected an item to have a 'launch' function but 'launch' is of type".format( |
| 191 | + type_of_launch |
| 192 | + ) |
| 193 | + ) |
| 194 | + |
| 195 | + return item |
0 commit comments