Qgoda/Repository/Fetcher/File.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 69 CC 5 SCAR 18.9
CC
5
Cov
0%
fetch
12.0
_extractArchive
12.0
BEGIN
2.0
CRAP 6.6
1 #! /bin/false
2  
3 # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15  
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18  
19 package Qgoda::Repository::Fetcher::File;
20 $Qgoda::Repository::Fetcher::File::VERSION = 'v0.9.2';
21 use strict;
22  
23 use Locale::TextDomain qw(qgoda);
24 use File::Copy::Recursive qw(dircopy);
25 use File::Spec;
26 use Archive::Extract;
27  
28 use Qgoda;
29  
30 use base qw(Qgoda::Repository::Fetcher);
31  
32 sub fetch {
33 my ($self, $uri, $destination) = @_;
34  
35 my $logger = Qgoda->new->logger;
36  
37 my $path = $uri->file;
38 if (-d $path) {
39 if (!dircopy $path, $destination) {
40 $logger->fatal(__x("error copying '{src}' to '{dest}': {error}",
41 src => $path, dest => $destination,
42 error => $!));
43 }
44  
45 return $destination;
46 };
47  
48 return $self->_extractArchive($path, $destination);
49 }
50  
51 sub _extractArchive {
52 my ($self, $path, $destination) = @_;
53  
54 my $logger = Qgoda->new->logger;
55  
56 my $ae = Archive::Extract->new(archive => $path);
57 $ae->extract(to => $destination)
58 or $logger->fatal(__x("error extracing '{archive}' to"
59 ." '{destination}': {error}"),
60 archive => $path,
61 destination => $destination,
62 error => $ae->error);
63  
64 opendir my $dh, $destination
65 or $logger->fatal(__x("error reading directory '{directory}': {error}",
66 directory => $destination,
67 error => $ae->error));
68  
69 my @contents = sort File::Spec->no_upwards(readdir $dh)
70 or $logger->fatal(__x("archive '{archive} is empty",
71 archive => $path));
72  
73 $logger->warning(__x("archive '{archive}' has ambiguous content,"
74 . " trying first entry '{first}''",
75 archive => $path, first => $contents[0]))
76 if @contents > 1;
77  
78 my $first = File::Spec->catfile($destination, $contents[0]);
79 if (-d $contents[0]) {
80 # Properly packaged, return the directory.
81 return $contents[0];
82 }
83  
84 # Archive does not unpack into a single directory. Let's assume that
85 # the directory level is simply missing.
86 return $destination;
87 }
88  
89 1;