Build/PPK/Dist.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 232 CC 17 SCAR 26.1
CC
17
Cov
0%
_find_dist_base
30.0
unpack
30.0
cleanup
20.0
CRAP 13.7
1 package Build::PPK::Dist;
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 POSIX ();
14  
15 use Cwd ();
16 use File::Temp ();
17 use File::Find ();
18  
19 use Build::PPK::Exec ();
20  
21 use Carp ('confess');
22  
23 sub new {
24 my ( $class, $path ) = @_;
25  
26 return bless {
27 'unpacked' => 0,
28 'prepared' => 0,
29 'path' => Cwd::realpath($path),
30 'dirs' => []
31 }, $class;
32 }
33  
34 sub DESTROY {
35 shift->cleanup;
36 }
37  
38 sub pushd {
39 my ( $self, $dir ) = @_;
40 my $cwd = Cwd::getcwd() or confess("Unable to get current working directory: $!");
41  
42 push @{ $self->{'dirs'} }, $cwd;
43  
44 chdir($dir) or confess("Unable to change current working directory to $dir: $!");
45  
46 return $dir;
47 }
48  
49 sub popd {
50 my ($self) = @_;
51 return unless @{ $self->{'dirs'} };
52  
53 my $dir = pop @{ $self->{'dirs'} };
54 chdir($dir) or confess("Unable to change current working directory to $dir: $!");
55 return $dir;
56 }
57  
58 sub _find_dist_base {
59 my ( $self, $distdir ) = @_;
60 my @dirs;
61  
62 confess("$distdir is not a directory") unless -d $distdir;
63  
64 push @dirs, $distdir;
65  
66 while ( defined( my $dir = pop @dirs ) ) {
67 opendir( my $dh, $dir ) or confess("Unable to opendir() on $dir: $!");
68  
69 while ( defined( my $item = readdir($dh) ) ) {
70 next if $item eq '.' || $item eq '..';
71  
72 my $path = "$dir/$item";
73 stat $path;
74  
75 if ( ( -d _ && $item eq 'lib' ) || ( -f _ && $item =~ /^(?:Makefile\.PL|MANIFEST)$/ ) ) {
76 closedir $dh;
77 return $dir;
78 }
79 elsif ( -d _ ) {
80 push @dirs, $path;
81 }
82 }
83  
84 closedir $dh;
85 }
86  
87 confess("Could not find lib/, Makefile.PL or MANIFEST anywhere within $self->{'path'}");
88 }
89  
90 sub unpack {
91 my ($self) = @_;
92  
93 return $self if $self->{'unpacked'};
94  
95 if ( -d $self->{'path'} ) {
96 $self->{'basedir'} = $self->_find_dist_base( $self->{'path'} );
97  
98 return $self;
99 }
100  
101 $self->{'tmpdir'} = File::Temp::mkdtemp('/tmp/.ppk-XXXXXX') or confess("Failed mkdtemp(): $!");
102  
103 pipe my ( $error_out, $error_in ) or confess("Unable to pipe(): $!");
104 my $pid = fork();
105  
106 if ( $pid == 0 ) {
107 close STDIN;
108 close STDOUT;
109  
110 POSIX::dup2( fileno($error_in), fileno(STDERR) ) or confess("Unable to dup2(): $!");
111  
112 chdir $self->{'tmpdir'} or confess("Unable to chdir() to $self->{'tmpdir'}: $!");
113 exec qw(tar pzxf), $self->{'path'};
114 }
115 elsif ( !defined($pid) ) {
116 confess("Unable to fork(): $!");
117 }
118  
119 waitpid( $pid, 0 );
120  
121 $self->{'basedir'} = $self->_find_dist_base( $self->{'tmpdir'} );
122 $self->{'unpacked'} = 1;
123  
124 return $self;
125 }
126  
127 sub build {
128 my ($self) = @_;
129  
130 $self->pushd( $self->{'basedir'} );
131  
132 Build::PPK::Exec->silent( $^X, 'Makefile.PL' ) == 0 or confess("Unable to run Makefile.PL: $@");
133 Build::PPK::Exec->silent('make') == 0 or confess("Unable to build distribution with GNU make: $@");
134  
135 $self->popd;
136  
137 return $self;
138 }
139  
140 sub prepare {
141 my ($self) = @_;
142  
143 return $self if $self->{'prepared'};
144  
145 $self->unpack;
146  
147 unless ( -d "$self->{'basedir'}/lib" ) {
148 $self->build;
149 $self->{'basedir'} .= '/blib';
150 }
151  
152 $self->{'libdir'} = "$self->{'basedir'}/lib";
153 $self->{'prepared'} = 1;
154  
155 return $self;
156 }
157  
158 sub prepared {
159 return shift->{'prepared'};
160 }
161  
162 sub libdir {
163 return shift->{'libdir'};
164 }
165  
166 sub modules {
167 my ($self) = @_;
168 my @modules;
169  
170 $self->pushd( $self->{'basedir'} );
171  
172 File::Find::find(
173 {
174 'no_chdir' => 1,
175 'wanted' => sub {
176 return unless -f $File::Find::name;
177 return unless $File::Find::name =~ /\.pm$/;
178  
179 push @modules, $File::Find::name;
180 }
181 },
182 'lib'
183 );
184  
185 $self->popd;
186  
187 return \@modules;
188 }
189  
190 sub cleanup {
191 my ($self) = @_;
192  
193 return unless $self->{'tmpdir'};
194 return unless -d $self->{'tmpdir'};
195  
196 File::Find::finddepth(
197 {
198 'no_chdir' => 1,
199 'wanted' => sub {
200 if ( -d $File::Find::name ) {
201 rmdir $File::Find::name;
202 }
203 else {
204 unlink $File::Find::name;
205 }
206 }
207 },
208 $self->{'tmpdir'}
209 );
210  
211 delete $self->{'tmpdir'};
212 }
213  
214 1;