Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os |
| 4 | import pdb |
| 5 | import sys |
| 6 | import json |
| 7 | |
| 8 | sys.path.append('.') |
| 9 | |
Scott Baker | 9ecc839 | 2015-02-18 11:29:05 -0800 | [diff] [blame] | 10 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 11 | |
| 12 | from django.db.models.fields.related import ForeignKey |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 13 | |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 14 | # defaults |
Scott Baker | 72bee30 | 2015-10-19 16:49:52 -0700 | [diff] [blame] | 15 | apps = ["core", "hpc", "cord", "requestrouter", "services.onos"] |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 16 | output = "-json" |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 17 | |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 18 | # syntax: dmdot [-json | -dot] [app_name] |
| 19 | |
| 20 | # poor man's argument parser |
| 21 | for arg in sys.argv[1:]: |
| 22 | if arg.startswith("-"): |
| 23 | output = arg |
| 24 | else: |
Sapan Bhatia | 4cbe700 | 2015-10-21 17:03:33 +0200 | [diff] [blame] | 25 | apps+= [arg] |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 26 | |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 27 | model_classes = [] |
| 28 | class_names = [] |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 29 | lower_class_names = {} |
Sapan Bhatia | c92e505 | 2015-08-26 11:43:10 -0400 | [diff] [blame] | 30 | synonyms = { |
| 31 | 'user':'creator' |
| 32 | } |
| 33 | |
| 34 | for app in apps: |
| 35 | app = app + ".models" |
| 36 | #models_module = imp.load_source(app, ".") |
| 37 | models_module = __import__(app) |
| 38 | for part in app.split(".")[1:]: |
| 39 | if hasattr(models_module, "PlCoreBase"): |
| 40 | break |
| 41 | models_module = getattr(models_module,part) |
| 42 | |
| 43 | PlCoreBase = getattr(models_module,"PlCoreBase") |
| 44 | |
Sapan Bhatia | c92e505 | 2015-08-26 11:43:10 -0400 | [diff] [blame] | 45 | for classname in dir(models_module): |
| 46 | c = getattr(models_module, classname, None) |
| 47 | if type(c)==type(PlCoreBase): |
| 48 | model_classes.append(c) |
| 49 | class_names.append(c.__name__) |
| 50 | lower_class_names[c.__name__.lower()] = c |
| 51 | try: |
| 52 | synonym = synonyms[c.__name__.lower()] |
| 53 | lower_class_names[synonym] = c |
| 54 | except: |
| 55 | pass |
| 56 | |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 57 | |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 58 | # django doesn't use the correct case in field.name.title() for objects that |
| 59 | # have CamelCased class names. So, compare everything in lower case. |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 60 | |
| 61 | if (output=='-dot'): |
Sapan Bhatia | cb7aa3a | 2015-01-29 20:40:13 +0000 | [diff] [blame] | 62 | print "digraph plstack {"; |
| 63 | for c in model_classes: |
| 64 | fields = c._meta.fields |
| 65 | |
| 66 | for f in fields: |
Sapan Bhatia | 8f8eb6b | 2015-09-02 11:49:51 -0400 | [diff] [blame] | 67 | if type(f)==ForeignKey and f.name.lower().split('_') in lower_class_names: |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 68 | linked_class = lower_class_names[f.name.lower()] |
Sapan Bhatia | 8f8eb6b | 2015-09-02 11:49:51 -0400 | [diff] [blame] | 69 | if ('backref' in f.name): |
| 70 | print '\t"%s"->"%s";'%(linked_class.__name__,c.__name__) |
| 71 | else: |
| 72 | print '\t"%s"->"%s";'%(c.__name__,linked_class.__name__) |
Sapan Bhatia | cb7aa3a | 2015-01-29 20:40:13 +0000 | [diff] [blame] | 73 | print "}\n"; |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 74 | elif (output=='-json'): |
Sapan Bhatia | cb7aa3a | 2015-01-29 20:40:13 +0000 | [diff] [blame] | 75 | d = {} |
| 76 | for c in model_classes: |
| 77 | fields = c._meta.fields |
Sapan Bhatia | 8f8eb6b | 2015-09-02 11:49:51 -0400 | [diff] [blame] | 78 | |
Sapan Bhatia | cb7aa3a | 2015-01-29 20:40:13 +0000 | [diff] [blame] | 79 | for f in fields: |
Sapan Bhatia | 8f8eb6b | 2015-09-02 11:49:51 -0400 | [diff] [blame] | 80 | field_type = f.name.lower().split('_')[0] |
| 81 | if type(f)==ForeignKey and field_type in lower_class_names: |
| 82 | linked_class = lower_class_names[field_type] |
| 83 | if ('backref' in f.name.lower()): |
| 84 | a = linked_class.__name__ |
| 85 | b = c.__name__ |
| 86 | else: |
| 87 | b = linked_class.__name__ |
| 88 | a = c.__name__ |
| 89 | |
Sapan Bhatia | cb7aa3a | 2015-01-29 20:40:13 +0000 | [diff] [blame] | 90 | try: |
Sapan Bhatia | 8f8eb6b | 2015-09-02 11:49:51 -0400 | [diff] [blame] | 91 | if (b not in d[a]): |
| 92 | d[a].append(b) |
Sapan Bhatia | cb7aa3a | 2015-01-29 20:40:13 +0000 | [diff] [blame] | 93 | except KeyError: |
| 94 | d[c.__name__]=[linked_class.__name__] |
| 95 | #d['ControllerNetwork'].append('SliceDeployments') |
| 96 | print json.dumps(d,indent=4) |
| 97 | |
| 98 | |