blob: 9e13df2725ebd6131825b2f1864a451e4e5bfc4a [file] [log] [blame]
Sapan Bhatiad7425452013-09-03 11:45:15 -04001#!/usr/bin/python
2
3import os
4import pdb
5import sys
6import json
7
8sys.path.append('.')
9
10os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
11
12from django.db.models.fields.related import ForeignKey
Sapan Bhatiad7425452013-09-03 11:45:15 -040013
Scott Bakerceb464b2014-04-03 11:03:11 -070014# defaults
15app = "core"
16output = "-json"
Sapan Bhatiad7425452013-09-03 11:45:15 -040017
Scott Bakerceb464b2014-04-03 11:03:11 -070018# syntax: dmdot [-json | -dot] [app_name]
19
20# poor man's argument parser
21for arg in sys.argv[1:]:
22 if arg.startswith("-"):
23 output = arg
24 else:
25 app = arg
26
27app = app + ".models"
28#models_module = imp.load_source(app, ".")
29models_module = __import__(app)
30for part in app.split(".")[1:]:
Scott Bakerbff6b132014-04-03 11:35:25 -070031 if hasattr(models_module, "PlCoreBase"):
32 break
Scott Bakerceb464b2014-04-03 11:03:11 -070033 models_module = getattr(models_module,part)
34
35PlCoreBase = getattr(models_module,"PlCoreBase")
36
Sapan Bhatiad7425452013-09-03 11:45:15 -040037model_classes = []
38class_names = []
Scott Bakerceb464b2014-04-03 11:03:11 -070039lower_class_names = {}
40for classname in dir(models_module):
41 c = getattr(models_module, classname, None)
Sapan Bhatiad7425452013-09-03 11:45:15 -040042 if type(c)==type(PlCoreBase):
43 model_classes.append(c)
44 class_names.append(c.__name__)
Scott Bakerceb464b2014-04-03 11:03:11 -070045 lower_class_names[c.__name__.lower()] = c
Sapan Bhatiad7425452013-09-03 11:45:15 -040046
Scott Bakerceb464b2014-04-03 11:03:11 -070047# django doesn't use the correct case in field.name.title() for objects that
48# have CamelCased class names. So, compare everything in lower case.
Sapan Bhatiad7425452013-09-03 11:45:15 -040049
50if (output=='-dot'):
51 print "digraph plstack {";
52 for c in model_classes:
53 fields = c._meta.fields
54 for f in fields:
Scott Bakerceb464b2014-04-03 11:03:11 -070055 if type(f)==ForeignKey and f.name.lower() in lower_class_names:
56 linked_class = lower_class_names[f.name.lower()]
57 print '\t"%s"->"%s";'%(c.__name__,linked_class.__name__)
Sapan Bhatiad7425452013-09-03 11:45:15 -040058 print "}\n";
59elif (output=='-json'):
60 d = {}
61 for c in model_classes:
62 fields = c._meta.fields
63 for f in fields:
Scott Bakerceb464b2014-04-03 11:03:11 -070064 if type(f)==ForeignKey and f.name.lower() in lower_class_names:
65 linked_class = lower_class_names[f.name.lower()]
Sapan Bhatiad7425452013-09-03 11:45:15 -040066 try:
Scott Bakerceb464b2014-04-03 11:03:11 -070067 d[c.__name__].append(linked_class.__name__)
Sapan Bhatiad7425452013-09-03 11:45:15 -040068 except KeyError:
Scott Bakerceb464b2014-04-03 11:03:11 -070069 d[c.__name__]=[linked_class.__name__]
Sapan96a797a2014-12-03 14:51:01 -050070 d['NetworkDeployments'].append('SliceDeployments')
Sapan Bhatiad7425452013-09-03 11:45:15 -040071 print json.dumps(d,indent=4)
Scott Bakerceb464b2014-04-03 11:03:11 -070072
73