WARC/Index/File/CDX/Builder.pm

filter: Statementstmt 100% 103 / 103 Branchbran 88.2% 30 / 34 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 24 / 24 Podpod 100% 1 / 1 totaltotal 96.5% 166 / 172 CC 20 SCAR 12.2
CC
20
Cov
97%
_new
9.0
__ANON__
4.0
_add_record
3.0
CRAP 3.4
« WARC/Index/Builder.pm ↑ Summary WARC/Record/Replay/HTTP/Response.pm »
1 package WARC::Index::File::CDX::Builder; # -*- CPerl -*-
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 require WARC::Index::Builder;
7 1 our @ISA = qw(WARC::Index::Builder);
8  
9 1 require WARC; *WARC::Index::File::CDX::Builder::VERSION = \$WARC::VERSION;
10  
11 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Cwd qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Fcntl qw/:seek/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 require File::Spec::Unix;
17  
18 our %Record_Field_Handlers =
19 # each handler is called with WARC::Record and index builder objects and
20 # returns the text value for that field or undef, which is written as '-'
21 29 ▶ (a => sub { (shift)->field('WARC-Target-URI') },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 29 ▶ k => sub { (shift)->field('WARC-Payload-Digest') },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 37 ▶ u => sub { (shift)->id },
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 29 ▶ b => sub { my $date = (shift)->date;
Subroutine __ANON__: called CC=1 SCAR=0.0
26 29 $date =~ y/-T:Z//d; substr $date, 0, 14 },
27 29 ▶ N => sub { my $uri = (shift)->field('WARC-Target-URI');
Subroutine __ANON__: called CC=4 SCAR=13.9
28 29 ▶ return undef unless $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri 6 23 29
29 23 $uri = new URI ($uri);
30 23 ▶ return undef unless $uri->can('host') && $uri->can('path');
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->can("host") and $uri->can("path") 2 21 23
Condition66%2/3
$uri->can("host") and $uri->can("path")
!ll&&!rl&&r
2021
MC/DC50%1/2
$uri->can("host") and $uri->can("path")
$uri->can("host") $uri->can("path")
Truth table66%2/3
A and B
A= $uri->can("host")B= $uri->can("path")
ABresult
0X0
100
111
31 21 my $surt_host = join ',', reverse split /[.]/, $uri->host;
32 21 return $surt_host.')'.$uri->path
33 },
34  
35 37 ▶ g => sub { my $record = shift; my $builder = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
36 37 return $builder->_get_relvolname($record->volume) },
37 29 ▶ S => sub { (shift)->{sl_packed_size} },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 4 ▶ v => sub { my $record = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
39 4 ▶ return undef if $record->volume->filename !~ m/[.]warc\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $record->volume->filename =~ /[.]warc\z/ 0 4 4
40 4 return $record->offset },
41 33 ▶ V => sub { my $record = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
42 33 ▶ return undef if $record->volume->filename =~ m/[.]warc\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $record->volume->filename =~ /[.]warc\z/ 25 8 33
43 8 return $record->offset },
44  
45 # HTTP responses only
46 29 ▶ m => sub { my $response = (shift)->replay;
Subroutine __ANON__: called CC=2 SCAR=6.9
47 29 ▶ return undef unless UNIVERSAL::can($response, 'headers');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($response, "headers") 8 21 29
48 21 $response->headers->content_type },
49 29 ▶ r => sub { my $response = (shift)->replay;
Subroutine __ANON__: called CC=2 SCAR=6.9
50 29 ▶ return undef unless UNIVERSAL::can($response, 'headers');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($response, "headers") 8 21 29
51 21 $response->headers->header('Location') },
52 29 ▶ s => sub { my $response = (shift)->replay;
Subroutine __ANON__: called CC=2 SCAR=6.9
53 29 ▶ return undef unless UNIVERSAL::can($response, 'code');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($response, "code") 18 11 29
54 11 $response->code },
55 1 );
56  
57 # This implementation uses a hash as the underlying structure.
58  
59 # Keys defined by this class:
60 #
61 # file_name
62 # Name of CDX file where records will be appended.
63 # file
64 # Handle opened for writing/appending on that file.
65 # fields
66 # CDX field letters to be written.
67 # fieldgen
68 # Array of handlers to call to produce field values.
69 # delimiter
70 # Field delimiter used in CDX file. Default is space; cannot be set
71 # as an option but can be read from an existing CDX file header.
72 # volnames
73 # Hash mapping volume names to relative paths from the CDX file.
74  
75 sub _get_relvolname {
76 37 ▶ my $self = shift;
Subroutine _get_relvolname: called CC=2 SCAR=6.9
77 37 my $name = (shift)->filename;
78  
79 37 ▶ return $self->{volnames}->{$name} if defined $self->{volnames}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"volnames"}{$name} 33 4 37
80  
81 # otherwise ...
82 4 my ($vol, $cdx_dirs, $file) = File::Spec->splitpath($self->{file_name});
83 4 my $relname = File::Spec->abs2rel
84 ($name, File::Spec->catpath($vol, $cdx_dirs, undef));
85 4 my ($rvol, $rel_dirs, $rel_file) = File::Spec->splitpath($relname);
86 4 my @rel_dirs = File::Spec->splitdir($rel_dirs);
87 4 my $warcfilename = File::Spec::Unix->catpath
88 ($rvol, File::Spec::Unix->catdir(@rel_dirs), $rel_file);
89 4 return $self->{volnames}{$name} = $warcfilename;
90 }
91  
92 sub _new {
93 7 ▶ my $class = shift;
Subroutine _new: called CC=9 SCAR=22.0
94 7 my %args = @_;
95  
96 7 ▶ croak "required parameter 'into' missing" unless $args{into};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"into"} 1 6 7
97  
98 my $ob = { delimiter => ' ', fields => [qw/N b a m s k r M S V g u/],
99 6 file_name => Cwd::abs_path($args{into}) };
100  
101 6 ▶ $ob->{fields} = $args{fields} if $args{fields};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"fields"} 2 4 6
102  
103 6 ▶ open my $fh, '+>>', $args{into} or croak $args{into}.': '.$!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "+>>", $args{"into"} or croak($args{"into"} . ': ' . $!) 0 6 6
104 {
105 6 local $/ = "\012";
106 6 ▶ seek $fh, 0, SEEK_SET or croak 'seek '.$args{into}.': '.$!;
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or croak('seek ' . $args{"into"} . ': ' . $!) 0 6 6
107 6 my $header = <$fh>;
108 6 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) { } 3 3 6
109 $header =~ m/^(.)CDX((?:\1[[:alpha:]])+)/
110 3 ▶ or croak $args{into}.' exists but lacks CDX header';
Branch: 1 branch
BranchTrueFalseTotal
$header =~ /^(.)CDX((?:\1[[:alpha:]])+)/ or croak($args{"into"} . ' exists but lacks CDX header') 2 1 3
111 1 $ob->{delimiter} = $1;
112 1 $ob->{fields} = [split /\Q$1/, $2];
113 1 shift @{$ob->{fields}}; # remove leading empty field
114 } else {
115 # write CDX header
116 3 print $fh ' CDX ', join(' ', @{$ob->{fields}}), "\012";
117 }
118 4 ▶ seek $fh, 0, SEEK_END or croak 'seek '.$args{into}.': '.$!;
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 2 or croak('seek ' . $args{"into"} . ': ' . $!) 0 4 4
119 }
120 4 $ob->{file} = $fh;
121  
122 $ob->{fieldgen} =
123 4 ▶ [map { $Record_Field_Handlers{$_} or sub { undef } } @{$ob->{fields}}];
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$WARC::Index::File::CDX::Builder::Record_Field_Handlers{$_} or sub { undef; } 2 28 30
124  
125 4 bless $ob, $class
126 }
127  
128 # inherit add
129  
130 sub _add_record {
131 37 ▶ my $self = shift;
Subroutine _add_record: called CC=3 SCAR=11.0
132 37 my $record = shift;
133  
134 356 ▶ my $line = join $self->{delimiter}, map { defined $_ && $_ ne '' ? $_ : '-' }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && $_ ne '' ? : 187 169 356
Condition100%3/3
defined $_ and $_ ne ""
!ll&&!rl&&r
15811187
MC/DC100%2/2
defined $_ and $_ ne ""
defined $_ $_ ne ""
Truth table100%3/3
A and B
A= defined $_B= $_ ne ""
ABresult
0X0
100
111
135 37 map { $_->($record, $self) } @{$self->{fieldgen}};
136  
137 37 print {$self->{file}} $line, "\012";
138 }
139  
140 sub flush {
141 4 ▶ my $self = shift;
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
142  
143 4 seek $self->{file}, 0, SEEK_END
144 }
145  
146 1 1;
147 __END__
« WARC/Index/Builder.pm ↑ Summary WARC/Record/Replay/HTTP/Response.pm »