Skip to content

Instantly share code, notes, and snippets.

@markito
Forked from paulosuzart/gist:494985
Created July 28, 2010 17:45
Show Gist options
  • Save markito/495514 to your computer and use it in GitHub Desktop.
Save markito/495514 to your computer and use it in GitHub Desktop.
projects = {'ProjectA' : {'deps' : ['ProjectB']},
'ProjectB' : {'deps' : None},
'ProjectC' : {'deps' : ['ProjectA']}}
cont = 0
def deploy(name, desc):
"""
Detects the order of deploy for projects in a dict.
Each dic entry is the name of Projet : {desc}, where desc is a dict containing:
deps : Name of projects it depends on.
description: optional description
"""
global cont
if 'deployed' in desc:
return
if not desc['deps']:
cont += 1
desc['deployed'] = cont
else:
for dep in desc['deps']:
if projects[dep]['deps'] and name in projects[dep]['deps']:
raise Exception('Cyclic reference between %s to %s' % (name, dep))
deploy(dep, projects[dep])
cont += 1
desc['deployed'] = cont
if __name__ == '__main__':
for k,v in projects.iteritems():
deploy(k,v)
print 'The deploy order is: ', sorted(projects.keys(), key = lambda k: projects[k]['deployed'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment