Sync networks fully converted over to ansible
[plstackapi.git] / planetstack / dependency_walker.py
1 #!/usr/bin/python
2
3 import os
4 import imp
5 from planetstack.config import Config
6 import inspect
7 import time
8 import traceback
9 import commands
10 import threading
11 import json
12 import pdb
13 from core.models import *
14
15 missing_links={}
16
17 try:
18         dep_data = open(Config().dependency_graph).read()
19 except:
20         dep_data = open('/opt/planetstack/model-deps').read()
21
22 dependencies = json.loads(dep_data)
23
24 inv_dependencies = {}
25 for k, lst in dependencies.items():
26         for v in lst:
27                 try:
28                         inv_dependencies[v].append(k)
29                 except KeyError:
30                         inv_dependencies[v]=[k]
31         
32
33 def plural(name):
34         if (name.endswith('s')):
35                 return name+'es'
36         else:
37                 return name+'s'
38
39
40 def walk_deps(fn, object):
41         model = object.__class__.__name__
42         try:    
43                 deps = dependencies[model]
44         except:
45                 deps = []
46         __walk_deps(fn, object, deps)
47
48 def walk_inv_deps(fn, object):
49         model = object.__class__.__name__
50         try:    
51                 deps = inv_dependencies[model]
52         except:
53                 deps = []
54         __walk_deps(fn, object, deps)
55
56 def __walk_deps(fn, object, deps):
57         model = object.__class__.__name__
58
59         for dep in deps:
60                 #print "Checking dep %s"%dep
61                 peer=None
62                 link = dep.lower()
63                 try:
64                         peer = getattr(object, link)
65                 except AttributeError:
66                         link = plural(link)
67                         try:
68                                 peer = getattr(object, link)
69                         except AttributeError:
70                                 if not missing_links.has_key(model+'.'+link):
71                                         print "Model %s missing link for dependency %s"%(model, link)
72                                         missing_links[model+'.'+link]=True
73
74                 if (peer):
75                         try:
76                                 peer_objects = peer.all()
77                         except:
78                                 peer_objects = [peer]
79
80                         for o in peer_objects:
81                                 fn(o, object)
82                                 # Uncomment the following line to enable recursion
83                                 # walk_inv_deps(fn, o)
84
85 def p(x):
86         print x,x.__class__.__name__
87         return
88
89 def main():
90         #pdb.set_trace()
91         import django
92         django.setup()
93         s = Site.objects.filter(login_base='onlab')
94         #pdb.set_trace()
95         walk_inv_deps(p,s[0])
96         
97 if __name__=='__main__':
98         main()