/
Entries.pm
463 lines (318 loc) · 10 KB
/
Entries.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use 5.008;
package CPAN::PackageDetails::Entries;
use strict;
use warnings::register;
our $VERSION = '0.263';
use Carp;
use version;
sub DESTROY { }
=encoding utf8
=head1 NAME
CPAN::PackageDetails::Entries - Handle the collection of records of 02packages.details.txt.gz
=head1 SYNOPSIS
Used internally by CPAN::PackageDetails
=head1 DESCRIPTION
=head2 Methods
=over 4
=item new
Creates a new Entries object. This doesn't do anything fancy. To add
to it, use C<add_entry>.
entry_class => the class to use for each entry object
columns => the column names, in order that you want them in the output
If you specify the C<allow_packages_only_once> option with a true value
and you try to add that package twice, the object will die. See C<add_entry>.
=cut
sub new {
my( $class, %args ) = @_;
my %hash = (
entry_class => 'CPAN::PackageDetails::Entry',
allow_packages_only_once => 1,
allow_suspicious_names => 0,
columns => [],
entries => {},
%args
);
$hash{max_widths} = [ (0) x @{ $hash{columns} } ];
bless \%hash, $_[0]
}
=item entry_class
Returns the class that Entries uses to make a new Entry object.
=cut
sub entry_class { $_[0]->{entry_class} }
=item columns
Returns a list of the column names in the entry
=cut
sub columns { @{ $_[0]->{columns} } };
=item column_index_for( COLUMN )
Returns the list position of the named COLUMN.
=cut
sub column_index_for {
my( $self, $column ) = @_;
my $index = grep {
$self->{columns}[$_] eq $column
} 0 .. @{ $self->columns };
return unless defined $index;
return $index;
}
=item count
Returns the number of entries. This is not the same as the number of
lines that would show up in the F<02packages.details.txt> file since
this method counts duplicates as well.
=cut
sub count {
my $self = shift;
my $count = 0;
foreach my $package ( keys %{ $self->{entries} } ) {
$count += keys %{ $self->{entries}{$package} };
}
return $count;
}
=item entries
DEPRECATED: use C<get_hash>
=item get_hash
Returns the list of entries as an hash reference. The hash key is the
package name.
=cut
sub entries {
carp "entries is deprecated. Use get_hash instead";
&get_hash;
}
sub get_hash { $_[0]->{entries} }
=item allow_packages_only_once( [ARG] )
Set or retrieve the value of the allow_packages_only_once setting. It's
a boolean.
=cut
sub allow_packages_only_once {
$_[0]->{allow_packages_only_once} = !! $_[1] if defined $_[1];
$_[0]->{allow_packages_only_once};
}
=item allow_suspicious_names( [ARG] )
Allow an entry to accept an illegal name. Normally you shouldn't use this,
but PAUSE has made bad files before.
=cut
sub allow_suspicious_names {
$_[0]->{allow_suspicious_names} = !! $_[1] if defined $_[1];
$_[0]->{allow_suspicious_names};
}
=item disallow_alpha_versions( [ARG] )
Set or retrieve the value of the disallow_alpha_versions settings. It's
a boolean.
=cut
sub disallow_alpha_versions {
$_[0]->{disallow_alpha_versions} = !! $_[1] if defined $_[1];
$_[0]->{disallow_alpha_versions};
}
=item add_entry
Add an entry to the collection. Call this on the C<CPAN::PackageDetails>
object and it will take care of finding the right handler.
If you've set C<allow_packages_only_once> to a true value (which is the
default, too), C<add_entry> will die if you try to add another entry with
the same package name even if it has a different or greater version. You can
set this to a false value and add as many entries as you like then use
C<as_unqiue_sorted_list> to get just the entries with the highest
versions for each package.
=cut
sub _parse_version {
my( $self, $version ) = @_;
my $warning;
local $SIG{__WARN__} = sub { $warning = join "\n", @_ };
my( $parsed, $alpha ) = eval {
die "Version string is undefined\n" unless defined $version;
die "Version string is empty\n" if '' eq $version;
my $v = version->parse($version);
map { $v->$_() } qw( numify is_alpha );
};
do {
no warnings 'uninitialized';
my $at = $@;
chomp, s/\s+at\s+.*// for ( $at, $warning );
$warning = undef if $warning =~ m/numify\(\) is lossy/i;
if( $at ) { ( 0, $alpha, $at ) }
elsif( defined $warning ) { ( $parsed, $alpha, $warning ) }
else { ( $parsed, $alpha, undef ) }
};
}
sub add_entry {
my( $self, %args ) = @_;
$self->_mark_as_dirty;
# The column name has a space in it, but that looks weird in a
# hash constructor and I keep doing it wrong. If I type "package_name"
# I'll just make it work.
if( exists $args{package_name} ) {
$args{'package name'} = $args{package_name};
delete $args{package_name};
}
my( $parsed, $alpha, $warning ) = $self->_parse_version( $args{'version'} );
if( defined $warning and warnings::enabled() ) {
$warning = "add_entry has a problem parsing [$args{'version'}] for package [$args{'package name'}]: [$warning] I'm using [$parsed] as the version for [$args{'package name'}].\n";
warnings::warn( $warning );
}
if( $self->disallow_alpha_versions && $alpha ) {
croak "add_entry interprets [$parsed] as an alpha version, and disallow_alpha_versions is on";
}
unless( defined $args{'package name'} ) {
croak "No 'package name' parameter!";
return;
}
unless( $args{'package name'} =~ m/
^
[A-Za-z0-9_]+
(?:
(?:\::|')
[A-Za-z0-9_]+
)*
\z
/x || $self->allow_suspicious_names ) {
croak "Package name [$args{'package name'}] looks suspicious. Not adding it!";
return;
}
if( $self->allow_packages_only_once and $self->already_added( $args{'package name'} ) ) {
croak "$args{'package name'} was already added to CPAN::PackageDetails!";
return;
}
# should check for allowed columns here
# XXX: this part needs to change based on storage
$self->{entries}{
$args{'package name'}
}{$args{'version'}
} = $self->entry_class->new( %args );
return 1;
}
sub _mark_as_dirty {
delete $_[0]->{sorted};
}
=item already_added( PACKAGE )
Returns true if there is already an entry for PACKAGE.
=cut
# XXX: this part needs to change based on storage
sub already_added { exists $_[0]->{entries}{$_[1]} }
=item as_string
Returns a text version of the Entries object. This calls C<as_string>
on each Entry object, and concatenates the results for all Entry objects.
=cut
sub as_string {
my( $self ) = @_;
my $string;
my( $return ) = $self->as_unique_sorted_list;
foreach my $entry ( @$return ) {
$string .= $entry->as_string( $self->columns );
}
$string || '';
}
=item as_unique_sorted_list
In list context, this returns a list of entries sorted by package name
and version. Each package exists exactly once in the list and with the
largest version number seen.
In scalar context this returns the count of the number of unique entries.
Once called, it caches its result until you add more entries.
=cut
sub VERSION_PM () { 9 }
sub as_unique_sorted_list {
my( $self ) = @_;
unless( ref $self->{sorted} eq ref [] ) {
$self->{sorted} = [];
my %Seen;
my( $k1, $k2 ) = ( $self->columns )[0,1];
my $e = $self->get_hash;
# We only want the latest versions of everything:
foreach my $package ( sort keys %$e ) {
my $entries = $e->{$package};
eval {
eval { require version } or die "Could not load version.pm!";
die "Your version of the version module doesn't handle the parse method!"
unless version->can('parse');
} or croak( {
message => $@,
have_version => eval { version->VERSION },
need_version => 0.74,
inc => [ @INC ],
error => VERSION_PM,
}
);
my( $highest_version ) =
map { $_->[0] }
sort { $b->[1] <=> $a->[1] } # sort on version objects
map {
my $w;
local $SIG{__WARN__} = sub { $w = join "\n", @_ };
my $v = eval { version->new( $_ ) };
$w = $w || $@;
$w = s/\s+at\s+//;
carp "Version [$_] for package [$package] parses with a warning: [$w]. Using [$v] as the version."
if $w;
if( $self->disallow_alpha_versions and $v->is_alpha ) {
carp "Skipping alpha version [$v] for [$package] while sorting versions.";
()
}
else { [ $_, $v ] }
}
keys %$entries;
push @{ $self->{sorted} }, $entries->{$highest_version};
}
}
my $return = wantarray ?
$self->{sorted}
:
scalar @{ $self->{sorted} };
return $return;
}
=item get_entries_by_distribution( DISTRIBUTION )
Returns the entry objects for the named DISTRIBUTION.
=cut
sub get_entries_by_distribution {
require CPAN::DistnameInfo;
my( $self, $distribution ) = @_;
croak "You must specify a distribution!" unless defined $distribution;
my @entries =
grep { # $_ is the entry hash
my $info = CPAN::DistnameInfo->new( $_->{'path'} );
defined $info->dist && $info->dist eq $distribution;
}
map { # $_ is the package name
values %{ $self->{entries}{$_} }
}
keys %{ $self->{entries} };
}
=item get_entries_by_package( PACKAGE )
Returns the entry objects for the named PACKAGE.
=cut
sub get_entries_by_package {
my( $self, $package ) = @_;
my @entries =
map { values %{$self->{entries}{$package}} }
grep { $_ eq $package }
keys %{ $self->{entries} };
}
=item get_entries_by_path( PATH )
Returns the entry objects for any entries with PATH.
=cut
sub get_entries_by_path {
my( $self, $path ) = @_;
my @entries =
map { $self->{entries}{$_}{$path} }
grep { exists $self->{entries}{$_}{$path} }
keys %{ $self->{entries} };
}
=item get_entries_by_version( VERSION )
Returns the entry objects for any entries with VERSION.
=cut
sub get_entries_by_version {
my( $self, $version ) = @_;
my @entries =
map { $self->{entries}{$_}{$version} }
grep { exists $self->{entries}{$_}{$version} }
keys %{ $self->{entries} };
}
=back
=head1 TO DO
=head1 SEE ALSO
=head1 SOURCE AVAILABILITY
This source is in Github:
https://github.com/briandfoy/cpan-packagedetails
=head1 AUTHOR
brian d foy, C<< <briandfoy@pobox.com> >>
=head1 COPYRIGHT AND LICENSE
Copyright © 2009-2024, brian d foy <briandfoy@pobox.com>. All rights reserved.
You may redistribute this under the terms of the Artistic License 2.0.
=cut
1;