blib/lib/App/CopyrightImage.pm

filter: Statementstmt 98.8% 87 / 88 Branchbran 78.9% 30 / 38 Conditioncond 86.6% 13 / 15 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 91.3% 147 / 161 CC 27 SCAR 22.1
CC
27
Cov
92%
_exif
14.5
imgcopyright
10.0
_check
5.0
CRAP 9.1
↑ Summary
1 package App::CopyrightImage;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Image::ExifTool qw(:Public);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our @EXPORT = qw(imgcopyright);
14  
15 3 our $VERSION = '1.01';
16  
17 10 ▶sub imgcopyright {
Subroutine imgcopyright: called CC=10 SCAR=23.1
Pod documented
18 10 my (%data) = @_;
19  
20 10 ▶ die "need to supply the 'image' argument!\n" if ! $data{src};
Branch: 1 branch
BranchTrueFalseTotal
unless $data{"src"} 1 9 10
21 9 ▶ if (! $data{name} && (! $data{check} && ! $data{remove})){
Branch: 1 branch
BranchTrueFalseTotal
unless ($data{"name"} or ($data{"check"} or $data{"remove"})) 1 8 9
Condition100%6/6
$data{"name"} or ($data{"check"} or $data{"remove"})
l!l&&r!l&&!r
621
$data{"check"} or $data{"remove"}
l!l&&r!l&&!r
111
MC/DC100%3/3
$data{"name"} or ($data{"check"} or $data{"remove"})
$data{"name"} $data{"check"} $data{"remove"}
Truth table100%4/4
A or B or C
A= $data{"name"}B= $data{"check"}C= $data{"remove"}
ABCresult
1XX1
01X1
0011
0000
22 1 die "need to supply the 'name' argument\n";
23 }
24 8 $data{year} = (localtime(time))[5] + 1900;
25  
26 8 ▶ if ($data{dst} && -d $data{dst}){
Branch: 2 branches
BranchTrueFalseTotal
if ($data{'dst'} and -d $data{'dst'}) { } 0 8 8
elsif (-d $data{'src'}) { } 3 5 8
Condition66%2/3
$data{"dst"} and -d $data{"dst"}
!ll&&!rl&&r
620
MC/DC0%0/2
$data{"dst"} and -d $data{"dst"}
$data{"dst"} -d $data{"dst"}
Truth table66%2/3
A and B
A= $data{"dst"}B= -d $data{"dst"}
ABresult
0X0
100
111
27 0 $data{basename} = $data{dst};
28 }
29 elsif (-d $data{src})
30 {
31 3 $data{basename} = $data{src};
32 }
33 else {
34 5 $data{basename} = dirname $data{src};
35 }
36  
37 8 ▶ if (-d $data{src}){
Branch: 1 branch
BranchTrueFalseTotal
if (-d $data{'src'}) { } 3 5 8
38 3 @{ $data{images} } = File::Find::Rule->file()
39 ->name('*.jpg', '*.jpeg')
40 ->maxdepth(1)
41 3 ->in($data{src});
42 }
43 else {
44 5 push @{ $data{images} }, $data{src};
45 }
46  
47 8 ▶ if ($data{check}){
Branch: 1 branch
BranchTrueFalseTotal
if ($data{'check'}) { } 3 5 8
48 3 return _check(\%data);
49 }
50 else {
51 5 _exif(\%data);
52 }
53 }
54 5 ▶sub _exif {
Subroutine _exif: called CC=14 SCAR=26.7
55 5 my $data = shift;
56  
57 my $dst = $data->{dst}
58 ? $data->{dst}
59 5 ▶ : "$data->{basename}/ci";
Branch: 1 branch
BranchTrueFalseTotal
$data->{'dst'} ? : 2 3 5
60
61 5 ▶ if (! -d $dst){
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dst) 5 0 5
62 5 ▶ mkdir $dst
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dst or die "can't create the destination image directory $dst!: $!" 5 0 5
63 or die "can't create the destination image directory $dst!: $!";
64 }
65  
66 5 my $et = Image::ExifTool->new;
67 5 my %errors;
68  
69 5 for my $img (@{ $data->{images} }){
70  
71 # original
72
73 7 $et->ExtractInfo($img);
74  
75 7 ▶ if ($data->{remove}){
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"remove"}) 1 6 7
76 1 $et->SetNewValue('Copyright', '');
77 1 $et->SetNewValue('Creator', '');
78 1 $et->WriteInfo($img, "$img.tmp");
79 1 move "$img.tmp", $img;
80 1 next;
81 }
82  
83 6 my $cp = $et->GetValue('Copyright');
84 6 my $cr = $et->GetValue('Creator');
85  
86 6 ▶ if (! $data->{force} && ($cp || $cr)){
Branch: 1 branch
BranchTrueFalseTotal
if (not $data->{"force"} and $cp || $cr) 1 5 6
Condition83%5/6
not $data->{"force"} and $cp || $cr
!ll&&!rl&&r
231
$cp or $cr
l!l&&r!l&&!r
103
MC/DC66%2/3
not $data->{"force"} and $cp || $cr
not $data->{"force"} $cp $cr
Truth table75%3/4
not(A) and B or C
A= not $data->{"force"}B= $cpC= $cr
ABCresult
0XX0
1000
11X1
1011
87 1 ▶ my $set = "Copyright is already set;" if $cp;
Branch: 1 branch
BranchTrueFalseTotal
if $cp 1 0 1
88 1 ▶ $set .= " Creator is already set;" if $cr;
Branch: 1 branch
BranchTrueFalseTotal
if $cr 1 0 1
89 1 $errors{$img} = $set;
90 1 next;
91 }
92
93 5 $et->SetNewValue('Copyright', "Copyright (C) $data->{year} by $data->{name}");
94 5 my $creator_string = $data->{name};
95 5 ▶ $creator_string .= " ($data->{email})" if $data->{email};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"email"} 0 5 5
96  
97 5 $et->SetNewValue('Creator', $creator_string);
98  
99 5 my $ci_img = (fileparse($img))[0];
100 5 $ci_img = "$dst/ci_$ci_img";
101
102 # write out the new image
103  
104 5 $et->WriteInfo($img, $ci_img);
105  
106 # updated
107  
108 5 $et->ExtractInfo($ci_img);
109  
110 5 ▶ $errors{$img} = "failed to add Copyright; "
Branch: 1 branch
BranchTrueFalseTotal
unless $et->GetValue("Copyright") 0 5 5
111 if ! $et->GetValue('Copyright');
112  
113 5 ▶ $errors{$img} .= "failed to add Creator"
Branch: 1 branch
BranchTrueFalseTotal
unless $et->GetValue("Creator") 0 5 5
114 if ! $et->GetValue('Creator');
115 }
116 5 return %errors;
117 }
118 3 ▶sub _check {
Subroutine _check: called CC=5 SCAR=16.1
119 3 my $data = shift;
120  
121 3 my $et = Image::ExifTool->new;
122  
123 3 for (@{ $data->{images} }){
124 4 $et->ExtractInfo($_);
125 4 my $cp = $et->GetValue('Copyright');
126 4 my $cr = $et->GetValue('Creator');
127  
128 4 my $err_str;
129 4 ▶ $err_str .= " missing Copyright; " if ! $cp;
Branch: 1 branch
BranchTrueFalseTotal
unless $cp 3 1 4
130 4 ▶ $err_str .= " missing Creator; " if ! $cr;
Branch: 1 branch
BranchTrueFalseTotal
unless $cr 3 1 4
131  
132 4 ▶ print "$_: $err_str\n" if $err_str;
Branch: 1 branch
BranchTrueFalseTotal
if $err_str 3 1 4
133 }
134 3 return ();
135 }
136  
137 3 1;
138 __END__
↑ Summary