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 = [] |
| 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__ |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 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) |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 74 | logger.log_exc("Model %s missing link for dependency %s"%(model, link)) |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 75 | missing_links[model+'.'+link]=True |
| 76 | |
| 77 | if (peer): |
| 78 | try: |
| 79 | peer_objects = peer.all() |
Sapan Bhatia | fbf6127 | 2014-12-21 02:32:15 -0500 | [diff] [blame] | 80 | except AttributeError: |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 81 | peer_objects = [peer] |
Sapan Bhatia | fbf6127 | 2014-12-21 02:32:15 -0500 | [diff] [blame] | 82 | except: |
| 83 | peer_objects = [] |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 84 | |
| 85 | for o in peer_objects: |
| 86 | fn(o, object) |
Sapan Bhatia | f6613e3 | 2014-11-12 10:38:23 -0500 | [diff] [blame] | 87 | # Uncomment the following line to enable recursion |
Sapan Bhatia | 5820336 | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 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() |