code: def order_by_refs(envs):
      
      topology = {
          env['name']: set(env['refs'])
          for env in envs
      }
      by_name = {
          env['name']: env
          for env in envs
      }
      return [
          by_name[name]
          for name in toposort_flatten(topology)
      ]
docs: Return topologicaly sorted list of environments.
      I.e. all referenced environments are placed before their references.