blob: 4bcf7e12f092c0df9e2f5a241a9b9c33df312e0a [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 Bhatia5ef878c2015-01-29 20:40:13 +000037synonyms = {
38 'user':'creator'
39}
40
Sapan Bhatiad7425452013-09-03 11:45:15 -040041model_classes = []
42class_names = []
Scott Bakerceb464b2014-04-03 11:03:11 -070043lower_class_names = {}
44for classname in dir(models_module):
45 c = getattr(models_module, classname, None)
Sapan Bhatia5ef878c2015-01-29 20:40:13 +000046 if type(c)==type(PlCoreBase):
47 model_classes.append(c)
48 class_names.append(c.__name__)
Scott Bakerceb464b2014-04-03 11:03:11 -070049 lower_class_names[c.__name__.lower()] = c
Sapan Bhatia5ef878c2015-01-29 20:40:13 +000050 try:
51 synonym = synonyms[c.__name__.lower()]
52 lower_class_names[synonym] = c
53 except:
54 pass
55
Sapan Bhatiad7425452013-09-03 11:45:15 -040056
Scott Bakerceb464b2014-04-03 11:03:11 -070057# 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 Bhatiad7425452013-09-03 11:45:15 -040059
60if (output=='-dot'):
Sapan Bhatia5ef878c2015-01-29 20:40:13 +000061 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 Bakerceb464b2014-04-03 11:03:11 -070067 linked_class = lower_class_names[f.name.lower()]
Sapan Bhatia5ef878c2015-01-29 20:40:13 +000068 print '\t"%s"->"%s";'%(c.__name__,linked_class.__name__)
69 print "}\n";
Sapan Bhatiad7425452013-09-03 11:45:15 -040070elif (output=='-json'):
Sapan Bhatia5ef878c2015-01-29 20:40:13 +000071 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 Bakerceb464b2014-04-03 11:03:11 -070077 linked_class = lower_class_names[f.name.lower()]
Sapan Bhatia5ef878c2015-01-29 20:40:13 +000078 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