S.Çağlar Onur | 3e92b4d | 2015-02-09 13:34:11 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 2 | |
| 3 | import os |
| 4 | import imp |
Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 5 | from xos.config import Config, XOS_DIR |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 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 | |
Scott Baker | 25c6e7a | 2015-01-06 22:21:48 -0800 | [diff] [blame] | 15 | from util.logger import Logger, logging |
Scott Baker | 1355d7d | 2015-02-04 16:50:56 -0800 | [diff] [blame] | 16 | logger = Logger(level=logging.INFO) |
Scott Baker | 25c6e7a | 2015-01-06 22:21:48 -0800 | [diff] [blame] | 17 | |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 18 | missing_links={} |
| 19 | |
| 20 | try: |
| 21 | dep_data = open(Config().dependency_graph).read() |
| 22 | except: |
Scott Baker | 1355d7d | 2015-02-04 16:50:56 -0800 | [diff] [blame] | 23 | dep_data = open(XOS_DIR + '/model-deps').read() |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 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 = [] |
Sapan Bhatia | 35dab9d | 2015-05-27 19:11:12 +0200 | [diff] [blame] | 49 | return __walk_deps(fn, object, deps) |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 50 | |
| 51 | def walk_inv_deps(fn, object): |
| 52 | model = object.__class__.__name__ |
| 53 | try: |
| 54 | deps = inv_dependencies[model] |
| 55 | except: |
| 56 | deps = [] |
Sapan Bhatia | 35dab9d | 2015-05-27 19:11:12 +0200 | [diff] [blame] | 57 | return __walk_deps(fn, object, deps) |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 58 | |
| 59 | def __walk_deps(fn, object, deps): |
| 60 | model = object.__class__.__name__ |
Sapan Bhatia | 35dab9d | 2015-05-27 19:11:12 +0200 | [diff] [blame] | 61 | ret = [] |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 62 | 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 Bhatia | b1a0d41 | 2015-05-09 18:07:58 +0200 | [diff] [blame] | 75 | logger.log_exc("WARNING: Model %s missing link for dependency %s."%(model, link)) |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 76 | missing_links[model+'.'+link]=True |
| 77 | |
Sapan Bhatia | b1a0d41 | 2015-05-09 18:07:58 +0200 | [diff] [blame] | 78 | |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 79 | if (peer): |
| 80 | try: |
| 81 | peer_objects = peer.all() |
Sapan Bhatia | fbf6127 | 2014-12-21 02:32:15 -0500 | [diff] [blame] | 82 | except AttributeError: |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 83 | peer_objects = [peer] |
Sapan Bhatia | fbf6127 | 2014-12-21 02:32:15 -0500 | [diff] [blame] | 84 | except: |
| 85 | peer_objects = [] |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 86 | |
| 87 | for o in peer_objects: |
Sapan Bhatia | 9e8a255 | 2015-05-09 18:08:24 +0200 | [diff] [blame] | 88 | #if (isinstance(o,PlCoreBase)): |
| 89 | if (hasattr(o,'updated')): |
| 90 | fn(o, object) |
Sapan Bhatia | 35dab9d | 2015-05-27 19:11:12 +0200 | [diff] [blame] | 91 | ret.append(o) |
Sapan Bhatia | f6613e3 | 2014-11-12 10:38:23 -0500 | [diff] [blame] | 92 | # Uncomment the following line to enable recursion |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 93 | # walk_inv_deps(fn, o) |
Sapan Bhatia | 35dab9d | 2015-05-27 19:11:12 +0200 | [diff] [blame] | 94 | return ret |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 95 | |
Sapan Bhatia | 9e8a255 | 2015-05-09 18:08:24 +0200 | [diff] [blame] | 96 | def p(x,source): |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 97 | print x,x.__class__.__name__ |
| 98 | return |
| 99 | |
| 100 | def main(): |
| 101 | #pdb.set_trace() |
Sapan Bhatia | 35dab9d | 2015-05-27 19:11:12 +0200 | [diff] [blame] | 102 | s = Slice.objects.filter(name='princeton_sapan62') |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 103 | #pdb.set_trace() |
Sapan Bhatia | 35dab9d | 2015-05-27 19:11:12 +0200 | [diff] [blame] | 104 | print walk_inv_deps(p,s[0]) |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 105 | |
| 106 | if __name__=='__main__': |
| 107 | main() |