better lookup method in log any injector; %n adds newline in log format string
[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
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}
80
4a9fa1a5 81
eee9a548 82my $start = stripspace <<'END_START';
83 # This chunk of stuff was generated by Object::Remote::FatNode. To find
84 # the original file's code, look for the end of this BEGIN block or the
85 # string 'FATPACK'
86 BEGIN {
1ef3d225 87 my (%fatpacked,%fatpacked_extra);
eee9a548 88END_START
f6ac5927 89
cc62b744 90$start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
f6ac5927 91
eee9a548 92my $end = stripspace <<'END_END';
1ef3d225 93 s/^ //mg for values %fatpacked, values %fatpacked_extra;
eee9a548 94
1ef3d225 95 sub load_from_hash {
96 if (my $fat = $_[0]->{$_[1]}) {
f6ac5927 97 if ($exclude{$_[1]}) {
98 warn "Will not pre-load '$_[1]'";
99 return undef;
100 }
101
cc62b744 102 #warn "Handling $_[1]";
eee9a548 103 open my $fh, '<', \$fat;
104 return $fh;
105 }
1900601d 106
8c81a0e5 107 #Uncomment this to find brokenness
108 #warn "Missing $_[1]";
f6ac5927 109 return;
1ef3d225 110 }
111
112 unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
113 push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
eee9a548 114
115 } # END OF FATPACK CODE
116
117 use strictures 1;
118 use Object::Remote::Node;
f129bfaf 119
120 unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
121 Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);
122 }
123
eee9a548 124END_END
125
126my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
f6ac5927 127 @before_inc, @after_inc;
eee9a548 128
1ef3d225 129sub generate_fatpack_hash {
130 my ($hash_name, $orig) = @_;
131 (my $stub = $orig) =~ s/\.pm$//;
eee9a548 132 my $name = uc join '_', split '/', $stub;
1ef3d225 133 my $data = $files{$orig} or die $orig; $data =~ s/^/ /mg;
134 return '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
eee9a548 135 .qq!${data}${name}\n!;
1ef3d225 136}
137
138my @segments = (
f6ac5927 139 map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @before_inc),
140 map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @after_inc),
1ef3d225 141);
eee9a548 142
4a9fa1a5 143our $DATA = join "\n", $start, $env_pass, @segments, $end;
eee9a548 144
1451;