734929b5534fc5687758966997ca3fd011a89dbf
[scpubgit/Object-Remote.git] / lib / Object / Remote / FatNode.pm
1 package Object::Remote::FatNode;
2
3 use strictures 1;
4 use Config;
5 use B qw(perlstring);
6
7 my @exclude_mods = qw(XSLoader.pm DynaLoader.pm);
8 #used by t/watchdog_fatnode 
9 our $INHIBIT_RUN_NODE = 0;
10
11 sub stripspace {
12   my ($text) = @_;
13   $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
14   $text;
15 }
16
17 my %maybe_libs = map +($_ => 1), grep defined, (values %Config, '.');
18
19 my @extra_libs = grep not(ref($_) or $maybe_libs{$_}), @INC;
20
21 my $extra_libs = join '', map "  -I$_\n", @extra_libs;
22
23 my $command = qq(
24   $^X
25   $extra_libs
26   -mObject::Remote
27   -mObject::Remote::Connector::STDIO
28   -mCPS::Future
29   -mMRO::Compat
30   -mClass::C3
31   -mClass::C3::next
32   -mAlgorithm::C3
33   -mObject::Remote::ModuleLoader
34   -mObject::Remote::Node
35   -mMethod::Generate::BuildAll
36   -mMethod::Generate::DemolishAll
37   -mJSON::PP
38   -e 'print join "\\n", \%INC'
39 );
40
41 $command =~ s/\n/ /g;
42
43 chomp(my @inc = qx($command));
44
45 my %exclude = map { $_ => 1 } @exclude_mods; 
46 my %mods = reverse @inc;
47
48 foreach(keys(%mods)) {
49   if ($exclude{ $mods{$_} }) {
50     delete($mods{$_});    
51   }
52 }
53
54 sub filter_not_core {
55   not (
56     /^\Q$Config{privlibexp}/ or /^\Q$Config{archlibexp}/
57   )        
58 }
59
60 my @file_names = keys %mods;
61 my @before_inc = grep { filter_not_core() } @file_names;
62 my @after_inc;
63
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
67 my $env_pass = '';
68 if (defined($ENV{OBJECT_REMOTE_LOG_LEVEL})) {
69   my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
70   $env_pass .= '$ENV{OBJECT_REMOTE_LOG_LEVEL} = "' . $level . "\";\n";
71 }
72 if (defined($ENV{OBJECT_REMOTE_LOG_FORMAT})) {
73   my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT};
74   $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORMAT} = "' . $format . "\";\n";
75 }
76 if (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 if (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 }
84 if (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 }
88
89
90
91 my $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 {
96   my (%fatpacked,%fatpacked_extra);
97 END_START
98
99 $start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
100
101 my $end = stripspace <<'END_END';
102   s/^  //mg for values %fatpacked, values %fatpacked_extra;
103
104   sub load_from_hash {
105     if (my $fat = $_[0]->{$_[1]}) {
106       if ($exclude{$_[1]}) {
107         warn "Will not pre-load '$_[1]'";
108         return undef; 
109       }
110  
111       #warn "Handling $_[1]";
112       open my $fh, '<', \$fat;
113       return $fh;
114     }
115     
116     #Uncomment this to find brokenness
117     #warn "Missing $_[1]";
118     return;
119   }
120
121   unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
122   push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
123
124   } # END OF FATPACK CODE
125
126   use strictures 1;
127   use Object::Remote::Node;
128   
129   unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
130     Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);    
131   }
132   
133 END_END
134
135 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
136               @before_inc, @after_inc;
137
138 sub generate_fatpack_hash {
139   my ($hash_name, $orig) = @_;
140   (my $stub = $orig) =~ s/\.pm$//;
141   my $name = uc join '_', split '/', $stub;
142   my $data = $files{$orig} or die $orig; $data =~ s/^/  /mg;
143   $data .= "\n" unless $data =~ m/\n$/;
144   my $ret = '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
145     .qq!${data}${name}\n!;
146 #  warn $ret;
147   return $ret;
148 }
149
150 my @segments = (
151   map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @before_inc),
152   map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @after_inc),
153 );
154
155 our $DATA = join "\n", $start, $env_pass, @segments, $end;
156
157 1;