fix bug that allowed forwarded logs to be output from the logger built via env vars
[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
15b847bb 54my @non_core_non_arch = ( $mods{'Devel/GlobalDestruction.pm'} );
55push @non_core_non_arch, grep +(
61c6bba7 56 not (
15b847bb 57 #some of the config variables can be empty which will eval as a matching regex
58 $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
59 or $Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
60 or $Config{vendorarchexp} ne '' && /^\Q$Config{vendorarchexp}/
61 or $Config{sitearchexp} ne '' && /^\Q$Config{sitearchexp}/
62 )
63), grep !/\Q$Config{archname}/, grep !/\Q$Config{myarchname}/, keys %mods;
64
65my @core_non_arch = grep +(
66 $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
67 and not($Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
68 or /\Q$Config{archname}/ or /\Q$Config{myarchname}/)
69), keys %mods;
70
71#print STDERR "non-core non-arch ", Dumper(\@non_core_non_arch);
72#print STDERR "core non-arch ", Dumper(\@core_non_arch);
1ef3d225 73
293fb1ee 74#TODO this is the wrong path to go down - fork() will bring
75#the env vars with it and the ssh connector can handle
76#forwarding the env vars
4a9fa1a5 77my $env_pass = '';
78if (defined($ENV{OBJECT_REMOTE_LOG_LEVEL})) {
37efeb68 79 my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
0fe333eb 80 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_LEVEL} = "' . $level . "\";\n";
81}
82if (defined($ENV{OBJECT_REMOTE_LOG_FORMAT})) {
83 my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT};
84 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORMAT} = "' . $format . "\";\n";
4a9fa1a5 85}
eb49c7df 86if (defined($ENV{OBJECT_REMOTE_LOG_SELECTIONS})) {
87 my $selections = $ENV{OBJECT_REMOTE_LOG_SELECTIONS};
88 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_SELECTIONS} = "' . $selections . "\";\n";
89}
837cfa50 90if (defined($ENV{OBJECT_REMOTE_LOG_FORWARDING})) {
91 my $forwarding = $ENV{OBJECT_REMOTE_LOG_FORWARDING};
92 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORWARDING} = "' . $forwarding . "\";\n";
93}
302ecfbf 94if (defined($ENV{OBJECT_REMOTE_PERL_BIN})) {
95 my $perl_bin = $ENV{OBJECT_REMOTE_PERL_BIN};
96 $env_pass .= '$ENV{OBJECT_REMOTE_PERL_BIN} = "' . $perl_bin . "\";\n";
97}
837cfa50 98
eb49c7df 99
4a9fa1a5 100
eee9a548 101my $start = stripspace <<'END_START';
102 # This chunk of stuff was generated by Object::Remote::FatNode. To find
103 # the original file's code, look for the end of this BEGIN block or the
104 # string 'FATPACK'
105 BEGIN {
1ef3d225 106 my (%fatpacked,%fatpacked_extra);
eee9a548 107END_START
f6ac5927 108
cc62b744 109$start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
f6ac5927 110
eee9a548 111my $end = stripspace <<'END_END';
1ef3d225 112 s/^ //mg for values %fatpacked, values %fatpacked_extra;
eee9a548 113
1ef3d225 114 sub load_from_hash {
115 if (my $fat = $_[0]->{$_[1]}) {
f6ac5927 116 if ($exclude{$_[1]}) {
117 warn "Will not pre-load '$_[1]'";
118 return undef;
119 }
120
cc62b744 121 #warn "Handling $_[1]";
eee9a548 122 open my $fh, '<', \$fat;
123 return $fh;
124 }
1900601d 125
8c81a0e5 126 #Uncomment this to find brokenness
127 #warn "Missing $_[1]";
f6ac5927 128 return;
1ef3d225 129 }
130
131 unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
132 push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
eee9a548 133
134 } # END OF FATPACK CODE
135
136 use strictures 1;
137 use Object::Remote::Node;
f129bfaf 138
139 unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
140 Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);
141 }
142
eee9a548 143END_END
144
145my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
15b847bb 146 @non_core_non_arch, @core_non_arch;
eee9a548 147
1ef3d225 148sub generate_fatpack_hash {
149 my ($hash_name, $orig) = @_;
150 (my $stub = $orig) =~ s/\.pm$//;
eee9a548 151 my $name = uc join '_', split '/', $stub;
1ef3d225 152 my $data = $files{$orig} or die $orig; $data =~ s/^/ /mg;
3ab6f7e2 153 $data .= "\n" unless $data =~ m/\n$/;
154 my $ret = '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
155 .qq!${data}${name}\n!;
156# warn $ret;
157 return $ret;
1ef3d225 158}
159
160my @segments = (
15b847bb 161 map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @non_core_non_arch),
162 map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @core_non_arch),
1ef3d225 163);
eee9a548 164
4a9fa1a5 165our $DATA = join "\n", $start, $env_pass, @segments, $end;
eee9a548 166
1671;