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 | 2125dab | 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 | f731a62 | 2015-12-31 13:24:55 -0800 | [diff] [blame] | 14 | # try to make sure we're running from the right place |
| 15 | if (not os.path.exists("core")): |
| 16 | if (os.path.exists("../core")): |
| 17 | os.chdir("..") |
| 18 | else: |
| 19 | print >> sys.stderr, "Are you sure you're running dmdot from the root of an XOS installation" |
| 20 | sys.exit(-1) |
| 21 | |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 22 | # defaults |
Scott Baker | 16d4ac3 | 2016-06-13 11:48:41 -0700 | [diff] [blame] | 23 | apps = ["core", "services.hpc", "services.requestrouter", "services.onos"] |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 24 | output = "-json" |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 25 | |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 26 | # syntax: dmdot [-json | -dot] [app_name] |
| 27 | |
| 28 | # poor man's argument parser |
| 29 | for arg in sys.argv[1:]: |
| 30 | if arg.startswith("-"): |
| 31 | output = arg |
| 32 | else: |
Sapan Bhatia | 2e3d03b | 2015-10-21 17:03:33 +0200 | [diff] [blame] | 33 | apps+= [arg] |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 34 | |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 35 | model_classes = [] |
| 36 | class_names = [] |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 37 | lower_class_names = {} |
Sapan Bhatia | 6f350ac | 2015-08-26 11:43:10 -0400 | [diff] [blame] | 38 | synonyms = { |
| 39 | 'user':'creator' |
| 40 | } |
| 41 | |
| 42 | for app in apps: |
| 43 | app = app + ".models" |
| 44 | #models_module = imp.load_source(app, ".") |
| 45 | models_module = __import__(app) |
| 46 | for part in app.split(".")[1:]: |
| 47 | if hasattr(models_module, "PlCoreBase"): |
| 48 | break |
| 49 | models_module = getattr(models_module,part) |
| 50 | |
| 51 | PlCoreBase = getattr(models_module,"PlCoreBase") |
| 52 | |
Sapan Bhatia | 6f350ac | 2015-08-26 11:43:10 -0400 | [diff] [blame] | 53 | for classname in dir(models_module): |
| 54 | c = getattr(models_module, classname, None) |
| 55 | if type(c)==type(PlCoreBase): |
| 56 | model_classes.append(c) |
| 57 | class_names.append(c.__name__) |
| 58 | lower_class_names[c.__name__.lower()] = c |
| 59 | try: |
| 60 | synonym = synonyms[c.__name__.lower()] |
| 61 | lower_class_names[synonym] = c |
| 62 | except: |
| 63 | pass |
| 64 | |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 65 | |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 66 | # django doesn't use the correct case in field.name.title() for objects that |
| 67 | # have CamelCased class names. So, compare everything in lower case. |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 68 | |
| 69 | if (output=='-dot'): |
Sapan Bhatia | 5ef878c | 2015-01-29 20:40:13 +0000 | [diff] [blame] | 70 | print "digraph plstack {"; |
| 71 | for c in model_classes: |
| 72 | fields = c._meta.fields |
| 73 | |
| 74 | for f in fields: |
Sapan Bhatia | 6731db9 | 2015-09-02 11:49:51 -0400 | [diff] [blame] | 75 | 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] | 76 | linked_class = lower_class_names[f.name.lower()] |
Sapan Bhatia | 6731db9 | 2015-09-02 11:49:51 -0400 | [diff] [blame] | 77 | if ('backref' in f.name): |
| 78 | print '\t"%s"->"%s";'%(linked_class.__name__,c.__name__) |
| 79 | else: |
| 80 | print '\t"%s"->"%s";'%(c.__name__,linked_class.__name__) |
Sapan Bhatia | 5ef878c | 2015-01-29 20:40:13 +0000 | [diff] [blame] | 81 | print "}\n"; |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 82 | elif (output=='-json'): |
Sapan Bhatia | 5ef878c | 2015-01-29 20:40:13 +0000 | [diff] [blame] | 83 | d = {} |
| 84 | for c in model_classes: |
| 85 | fields = c._meta.fields |
Sapan Bhatia | 6731db9 | 2015-09-02 11:49:51 -0400 | [diff] [blame] | 86 | |
Sapan Bhatia | 5ef878c | 2015-01-29 20:40:13 +0000 | [diff] [blame] | 87 | for f in fields: |
Sapan Bhatia | 6731db9 | 2015-09-02 11:49:51 -0400 | [diff] [blame] | 88 | field_type = f.name.lower().split('_')[0] |
| 89 | if type(f)==ForeignKey and field_type in lower_class_names: |
| 90 | linked_class = lower_class_names[field_type] |
| 91 | if ('backref' in f.name.lower()): |
| 92 | a = linked_class.__name__ |
| 93 | b = c.__name__ |
| 94 | else: |
| 95 | b = linked_class.__name__ |
| 96 | a = c.__name__ |
| 97 | |
Sapan Bhatia | 5ef878c | 2015-01-29 20:40:13 +0000 | [diff] [blame] | 98 | try: |
Sapan Bhatia | 6731db9 | 2015-09-02 11:49:51 -0400 | [diff] [blame] | 99 | if (b not in d[a]): |
| 100 | d[a].append(b) |
Sapan Bhatia | 5ef878c | 2015-01-29 20:40:13 +0000 | [diff] [blame] | 101 | except KeyError: |
| 102 | d[c.__name__]=[linked_class.__name__] |
| 103 | #d['ControllerNetwork'].append('SliceDeployments') |
| 104 | print json.dumps(d,indent=4) |
| 105 | |
| 106 | |