root / env / lib / python2.7 / site-packages / south / management / commands / graphmigrations.py @ d1a4905f
History | View | Annotate | Download (2.25 KB)
1 |
"""
|
---|---|
2 |
Outputs a graphviz dot file of the dependencies.
|
3 |
"""
|
4 |
|
5 |
from optparse import make_option |
6 |
import re |
7 |
import textwrap |
8 |
|
9 |
from django.core.management.base import BaseCommand |
10 |
from django.core.management.color import no_style |
11 |
|
12 |
from south.migration import Migrations, all_migrations |
13 |
|
14 |
class Command(BaseCommand): |
15 |
|
16 |
help = "Outputs a GraphViz dot file of all migration dependencies to stdout."
|
17 |
|
18 |
def handle(self, **options): |
19 |
|
20 |
# Resolve dependencies
|
21 |
Migrations.calculate_dependencies() |
22 |
|
23 |
colors = [ 'crimson', 'darkgreen', 'darkgoldenrod', 'navy', |
24 |
'brown', 'darkorange', 'aquamarine' , 'blueviolet' ] |
25 |
color_index = 0
|
26 |
wrapper = textwrap.TextWrapper(width=40)
|
27 |
|
28 |
print "digraph G {" |
29 |
|
30 |
# Group each app in a subgraph
|
31 |
for migrations in all_migrations(): |
32 |
print " subgraph %s {" % migrations.app_label() |
33 |
print " node [color=%s];" % colors[color_index] |
34 |
for migration in migrations: |
35 |
# Munge the label - text wrap and change _ to spaces
|
36 |
label = "%s - %s" % (
|
37 |
migration.app_label(), migration.name()) |
38 |
label = re.sub(r"_+", " ", label) |
39 |
label= "\\n".join(wrapper.wrap(label))
|
40 |
print ' "%s.%s" [label="%s"];' % ( |
41 |
migration.app_label(), migration.name(), label) |
42 |
print " }" |
43 |
color_index = (color_index + 1) % len(colors) |
44 |
|
45 |
# For every migration, print its links.
|
46 |
for migrations in all_migrations(): |
47 |
for migration in migrations: |
48 |
for other in migration.dependencies: |
49 |
# Added weight tends to keep migrations from the same app
|
50 |
# in vertical alignment
|
51 |
attrs = "[weight=2.0]"
|
52 |
# But the more interesting edges are those between apps
|
53 |
if other.app_label() != migration.app_label():
|
54 |
attrs = "[style=bold]"
|
55 |
print ' "%s.%s" -> "%s.%s" %s;' % ( |
56 |
other.app_label(), other.name(), |
57 |
migration.app_label(), migration.name(), |
58 |
attrs |
59 |
) |
60 |
|
61 |
print "}"; |