blib/lib/UR/Namespace/Command/RunsOnModulesInTree.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 193 SCAR 65.5
CC
26
Cov
0%
execute
342.0
shell_args_description
56.0
is_abstract
6.0
CRAP 702.0
1  
2 # Abstract base command for commands which run on all or part of a class tree.
3  
4 package UR::Namespace::Command::RunsOnModulesInTree;
5  
6 use strict;
7 use warnings;
8  
9 use UR;
10 our $VERSION = "0.46"; # UR $VERSION;
11  
12 UR::Object::Type->define(
13 class_name => __PACKAGE__,
14 is => 'UR::Namespace::Command::Base',
15 has => [
16 classes_or_modules => {
17 is_many => 1,
18 is_optional => 1,
19 shell_args_position => 99
20 }
21 ]
22 );
23  
24  
25 sub is_abstract
26 {
27 my $self = shift;
28 my $class = ref($self) || $self;
29 return 1 if $class eq __PACKAGE__;
30 return;
31 }
32  
33 sub _help_detail_footer
34 {
35 my $text =
36 return <<EOS
37 This command requires that the current working directory be under a namespace module.
38  
39 If no modules or class names are specified as parameters, it runs on all modules in the namespace.
40  
41 If modules or class names ARE listed, it will operate only on those.
42  
43 Words containing double-colons will be interpreted as absolute class names.
44  
45 All other words will be interpreted as relative file paths to modules.
46 EOS
47 }
48  
49  
50  
51 sub execute
52 {
53 my $self = shift;
54  
55 my $params = shift;
56  
57 my $namespace = $self->namespace_name;
58 unless ($namespace) {
59 die "This command can only be run from a directory tree under a UR namespace module.\n";
60 }
61  
62 my @subject_list = $self->classes_or_modules;
63  
64 if ($self->can("for_each_class_object") ne __PACKAGE__->can("for_each_class_object")) {
65  
66 my @classes = $self->_class_objects_in_tree(@subject_list);
67  
68 unless ($self->before(\@classes)) {
69 print STDERR "Terminating.\n";
70 return;
71 }
72 for my $class (@classes) {
73 unless ($self->for_each_class_object($class)) {
74 print STDERR "Terminating...\n";
75 return;
76 }
77 }
78 }
79 elsif ($self->can("for_each_class_name") ne __PACKAGE__->can("for_each_class_name")) {
80 my @class_names = $self->_class_names_in_tree(@subject_list);
81 unless ($self->before(\@class_names)) {
82 print STDERR "Terminating.\n";
83 return;
84 }
85 for my $class (@class_names) {
86 unless ($self->for_each_class_name($class)) {
87 print STDERR "Terminating...\n";
88 return;
89 }
90 }
91 }
92 elsif ($self->can("for_each_module_file") ne __PACKAGE__->can("for_each_module_file")) {
93 my @modules = $self->_modules_in_tree(@subject_list);
94 unless ($self->before(\@modules)) {
95 print STDERR "Terminating.\n";
96 return;
97 }
98 for my $module (@modules) {
99 unless ($self->for_each_module_file($module)) {
100 print STDERR "Terminating...\n";
101 return;
102 }
103 }
104 }
105 elsif ($self->can("for_each_module_file_in_parallel") ne __PACKAGE__->can("for_each_module_file_in_parallel")) {
106 my @modules = $self->_modules_in_tree(@subject_list);
107 unless ($self->before(\@modules)) {
108 print STDERR "Terminating.\n";
109 return;
110 }
111 my $bucket_count = 10;
112 my @buckets;
113 my %child_processes;
114 for my $bucket_number (0..$bucket_count-1) {
115 $buckets[$bucket_number] ||= [];
116 }
117 while (@modules) {
118 for my $bucket_number (0..$bucket_count-1) {
119 my $module = shift @modules;
120 last if not $module;
121 push @{ $buckets[$bucket_number] }, $module;
122 }
123 }
124  
125 for my $bucket (@buckets) {
126 my $child_pid = fork();
127 if ($child_pid) {
128 # the parent process continues forking...
129 $child_processes{$child_pid} = 1;
130 }
131 else {
132 # the child process does handles its bucket
133 for my $module (@$bucket) {
134 unless ($self->for_each_module_file_in_parallel($module)) {
135 exit 1;
136 }
137 }
138 # and then exits quietly
139 exit 0;
140 }
141 }
142 #$DB::single = 1;
143 while (keys %child_processes) {
144 my $child_pid = wait();
145 if ($child_pid == -1) {
146 print "lost children? " . join(" ", keys %child_processes);
147 }
148 delete $child_processes{$child_pid};
149 }
150 }
151 else {
152 die "$self does not implement: for_each_[class_object|class_name|module_file]!";
153 }
154  
155 unless ($self->after()) {
156 print STDERR "Terminating.\n";
157 return;
158 }
159  
160 return 1;
161 }
162  
163 sub before {
164 return 1;
165 }
166  
167 sub for_each_module_file {
168 die "The for_each_module_file method is not defined by/in " . shift;
169 }
170  
171  
172 sub for_each_class_name {
173 die "The for_each_class_name method is not defined by/in " . shift;
174 }
175  
176 sub for_each_class_object {
177 Carp::confess "The for_each_class_object method is not defined by/in " . shift;
178 }
179  
180 sub after {
181 return 1;
182 }
183  
184 sub loop_methods
185 {
186 my $self = shift;
187 my @methods;
188 for my $method (qw/
189 for_each_class_object
190 for_each_class_name
191 for_each_module_file
192 for_each_module_file_in_parallel
193 /) {
194 no warnings;
195 if ($self->can($method) ne __PACKAGE__->can($method)) {
196 push @methods, $method;
197 }
198 }
199 return @methods;
200 }
201  
202 sub shell_args_description
203 {
204 my $self = shift;
205  
206 my @loop_methods = $self->loop_methods;
207 my $takes_classes = 1 if grep { /class/ } @loop_methods;
208 my $takes_modules = 1 if grep { /modul/ } @loop_methods;
209  
210 my $text;
211 if ($takes_classes and $takes_modules) {
212 $text = "[CLASS|MODULE] [CLASS|MODULE] ...";
213 }
214 elsif ($takes_classes) {
215 $text = "[CLASS] [CLASS]..";
216 }
217 elsif ($takes_modules) {
218 $text = "[MODULE] [MODULE] ...";
219 }
220 else {
221 $text = "<module broken!>";
222 }
223  
224 $text .= " " . $self->SUPER::shell_args_description(@_);
225  
226 if ($self->is_sub_command_delegator) {
227 my @names = $self->sub_command_names;
228 return "[" . join("|",@names) . "] $text"
229 }
230 return $text;
231 }
232  
233  
234 1;