bin/vcardtidy

filter: Statementstmt 60.9% 78 / 128 Branchbran 38.6% 17 / 44 Conditioncond 28.5% 4 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 73.3% 11 / 15 Podpod n/a totaltotal 52.6% 110 / 209 CC 28 SCAR 43.3
CC
28
Cov
53%
vcardtidy
121.5
__ANON__
6.0
_diff
6.0
CRAP 76.1
↑ Summary blib/lib/App/vcardtidy.pm »
1 #!/usr/bin/env perl
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use OptArgs2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Text::vCard::Addressbook;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = 'v1.1.0';
10  
11 my $opts = optargs(
12 comment => 'tidy (normalize) VCARD contact files',
13 optargs => [
14 files => {
15 isa => 'ArrayRef',
16 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
17 0 ▶ [ ( -t STDIN ) ? ( die OptArgs2::usage(__PACKAGE__) ) : '-' ]
Branch: 1 branch
BranchTrueFalseTotal
-t STDIN ? : 0 0 0
18 },
19 greedy => 1,
20 comment => 'file to tidy (default is stdin)',
21 },
22 debug => {
23 isa => '--Flag',
24 alias => 'd',
25 comment => 'output debugging information to STDERR',
26 },
27 regex => {
28 isa => '--ArrayRef',
29 isa_name => 'REGEX',
30 alias => 'r',
31 comment => 'Regular expression to run against $_ first',
32 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 },
34 force => {
35 isa => '--Flag',
36 alias => 'f',
37 comment => 'insert missing fields where required',
38 },
39 no_rev => {
40 isa => '--Flag',
41 alias => 'R',
42 comment => 'do not update REV value'
43 },
44 nothing => {
45 isa => '--Flag',
46 alias => 'n',
47 comment => q{don't modify files, only report errors},
48 },
49 vcard_version => {
50 isa => '--Str',
51 alias => 'v',
52 default => '4.0',
53 comment => 'value for vCard VERSION field'
54 },
55 version => {
56 isa => '--Flag',
57 alias => 'V',
58 comment => 'print version information and exit',
59 0 ▶ trigger => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 require File::Basename;
61 0 die File::Basename::basename($0)
62 . ' version '
63 . $VERSION . "\n";
64 },
65 },
66 1 ],
67 );
68  
69 1 my $dtstamp = localtime->strftime('%Y-%m-%dT%H%M%SZ');
70  
71 1 my $badcount = 0;
72 1 foreach my $f ( @{ $opts->{files} } ) {
73 1 $opts->{input} = $f;
74 1 ▶ vcardtidy($opts) || $badcount++;
Branch: 1 branch
BranchTrueFalseTotal
vcardtidy($opts) or ++$badcount 1 0 1
75 }
76  
77 1 ▶die "vcardtidy failure count: $badcount\n" if $badcount;
Branch: 1 branch
BranchTrueFalseTotal
if $badcount 0 1 1
78  
79 1 ▶sub vcardtidy {
Subroutine vcardtidy: called CC=26 SCAR=48.0
80 1 my $opts = shift;
81  
82 1 my $data;
83 my $file;
84  
85 1 ▶ if ( $opts->{input} eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'input'} eq '-') { } 0 1 1
86 0 local $/;
87 0 binmode STDIN, ':raw:encoding(UTF-8)';
88 0 $data = <STDIN>;
89 }
90 else {
91 1 $file = path( $opts->{input} );
92 1 $data = $file->slurp( { binmode => ':raw:encoding(UTF-8)' } );
93 }
94  
95 1 my $filtered = $data;
96 1 foreach my $user_re ( @{ $opts->{regex} } ) {
97 1 ▶ use experimental 're_strict';
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 ▶ use re 'strict';
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 0 ▶ print STDERR "Regex: $user_re\n" if $opts->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"debug"} 0 0 0
101 0 my $old = $filtered;
102 0 eval {
103 0 ▶ local $SIG{ALRM} = sub { die "Timeout\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 0 alarm 1;
105 0 eval "\$filtered =~ $user_re";
106 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
107 0 alarm 0;
108 };
109  
110 0 ▶ die "Regex failed: $user_re:" . $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
111 0 ▶ _diff( $old, $filtered ) if $opts->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"debug"} 0 0 0
112 }
113  
114 my $ab =
115 1 eval { Text::vCard::Addressbook->new( { 'source_text' => $filtered } ); };
116  
117 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
118 0 warn "$opts->{input}: $@";
119 0 return 0;
120 }
121  
122 1 my @vcards = $ab->vcards;
123 1 ▶ if ( 0 == @vcards ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == @vcards) 0 1 1
124 0 warn "$opts->{input}: No cards to tidy!\n";
125 0 return 1; # not considered an error. TODO If '-' then print?
126 }
127  
128 1 foreach my $vcard (@vcards) {
129 1 my $i = 0;
130 1 ▶ my $u = $vcard->get_simple_type('UID') // do {
Condition33%1/3
$vcard->get_simple_type("UID") // do { use feature 'state'; state @c = (("a", "b", "c", "d", "e", "f"), 0..9); state $len_c = scalar @c; my $uid = join("-", map({join "", map({$c[rand $len_c];} @$_);} [1..8], [1..4], [1..4], [1..4], [1..12])); $vcard->add_node({"node_type", "UID"})->value($uid); warn qq[$opts->{"input"}: VCARD $i missing UID (set to "$uid")\n]; $uid }
l!l&&r!l&&!r
100
MC/DC0%0/2
$vcard->get_simple_type("UID") // do { use feature 'state'; state @c = (("a", "b", "c", "d", "e", "f"), 0..9); state $len_c = scalar @c; my $uid = join("-", map({join "", map({$c[rand $len_c];} @$_);} [1..8], [1..4], [1..4], [1..4], [1..12])); $vcard->add_node({"node_type", "UID"})->value($uid); warn qq[$opts->{"input"}: VCARD $i missing UID (set to "$uid")\n]; $uid }
$vcard->get_simple_type("UID") do { use feature 'state'; state @c = (("a", "b", "c", "d", "e", "f"), 0..9); state $len_c = scalar @c; my $uid = join("-", map({join "", map({$c[rand $len_c];} @$_);} [1..8], [1..4], [1..4], [1..4], [1..12])); $vcard->add_node({"node_type", "UID"})->value($uid); warn qq[$opts->{"input"}: VCARD $i missing UID (set to "$uid")\n]; $uid }
Truth table33%1/3
A // B
A= $vcard->get_simple_type("UID")B= do { use feature 'state'; state @c = (("a", "b", "c", "d", "e", "f"), 0..9); state $len_c = scalar @c; my $uid = join("-", map({join "", map({$c[rand $len_c];} @$_);} [1..8], [1..4], [1..4], [1..4], [1..12])); $vcard->add_node({"node_type", "UID"})->value($uid); warn qq[$opts->{"input"}: VCARD $i missing UID (set to "$uid")\n]; $uid }
ABresult
1X1
011
000
131 1 ▶ use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
132 0 state @c = ( 'a' .. 'f', 0 .. 9 );
133 0 state $len_c = scalar @c;
134 my $uid = join '-', map {
135 0 join '',
136 0 map { $c[ rand($len_c) ] }
137 @$_
138 } [ 1 .. 8 ], [ 1 .. 4 ], [ 1 .. 4 ], [ 1 .. 4 ], [ 1 .. 12 ];
139  
140 0 $vcard->add_node( { 'node_type' => 'UID', } )->value($uid);
141 0 warn qq/$opts->{input}: VCARD $i missing UID (set to "$uid")\n/;
142 0 $uid;
143 };
144 1 $u =~ s/-.*//;
145  
146 1 ▶ if ( not $vcard->get('FN') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vcard->get("FN")) 0 1 1
147 0 ▶ if ( $opts->{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'force'}) { } 0 0 0
148 0 $vcard->fn($u);
149 0 warn
150 qq/$opts->{input}: VCARD $i missing FN field (set to "$u")\n/;
151 }
152 else {
153 0 warn qq/$opts->{input}: VCARD $i missing FN field!\n/;
154 0 return 0;
155 }
156 }
157  
158 1 ▶ if ( not $vcard->get('N') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vcard->get("N")) 0 1 1
159 0 ▶ if ( $opts->{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'force'}) { } 0 0 0
160 0 $vcard->add_node(
161 {
162 'node_type' => 'N',
163 data => [ { value => $u . ';;;;' } ]
164 }
165 );
166 0 warn
167 qq/$opts->{input}: VCARD $i missing N type (set to "$u")\n/;
168 }
169 else {
170 0 warn qq/$opts->{input}: VCARD $i missing N field!\n/;
171 0 return 0;
172 }
173 }
174  
175 1 my $v = $vcard->version;
176 1 ▶ if ( not length $v ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not length $v) { } 0 1 1
elsif ($v ne $opts->{'vcard_version'} and $opts->{'force'}) { } 0 1 1
177 0 $vcard->version( $opts->{vcard_version} );
178 0 warn qq/$opts->{input}: VCARD $i missing VERSION /
179 . qq/(set to "$opts->{vcard_version}")\n/;
180 }
181 ▶ elsif ( $v ne $opts->{vcard_version} and $opts->{force} ) {
Condition33%1/3
$v ne $opts->{"vcard_version"} and $opts->{"force"}
!ll&&!rl&&r
010
MC/DC0%0/2
$v ne $opts->{"vcard_version"} and $opts->{"force"}
$v ne $opts->{"vcard_version"} $opts->{"force"}
Truth table33%1/3
A and B
A= $v ne $opts->{"vcard_version"}B= $opts->{"force"}
ABresult
0X0
100
111
182 0 $vcard->version( $opts->{vcard_version} );
183 0 warn qq/$opts->{input}: forcing VCARD $i VERSION /
184 . qq/to $opts->{vcard_version}\n/;
185 }
186  
187 1 ▶ $vcard->REV($dtstamp) unless $opts->{no_rev};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"no_rev"} 1 0 1
188 1 $vcard->PRODID("vcardtidy $VERSION");
189  
190 1 $i++;
191 }
192  
193 # Remove duplicate fields
194 1 my $prev = '';
195 1 my $seen;
196 my $tidy = join '', map {
197 1 $seen = $_ eq $prev;
198 14 $prev = $_;
199 14 ▶ $seen ? () : $_ . "\x0D\x0A"
Branch: 1 branch
BranchTrueFalseTotal
$seen ? : 1 13 14
200 } split "\x0D\x0A", $ab->export;
201  
202 # Fix for multiple categories
203 1 ▶ while ( $tidy =~ s/^(CATEGORIES:.*?)\\,/$1,/mg ) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($tidy =~ s/^(CATEGORIES:.*?)\\,/$1,/gm) 1 1 2
204  
205 1 ▶ if ( $opts->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"debug"}) 0 1 1
206 0 print STDERR "vcardtidy:\n";
207 0 _diff( $data, $tidy );
208 }
209  
210 1 ▶ if ( $opts->{input} eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'input'} eq '-') { } 0 1 1
211 0 binmode STDOUT, ':raw:encoding(UTF-8)';
212 0 print $tidy;
213 }
214 else {
215 1 my $data2 = $data;
216 1 $data2 =~ s/^REV:.*\015\012//m;
217 1 $data2 =~ s/^PRODID:.*\015\012//m;
218  
219 1 my $tidy2 = $tidy;
220 1 $tidy2 =~ s/^REV:.*\015\012//m;
221 1 $tidy2 =~ s/^PRODID:.*\015\012//m;
222  
223 $file->spew( { binmode => ':raw:encoding(UTF-8)' }, $tidy )
224 unless $opts->{nothing}
225 1 ▶ or ( $data2 eq $tidy2 and not $opts->{force} );
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"nothing"} or $data2 eq $tidy2 and not $opts->{"force"} 1 0 1
Condition33%2/6
$opts->{"nothing"} or $data2 eq $tidy2 and not $opts->{"force"}
l!l&&r!l&&!r
001
$data2 eq $tidy2 and not $opts->{"force"}
!ll&&!rl&&r
100
MC/DC0%0/3
$opts->{"nothing"} or $data2 eq $tidy2 and not $opts->{"force"}
$opts->{"nothing"} $data2 eq $tidy2 not $opts->{"force"}
Truth table25%1/4
A or B and not(C)
A= $opts->{"nothing"}B= $data2 eq $tidy2C= not $opts->{"force"}
ABCresult
1XX1
0111
00X0
0100
226 }
227  
228 1 1;
229 }
230  
231 0 ▶sub _diff {
Subroutine _diff: not called CC=2 SCAR=17.9
232 0 my ( $t1, $t2 ) = @_;
233 0 require Text::Diff;
234 0 ▶ print STDERR ( Text::Diff::diff( \$t1, \$t2 ) || "(No change)\n" ) =~
Condition0%0/2
Text::Diff::diff(\$t1, \$t2) || "(No change)\n"
l!l
00
MC/DC0%0/1
Text::Diff::diff(\$t1, \$t2) || "(No change)\n"
Text::Diff::diff(\$t1, \$t2)
Truth table0%0/2
A
A= Text::Diff::diff(\$t1, \$t2)
Aresult
11
00
235 s/^/ /gmr;
236 }
237  
238 __END__
↑ Summary blib/lib/App/vcardtidy.pm »