Die bei Lx-Office mitgelieferten Perl-Module, die nicht zu Lx-Office selber gehören...
[kivitendo-erp.git] / scripts / dbupgrade2_tool.pl
1 #!/usr/bin/perl
2
3 BEGIN {
4   if (! -d "bin" || ! -d "SL") {
5     print("This tool must be run from the Lx-Office ERP base directory.\n");
6     exit(1);
7   }
8
9   push(@INC, "modules");
10 }
11
12 use DBI;
13 use Data::Dumper;
14 use Getopt::Long;
15
16 use SL::LXDebug;
17
18 $lxdebug = LXDebug->new();
19
20 use SL::Form;
21 use SL::Locale;
22 use SL::DBUpgrade2;
23
24 #######
25 #######
26 #######
27
28 sub show_help {
29   print("dbupgrade2_tool.pl [--list] [--tree] [--rtree] [--graphviz]\n" .
30         "                   [--nodepds] [--help]\n");
31 }
32
33 sub calc_rev_depends {
34   map({ $_->{"rev_depends"} = []; } values(%{$controls}));
35   foreach my $control (values(%{$controls})) {
36     map({ push(@{$controls->{$_}{"rev_depends"}}, $control->{"tag"}) }
37         @{$control->{"depends"}});
38   }
39 }
40
41 sub dump_list {
42   my @sorted_controls = sort_dbupdate_controls($controls);
43
44   print("LIST VIEW\n\n");
45   print("number tag depth priority\n");
46   $i = 0;
47   foreach (@sorted_controls) {
48     print("$i $_->{tag} $_->{depth} $_->{priority}\n");
49     $i++;
50   }
51
52   print("\n");
53 }
54
55 sub dump_node {
56   my ($tag, $depth) = @_;
57
58   print(" " x $depth . $tag . "\n");
59
60   my $c = $controls->{$tag};
61   my $num = scalar(@{$c->{"depends"}});
62   for (my $i = 0; $i < $num; $i++) {
63     dump_node($c->{"depends"}[$i], $depth + 1);
64   }
65 }
66
67 sub dump_tree {
68   print("TREE VIEW\n\n");
69
70   calc_rev_depends();
71
72   my @sorted_controls = sort_dbupdate_controls($controls);
73
74   foreach my $control (@sorted_controls) {
75     dump_node($control->{"tag"}, "") unless (@{$control->{"rev_depends"}});
76   }
77
78   print("\n");
79 }
80
81 sub dump_node_reverse {
82   my ($tag, $depth) = @_;
83
84   print(" " x $depth . $tag . "\n");
85
86   my $c = $controls->{$tag};
87   my $num = scalar(@{$c->{"rev_depends"}});
88   for (my $i = 0; $i < $num; $i++) {
89     dump_node_reverse($c->{"rev_depends"}[$i], $depth + 1);
90   }
91 }
92
93 sub dump_tree_reverse {
94   print("REVERSE TREE VIEW\n\n");
95
96   calc_rev_depends();
97
98   my @sorted_controls = sort_dbupdate_controls($controls);
99
100   foreach my $control (@sorted_controls) {
101     last if ($control->{"depth"} > 1);
102     dump_node_reverse($control->{"tag"}, "");
103   }
104
105   print("\n");
106 }
107
108 sub dump_graphviz {
109   print("GRAPHVIZ POSTCRIPT\n\n");
110   print("Output will be written to db_dependencies.ps\n");
111   $dot = "|dot -Tps ";
112   open(OUT, "${dot}> db_dependencies.ps");
113   print(OUT
114         "digraph db_dependencies {\n" .
115         "node [shape=box];\n");
116   my %ranks;
117   foreach my $c (values(%{$controls})) {
118     $ranks{$c->{"depth"}} = [] unless ($ranks{$c->{"depth"}});
119     push(@{$ranks{$c->{"depth"}}}, $c->{"tag"});
120   }
121   foreach (sort(keys(%ranks))) {
122     print(OUT "{ rank = same; " .
123           join("", map({ '"' . $_ . '"; ' } @{$ranks{$_}})) .
124           " }\n");
125   }
126   foreach my $c (values(%{$controls})) {
127     print(OUT "$c->{tag};\n");
128     foreach my $d (@{$c->{"depends"}}) {
129       print(OUT "$c->{tag} -> $d;\n");
130     }
131   }
132   print(OUT "}\n");
133   close(OUT);
134 }
135
136 sub dump_nodeps {
137   calc_rev_depends();
138
139   print("SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
140         join("\n",
141              map({ $_->{"tag"} }
142                  grep({ !@{$_->{"rev_depends"}} }
143                       values(%{$controls})))) .
144         "\n\n");
145 }
146
147 #######
148 #######
149 #######
150
151 eval { require "lx-erp.conf"; };
152
153 $form = Form->new();
154 $locale = Locale->new("de", "login");
155
156 #######
157 #######
158 #######
159
160 my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help);
161
162 GetOptions("list" => \$opt_list,
163            "tree" => \$opt_tree,
164            "rtree" => \$opt_rtree,
165            "nodeps" => \$opt_nodeps,
166            "graphviz" => \$opt_graphviz,
167            "help" => \$opt_help,
168   );
169
170 if ($opt_help) {
171   show_help();
172   exit(0);
173 }
174
175 $controls = parse_dbupdate_controls($form, "Pg");
176
177 if ($opt_list) {
178   dump_list();
179 }
180
181 if ($opt_tree) {
182   dump_tree();
183 }
184
185 if ($opt_rtree) {
186   dump_tree_reverse();
187 }
188
189 if ($opt_graphviz) {
190   dump_graphviz();
191 }
192
193 if ($opt_nodeps) {
194   dump_nodeps();
195 }