Added model policies as a thread in the observer
[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 from util.logger import Logger, logging
16 logger = Logger(level=logging.INFO)\r
17
18 missing_links={}
19
20 try:
21         dep_data = open(Config().dependency_graph).read()
22 except:
23         dep_data = open('/opt/planetstack/model-deps').read()
24
25 dependencies = json.loads(dep_data)
26
27 inv_dependencies = {}
28 for k, lst in dependencies.items():
29         for v in lst:
30                 try:
31                         inv_dependencies[v].append(k)
32                 except KeyError:
33                         inv_dependencies[v]=[k]
34         
35
36 def plural(name):
37         if (name.endswith('s')):
38                 return name+'es'
39         else:
40                 return name+'s'
41
42
43 def walk_deps(fn, object):
44         model = object.__class__.__name__
45         try:    
46                 deps = dependencies[model]
47         except:
48                 deps = []
49         __walk_deps(fn, object, deps)
50
51 def walk_inv_deps(fn, object):
52         model = object.__class__.__name__
53         try:    
54                 deps = inv_dependencies[model]
55         except:
56                 deps = []
57         __walk_deps(fn, object, deps)
58
59 def __walk_deps(fn, object, deps):
60         model = object.__class__.__name__
61         for dep in deps:
62                 #print "Checking dep %s"%dep
63                 peer=None
64                 link = dep.lower()
65                 try:
66                         peer = getattr(object, link)
67                 except AttributeError:
68                         link = plural(link)
69                         try:
70                                 peer = getattr(object, link)
71                         except AttributeError:
72                                 if not missing_links.has_key(model+'.'+link):
73                                         print "Model %s missing link for dependency %s"%(model, link)
74                                         logger.log_exc("Model %s missing link for dependency %s"%(model, link))
75                                         missing_links[model+'.'+link]=True
76
77                 if (peer):
78                         try:
79                                 peer_objects = peer.all()
80                         except AttributeError:
81                                 peer_objects = [peer]
82                         except:
83                                 peer_objects = []
84
85                         for o in peer_objects:
86                                 fn(o, object)
87                                 # Uncomment the following line to enable recursion
88                                 # walk_inv_deps(fn, o)
89
90 def p(x):
91         print x,x.__class__.__name__
92         return
93
94 def main():
95         #pdb.set_trace()
96         import django
97         django.setup()
98         s = Site.objects.filter(login_base='onlab')
99         #pdb.set_trace()
100         walk_inv_deps(p,s[0])
101         
102 if __name__=='__main__':
103         main()