Build/PPK/Bundle.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 178 CC 10 SCAR 19.4
CC
10
Cov
0%
assemble
20.0
new
12.0
check
12.0
CRAP 6.9
1 package Build::PPK::Bundle;
2  
3 # Copyright (c) 2018, cPanel, L.L.C.
4 # All rights reserved.
5 # http://cpanel.net/
6 #
7 # This is free software; you can redistribute it and/or modify it under the same
8 # terms as Perl itself. See the LICENSE file for further details.
9  
10 use strict;
11 use warnings;
12  
13 use Fcntl ();
14 use MIME::Base64 ();
15  
16 use Filesys::POSIX ();
17 use Filesys::POSIX::IO::Handle ();
18 use Filesys::POSIX::Mem ();
19 use Filesys::POSIX::Path ();
20 use Filesys::POSIX::Extensions ();
21 use Filesys::POSIX::Userland::Tar ();
22  
23 use Build::PPK::Exec ();
24 use Build::PPK::Pipeline ();
25  
26 use Carp;
27  
28 sub new {
29 my ( $class, $main, $opts ) = @_;
30  
31 confess('No main entry point defined') unless $main;
32 confess('No output file specified') unless $opts->{'output'};
33  
34 return bless {
35 'main' => $main,
36 'output' => $opts->{'output'},
37 'header' => $opts->{'header'},
38 'desc' => $opts->{'desc'},
39 'modules' => {},
40 'libdirs' => {}
41 }, $class;
42 }
43  
44 sub _module_path {
45 my ($module) = @_;
46 my $pat = qr/[a-z0-9_]*/i;
47  
48 confess('Invalid module name') unless $module =~ /^$pat(::$pat)*$/i;
49  
50 my @path_components = split /::/, $module;
51 $path_components[-1] .= '.pm';
52  
53 return Filesys::POSIX::Path->full( join( '/', @path_components ) );
54 }
55  
56 sub add_module {
57 my ( $self, $file, $name ) = @_;
58 my $short_module_path = _module_path($name);
59 my $bundle_module_path = "lib/$short_module_path";
60  
61 my $libdir = Filesys::POSIX::Path->full($file);
62 $libdir =~ s/\/$short_module_path$//;
63  
64 $self->{'libdirs'}->{$libdir} = 1;
65 $self->{'modules'}->{$bundle_module_path} = $file;
66 }
67  
68 sub add_dist {
69 my ( $self, $dist ) = @_;
70  
71 confess("Cannot add unprepared dist $dist->{'path'} to bundle") unless $dist->prepared;
72  
73 foreach my $path ( @{ $dist->modules } ) {
74 my $file = "$dist->{'basedir'}/$path";
75  
76 $self->{'modules'}->{$path} = $file;
77 }
78  
79 $self->{'libdirs'}->{ $dist->libdir } = 1;
80 }
81  
82 sub libdirs {
83 my ($self) = @_;
84  
85 return sort keys %{ $self->{'libdirs'} };
86 }
87  
88 sub check {
89 my ($self) = @_;
90  
91 my @dirs = @INC;
92 push @dirs, $self->libdirs;
93  
94 local $ENV{'PERL5LIB'} = join( ':', @dirs );
95  
96 confess("Main entry point $self->{'main'} not found") unless -f $self->{'main'};
97  
98 unless ( Build::PPK::Exec->silent( $^X, '-c', $self->{'main'} ) == 0 ) {
99 confess("Errors while checking $self->{'main'}: $@");
100 }
101 }
102  
103 sub prepare {
104 my ($self) = @_;
105  
106 my $fs = Filesys::POSIX->new(
107 Filesys::POSIX::Mem->new,
108 'noatime' => 1
109 );
110  
111 foreach my $dir (qw(lib scripts)) {
112 $fs->mkdir($dir);
113 }
114  
115 #
116 # Map the given main entry point script into scripts/main.pl.
117 #
118 $fs->map( $self->{'main'}, 'scripts/main.pl' );
119  
120 #
121 # Map each module dependency into lib/ as appropriate.
122 #
123 foreach my $bundle_module_path ( keys %{ $self->{'modules'} } ) {
124 my $file = $self->{'modules'}->{$bundle_module_path};
125 my $path = Filesys::POSIX::Path->new($bundle_module_path);
126  
127 $fs->mkpath( $path->dirname );
128 $fs->map( $file, $path->full );
129 }
130  
131 return $fs;
132 }
133  
134 sub assemble {
135 my ( $self, $fs ) = @_;
136 my $stub = File::ShareDir::dist_file( 'Build-PPK', 'stub.pl' );
137  
138 my $output_fh;
139  
140 unless ( sysopen( $output_fh, $self->{'output'}, &Fcntl::O_CREAT | &Fcntl::O_TRUNC | &Fcntl::O_WRONLY, 0755 ) ) {
141 confess("Unable to open $self->{'output'} for writing: $!");
142 }
143  
144 #
145 # Print the Perl shebang.
146 #
147 print {$output_fh} "#! /usr/bin/perl\n";
148  
149 #
150 # Check for a header file to include.
151 #
152 if ( $self->{'header'} ) {
153 open( my $header_fh, '<', $self->{'header'} ) or confess("Unable to open $self->{'header'} for reading: $!");
154  
155 while ( my $line = readline($header_fh) ) {
156 chomp $line;
157 $line =~ s/\$Desc\$/\$Desc: $self->{'desc'}\$/g if $self->{'desc'};
158 print {$output_fh} "$line\n";
159 }
160  
161 close $header_fh;
162 }
163  
164 #
165 # Add a newline for style's sake.
166 #
167 print {$output_fh} "\n";
168  
169 #
170 # Then, copy the self-executing stub into the output file.
171 #
172 open( my $stub_fh, '<', $stub ) or confess("Unable to open stub $stub for reading: $!");
173  
174 while ( my $len = read( $stub_fh, my $buf, 4096 ) ) {
175 print {$output_fh} $buf;
176 }
177  
178 close $stub_fh;
179  
180 #
181 # Next, tar up the prepared filesystem, and pass it through gzip, then
182 # base64 encoding, while appending the result to the end of the output
183 # file. There's no need to read the end of this pipe, as it is all
184 # being dumped to the output file handle anyway.
185 #
186 my $pipeline = Build::PPK::Pipeline->open(
187 sub {
188 $fs->tar( Filesys::POSIX::IO::Handle->new( \*STDOUT ), '.' );
189 },
190  
191 sub {
192 exec 'gzip' or confess("Unable to exec() gzip: $!");
193 },
194  
195 sub {
196 while ( my $len = read( STDIN, my $buf, 4047 ) ) {
197 print {$output_fh} MIME::Base64::encode_base64($buf);
198 }
199 }
200 );
201  
202 $pipeline->close;
203  
204 close $output_fh;
205 }
206  
207 1;