blib/lib/Pinto/IndexWriter.pm

filter: Statementstmt 100% 90 / 90 Branchbran 66.6% 4 / 6 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 1 totaltotal 92.6% 114 / 123 CC 9 SCAR 8.4
CC
9
Cov
95%
_get_fake_records
5.1
_write_records
3.0
_write_header
2.0
CRAP 2.3
« blib/lib/Pinto/Action/Copy.pm ↑ Summary blib/lib/Pinto/Statistics.pm »
1 # ABSTRACT: Write records to an 02packages.details.txt file
2  
3 package Pinto::IndexWriter;
4  
5 52 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 52 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 52 ▶use MooseX::MarkAsMethods ( autoclean => 1 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 52 ▶use IO::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 52 ▶use Module::CoreList;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 52 ▶use Path::Class qw(file);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 52 ▶use HTTP::Date qw(time2str);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 52 ▶use Pinto::Types qw(File);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 52 ▶use Pinto::Util qw(debug throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #------------------------------------------------------------------------------
18  
19 52 our $VERSION = '0.14'; # VERSION
20  
21 #------------------------------------------------------------------------------
22  
23 52 has stack => (
24 is => 'ro',
25 isa => 'Pinto::Schema::Result::Stack',
26 required => 1,
27 );
28  
29 has index_file => (
30 is => 'ro',
31 isa => File,
32 93 ▶ default => sub { $_[0]->stack->modules_dir->file('02packages.details.txt.gz') },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 52 lazy => 1,
34 );
35  
36 #------------------------------------------------------------------------------
37  
38 93 ▶sub write_index {
Subroutine write_index: called CC=2 SCAR=6.9
Pod undocumented
39 93 my ($self) = @_;
40  
41 93 my $index_file = $self->index_file;
42 93 my $stack = $self->stack;
43  
44 93 debug("Writing index for stack $stack at $index_file");
45  
46 93 ▶ my $handle = IO::Zlib->new( $index_file->stringify, 'wb' )
Branch: 1 branch
BranchTrueFalseTotal
my $handle = "IO::Zlib"->new($index_file->stringify, "wb") or throw("Cannot open ${index_file}: $!") 93 0 93
47 or throw "Cannot open $index_file: $!";
48  
49 93 my @records = $self->_get_index_records($stack);
50 93 my $count = scalar @records;
51  
52 93 debug("Index for stack $stack has $count records");
53  
54 93 $self->_write_header( $handle, $index_file, $count );
55 93 $self->_write_records( $handle, @records );
56 93 close $handle;
57  
58 93 return $self;
59 }
60  
61 #------------------------------------------------------------------------------
62  
63 93 ▶sub _write_header {
Subroutine _write_header: called CC=2 SCAR=6.9
64 93 my ( $self, $fh, $filename, $line_count ) = @_;
65  
66 93 my $base = $filename->basename;
67 93 my $uri = 'file://' . $filename->absolute->as_foreign('Unix');
68  
69 93 my $writer = ref $self;
70 93 ▶ my $version = $self->VERSION || 'UNKNOWN';
Condition50%1/2
$self->VERSION || 'UNKNOWN'
l!l
930
MC/DC0%0/1
$self->VERSION || 'UNKNOWN'
$self->VERSION
Truth table50%1/2
A
A= $self->VERSION
Aresult
11
00
71 93 my $date = time2str(time);
72  
73 93 print {$fh} <<"END_PACKAGE_HEADER";
74 File: $base
75 URL: $uri
76 Description: Package names found in directory \$CPAN/authors/id/
77 Columns: package name, version, path
78 Intended-For: Automated fetch routines, namespace documentation.
79 Written-By: $writer version $version
80 Line-Count: $line_count
81 Last-Updated: $date
82  
83 END_PACKAGE_HEADER
84  
85 93 return $self;
86 }
87  
88 #------------------------------------------------------------------------------
89  
90 93 ▶sub _write_records {
Subroutine _write_records: called CC=3 SCAR=11.0
91 93 my ( $self, $fh, @records ) = @_;
92  
93 93 for my $record (@records) {
94 73195 my ( $name, $version, $author, $archive ) = @{$record};
95 73195 my $path = join '/', substr( $author, 0, 1 ), substr( $author, 0, 2 ), $author, $archive;
96 73195 my $width = 38 - length $version;
97 73195 ▶ $width = length $name if $width < length $name;
Branch: 1 branch
BranchTrueFalseTotal
if $width < length $name 3906 69289 73195
98 73195 printf {$fh} "%-${width}s %s %s\n", $name, $version, $path;
99 }
100  
101 93 return $self;
102 }
103  
104 #------------------------------------------------------------------------------
105  
106 93 ▶sub _get_index_records {
Subroutine _get_index_records: called CC=1 SCAR=0.0
107 93 my ( $self, $stack ) = @_;
108  
109 # The index is rewritten after almost every action, so we want
110 # this to be as fast as possible (especially during an Add or
111 # Remove action). Therefore, we use a cursor to get raw data and
112 # skip all the DBIC extras.
113  
114 # Yes, slurping all the records at once consumes a lot of memory,
115 # but I want them to be sorted the way perl sorts them, not the
116 # way sqlite sorts them. That way, the index file looks more
117 # like one produced by PAUSE. Also, this is about twice as fast
118 # as using an iterator to read each record lazily.
119  
120 93 my @joins = qw(package distribution);
121 93 my @selects = qw(package.name package.version distribution.author distribution.archive);
122  
123 93 my $attrs = { join => \@joins, select => \@selects };
124 93 my $rs = $stack->head->search_related( 'registrations', {}, $attrs );
125 93 my %stack_records = map { ($_->[0] => $_) } $rs->cursor->all;
126  
127 # Now, we merge the stuff from the stack with core modules. If
128 # the stack has a newer version of a core module (dual-life) then
129 # it should be the one that appears in the index. Then finally
130 # we sort them.
131  
132 93 my %fake_records = $self->_get_fake_records;
133 93 my %merged_records = (%fake_records, %stack_records);
134  
135 73195 return map { $merged_records{$_} }
136 93 sort {lc $a cmp lc $b}
137 keys %merged_records;
138  
139 }
140  
141 #------------------------------------------------------------------------------
142  
143 93 ▶sub _get_fake_records {
Subroutine _get_fake_records: called CC=5 SCAR=16.2
144 93 my ($self) = @_;
145  
146 # We generate artificial records for all the (non-deprecated) core modules
147 # that are in the target perl. That way, the index appears to have perl
148 # itself (just like the real CPAN) and installers can handle requests to
149 # install a core module.
150  
151 93 my $tpv = $self->stack->target_perl_version;
152 93 my $tpv_normal = $tpv->normal; $tpv_normal =~ s/^v//;
153 93 my @fake = ("FAKE", "perl-$tpv_normal.tar.gz");
154  
155 93 my $core_versions = $Module::CoreList::version{$tpv->numify + 0};
156 93 my $deprecated_modules = $Module::CoreList::deprecated{$tpv->numify + 0};
157  
158 93 my $fake_records = {};
159 93 for my $module (keys %{ $core_versions }) {
160 73191 ▶ next if $deprecated_modules && exists $deprecated_modules->{ $module };
Branch: 1 branch
BranchTrueFalseTotal
if $deprecated_modules and exists $deprecated_modules->{$module} 0 73191 73191
Condition33%1/3
$deprecated_modules and exists $deprecated_modules->{$module}
!ll&&!rl&&r
0731910
MC/DC0%0/2
$deprecated_modules and exists $deprecated_modules->{$module}
$deprecated_modules exists $deprecated_modules->{$module}
Truth table33%1/3
A and B
A= $deprecated_modulesB= exists $deprecated_modules->{$module}
ABresult
0X0
100
111
161 73191 ▶ $fake_records->{$module} = [$module, $core_versions->{$module} || 0, @fake];
Condition100%2/2
$core_versions->{$module} || 0
l!l
72726465
MC/DC100%1/1
$core_versions->{$module} || 0
$core_versions->{$module}
Truth table100%2/2
A
A= $core_versions->{$module}
Aresult
11
00
162 }
163  
164 93 return %{ $fake_records };
165 }
166  
167 #------------------------------------------------------------------------------
168  
169 52 __PACKAGE__->meta->make_immutable;
170  
171 #------------------------------------------------------------------------------
172  
173 52 1;
174  
175 __END__
« blib/lib/Pinto/Action/Copy.pm ↑ Summary blib/lib/Pinto/Statistics.pm »