blob: 8b15a8520641229481e5377e4b8b407cb4ab7696 [file] [log] [blame]
Sapan Bhatia58203362014-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
Scott Baker25c6e7a2015-01-06 22:21:48 -080015from util.logger import Logger, logging
16logger = Logger(level=logging.INFO)
17
Sapan Bhatia58203362014-11-05 13:02:48 -050018missing_links={}
19
20try:
21 dep_data = open(Config().dependency_graph).read()
22except:
23 dep_data = open('/opt/planetstack/model-deps').read()
24
25dependencies = json.loads(dep_data)
26
27inv_dependencies = {}
28for 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
36def plural(name):
37 if (name.endswith('s')):
38 return name+'es'
39 else:
40 return name+'s'
41
42
43def 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
51def 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
59def __walk_deps(fn, object, deps):
60 model = object.__class__.__name__
Sapan Bhatia58203362014-11-05 13:02:48 -050061 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)
Tony Mack3066a952015-01-05 22:48:11 -050074 logger.log_exc("Model %s missing link for dependency %s"%(model, link))
Sapan Bhatia58203362014-11-05 13:02:48 -050075 missing_links[model+'.'+link]=True
76
77 if (peer):
78 try:
79 peer_objects = peer.all()
Sapan Bhatiafbf61272014-12-21 02:32:15 -050080 except AttributeError:
Sapan Bhatia58203362014-11-05 13:02:48 -050081 peer_objects = [peer]
Sapan Bhatiafbf61272014-12-21 02:32:15 -050082 except:
83 peer_objects = []
Sapan Bhatia58203362014-11-05 13:02:48 -050084
85 for o in peer_objects:
86 fn(o, object)
Sapan Bhatiaf6613e32014-11-12 10:38:23 -050087 # Uncomment the following line to enable recursion
Sapan Bhatia58203362014-11-05 13:02:48 -050088 # walk_inv_deps(fn, o)
89
90def p(x):
91 print x,x.__class__.__name__
92 return
93
94def 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
102if __name__=='__main__':
103 main()