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