blob: e8d9e9a9e42510f30d6852af5b8b19ca00c0be0e [file] [log] [blame]
Sapan Bhatiae75416c2014-11-05 13:02:48 -05001#!/usr/bin/python
2
3import os
4import imp
5from planetstack.config import Config
6import inspect
7import time
8import traceback
9import commands
10import threading
11import json
12import pdb
13from core.models import *
14
15missing_links={}
16
17try:
18 dep_data = open(Config().dependency_graph).read()
19except:
20 dep_data = open('/opt/planetstack/model-deps').read()
21
22dependencies = json.loads(dep_data)
23
24inv_dependencies = {}
25for 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
33def plural(name):
34 if (name.endswith('s')):
35 return name+'es'
36 else:
37 return name+'s'
38
39
40def 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
48def 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
56def __walk_deps(fn, object, deps):
57 model = object.__class__.__name__
Sapan Bhatiae75416c2014-11-05 13:02:48 -050058 for dep in deps:
59 #print "Checking dep %s"%dep
60 peer=None
61 link = dep.lower()
62 try:
63 peer = getattr(object, link)
64 except AttributeError:
65 link = plural(link)
66 try:
67 peer = getattr(object, link)
68 except AttributeError:
69 if not missing_links.has_key(model+'.'+link):
70 print "Model %s missing link for dependency %s"%(model, link)
Tony Macka7dbd422015-01-05 22:48:11 -050071 logger.log_exc("Model %s missing link for dependency %s"%(model, link))
Sapan Bhatiae75416c2014-11-05 13:02:48 -050072 missing_links[model+'.'+link]=True
73
74 if (peer):
75 try:
76 peer_objects = peer.all()
Sapan Bhatia3d829472014-12-21 02:32:15 -050077 except AttributeError:
Sapan Bhatiae75416c2014-11-05 13:02:48 -050078 peer_objects = [peer]
Sapan Bhatia3d829472014-12-21 02:32:15 -050079 except:
80 peer_objects = []
Sapan Bhatiae75416c2014-11-05 13:02:48 -050081
82 for o in peer_objects:
83 fn(o, object)
Sapan Bhatia40bbfd92014-11-12 10:38:23 -050084 # Uncomment the following line to enable recursion
Sapan Bhatiae75416c2014-11-05 13:02:48 -050085 # walk_inv_deps(fn, o)
86
87def p(x):
88 print x,x.__class__.__name__
89 return
90
91def main():
92 #pdb.set_trace()
93 import django
94 django.setup()
95 s = Site.objects.filter(login_base='onlab')
96 #pdb.set_trace()
97 walk_inv_deps(p,s[0])
98
99if __name__=='__main__':
100 main()