File Coverage

blib/lib/Object/Remote/FatNode.pm
Criterion Covered Total %
statement 24 35 68.5
branch 4 10 40.0
condition n/a
subroutine 6 7 85.7
pod 0 3 0.0
total 34 55 61.8


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