Sapan Bhatia | e75416c | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os |
| 4 | import imp |
| 5 | from planetstack.config import Config |
| 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 | |
| 15 | missing_links={} |
| 16 | |
| 17 | try: |
| 18 | dep_data = open(Config().dependency_graph).read() |
| 19 | except: |
| 20 | dep_data = open('/opt/planetstack/model-deps').read() |
| 21 | |
| 22 | dependencies = json.loads(dep_data) |
| 23 | |
| 24 | inv_dependencies = {} |
| 25 | for 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 | |
| 33 | def plural(name): |
| 34 | if (name.endswith('s')): |
| 35 | return name+'es' |
| 36 | else: |
| 37 | return name+'s' |
| 38 | |
| 39 | |
| 40 | def 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 | |
| 48 | def 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 | |
| 56 | def __walk_deps(fn, object, deps): |
| 57 | model = object.__class__.__name__ |
Sapan Bhatia | e75416c | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 58 | 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) |
| 71 | missing_links[model+'.'+link]=True |
| 72 | |
| 73 | if (peer): |
| 74 | try: |
| 75 | peer_objects = peer.all() |
Sapan Bhatia | 3d82947 | 2014-12-21 02:32:15 -0500 | [diff] [blame] | 76 | except AttributeError: |
Sapan Bhatia | e75416c | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 77 | peer_objects = [peer] |
Sapan Bhatia | 3d82947 | 2014-12-21 02:32:15 -0500 | [diff] [blame] | 78 | except: |
| 79 | peer_objects = [] |
Sapan Bhatia | e75416c | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 80 | |
| 81 | for o in peer_objects: |
| 82 | fn(o, object) |
Sapan Bhatia | 40bbfd9 | 2014-11-12 10:38:23 -0500 | [diff] [blame] | 83 | # Uncomment the following line to enable recursion |
Sapan Bhatia | e75416c | 2014-11-05 13:02:48 -0500 | [diff] [blame] | 84 | # walk_inv_deps(fn, o) |
| 85 | |
| 86 | def p(x): |
| 87 | print x,x.__class__.__name__ |
| 88 | return |
| 89 | |
| 90 | def main(): |
| 91 | #pdb.set_trace() |
| 92 | import django |
| 93 | django.setup() |
| 94 | s = Site.objects.filter(login_base='onlab') |
| 95 | #pdb.set_trace() |
| 96 | walk_inv_deps(p,s[0]) |
| 97 | |
| 98 | if __name__=='__main__': |
| 99 | main() |