Maker/Command/index.pm

filter: Statementstmt 98.9% 96 / 97 Branchbran 88.8% 16 / 18 Conditioncond 100% 11 / 11 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 98.0% 148 / 151 CC 22 SCAR 22.8
CC
22
Cov
98%
execute
18.0
validate_args
5.0
BEGIN
1.0
CRAP 9.8
« Maker/Command/info.pm ↑ Summary Maker/Utils.pm »
1 package OPM::Maker::Command::index;
2 21 $OPM::Maker::Command::index::VERSION = '1.17';
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Build index for an OPM repository
7  
8 21 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use Path::Class ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 21 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 21 ▶use XML::LibXML::PrettyPrint;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 21 ▶use OPM::Maker -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 21 ▶use OPM::Maker::Utils qw(reformat_size);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶sub abstract {
Subroutine abstract: called CC=1 SCAR=0.0
Pod documented
21 1 return "build index for an OPM repository";
22 }
23  
24 1 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
25 1 return "opmbuild index <path_to_directory>";
26 }
27  
28 7 ▶sub validate_args {
Subroutine validate_args: called CC=5 SCAR=16.1
Pod documented
29 7 my ($self, $opt, $args) = @_;
30
31 7 ▶ $self->usage_error( 'need path to directory that contains opm files' ) if
Branch: 1 branch
BranchTrueFalseTotal
if not $args or "ARRAY" ne ref $args or not $args->[0] or not -d $args->[0] 6 1 7
Condition100%9/9
not $args or "ARRAY" ne ref $args or not $args->[0] or not -d $args->[0]
l!l&&r!l&&!r
511
not $args or "ARRAY" ne ref $args or not $args->[0]
l!l&&r!l&&!r
322
not $args or "ARRAY" ne ref $args
l!l&&r!l&&!r
214
MC/DC100%4/4
not $args or "ARRAY" ne ref $args or not $args->[0] or not -d $args->[0]
not $args "ARRAY" ne ref $args not $args->[0] not -d $args->[0]
Truth table100%5/5
not(A) or B or not(C) or not(D)
A= not $argsB= "ARRAY" ne ref $argsC= not $args->[0]D= not -d $args->[0]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
32 !$args ||
33 'ARRAY' ne ref $args ||
34 !$args->[0] ||
35 !-d $args->[0];
36 }
37  
38 11 ▶sub execute {
Subroutine execute: called CC=18 SCAR=28.9
Pod documented
39 11 my ($self, $opt, $args) = @_;
40
41 11 my $dir = $args->[0];
42
43 11 my @opm_files = File::Find::Rule->file->name( '*.opm' )->in( $dir );
44
45 11 my @packages;
46 11 my $pp = XML::LibXML::PrettyPrint->new(
47 indent_string => ' ',
48 element => {
49 compact => [qw(
50 Vendor Name Description Version Framework
51 ModuleRequired PackageRequired URL License
52 File
53 )],
54 },
55 );
56  
57 11 my $root_name;
58
59 11 for my $opm_file ( sort @opm_files ) {
60 16 my $size = -s $opm_file;
61 16 my %opts;
62  
63 16 ▶ if ( !$ENV{OPM_UNSECURE} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"OPM_UNSECURE"}) 16 0 16
64 16 %opts = (
65 no_network => 1,
66 expand_entities => 0,
67 );
68 }
69  
70 # if file is big, but not "too big"
71 16 my $max_size = 31_457_280;
72 16 ▶ if ( $ENV{OPM_MAX_SIZE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OPM_MAX_SIZE"}) 11 5 16
73 11 $max_size = reformat_size( $ENV{OPM_MAX_SIZE} );
74 }
75  
76 16 ▶ if ( $size > $max_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $max_size) 3 13 16
77 3 croak "$opm_file too big (max size: $max_size bytes)";
78 }
79  
80 13 ▶ if ( $size > 10_000_000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 10000000) 0 13 13
81 0 $opts{huge} = 1;
82 }
83  
84 13 my $parser = XML::LibXML->new( %opts );
85 13 my $tree = $parser->parse_file( $opm_file );
86
87 13 $tree->setStandalone( 0 );
88
89 13 my $root_elem = $tree->getDocumentElement;
90 13 $root_name = $root_elem->nodeName();
91 13 $root_elem->setNodeName( 'Package' );
92 13 $root_elem->removeAttribute( 'version' );
93
94 # retrieve file information
95 13 my @files = $root_elem->findnodes( 'Filelist/File' );
96
97 FILE:
98 13 for my $file ( @files ) {
99 26 my $location = $file->findvalue( '@Location' );
100
101 # keep only documentation in file list
102 26 ▶ if ( $location !~ m{\A doc/}x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $location =~ m[\A doc/]x) { } 19 7 26
103 19 $file->parentNode->removeChild( $file );
104 }
105 else {
106 7 my @child_nodes = $file->childNodes;
107
108 # clean nodes
109 7 $file->removeChild( $_ ) for @child_nodes;
110 7 $file->removeAttribute( 'Encode' );
111 7 $file->setNodeName( 'FileDoc' );
112 }
113 }
114
115 # remove unnecessary nodes
116 13 for my $node_name ( qw(Code Intro Database)) {
117 39 for my $phase ( qw(Install Upgrade Reinstall Uninstall) ) {
118 156 my @nodes = $root_elem->findnodes( $node_name . $phase );
119 156 $_->parentNode->removeChild( $_ ) for @nodes;
120 }
121 }
122
123 13 for my $node_name ( qw(BuildHost BuildDate)) {
124 26 my @nodes = $root_elem->findnodes( $node_name );
125 26 $_->parentNode->removeChild( $_ ) for @nodes;
126 }
127
128 13 my $file_node = XML::LibXML::Element->new( 'File' );
129 13 my $file_path = $opm_file;
130  
131 13 ▶ $file_path =~ s/\Q$dir// if $dir ne '.';
Branch: 1 branch
BranchTrueFalseTotal
if $dir ne "." 12 1 13
132 13 ▶ $file_path = '/' . $file_path if '/' ne substr $file_path, 0, 1;
Branch: 1 branch
BranchTrueFalseTotal
if "/" ne substr($file_path, 0, 1) 1 12 13
133  
134 13 $file_node->appendText( $file_path );
135 13 $root_elem->addChild( $file_node );
136
137 13 $pp->pretty_print( $tree );
138
139 13 my $xml = $tree->toString;
140 13 $xml =~ s{<\?xml .*? \?> \s+}{}x;
141
142 13 push @packages, $xml;
143 }
144  
145 8 ▶ $root_name //= 'otrs';
Condition100%2/2
$root_name //= "otrs"
l!l
71
MC/DC100%1/1
$root_name //= "otrs"
$root_name
Truth table100%2/2
A
A= $root_name
Aresult
11
00
146 8 ▶ my $product = $root_name =~ m{otobo} ? 'otobo' : 'otrs';
Branch: 1 branch
BranchTrueFalseTotal
$root_name =~ /otobo/ ? : 1 7 8
147
148 8 my $packages_list = join '', @packages;
149
150 8 print sprintf qq~<?xml version="1.0" encoding="utf-8" ?>
151 <%s_package_list version="1.0">
152 %s
153 </%s_package_list>
154 ~, $product, $packages_list, $product;
155 }
156  
157 21 1;
158  
159 __END__
« Maker/Command/info.pm ↑ Summary Maker/Utils.pm »