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 | |
| 10 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
| 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 |
| 15 | app = "core" |
| 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: |
| 25 | app = arg |
| 26 | |
| 27 | app = app + ".models" |
| 28 | #models_module = imp.load_source(app, ".") |
| 29 | models_module = __import__(app) |
| 30 | for part in app.split(".")[1:]: |
Scott Baker | bff6b13 | 2014-04-03 11:35:25 -0700 | [diff] [blame] | 31 | if hasattr(models_module, "PlCoreBase"): |
| 32 | break |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 33 | models_module = getattr(models_module,part) |
| 34 | |
| 35 | PlCoreBase = getattr(models_module,"PlCoreBase") |
| 36 | |
Sapan Bhatia | 5ef878c | 2015-01-29 20:40:13 +0000 | [diff] [blame^] | 37 | synonyms = { |
| 38 | 'user':'creator' |
| 39 | } |
| 40 | |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 41 | model_classes = [] |
| 42 | class_names = [] |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 43 | lower_class_names = {} |
| 44 | for classname in dir(models_module): |
| 45 | c = getattr(models_module, classname, None) |
Sapan Bhatia | 5ef878c | 2015-01-29 20:40:13 +0000 | [diff] [blame^] | 46 | if type(c)==type(PlCoreBase): |
| 47 | model_classes.append(c) |
| 48 | class_names.append(c.__name__) |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 49 | lower_class_names[c.__name__.lower()] = c |
Sapan Bhatia | 5ef878c | 2015-01-29 20:40:13 +0000 | [diff] [blame^] | 50 | try: |
| 51 | synonym = synonyms[c.__name__.lower()] |
| 52 | lower_class_names[synonym] = c |
| 53 | except: |
| 54 | pass |
| 55 | |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 56 | |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 57 | # django doesn't use the correct case in field.name.title() for objects that |
| 58 | # have CamelCased class names. So, compare everything in lower case. |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 59 | |
| 60 | if (output=='-dot'): |
Sapan Bhatia | 5ef878c | 2015-01-29 20:40:13 +0000 | [diff] [blame^] | 61 | print "digraph plstack {"; |
| 62 | for c in model_classes: |
| 63 | fields = c._meta.fields |
| 64 | |
| 65 | for f in fields: |
| 66 | if type(f)==ForeignKey and f.name.lower() in lower_class_names: |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 67 | linked_class = lower_class_names[f.name.lower()] |
Sapan Bhatia | 5ef878c | 2015-01-29 20:40:13 +0000 | [diff] [blame^] | 68 | print '\t"%s"->"%s";'%(c.__name__,linked_class.__name__) |
| 69 | print "}\n"; |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 70 | elif (output=='-json'): |
Sapan Bhatia | 5ef878c | 2015-01-29 20:40:13 +0000 | [diff] [blame^] | 71 | d = {} |
| 72 | for c in model_classes: |
| 73 | fields = c._meta.fields |
| 74 | |
| 75 | for f in fields: |
| 76 | if type(f)==ForeignKey and f.name.lower() in lower_class_names: |
Scott Baker | ceb464b | 2014-04-03 11:03:11 -0700 | [diff] [blame] | 77 | linked_class = lower_class_names[f.name.lower()] |
Sapan Bhatia | 5ef878c | 2015-01-29 20:40:13 +0000 | [diff] [blame^] | 78 | try: |
| 79 | d[c.__name__].append(linked_class.__name__) |
| 80 | except KeyError: |
| 81 | d[c.__name__]=[linked_class.__name__] |
| 82 | #d['ControllerNetwork'].append('SliceDeployments') |
| 83 | print json.dumps(d,indent=4) |
| 84 | |
| 85 | |