all tests run at trace log level with a null log output; new tests for watchdog,...
[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 #used by t/watchdog_fatnode 
8 our $INHIBIT_RUN_NODE = 0; 
9
10 sub stripspace {
11   my ($text) = @_;
12   $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
13   $text;
14 }
15
16 my %maybe_libs = map +($_ => 1), grep defined, (values %Config, '.');
17
18 my @extra_libs = grep not(ref($_) or $maybe_libs{$_}), @INC;
19
20 my $extra_libs = join '', map "  -I$_\n", @extra_libs;
21
22 my $command = qq(
23   $^X
24   $extra_libs
25   -mObject::Remote
26   -mObject::Remote::Connector::STDIO
27   -mCPS::Future
28   -mMRO::Compat
29   -mClass::C3
30   -mClass::C3::next
31   -mAlgorithm::C3
32   -mObject::Remote::ModuleLoader
33   -mObject::Remote::Node
34   -mMethod::Generate::BuildAll
35   -mMethod::Generate::DemolishAll
36   -mJSON::PP
37   -e 'print join "\\n", \%INC'
38 );
39
40 $command =~ s/\n/ /g;
41
42 chomp(my @inc = qx($command));
43
44 my %mods = reverse @inc;
45
46 my @non_core = grep +(
47   not (
48     /^\Q$Config{privlibexp}/ or /^\Q$Config{archlibexp}/
49   )
50 ), keys %mods;
51
52 my @core = grep +(
53   /^\Q$Config{privlibexp}/
54 ), keys %mods;
55
56 my $env_pass = '';
57 if (defined($ENV{OBJECT_REMOTE_LOG_LEVEL})) {
58   my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
59   return unless $level =~ /^\w+$/;
60   $env_pass = '$ENV{OBJECT_REMOTE_LOG_LEVEL} = "' . $level . "\";\n";
61 }
62
63 my $start = stripspace <<'END_START';
64   # This chunk of stuff was generated by Object::Remote::FatNode. To find
65   # the original file's code, look for the end of this BEGIN block or the
66   # string 'FATPACK'
67   BEGIN {
68   my (%fatpacked,%fatpacked_extra);
69 END_START
70 my $end = stripspace <<'END_END';
71   s/^  //mg for values %fatpacked, values %fatpacked_extra;
72
73   sub load_from_hash {
74     if (my $fat = $_[0]->{$_[1]}) {
75       open my $fh, '<', \$fat;
76       return $fh;
77     }
78     #Uncomment this to find brokenness
79     #warn "Missing $_[1]";
80     return
81   }
82
83   unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
84   push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
85
86   } # END OF FATPACK CODE
87
88   use strictures 1;
89   use Object::Remote::Node;
90   
91   unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
92     Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);    
93   }
94   
95 END_END
96
97 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
98               @non_core, @core;
99
100 sub generate_fatpack_hash {
101   my ($hash_name, $orig) = @_;
102   (my $stub = $orig) =~ s/\.pm$//;
103   my $name = uc join '_', split '/', $stub;
104   my $data = $files{$orig} or die $orig; $data =~ s/^/  /mg;
105   return '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
106   .qq!${data}${name}\n!;
107 }
108
109 my @segments = (
110     map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @non_core),
111     map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @core),
112 );
113
114 our $DATA = join "\n", $start, $env_pass, @segments, $end;
115
116 1;