-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFile.pm
357 lines (279 loc) · 9.03 KB
/
File.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
package MetaCPAN::File;
use strict;
use warnings;
use v5.36;
use List::AllUtils qw< any >;
use Path::Tiny qw< path >;
use MetaCPAN::Logger qw< :log :dlog >;
use MetaCPAN::Ingest qw<
extract_section
strip_pod
>;
my @NOT_PERL_FILES = qw< SIGNATURE >;
my $RE_SECTION = qr/^\s*(\S+)((\h+-+\h+(.+))|(\r?\n\h*\r?\n\h*(.+)))?/ms;
sub new ( $class, %args ) {
my $name = $args{name} or die "Missing name arg\n";
my $release = $args{release} or die "Missing release arg\n";
my $child = path($name);
return if $class->_is_broken_file($name);
my $relative = $child->relative( $release->{extract_dir} );
my $stat = do {
my $s = $child->stat;
+{ map { $_ => $s->$_ } qw< mode size mtime > };
};
return if ( $relative eq q{.} );
my $directory = $child->is_dir;
( my $fpath = "$relative" ) =~ s/^.*?\///;
my $filename = $fpath;
$directory
? $filename =~ s/^(.*\/)?(.+?)\/?$/$2/
: $filename =~ s/.*\///;
$fpath = q{}
if $relative !~ /\// && !$release->{archive}->is_impolite;
my ( $dist, $metadata ) = @{$release}{qw< dist_info metadata >};
my $self = DlogS_trace {"adding file $_"} +{
author => $dist->cpanid,
binary => -B $child,
content => $directory ? "" : ( scalar $child->slurp ),
date => DateTime->from_epoch( epoch => $child->stat->mtime ) . "",
directory => $directory,
distribution => $dist->dist,
local_path => $child . "",
maturity => $dist->maturity,
metadata => $metadata->as_struct,
modules => [],
name => $filename,
path => $fpath,
download_url => $args{download_url},
release => $dist->distvname,
stat => $stat,
status => $release->{status},
version => $release->{version},
};
bless $self, $class;
# need the metadata object, not the struct
$self->{indexed} = $self->_should_index_file($metadata);
$self->_set_mime();
return $self;
}
sub as_struct ($self) {
return +{ map { $_ => $self->{$_} } keys %$self };
}
sub _is_broken_file ( $self, $name ) {
return 1 if ( -p $name || !-e $name );
if ( -l $name ) {
my $syml = readlink $name;
return 1 if ( !-e $name && !-l $name );
}
return 0;
}
sub add_module ( $self, $module ) {
push @{ $self->{modules} }, $module;
}
sub _should_index_file ( $self, $metadata ) {
return 0 if !$metadata->should_index_file( $self->{path} );
# files listed under 'other files' are not shown in a search
return 0 if $self->_is_in_other_files();
# files under no_index directories should not be indexed
return 0
if grep { $self->{path} eq $_ or $self->{path} =~ m|^$_/| }
@{ $metadata->no_index->{directory} };
return 1;
}
sub _set_mime ($self) {
my $mime;
if ( !$self->{directory}
&& $self->{name} !~ /\./
&& grep { $self->{name} ne $_ } @NOT_PERL_FILES )
{
$mime = "text/x-script.perl" if ( $self->{content} =~ /^#!.*?perl/ );
}
else {
$mime = Plack::MIME->mime_type( $self->{name} ) || 'text/plain';
}
$self->{mime} = $mime;
}
sub _is_in_other_files ($self) {
my @other = qw<
AUTHORS
Build.PL
Changelog
ChangeLog
CHANGELOG
Changes
CHANGES
CONTRIBUTING
CONTRIBUTING.md
CONTRIBUTING.pod
Copying
COPYRIGHT
cpanfile
CREDITS
dist.ini
FAQ
INSTALL
INSTALL.md
INSTALL.pod
LICENSE
Makefile.PL
MANIFEST
META.json
META.yml
NEWS
README
README.md
README.pod
THANKS
Todo
ToDo
TODO
>;
return any { $self->{name} eq $_ } @other;
}
sub full_path ($self) {
return join( '/', @{$self}{qw< author release path >} );
}
=head2 is_perl_file
Return true if the file extension is one of C<pl>, C<pm>, C<pod>, C<t>
or if the file has no extension, is not a binary file and its size is less
than 131072 bytes. This is an arbitrary limit but it keeps the pod parser
happy and the indexer fast.
=cut
sub _is_perl_file ($self) {
return 0 if ( $self->{directory} );
return 1 if ( $self->{name} =~ /\.(pl|pm|pod|t)$/i );
return 1 if ( $self->{mime} and $self->{mime} eq "text/x-script.perl" );
return 1
if ( $self->{name} !~ /\./
&& !( grep { $self->{name} eq $_ } @NOT_PERL_FILES )
&& !$self->{binary}
&& $self->{stat}{size} < 2**17 );
return 0;
}
sub _section ($self) {
my $section = extract_section( $self->{content}, 'NAME' );
# if it's a POD file without a name section, let's try to generate
# an abstract and name based on filename
if ( !$section && $self->{path} =~ /\.pod$/ ) {
$section = $self->{path};
$section =~ s{^(lib|pod|docs)/}{};
$section =~ s{\.pod$}{};
$section =~ s{/}{::}g;
}
return undef unless ($section);
$section =~ s/^=\w+.*$//mg;
$section =~ s/X<.*?>//mg;
return $section;
}
=head2 documentation
Holds the name for the documentation in this file.
If the file L<is a pod file|/is_pod_file>, the name is derived from the
C<NAME> section. If the file L<is a perl file|/is_perl_file> and the
name from the C<NAME> section matches one of the modules in L</module>,
it returns the name. Otherwise it returns the name of the first module
in L</module>. If there are no modules in the file the documentation is
set to C<undef>.
=cut
sub add_documentation ($self) {
return undef unless $self->_is_perl_file();
my $section = $self->_section();
return undef unless $section;
my $doc;
my $val;
if ( $section =~ $RE_SECTION ) {
my $name = strip_pod($1);
$doc = $name if $name =~ /^[\w\.:\-_']+$/;
}
return undef unless length $doc;
# Modules to be indexed
my @indexed = grep { $_->{indexed} } @{ $self->{modules} };
# This is a Pod file, return its name
$val = $doc
if !$val
and $doc
and $self->_is_perl_file();
# OR: found an indexed module with the same name
$val = $doc
if !$val
and $doc
and grep { $_->{name} eq $doc } @indexed;
# OR: found an indexed module with a name
if ( !$val and my ($mod) = grep { defined $_->{name} } @indexed ) {
$val = $mod->{name};
}
# OR: we have a parsed documentation
$val = $doc if !$val and defined $doc;
# OR: found ANY module with a name (better than nothing)
if ( !$val
and my ($mod) = grep { defined $_->{name} } @{ $self->{modules} } )
{
return $mod->{name};
}
$self->{documentation} = $val;
$self->{documentation_length} = length($val);
return undef;
}
sub _is_pod_file ($self) {
$self->{name} =~ /\.pod$/i;
}
=head2 suggest
Autocomplete info for documentation.
=cut
sub set_suggest ($self) {
my $doc = $self->{documentation};
return "" unless $doc;
my $weight = 1000 - length($doc);
$weight = 0 if $weight < 0;
$self->{suggest} = +{
input => [$doc],
payload => { doc_name => $doc },
weight => $weight,
};
}
sub set_deprecated ( $self, $value ) {
$self->{deprecated} = $value;
}
sub empty_modules ($self) {
$self->{modules} = [];
}
=head2 set_authorized
Expects a C<$perms> parameter which is a HashRef. The key is the module name
and the value an ArrayRef of author names who are allowed to release
that module.
The method returns a list of unauthorized, but indexed modules.
Unauthorized modules are modules that were uploaded in the name of a
different author than stated in the C<06perms.txt.gz> file. One problem
with this file is, that it doesn't record historical data. It may very
well be that an author was authorized to upload a module at the time.
But then his co-maintainer rights might have been revoked, making consecutive
uploads of that release unauthorized. However, since this script runs
with the latest version of C<06perms.txt.gz>, the former upload will
be flagged as unauthorized as well. Same holds the other way round,
a previously unauthorized release would be flagged authorized if the
co-maintainership was added later on.
If a release contains unauthorized modules, the whole release is marked
as unauthorized as well.
=cut
sub set_authorized ( $self, $perms ) {
# only authorized perl distributions make it into the CPAN
return () if ( $self->{distribution} eq 'perl' );
foreach my $module ( @{ $self->{modules} } ) {
my $name = $module->{name};
if ( $perms->{$name}
&& !grep { $_ eq $self->{author} } @{ $perms->{$name} } )
{
$module->{authorized} = 0;
}
}
my $doc = $self->{documentation};
if ( $self->{authorized}
&& $doc
&& $perms->{$doc}
&& !grep { $_ eq $self->{author} } @{ $perms->{$doc} } )
{
$self->{authorized} = 0;
}
return grep { !$_->{authorized} && $_->{indexed} } @{ $self->{modules} };
}
1;
__END__