new test to validate connection inside a connection works
[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
f6ac5927 7my @exclude_mods = qw(XSLoader.pm DynaLoader.pm);
f129bfaf 8#used by t/watchdog_fatnode
f1d70835 9our $INHIBIT_RUN_NODE = 0;
f6ac5927 10
eee9a548 11sub stripspace {
12 my ($text) = @_;
13 $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
14 $text;
15}
16
17my %maybe_libs = map +($_ => 1), grep defined, (values %Config, '.');
18
19my @extra_libs = grep not(ref($_) or $maybe_libs{$_}), @INC;
20
21my $extra_libs = join '', map " -I$_\n", @extra_libs;
22
23my $command = qq(
24 $^X
25 $extra_libs
26 -mObject::Remote
27 -mObject::Remote::Connector::STDIO
28 -mCPS::Future
8c81a0e5 29 -mMRO::Compat
eee9a548 30 -mClass::C3
8c81a0e5 31 -mClass::C3::next
32 -mAlgorithm::C3
eee9a548 33 -mObject::Remote::ModuleLoader
34 -mObject::Remote::Node
4c8c83d7 35 -mMethod::Generate::BuildAll
36 -mMethod::Generate::DemolishAll
eee9a548 37 -mJSON::PP
df8e0ca6 38 -e 'print join "\\n", \%INC'
eee9a548 39);
40
41$command =~ s/\n/ /g;
42
df8e0ca6 43chomp(my @inc = qx($command));
44
f6ac5927 45my %exclude = map { $_ => 1 } @exclude_mods;
df8e0ca6 46my %mods = reverse @inc;
eee9a548 47
f6ac5927 48foreach(keys(%mods)) {
49 if ($exclude{ $mods{$_} }) {
50 delete($mods{$_});
51 }
52}
53
54sub filter_not_core {
61c6bba7 55 not (
56 /^\Q$Config{privlibexp}/ or /^\Q$Config{archlibexp}/
f6ac5927 57 )
58}
eee9a548 59
cc62b744 60my @file_names = keys %mods;
61my @before_inc = grep { filter_not_core() } @file_names;
f6ac5927 62my @after_inc;
1ef3d225 63
293fb1ee 64#TODO this is the wrong path to go down - fork() will bring
65#the env vars with it and the ssh connector can handle
66#forwarding the env vars
4a9fa1a5 67my $env_pass = '';
68if (defined($ENV{OBJECT_REMOTE_LOG_LEVEL})) {
37efeb68 69 my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
0fe333eb 70 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_LEVEL} = "' . $level . "\";\n";
71}
72if (defined($ENV{OBJECT_REMOTE_LOG_FORMAT})) {
73 my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT};
74 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORMAT} = "' . $format . "\";\n";
4a9fa1a5 75}
eb49c7df 76if (defined($ENV{OBJECT_REMOTE_LOG_SELECTIONS})) {
77 my $selections = $ENV{OBJECT_REMOTE_LOG_SELECTIONS};
78 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_SELECTIONS} = "' . $selections . "\";\n";
79}
837cfa50 80if (defined($ENV{OBJECT_REMOTE_LOG_FORWARDING})) {
81 my $forwarding = $ENV{OBJECT_REMOTE_LOG_FORWARDING};
82 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORWARDING} = "' . $forwarding . "\";\n";
83}
302ecfbf 84if (defined($ENV{OBJECT_REMOTE_PERL_BIN})) {
85 my $perl_bin = $ENV{OBJECT_REMOTE_PERL_BIN};
86 $env_pass .= '$ENV{OBJECT_REMOTE_PERL_BIN} = "' . $perl_bin . "\";\n";
87}
837cfa50 88
eb49c7df 89
4a9fa1a5 90
eee9a548 91my $start = stripspace <<'END_START';
92 # This chunk of stuff was generated by Object::Remote::FatNode. To find
93 # the original file's code, look for the end of this BEGIN block or the
94 # string 'FATPACK'
95 BEGIN {
1ef3d225 96 my (%fatpacked,%fatpacked_extra);
eee9a548 97END_START
f6ac5927 98
cc62b744 99$start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
f6ac5927 100
eee9a548 101my $end = stripspace <<'END_END';
1ef3d225 102 s/^ //mg for values %fatpacked, values %fatpacked_extra;
eee9a548 103
1ef3d225 104 sub load_from_hash {
105 if (my $fat = $_[0]->{$_[1]}) {
f6ac5927 106 if ($exclude{$_[1]}) {
107 warn "Will not pre-load '$_[1]'";
108 return undef;
109 }
110
cc62b744 111 #warn "Handling $_[1]";
eee9a548 112 open my $fh, '<', \$fat;
113 return $fh;
114 }
1900601d 115
8c81a0e5 116 #Uncomment this to find brokenness
117 #warn "Missing $_[1]";
f6ac5927 118 return;
1ef3d225 119 }
120
121 unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
122 push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
eee9a548 123
124 } # END OF FATPACK CODE
125
126 use strictures 1;
127 use Object::Remote::Node;
f129bfaf 128
129 unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
130 Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);
131 }
132
eee9a548 133END_END
134
135my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
f6ac5927 136 @before_inc, @after_inc;
eee9a548 137
1ef3d225 138sub generate_fatpack_hash {
139 my ($hash_name, $orig) = @_;
140 (my $stub = $orig) =~ s/\.pm$//;
eee9a548 141 my $name = uc join '_', split '/', $stub;
1ef3d225 142 my $data = $files{$orig} or die $orig; $data =~ s/^/ /mg;
143 return '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
eee9a548 144 .qq!${data}${name}\n!;
1ef3d225 145}
146
147my @segments = (
f6ac5927 148 map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @before_inc),
149 map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @after_inc),
1ef3d225 150);
eee9a548 151
4a9fa1a5 152our $DATA = join "\n", $start, $env_pass, @segments, $end;
eee9a548 153
1541;