Debian Codegraph @

check_rbl/src/inc/Module/Install/Metadata.pm

raw
1 #line 1
2 package Module::Install::Metadata;
3
4 use strict 'vars';
5 use Module::Install::Base ();
6
7 use vars qw{$VERSION @ISA $ISCORE};
8 BEGIN {
9 $VERSION = '1.21';
10 @ISA = 'Module::Install::Base';
11 $ISCORE = 1;
12 }
13
14 my @boolean_keys = qw{
15 sign
16 };
17
18 my @scalar_keys = qw{
19 name
20 module_name
21 abstract
22 version
23 distribution_type
24 tests
25 installdirs
26 };
27
28 my @tuple_keys = qw{
29 configure_requires
30 build_requires
31 requires
32 recommends
33 bundles
34 resources
35 };
36
37 my @resource_keys = qw{
38 homepage
39 bugtracker
40 repository
41 };
42
43 my @array_keys = qw{
44 keywords
45 author
46 };
47
48 *authors = \&author;
49
50 sub Meta { shift }
51 sub Meta_BooleanKeys { @boolean_keys }
52 sub Meta_ScalarKeys { @scalar_keys }
53 sub Meta_TupleKeys { @tuple_keys }
54 sub Meta_ResourceKeys { @resource_keys }
55 sub Meta_ArrayKeys { @array_keys }
56
57 foreach my $key ( @boolean_keys ) {
58 *$key = sub {
59 my $self = shift;
60 if ( defined wantarray and not @_ ) {
61 return $self->{values}->{$key};
62 }
63 $self->{values}->{$key} = ( @_ ? $_[0] : 1 );
64 return $self;
65 };
66 }
67
68 foreach my $key ( @scalar_keys ) {
69 *$key = sub {
70 my $self = shift;
71 return $self->{values}->{$key} if defined wantarray and !@_;
72 $self->{values}->{$key} = shift;
73 return $self;
74 };
75 }
76
77 foreach my $key ( @array_keys ) {
78 *$key = sub {
79 my $self = shift;
80 return $self->{values}->{$key} if defined wantarray and !@_;
81 $self->{values}->{$key} ||= [];
82 push @{$self->{values}->{$key}}, @_;
83 return $self;
84 };
85 }
86
87 foreach my $key ( @resource_keys ) {
88 *$key = sub {
89 my $self = shift;
90 unless ( @_ ) {
91 return () unless $self->{values}->{resources};
92 return map { $_->[1] }
93 grep { $_->[0] eq $key }
94 @{ $self->{values}->{resources} };
95 }
96 return $self->{values}->{resources}->{$key} unless @_;
97 my $uri = shift or die(
98 "Did not provide a value to $key()"
99 );
100 $self->resources( $key => $uri );
101 return 1;
102 };
103 }
104
105 foreach my $key ( grep { $_ ne "resources" } @tuple_keys) {
106 *$key = sub {
107 my $self = shift;
108 return $self->{values}->{$key} unless @_;
109 my @added;
110 while ( @_ ) {
111 my $module = shift or last;
112 my $version = shift || 0;
113 push @added, [ $module, $version ];
114 }
115 push @{ $self->{values}->{$key} }, @added;
116 return map {@$_} @added;
117 };
118 }
119
120 # Resource handling
121 my %lc_resource = map { $_ => 1 } qw{
122 homepage
123 license
124 bugtracker
125 repository
126 };
127
128 sub resources {
129 my $self = shift;
130 while ( @_ ) {
131 my $name = shift or last;
132 my $value = shift or next;
133 if ( $name eq lc $name and ! $lc_resource{$name} ) {
134 die("Unsupported reserved lowercase resource '$name'");
135 }
136 $self->{values}->{resources} ||= [];
137 push @{ $self->{values}->{resources} }, [ $name, $value ];
138 }
139 $self->{values}->{resources};
140 }
141
142 # Aliases for build_requires that will have alternative
143 # meanings in some future version of META.yml.
144 sub test_requires { shift->build_requires(@_) }
145 sub install_requires { shift->build_requires(@_) }
146
147 # Aliases for installdirs options
148 sub install_as_core { $_[0]->installdirs('perl') }
149 sub install_as_cpan { $_[0]->installdirs('site') }
150 sub install_as_site { $_[0]->installdirs('site') }
151 sub install_as_vendor { $_[0]->installdirs('vendor') }
152
153 sub dynamic_config {
154 my $self = shift;
155 my $value = @_ ? shift : 1;
156 if ( $self->{values}->{dynamic_config} ) {
157 # Once dynamic we never change to static, for safety
158 return 0;
159 }
160 $self->{values}->{dynamic_config} = $value ? 1 : 0;
161 return 1;
162 }
163
164 # Convenience command
165 sub static_config {
166 shift->dynamic_config(0);
167 }
168
169 sub perl_version {
170 my $self = shift;
171 return $self->{values}->{perl_version} unless @_;
172 my $version = shift or die(
173 "Did not provide a value to perl_version()"
174 );
175
176 # Normalize the version
177 $version = $self->_perl_version($version);
178
179 # We don't support the really old versions
180 unless ( $version >= 5.005 ) {
181 die "Module::Install only supports 5.005 or newer (use ExtUtils::MakeMaker)\n";
182 }
183
184 $self->{values}->{perl_version} = $version;
185 }
186
187 sub all_from {
188 my ( $self, $file ) = @_;
189
190 unless ( defined($file) ) {
191 my $name = $self->name or die(
192 "all_from called with no args without setting name() first"
193 );
194 $file = join('/', 'lib', split(/-/, $name)) . '.pm';
195 $file =~ s{.*/}{} unless -e $file;
196 unless ( -e $file ) {
197 die("all_from cannot find $file from $name");
198 }
199 }
200 unless ( -f $file ) {
201 die("The path '$file' does not exist, or is not a file");
202 }
203
204 $self->{values}{all_from} = $file;
205
206 # Some methods pull from POD instead of code.
207 # If there is a matching .pod, use that instead
208 my $pod = $file;
209 $pod =~ s/\.pm$/.pod/i;
210 $pod = $file unless -e $pod;
211
212 # Pull the different values
213 $self->name_from($file) unless $self->name;
214 $self->version_from($file) unless $self->version;
215 $self->perl_version_from($file) unless $self->perl_version;
216 $self->author_from($pod) unless @{$self->author || []};
217 $self->license_from($pod) unless $self->license;
218 $self->abstract_from($pod) unless $self->abstract;
219
220 return 1;
221 }
222
223 sub provides {
224 my $self = shift;
225 my $provides = ( $self->{values}->{provides} ||= {} );
226 %$provides = (%$provides, @_) if @_;
227 return $provides;
228 }
229
230 sub auto_provides {
231 my $self = shift;
232 return $self unless $self->is_admin;
233 unless (-e 'MANIFEST') {
234 warn "Cannot deduce auto_provides without a MANIFEST, skipping\n";
235 return $self;
236 }
237 # Avoid spurious warnings as we are not checking manifest here.
238 local $SIG{__WARN__} = sub {1};
239 require ExtUtils::Manifest;
240 local *ExtUtils::Manifest::manicheck = sub { return };
241
242 require Module::Build;
243 my $build = Module::Build->new(
244 dist_name => $self->name,
245 dist_version => $self->version,
246 license => $self->license,
247 );
248 $self->provides( %{ $build->find_dist_packages || {} } );
249 }
250
251 sub feature {
252 my $self = shift;
253 my $name = shift;
254 my $features = ( $self->{values}->{features} ||= [] );
255 my $mods;
256
257 if ( @_ == 1 and ref( $_[0] ) ) {
258 # The user used ->feature like ->features by passing in the second
259 # argument as a reference. Accomodate for that.
260 $mods = $_[0];
261 } else {
262 $mods = \@_;
263 }
264
265 my $count = 0;
266 push @$features, (
267 $name => [
268 map {
269 ref($_) ? ( ref($_) eq 'HASH' ) ? %$_ : @$_ : $_
270 } @$mods
271 ]
272 );
273
274 return @$features;
275 }
276
277 sub features {
278 my $self = shift;
279 while ( my ( $name, $mods ) = splice( @_, 0, 2 ) ) {
280 $self->feature( $name, @$mods );
281 }
282 return $self->{values}->{features}
283 ? @{ $self->{values}->{features} }
284 : ();
285 }
286
287 sub no_index {
288 my $self = shift;
289 my $type = shift;
290 push @{ $self->{values}->{no_index}->{$type} }, @_ if $type;
291 return $self->{values}->{no_index};
292 }
293
294 sub read {
295 my $self = shift;
296 $self->include_deps( 'YAML::Tiny', 0 );
297
298 require YAML::Tiny;
299 my $data = YAML::Tiny::LoadFile('META.yml');
300
301 # Call methods explicitly in case user has already set some values.
302 while ( my ( $key, $value ) = each %$data ) {
303 next unless $self->can($key);
304 if ( ref $value eq 'HASH' ) {
305 while ( my ( $module, $version ) = each %$value ) {
306 $self->can($key)->($self, $module => $version );
307 }
308 } else {
309 $self->can($key)->($self, $value);
310 }
311 }
312 return $self;
313 }
314
315 sub write {
316 my $self = shift;
317 return $self unless $self->is_admin;
318 $self->admin->write_meta;
319 return $self;
320 }
321
322 sub version_from {
323 require ExtUtils::MM_Unix;
324 my ( $self, $file ) = @_;
325 $self->version( ExtUtils::MM_Unix->parse_version($file) );
326
327 # for version integrity check
328 $self->makemaker_args( VERSION_FROM => $file );
329 }
330
331 sub abstract_from {
332 require ExtUtils::MM_Unix;
333 my ( $self, $file ) = @_;
334 $self->abstract(
335 bless(
336 { DISTNAME => $self->name },
337 'ExtUtils::MM_Unix'
338 )->parse_abstract($file)
339 );
340 }
341
342 # Add both distribution and module name
343 sub name_from {
344 my ($self, $file) = @_;
345 if (
346 Module::Install::_read($file) =~ m/
347 ^ \s*
348 package \s*
349 ([\w:]+)
350 [\s|;]*
351 /ixms
352 ) {
353 my ($name, $module_name) = ($1, $1);
354 $name =~ s{::}{-}g;
355 $self->name($name);
356 unless ( $self->module_name ) {
357 $self->module_name($module_name);
358 }
359 } else {
360 die("Cannot determine name from $file\n");
361 }
362 }
363
364 sub _extract_perl_version {
365 if (
366 $_[0] =~ m/
367 ^\s*
368 (?:use|require) \s*
369 v?
370 ([\d_\.]+)
371 \s* ;
372 /ixms
373 ) {
374 my $perl_version = $1;
375 $perl_version =~ s{_}{}g;
376 return $perl_version;
377 } else {
378 return;
379 }
380 }
381
382 sub perl_version_from {
383 my $self = shift;
384 my $perl_version=_extract_perl_version(Module::Install::_read($_[0]));
385 if ($perl_version) {
386 $self->perl_version($perl_version);
387 } else {
388 warn "Cannot determine perl version info from $_[0]\n";
389 return;
390 }
391 }
392
393 sub author_from {
394 my $self = shift;
395 my $content = Module::Install::_read($_[0]);
396 if ($content =~ m/
397 =head \d \s+ (?:authors?)\b \s*
398 ([^\n]*)
399 |
400 =head \d \s+ (?:licen[cs]e|licensing|copyright|legal)\b \s*
401 .*? copyright .*? \d\d\d[\d.]+ \s* (?:\bby\b)? \s*
402 ([^\n]*)
403 /ixms) {
404 my $author = $1 || $2;
405
406 # XXX: ugly but should work anyway...
407 if (eval "require Pod::Escapes; 1") {
408 # Pod::Escapes has a mapping table.
409 # It's in core of perl >= 5.9.3, and should be installed
410 # as one of the Pod::Simple's prereqs, which is a prereq
411 # of Pod::Text 3.x (see also below).
412 $author =~ s{ E<( (\d+) | ([A-Za-z]+) )> }
413 {
414 defined $2
415 ? chr($2)
416 : defined $Pod::Escapes::Name2character_number{$1}
417 ? chr($Pod::Escapes::Name2character_number{$1})
418 : do {
419 warn "Unknown escape: E<$1>";
420 "E<$1>";
421 };
422 }gex;
423 }
424 elsif (eval "require Pod::Text; 1" && $Pod::Text::VERSION < 3) {
425 # Pod::Text < 3.0 has yet another mapping table,
426 # though the table name of 2.x and 1.x are different.
427 # (1.x is in core of Perl < 5.6, 2.x is in core of
428 # Perl < 5.9.3)
429 my $mapping = ($Pod::Text::VERSION < 2)
430 ? \%Pod::Text::HTML_Escapes
431 : \%Pod::Text::ESCAPES;
432 $author =~ s{ E<( (\d+) | ([A-Za-z]+) )> }
433 {
434 defined $2
435 ? chr($2)
436 : defined $mapping->{$1}
437 ? $mapping->{$1}
438 : do {
439 warn "Unknown escape: E<$1>";
440 "E<$1>";
441 };
442 }gex;
443 }
444 else {
445 $author =~ s{E<lt>}{<}g;
446 $author =~ s{E<gt>}{>}g;
447 }
448 $self->author($author);
449 } else {
450 warn "Cannot determine author info from $_[0]\n";
451 }
452 }
453
454 #Stolen from M::B
455 my %license_urls = (
456 perl => 'http://dev.perl.org/licenses/',
457 apache => 'http://apache.org/licenses/LICENSE-2.0',
458 artistic => 'http://opensource.org/licenses/artistic-license.php',
459 lgpl => 'http://opensource.org/licenses/lgpl-license.php',
460 bsd => 'http://opensource.org/licenses/bsd-license.php',
461 gpl => 'http://opensource.org/licenses/gpl-license.php',
462 gpl2 => 'http://opensource.org/licenses/gpl-2.0.php',
463 gpl3 => 'http://opensource.org/licenses/gpl-3.0.html',
464 mit => 'http://opensource.org/licenses/mit-license.php',
465 mozilla => 'http://opensource.org/licenses/mozilla1.1.php',
466 open_source => undef,
467 unrestricted => undef,
468 restrictive => undef,
469 unknown => undef,
470
471 # these are not actually allowed in meta-spec v1.4 but are left here for compatibility:
472 apache_1_1 => 'http://apache.org/licenses/LICENSE-1.1',
473 artistic_2 => 'http://opensource.org/licenses/artistic-license-2.0.php',
474 lgpl2 => 'http://opensource.org/licenses/lgpl-2.1.php',
475 lgpl3 => 'http://opensource.org/licenses/lgpl-3.0.html',
476 );
477
478 sub license {
479 my $self = shift;
480 return $self->{values}->{license} unless @_;
481 my $license = shift or die(
482 'Did not provide a value to license()'
483 );
484 $license = __extract_license($license) || lc $license;
485 $self->{values}->{license} = $license;
486
487 # Automatically fill in license URLs
488 if ( $license_urls{$license} ) {
489 $self->resources( license => $license_urls{$license} );
490 }
491
492 return 1;
493 }
494
495 sub _extract_license {
496 my $pod = shift;
497 my $matched;
498 return __extract_license(
499 ($matched) = $pod =~ m/
500 (=head \d \s+ L(?i:ICEN[CS]E|ICENSING)\b.*?)
501 (=head \d.*|=cut.*|)\z
502 /xms
503 ) || __extract_license(
504 ($matched) = $pod =~ m/
505 (=head \d \s+ (?:C(?i:OPYRIGHTS?)|L(?i:EGAL))\b.*?)
506 (=head \d.*|=cut.*|)\z
507 /xms
508 );
509 }
510
511 sub __extract_license {
512 my $license_text = shift or return;
513 my @phrases = (
514 '(?:under )?the same (?:terms|license) as (?:perl|the perl (?:\d )?programming language)' => 'perl', 1,
515 '(?:under )?the terms of (?:perl|the perl programming language) itself' => 'perl', 1,
516 'Artistic and GPL' => 'perl', 1,
517 'GNU general public license' => 'gpl', 1,
518 'GNU public license' => 'gpl', 1,
519 'GNU lesser general public license' => 'lgpl', 1,
520 'GNU lesser public license' => 'lgpl', 1,
521 'GNU library general public license' => 'lgpl', 1,
522 'GNU library public license' => 'lgpl', 1,
523 'GNU Free Documentation license' => 'unrestricted', 1,
524 'GNU Affero General Public License' => 'open_source', 1,
525 '(?:Free)?BSD license' => 'bsd', 1,
526 'Artistic license 2\.0' => 'artistic_2', 1,
527 'Artistic license' => 'artistic', 1,
528 'Apache (?:Software )?license' => 'apache', 1,
529 'GPL' => 'gpl', 1,
530 'LGPL' => 'lgpl', 1,
531 'BSD' => 'bsd', 1,
532 'Artistic' => 'artistic', 1,
533 'MIT' => 'mit', 1,
534 'Mozilla Public License' => 'mozilla', 1,
535 'Q Public License' => 'open_source', 1,
536 'OpenSSL License' => 'unrestricted', 1,
537 'SSLeay License' => 'unrestricted', 1,
538 'zlib License' => 'open_source', 1,
539 'proprietary' => 'proprietary', 0,
540 );
541 while ( my ($pattern, $license, $osi) = splice(@phrases, 0, 3) ) {
542 $pattern =~ s#\s+#\\s+#gs;
543 if ( $license_text =~ /\b$pattern\b/i ) {
544 return $license;
545 }
546 }
547 return '';
548 }
549
550 sub license_from {
551 my $self = shift;
552 if (my $license=_extract_license(Module::Install::_read($_[0]))) {
553 $self->license($license);
554 } else {
555 warn "Cannot determine license info from $_[0]\n";
556 return 'unknown';
557 }
558 }
559
560 sub _extract_bugtracker {
561 my @links = $_[0] =~ m#L<(
562 https?\Q://rt.cpan.org/\E[^>]+|
563 https?\Q://github.com/\E[\w_]+/[\w_]+/issues|
564 https?\Q://code.google.com/p/\E[\w_\-]+/issues/list
565 )>#gx;
566 my %links;
567 @links{@links}=();
568 @links=keys %links;
569 return @links;
570 }
571
572 sub bugtracker_from {
573 my $self = shift;
574 my $content = Module::Install::_read($_[0]);
575 my @links = _extract_bugtracker($content);
576 unless ( @links ) {
577 warn "Cannot determine bugtracker info from $_[0]\n";
578 return 0;
579 }
580 if ( @links > 1 ) {
581 warn "Found more than one bugtracker link in $_[0]\n";
582 return 0;
583 }
584
585 # Set the bugtracker
586 bugtracker( $links[0] );
587 return 1;
588 }
589
590 sub requires_from {
591 my $self = shift;
592 my $content = Module::Install::_readperl($_[0]);
593 my @requires = $content =~ m/^use\s+([^\W\d]\w*(?:::\w+)*)\s+(v?[\d\.]+)/mg;
594 while ( @requires ) {
595 my $module = shift @requires;
596 my $version = shift @requires;
597 $self->requires( $module => $version );
598 }
599 }
600
601 sub test_requires_from {
602 my $self = shift;
603 my $content = Module::Install::_readperl($_[0]);
604 my @requires = $content =~ m/^use\s+([^\W\d]\w*(?:::\w+)*)\s+([\d\.]+)/mg;
605 while ( @requires ) {
606 my $module = shift @requires;
607 my $version = shift @requires;
608 $self->test_requires( $module => $version );
609 }
610 }
611
612 # Convert triple-part versions (eg, 5.6.1 or 5.8.9) to
613 # numbers (eg, 5.006001 or 5.008009).
614 # Also, convert double-part versions (eg, 5.8)
615 sub _perl_version {
616 my $v = $_[-1];
617 $v =~ s/^([1-9])\.([1-9]\d?\d?)$/sprintf("%d.%03d",$1,$2)/e;
618 $v =~ s/^([1-9])\.([1-9]\d?\d?)\.(0|[1-9]\d?\d?)$/sprintf("%d.%03d%03d",$1,$2,$3 || 0)/e;
619 $v =~ s/(\.\d\d\d)000$/$1/;
620 $v =~ s/_.+$//;
621 if ( ref($v) ) {
622 # Numify
623 $v = $v + 0;
624 }
625 return $v;
626 }
627
628 sub add_metadata {
629 my $self = shift;
630 my %hash = @_;
631 for my $key (keys %hash) {
632 warn "add_metadata: $key is not prefixed with 'x_'.\n" .
633 "Use appopriate function to add non-private metadata.\n" unless $key =~ /^x_/;
634 $self->{values}->{$key} = $hash{$key};
635 }
636 }
637
638
639 ######################################################################
640 # MYMETA Support
641
642 sub WriteMyMeta {
643 die "WriteMyMeta has been deprecated";
644 }
645
646 sub write_mymeta_yaml {
647 my $self = shift;
648
649 # We need YAML::Tiny to write the MYMETA.yml file
650 unless ( eval { require YAML::Tiny; 1; } ) {
651 return 1;
652 }
653
654 # Generate the data
655 my $meta = $self->_write_mymeta_data or return 1;
656
657 # Save as the MYMETA.yml file
658 print "Writing MYMETA.yml\n";
659 YAML::Tiny::DumpFile('MYMETA.yml', $meta);
660 }
661
662 sub write_mymeta_json {
663 my $self = shift;
664
665 # We need JSON to write the MYMETA.json file
666 unless ( eval { require JSON; 1; } ) {
667 return 1;
668 }
669
670 # Generate the data
671 my $meta = $self->_write_mymeta_data or return 1;
672
673 # Save as the MYMETA.yml file
674 print "Writing MYMETA.json\n";
675 Module::Install::_write(
676 'MYMETA.json',
677 JSON->new->pretty(1)->canonical->encode($meta),
678 );
679 }
680
681 sub _write_mymeta_data {
682 my $self = shift;
683
684 # If there's no existing META.yml there is nothing we can do
685 return undef unless -f 'META.yml';
686
687 # We need Parse::CPAN::Meta to load the file
688 unless ( eval { require Parse::CPAN::Meta; 1; } ) {
689 return undef;
690 }
691
692 # Merge the perl version into the dependencies
693 my $val = $self->Meta->{values};
694 my $perl = delete $val->{perl_version};
695 if ( $perl ) {
696 $val->{requires} ||= [];
697 my $requires = $val->{requires};
698
699 # Canonize to three-dot version after Perl 5.6
700 if ( $perl >= 5.006 ) {
701 $perl =~ s{^(\d+)\.(\d\d\d)(\d*)}{join('.', $1, int($2||0), int($3||0))}e
702 }
703 unshift @$requires, [ perl => $perl ];
704 }
705
706 # Load the advisory META.yml file
707 my @yaml = Parse::CPAN::Meta::LoadFile('META.yml');
708 my $meta = $yaml[0];
709
710 # Overwrite the non-configure dependency hashes
711 delete $meta->{requires};
712 delete $meta->{build_requires};
713 delete $meta->{recommends};
714 if ( exists $val->{requires} ) {
715 $meta->{requires} = { map { @$_ } @{ $val->{requires} } };
716 }
717 if ( exists $val->{build_requires} ) {
718 $meta->{build_requires} = { map { @$_ } @{ $val->{build_requires} } };
719 }
720
721 return $meta;
722 }
723
724 1;