bin/ptargrep

filter: Statementstmt 70.5% 48 / 68 Branchbran 41.1% 14 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 61.3% 73 / 119 CC 15 SCAR 24.7
CC
15
Cov
59%
save_file
42.0
match_file
4.4
process_archive
4.1
CRAP 11.8
« lib/Archive/Tar.pm ↑ Summary lib/Archive/Tar/File.pm »
1 #!/usr/bin/perl
2 ##############################################################################
3 # Tool for using regular expressions against the contents of files in a tar
4 # archive. See 'ptargrep --help' for more documentation.
5 #
6  
7 1 ▶BEGIN { pop @INC if $INC[-1] eq '.' }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 1 0 1
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Archive::Tar qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 my(%opt, $pattern);
17  
18 1 ▶if(!GetOptions(\%opt,
Branch: 1 branch
BranchTrueFalseTotal
unless (&GetOptions(\%opt, "basename|b", "ignore-case|i", "list-only|l", "verbose|v", "help|?")) 0 1 1
19 'basename|b',
20 'ignore-case|i',
21 'list-only|l',
22 'verbose|v',
23 'help|?',
24 )) {
25 0 pod2usage(-exitval => 1, -verbose => 0);
26 }
27  
28  
29 1 ▶pod2usage(-exitstatus => 0, -verbose => 2) if $opt{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 0 1 1
30  
31 1 ▶pod2usage(-exitval => 1, -verbose => 0,
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 1 1
32 -message => "No pattern specified",
33 ) unless @ARGV;
34 1 make_pattern( shift(@ARGV) );
35  
36 1 ▶pod2usage(-exitval => 1, -verbose => 0,
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 1 1
37 -message => "No tar files specified",
38 ) unless @ARGV;
39  
40 1 process_archive($_) foreach @ARGV;
41  
42 1 exit 0;
43  
44  
45 1 ▶sub make_pattern {
Subroutine make_pattern: called CC=2 SCAR=7.6
46 1 my($pat) = @_;
47  
48 1 ▶ if($opt{'ignore-case'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'ignore-case'}) { } 0 1 1
49 0 $pattern = qr{(?im)$pat};
50 }
51 else {
52 1 $pattern = qr{(?m)$pat};
53 }
54 }
55  
56  
57 1 ▶sub process_archive {
Subroutine process_archive: called CC=4 SCAR=14.0
58 1 my($filename) = @_;
59  
60 1 _log("Processing archive: $filename");
61 1 my $next = Archive::Tar->iter($filename);
62 1 ▶ while( my $f = $next->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = &$next()) 1 1 2
63 1 ▶ next unless $f->is_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_file 0 1 1
64 1 ▶ match_file($f) if $f->size > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $f->size > 0 1 0 1
65 }
66 }
67  
68  
69 1 ▶sub match_file {
Subroutine match_file: called CC=4 SCAR=14.9
70 1 my($f) = @_;
71 1 my $path = $f->name;
72 1 my $prefix = $f->prefix;
73 1 ▶ if (defined $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix) 1 0 1
74 1 $path = File::Spec->catfile($prefix, $path);
75 }
76  
77 1 _log("filename: %s (%d bytes)", $path, $f->size);
78  
79 1 my $body = $f->get_content();
80 1 ▶ if($body !~ $pattern) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($body =~ /$pattern/) 0 1 1
81 0 _log(" no match");
82 0 return;
83 }
84  
85 1 ▶ if($opt{'list-only'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"list-only"}) 1 0 1
86 1 print $path, "\n";
87 1 return;
88 }
89  
90 0 save_file($path, $body);
91 }
92  
93  
94 0 ▶sub save_file {
Subroutine save_file: not called CC=6 SCAR=37.4
95 0 my($path, $body) = @_;
96  
97 0 _log(" found match - extracting");
98 0 my($fh);
99 0 my($dir, $file) = $path =~ m{\A(?:(.*)/)?([^/]+)\z};
100 0 ▶ if($dir and not $opt{basename}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir and not $opt{'basename'}) { } 0 0 0
Condition0%0/3
$dir and not $opt{"basename"}
!ll&&!rl&&r
000
MC/DC0%0/2
$dir and not $opt{"basename"}
$dir not $opt{"basename"}
Truth table0%0/3
A and not(B)
A= $dirB= not $opt{"basename"}
ABresult
0X0
100
111
101 0 _log(" writing to $dir/$file");
102 0 $dir =~ s{\A/}{./};
103 0 ▶ mkpath($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
104 0 ▶ open $fh, '>', "$dir/$file" or die "open($dir/$file): $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", "$dir/$file" or die "open($dir/$file): $!" 0 0 0
105 }
106 else {
107 0 _log(" writing to ./$file");
108 0 ▶ open $fh, '>', $file or die "open($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $file or die "open($file): $!" 0 0 0
109 }
110 0 print $fh $body;
111 0 close($fh);
112 }
113  
114  
115 2 ▶sub _log {
Subroutine _log: called CC=2 SCAR=10.5
116 2 ▶ return unless $opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"verbose"} 2 0 2
117 0 my($format, @args) = @_;
118 0 warn sprintf($format, @args) . "\n";
119 }
120  
121  
122 __END__
« lib/Archive/Tar.pm ↑ Summary lib/Archive/Tar/File.pm »