MLite/Helper/Skel.pm untested

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