MPMinus/Helper/Skel.pm untested

filter: Statementstmt 0.0% 0 / 202 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 55 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 357 SCAR 69.0
CC
31
Cov
0%
download
156.0
new
20.0
extract
20.0
CRAP 992.0
1 package MPMinus::Helper::Skel; # $Id: Skel.pm 202 2013-07-25 19:28:15Z minus $
2 use strict;
3
4 =head1 NAME
5
6 MPMinus::Helper::Skel - MPMinus project skeleton
7
8 =head1 VERSION
9
10 Version 1.01
11
12 =head1 SYNOPSIS
13
14 use MPMinus::Helper::Skel;
15
16 my $skel = new MPMinus::Helper::Skel( -c => $c );
17
18 =head1 DESCRIPTION
19
20 MPMinus project skeleton
21
22 =head2 METHODS
23
24 =over 8
25
26 =item B<new>
27
28 my $skel = new MPMinus::Helper::Skel(
29 -c => $c,
30 -sharedir => '/foo/bar/baz',
31 -url => 'http://search.cpan.org/src/ABALAMA/MPMinus-[VERSION]/src/mpminus-skel.tar.gz',
32 );
33
34 Creating Skel object
35
36 =item B<build>
37
38 my $status = $skel->build( $path );
39
40 Building skeleton files with $path using it as temporary folder for archive. This method returns
41 summary status of download and extract calls
42
43 =item B<checkstatus>
44
45 my $status = $skel->checkstatus;
46
47 Check skeleton status. True - OK, False - need downloading and extracting
48
49
50 =item B<download>
51
52 my $status = $skel->download( $path );
53
54 Download Skel's archive to $path
55
56 =item B<extract>
57
58 my $status = $skel->extract( $path );
59
60 Extract files from downloaded Skel's archive in $path to share directory
61
62 =back
63
64 =head1 AUTHOR
65
66 Serz Minus (Lepenkov Sergey) L<http://serzik.ru> E<lt>minus@mail333.comE<gt>
67
68 =head1 COPYRIGHT
69
70 Copyright (C) 1998-2013 D&D Corporation. All Rights Reserved
71
72 =head1 LICENSE
73
74 This program is free software: you can redistribute it and/or modify
75 it under the terms of the GNU General Public License as published by
76 the Free Software Foundation, either version 3 of the License, or
77 (at your option) any later version.
78
79 This program is distributed in the hope that it will be useful,
80 but WITHOUT ANY WARRANTY; without even the implied warranty of
81 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
82 GNU General Public License for more details.
83
84 See C<LICENSE> file
85
86 =cut
87
88 use vars qw($VERSION);
89 $VERSION = '1.01';
90
91 use constant {
92 SHAREDIR => 'mpminus',
93 SKELDIR => 'skel-[VERSION]',
94 SKELFILE => 'mpminus-skel-[VERSION].tar.gz',
95 SKELMD5 => 'mpminus-skel-[VERSION].md5',
96 SKELSHA1 => 'mpminus-skel-[VERSION].sha1',
97 SUMMARY => 'SUMMARY',
98 MIRRORS => [qw{
99 http://dist.suffit.org
100 http://dist.mnshome.info
101 }],
102 };
103
104 use CTK::Util qw/ :BASE /;
105 use Archive::Extract;
106 use Digest;
107 use URI;
108 use ExtUtils::Manifest qw/ maniread manicheck /;
109 use Cwd;
110 use File::Copy::Recursive qw(dircopy dirmove);
111
112 sub new {
113 my $class = shift;
114 my @in = read_attributes(
115 [
116 [qw/ C CTK /], # 0
117 [qw/ S SD SHARE SHAREDIR /], # 1
118 [qw/ URI URL ADDR ADDRESS LINK /], # 2
119 ],
120 ,@_);
121
122 my $mpmver = MPMinus::Helper::Skel::Backward->get_version();
123 my $rplc = {
124 VERSION => $mpmver,
125 };
126
127 my $c = $in[0];
128 croak("CTK object undefined") unless $c && ref($c) eq 'CTK';
129 my $sd = $in[1] || catdir($c->sharedir, SHAREDIR);
130 carp("Can't prepare directory \"$sd\"") unless -e $sd or preparedir($sd);
131 my $skeldir = catdir($sd, dformat(SKELDIR, $rplc));
132 carp("Can't prepare directory \"$skeldir\"") unless -e $skeldir or preparedir($skeldir);
133 my $url = $in[2] || '';
134
135 my @mirrors = (); push(@mirrors, dformat($_, $rplc)) for (@{(MIRRORS)});
136
137 return bless({
138 c => $c,
139 sharedir => $sd,
140 skeldir => $skeldir,
141 tmpdir => catfile($c->tmpdir),
142 mirrors => [ @mirrors ],
143 ls => _get_ls($skeldir),
144 skelfile => dformat(SKELFILE, $rplc),
145 md5file => dformat(SKELMD5, $rplc),
146 sha1file => dformat(SKELSHA1, $rplc),
147 url => dformat($url, $rplc),
148 rplc => $rplc,
149 version => $mpmver,
150 }, $class);
151 }
152 sub download {
153 my $self = shift;
154 my $path = shift || $self->{tmpdir};
155 my $c = $self->{c};
156 my $arcfile = catfile($path, $self->{skelfile});
157 my $url = $self->{url};
158
159 if (1
160 && $url
161 && $c->fetch(
162 -connect => { method => 'GET', url => $url },
163 -protocol => 'http',
164 -dir => $path,
165 -file => $self->{skelfile},
166 )
167 && -e $arcfile
168 ) {
169 return 1;
170 }
171
172 my @mirrors = @{($self->{mirrors})};
173 my $getstat;
174 my $f;
175
176 # Ñêà÷èâàåì ñàì ôàéë
177 $getstat = 0;
178 $f = $self->{skelfile};
179 foreach (@mirrors) {
180 $getstat = $c->fetch(
181 -connect => {
182 method => 'GET',
183 url => _get_uri($_, $f),
184 },
185 -protocol => 'http',
186 -dir => $path,
187 -file => $f,
188 );
189 last if $getstat && -e $arcfile;
190 }
191 unless ($getstat) {
192 carp "File can't fetching \"$f\" on mirrors: ".join("; ",@mirrors);
193 return 0;
194 }
195
196 # Êîíòðîëüíûå ñóììû
197 my $my_md5 = _md5($arcfile);
198 my $my_sha1 = _sha1($arcfile);
199
200 # Ñêà÷èâàåì ôàéë ñóììû md5
201 my $md5summ = '';
202 $f = $self->{md5file};
203 foreach (@mirrors) {
204 $md5summ = $c->fetch(
205 -connect => {
206 method => 'GET',
207 url => _get_uri($_, $f),
208 },
209 -protocol => 'http',
210 );
211 last if $md5summ;
212 }
213 if ($md5summ) {
214 # Ïðîâåðêà ñóììû md5
215 $md5summ = $1 if $md5summ =~ /([a-fA-F0-9]+)/s;
216 if ($md5summ ne $my_md5) {
217 carp "Checksums MD5 for file \"$arcfile\" don't match! Expected: $my_md5; got: $md5summ";
218 unlink $arcfile;
219 return 0;
220 }
221 } else {
222 carp "File can't fetching \"$f\" on mirrors: ".join("; ",@mirrors);
223 }
224
225 # Ñêà÷èâàåì ôàéë ñóììû sha1
226 my $sha1summ = '';
227 $f = $self->{sha1file};
228 foreach (@mirrors) {
229 $sha1summ = $c->fetch(
230 -connect => {
231 method => 'GET',
232 url => _get_uri($_, $f),
233 },
234 -protocol => 'http',
235 );
236 last if $sha1summ;
237 }
238 if ($sha1summ) {
239 # Ïðîâåðêà ñóììû sha1
240 $sha1summ = $1 if $sha1summ =~ /([a-fA-F0-9]+)/s;
241 if ($sha1summ ne $my_sha1) {
242 carp "Checksums SHA1 for file \"$arcfile\" don't match! Expected: $my_sha1; got: $sha1summ";
243 unlink $arcfile;
244 return 0;
245 }
246 } else {
247 carp "File can't fetching \"$f\" on mirrors: ".join("; ",@mirrors);
248 }
249
250 return 1;
251 }
252 sub extract {
253 my $self = shift;
254 my $path = shift || $self->{tmpdir};
255 my $c = $self->{c};
256 my $arcfile = catfile($path, $self->{skelfile});
257 my $skeldir = $self->{skeldir};
258 my $sharedir = $self->{sharedir};
259
260 unless (-e $arcfile) {
261 carp "File not found: \"$arcfile\"";
262 return 0;
263 }
264
265 my $ae = Archive::Extract->new( archive => $arcfile );
266 return 0 unless $ae;
267
268 my $ok = $ae->extract( to => $sharedir );
269
270 my $fromd = catdir($sharedir, 'skel');
271 unless (dirmove($fromd, $skeldir)) {
272 carp "Can't move \"$fromd\" to \"$skeldir\"";
273 $ok = 0;
274 }
275
276 return $ok;
277 }
278 sub checkstatus {
279 my $self = shift;
280 return 1 if $self->{ls} && @{($self->{ls})};
281 return 0;
282 }
283 sub build {
284 my $self = shift;
285 my $path = shift || $self->{tmpdir};
286 my $skeldir = $self->{skeldir};
287 my $arcfile = catfile($path, $self->{skelfile});
288
289 my $st = $self->download($path) && $self->extract($path) ? 1 : 0;
290 $self->{ls} = _get_ls($skeldir) if $st;
291
292 unlink $arcfile if -e $arcfile;
293
294 return $self->checkstatus;
295 }
296 sub readmanifest {
297 # ×òåíèå MANIFEST-ôàéëà è âîçâðàò ïðî÷òåíîãî ðåçóëüòàòà â âèäå õýøà ñ êëþ÷åì = çàïèñþ MANIFEST
298 # è çíà÷åíèåì = ïóòè äî ôàéëà
299 my $self = shift;
300 my $cdir = getcwd();
301 my $skeldir = $self->{skeldir};
302
303 chdir $skeldir;
304
305 my @missed = manicheck();
306 if (@missed) {
307 carp "Files are missing in your kit: ", join ", ", @missed;
308 } # else { say "Looks good" }
309
310 my $files = maniread();
311 $files = {} unless $files && ref($files) eq 'HASH';
312 my %outfiles;
313 foreach my $k (keys %$files) {
314 $outfiles{$k} = catfile($skeldir,$k) unless grep {$k && $_ && $_ eq $k} @missed;
315 }
316
317 chdir $cdir;
318
319 return {%outfiles};
320 }
321 sub readsummary {
322 # ×òåíèå SUMMARY-ôàéëà è âîçâðàò ïðî÷òåíîãî ðåçóëüòàòà
323 my $self = shift;
324 my $cdir = getcwd();
325 my $skeldir = $self->{skeldir};
326
327 chdir $skeldir;
328
329 my $summary = fload(SUMMARY);
330 unless ($summary) {
331 carp "Can't load ".SUMMARY." file";
332 $summary = '';
333 }
334
335 chdir $cdir;
336
337 return $summary;
338 }
339
340 sub _md5 { _digest( "MD5", @_ ) }
341 sub _sha1 { _digest( "SHA-1", @_ ) }
342 sub _digest {
343 my $s = shift;
344 my $f = shift;
345 my $d = Digest->new($s);
346
347 my $FHD;
348 open($FHD, $f) or croak "Can't open \"$f\": $!";
349 binmode($FHD);
350 my $summ = $d->addfile($FHD)->hexdigest;
351 close $FHD;
352 return $summ;
353 }
354 sub _get_uri {
355 my $mirror = shift;
356 my $file = shift;
357 my $u = URI->new($mirror);
358 my $path = $u->path;
359 if ($path) {
360 if ($path eq '/') {
361 $u->path($file);
362 } else {
363 my $newpath = $path.'/'.$file;
364 $newpath =~ s/\/{2,}/\//g;
365 $u->path($newpath);
366 }
367 } else {
368 $u->path($file);
369 }
370
371 return $u->as_string;
372 }
373 sub _get_ls {
374 my $p = shift;
375 return [] unless $p && -e $p;
376 return [ grep {$_ !~ m/^\.+$/} (ls($p)) ]
377 }
378
379 1;
380
381 package # hide me from PAUSE
382 MPMinus::Helper::Skel::Backward;
383
384 use CTK::Util qw/ :API /;
385 use ExtUtils::MM;
386
387 sub get_version {
388 my $mpminusf = '';
389 foreach (@INC) {
390 my $f = catfile($_,"MPMinus.pm");
391 if (-e $f) {
392 $mpminusf = $f;
393 last;
394 }
395 }
396 my $v = ExtUtils::MM->parse_version($mpminusf);
397 croak "Method ExtUtils::MM->parse_version returns false!" unless $v;
398 return $v;
399 }
400 sub get_make {
401 return ExtUtils::MM->new->make();
402 }
403
404 1;
405
406 __END__