blob: d99b7836aa4683d79d4373aa702059d618b0119f [file] [log] [blame]
S.Çağlar Onur3e92b4d2015-02-09 13:34:11 -05001#!/usr/bin/env python
Sapan Bhatia58203362014-11-05 13:02:48 -05002
3import os
4import imp
Scott Baker86e132c2015-02-11 21:38:09 -08005from xos.config import Config, XOS_DIR
Sapan Bhatia58203362014-11-05 13:02:48 -05006import 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
Scott Baker1355d7d2015-02-04 16:50:56 -080016logger = Logger(level=logging.INFO)
Scott Baker25c6e7a2015-01-06 22:21:48 -080017
Sapan Bhatia58203362014-11-05 13:02:48 -050018missing_links={}
19
20try:
21 dep_data = open(Config().dependency_graph).read()
22except:
Scott Baker1355d7d2015-02-04 16:50:56 -080023 dep_data = open(XOS_DIR + '/model-deps').read()
Sapan Bhatia58203362014-11-05 13:02:48 -050024
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 = []
Sapan Bhatia35dab9d2015-05-27 19:11:12 +020049 return __walk_deps(fn, object, deps)
Sapan Bhatia58203362014-11-05 13:02:48 -050050
51def walk_inv_deps(fn, object):
52 model = object.__class__.__name__
53 try:
54 deps = inv_dependencies[model]
55 except:
56 deps = []
Sapan Bhatia35dab9d2015-05-27 19:11:12 +020057 return __walk_deps(fn, object, deps)
Sapan Bhatia58203362014-11-05 13:02:48 -050058
59def __walk_deps(fn, object, deps):
60 model = object.__class__.__name__
Sapan Bhatia35dab9d2015-05-27 19:11:12 +020061 ret = []
Sapan Bhatia58203362014-11-05 13:02:48 -050062 for dep in deps:
63 #print "Checking dep %s"%dep
64 peer=None
65 link = dep.lower()
66 try:
67 peer = getattr(object, link)
68 except AttributeError:
69 link = plural(link)
70 try:
71 peer = getattr(object, link)
72 except AttributeError:
73 if not missing_links.has_key(model+'.'+link):
74 print "Model %s missing link for dependency %s"%(model, link)
Sapan Bhatiab1a0d412015-05-09 18:07:58 +020075 logger.log_exc("WARNING: Model %s missing link for dependency %s."%(model, link))
Sapan Bhatia58203362014-11-05 13:02:48 -050076 missing_links[model+'.'+link]=True
77
Sapan Bhatiab1a0d412015-05-09 18:07:58 +020078
Sapan Bhatia58203362014-11-05 13:02:48 -050079 if (peer):
80 try:
81 peer_objects = peer.all()
Sapan Bhatiafbf61272014-12-21 02:32:15 -050082 except AttributeError:
Sapan Bhatia58203362014-11-05 13:02:48 -050083 peer_objects = [peer]
Sapan Bhatiafbf61272014-12-21 02:32:15 -050084 except:
85 peer_objects = []
Sapan Bhatia58203362014-11-05 13:02:48 -050086
87 for o in peer_objects:
Sapan Bhatia9e8a2552015-05-09 18:08:24 +020088 #if (isinstance(o,PlCoreBase)):
89 if (hasattr(o,'updated')):
90 fn(o, object)
Sapan Bhatia35dab9d2015-05-27 19:11:12 +020091 ret.append(o)
Sapan Bhatiaf6613e32014-11-12 10:38:23 -050092 # Uncomment the following line to enable recursion
Sapan Bhatia58203362014-11-05 13:02:48 -050093 # walk_inv_deps(fn, o)
Sapan Bhatia35dab9d2015-05-27 19:11:12 +020094 return ret
Sapan Bhatia58203362014-11-05 13:02:48 -050095
Sapan Bhatia9e8a2552015-05-09 18:08:24 +020096def p(x,source):
Sapan Bhatia58203362014-11-05 13:02:48 -050097 print x,x.__class__.__name__
98 return
99
100def main():
101 #pdb.set_trace()
Sapan Bhatia35dab9d2015-05-27 19:11:12 +0200102 s = Slice.objects.filter(name='princeton_sapan62')
Sapan Bhatia58203362014-11-05 13:02:48 -0500103 #pdb.set_trace()
Sapan Bhatia35dab9d2015-05-27 19:11:12 +0200104 print walk_inv_deps(p,s[0])
Sapan Bhatia58203362014-11-05 13:02:48 -0500105
106if __name__=='__main__':
107 main()