experiment to test white listing Devel::GlobalDestruction and not shipping XS
[scpubgit/Object-Remote.git] / lib / Object / Remote / FatNode.pm
CommitLineData
eee9a548 1package Object::Remote::FatNode;
2
3use strictures 1;
4use Config;
5use B qw(perlstring);
6
d0a91ad2 7my @exclude_mods = qw(XSLoader.pm DynaLoader.pm);
8
c003f065 9#used by t/watchdog_fatnode
7985ed9e 10our $INHIBIT_RUN_NODE = 0;
e5b3f03f 11
eee9a548 12sub stripspace {
13 my ($text) = @_;
14 $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
15 $text;
16}
17
18my %maybe_libs = map +($_ => 1), grep defined, (values %Config, '.');
19
20my @extra_libs = grep not(ref($_) or $maybe_libs{$_}), @INC;
21
22my $extra_libs = join '', map " -I$_\n", @extra_libs;
23
24my $command = qq(
25 $^X
26 $extra_libs
27 -mObject::Remote
28 -mObject::Remote::Connector::STDIO
29 -mCPS::Future
8c81a0e5 30 -mMRO::Compat
eee9a548 31 -mClass::C3
8c81a0e5 32 -mClass::C3::next
33 -mAlgorithm::C3
eee9a548 34 -mObject::Remote::ModuleLoader
35 -mObject::Remote::Node
4c8c83d7 36 -mMethod::Generate::BuildAll
37 -mMethod::Generate::DemolishAll
eee9a548 38 -mJSON::PP
df8e0ca6 39 -e 'print join "\\n", \%INC'
eee9a548 40);
41
42$command =~ s/\n/ /g;
43
c003f065 44#warn $command;
df8e0ca6 45chomp(my @inc = qx($command));
46
c003f065 47my %exclude = map { $_ => 1 } @exclude_mods;
48my %mod_files = @inc;
df8e0ca6 49my %mods = reverse @inc;
eee9a548 50
d0a91ad2 51foreach(keys(%mods)) {
52 if ($exclude{ $mods{$_} }) {
53 delete($mods{$_});
54 }
55}
56
c003f065 57#TODO quick and dirty mod for testing - vendorarchexp from a perlbrew build
58#was set to '' which evaluates as a true regex
59
60#use Data::Dumper;
61#print STDERR Dumper([keys %mod_files]);
62#print STDERR Dumper([keys %mods]);
eee9a548 63
c003f065 64my @non_core_non_arch = ( $mod_files{'Devel/GlobalDestruction.pm'} );
65push @non_core_non_arch, grep +(
66 not (
67 $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
68 or $Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
69 or $Config{vendorarchexp} ne '' && /^\Q$Config{vendorarchexp}/
70 or $Config{sitearchexp} ne '' && /^\Q$Config{sitearchexp}/
71 )
72), grep !/\Q$Config{archname}/, grep !/\Q$Config{myarchname}/, keys %mods;
73
74my @core_non_arch = grep +(
75 $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
76 and not($Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
77 or /\Q$Config{archname}/ or /\Q$Config{myarchname}/)
78), keys %mods;
79
80#print STDERR "non-core non-arch ", Dumper(\@non_core_non_arch);
81#print STDERR "core non-arch ", Dumper(\@core_non_arch);
1ef3d225 82
42ff5efb 83#TODO this is the wrong path to go down - fork() will bring
84#the env vars with it and the ssh connector can handle
85#forwarding the env vars
23591f5f 86my $env_pass = '';
87if (defined($ENV{OBJECT_REMOTE_LOG_LEVEL})) {
1a7f821f 88 my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
c0d4da69 89 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_LEVEL} = "' . $level . "\";\n";
90}
91if (defined($ENV{OBJECT_REMOTE_LOG_FORMAT})) {
92 my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT};
93 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORMAT} = "' . $format . "\";\n";
23591f5f 94}
a0771eda 95if (defined($ENV{OBJECT_REMOTE_LOG_SELECTIONS})) {
96 my $selections = $ENV{OBJECT_REMOTE_LOG_SELECTIONS};
97 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_SELECTIONS} = "' . $selections . "\";\n";
98}
aa4a1d83 99if (defined($ENV{OBJECT_REMOTE_LOG_FORWARDING})) {
100 my $forwarding = $ENV{OBJECT_REMOTE_LOG_FORWARDING};
101 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORWARDING} = "' . $forwarding . "\";\n";
102}
103
eee9a548 104my $start = stripspace <<'END_START';
105 # This chunk of stuff was generated by Object::Remote::FatNode. To find
106 # the original file's code, look for the end of this BEGIN block or the
107 # string 'FATPACK'
108 BEGIN {
1ef3d225 109 my (%fatpacked,%fatpacked_extra);
eee9a548 110END_START
d0a91ad2 111
112$start .= 'my %exclude = map { $_ => 1 } qw(' . join(' ', @exclude_mods) . ");\n";
113
eee9a548 114my $end = stripspace <<'END_END';
1ef3d225 115 s/^ //mg for values %fatpacked, values %fatpacked_extra;
eee9a548 116
d0a91ad2 117sub load_from_hash {
1ef3d225 118 if (my $fat = $_[0]->{$_[1]}) {
d0a91ad2 119 if ($exclude{$_[1]}) {
120 warn "Will not pre-load '$_[1]'";
121 return undef;
122 }
123
124 #warn "handling $_[1]";
eee9a548 125 open my $fh, '<', \$fat;
126 return $fh;
127 }
d0a91ad2 128
8c81a0e5 129 #Uncomment this to find brokenness
130 #warn "Missing $_[1]";
d0a91ad2 131 return;
1ef3d225 132 }
133
134 unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
135 push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
eee9a548 136
137 } # END OF FATPACK CODE
138
139 use strictures 1;
140 use Object::Remote::Node;
e5b3f03f 141
142 unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
143 Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);
144 }
145
eee9a548 146END_END
147
148my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
c003f065 149 @non_core_non_arch, @core_non_arch;
eee9a548 150
c003f065 151my %did_pack;
1ef3d225 152sub generate_fatpack_hash {
153 my ($hash_name, $orig) = @_;
154 (my $stub = $orig) =~ s/\.pm$//;
eee9a548 155 my $name = uc join '_', split '/', $stub;
1ef3d225 156 my $data = $files{$orig} or die $orig; $data =~ s/^/ /mg;
c003f065 157 return () if $did_pack{$hash_name}{$orig};
158 $did_pack{$hash_name}{$orig} = 1;
1ef3d225 159 return '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
eee9a548 160 .qq!${data}${name}\n!;
1ef3d225 161}
162
163my @segments = (
c003f065 164 map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @non_core_non_arch),
165 map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @core_non_arch),
1ef3d225 166);
eee9a548 167
c003f065 168#print STDERR Dumper(\@segments);
23591f5f 169our $DATA = join "\n", $start, $env_pass, @segments, $end;
eee9a548 170
1711;