File Coverage

blib/lib/Object/Remote/FatNode.pm
Criterion Covered Total %
statement 20 20 100.0
branch 3 6 50.0
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 28 33 84.8


line stmt bran cond sub pod time code
1             package Object::Remote::FatNode;
2              
3 14     14   276994 use strictures 1;
  14         334  
  14         1027  
4 14     14   3343 use Config;
  14         137  
  14         1196  
5 14     14   103 use B qw(perlstring);
  14         46  
  14         21950  
6              
7             my @exclude_mods = qw(XSLoader.pm DynaLoader.pm);
8             #used by t/watchdog_fatnode
9             our $INHIBIT_RUN_NODE = 0;
10              
11             sub stripspace {
12 28     28 0 174 my ($text) = @_;
13 28 50       1902 $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
14 28         204 $text;
15             }
16              
17             my %maybe_libs = map +($_ => 1), grep defined, (values %Config, '.');
18              
19             my @extra_libs = grep not(ref($_) or $maybe_libs{$_}), @INC;
20             my $extra_libs = join '', map {
21             my $lib = $_;
22             $lib =~ s{'}{'\\''}g;
23             " -I'$lib'\n";
24             } @extra_libs;
25              
26             my $command = qq(
27             $^X
28             $extra_libs
29             -mObject::Remote
30             -mObject::Remote::Connector::STDIO
31             -mFuture
32             -mFuture::PP
33             -mMRO::Compat
34             -mClass::C3
35             -mClass::C3::next
36             -mAlgorithm::C3
37             -mObject::Remote::ModuleLoader
38             -mObject::Remote::Node
39             -mMethod::Generate::BuildAll
40             -mMethod::Generate::DemolishAll
41             -mMoo::HandleMoose::_TypeMap
42             -mJSON::PP
43             -mDevel::GlobalDestruction
44             -e 'print join "\\n", \%INC'
45             );
46              
47             $command =~ s/\n/ /g;
48              
49             chomp(my @inc = qx($command));
50              
51             my %exclude = map { $_ => 1 } @exclude_mods;
52              
53             my %file_names = @inc;
54              
55             # only include mods that match the filename,
56             # ie ones that will succeed with a require $module
57             # https://rt.cpan.org/Ticket/Display.html?id=100478
58             my %mods =
59             map { $file_names{$_} => $_ }
60             grep { $file_names{$_} =~ /\Q$_\E$/ } keys %file_names;
61              
62             foreach(keys(%mods)) {
63             if ($exclude{ $mods{$_} }) {
64             delete($mods{$_});
65             }
66             }
67              
68             my @non_core_non_arch = ( $file_names{'Devel/GlobalDestruction.pm'} );
69             push @non_core_non_arch, grep +(
70             not (
71             #some of the config variables can be empty which will eval as a matching regex
72             $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
73             or $Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
74             or $Config{vendorarchexp} ne '' && /^\Q$Config{vendorarchexp}/
75             or $Config{sitearchexp} ne '' && /^\Q$Config{sitearchexp}/
76             )
77             ), grep !/\Q$Config{archname}/, grep !/\Q$Config{myarchname}/, keys %mods;
78              
79             my @core_non_arch = grep +(
80             $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
81             and not($Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
82             or /\Q$Config{archname}/ or /\Q$Config{myarchname}/)
83             ), keys %mods;
84              
85             my $start = stripspace <<'END_START';
86             # This chunk of stuff was generated by Object::Remote::FatNode. To find
87             # the original file's code, look for the end of this BEGIN block or the
88             # string 'FATPACK'
89             BEGIN {
90             my (%fatpacked,%fatpacked_extra);
91             END_START
92              
93             $start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
94              
95             my $end = stripspace <<'END_END';
96             s/^ //mg for values %fatpacked, values %fatpacked_extra;
97              
98             sub load_from_hash {
99             if (my $fat = $_[0]->{$_[1]}) {
100             if ($exclude{$_[1]}) {
101             warn "Will not pre-load '$_[1]'";
102             return undef;
103             }
104              
105             #warn "Handling $_[1]";
106             open my $fh, '<', \$fat;
107             return $fh;
108             }
109              
110             #Uncomment this to find brokenness
111             #warn "Missing $_[1]";
112             return;
113             }
114              
115             unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
116             push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
117              
118             } # END OF FATPACK CODE
119              
120             use strictures 1;
121             use Object::Remote::Node;
122              
123             unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
124             Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);
125             }
126              
127             END_END
128              
129             my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
130             @non_core_non_arch, @core_non_arch;
131              
132             sub generate_fatpack_hash {
133 1496     1496 0 3631 my ($hash_name, $orig) = @_;
134 1496         5293 (my $stub = $orig) =~ s/\.pm$//;
135 1496         5183 my $name = uc join '_', split '/', $stub;
136 1496 50       5263 my $data = $files{$orig} or die $orig; $data =~ s/^/ /mg;
  1496         415601  
137 1496 50       8502 $data .= "\n" unless $data =~ m/\n$/;
138 1496         36226 my $ret = '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
139             .qq!${data}${name}\n!;
140             # warn $ret;
141 1496         6520 return $ret;
142             }
143              
144             my @segments = (
145             map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @non_core_non_arch),
146             map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @core_non_arch),
147             );
148              
149             #print STDERR Dumper(\@segments);
150             our $DATA = join "\n", $start, @segments, $end;
151              
152             1;