File: perl.prov

package info (click to toggle)
libb-perlreq-perl 0.82-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 700 kB
  • ctags: 909
  • sloc: perl: 1,090; sh: 69; makefile: 11
file content (236 lines) | stat: -rwxr-xr-x 6,088 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl

use 5.006;
use strict;

use PerlReq::Utils qw(argv explode inc mod2path path2mod path2dep sv_version verf);

sub pod2usage {
	eval { require Pod::Usage } or die $@;
	goto &Pod::Usage::pod2usage;
}

use Getopt::Long 2.24 qw(GetOptions :config gnu_getopt);
GetOptions
	"v|verbose+"	=> \my $Verbose,
	"h|help"	=> sub { pod2usage("00") }
		or pod2usage(2);
$Verbose = 2 if $ENV{RPM_SCRIPTS_DEBUG};
$| = 1;

# list of provides
my %prov;

# begin
process_file($_) foreach argv();

sub process_file {
	my $fname = shift;
	warn "# processing $fname\n" if $Verbose > 1;

	my ($prefix, $basename) = explode($fname);
	unless ($prefix) {
		warn "# $fname does not match any prefix\n" if $Verbose > 1;
		return;
	} else {
		warn "# $fname has prefix $prefix\n" if $Verbose > 1;
	}

	if ($fname =~ /\.p[lh]$/) {
		$prov{$basename}{prov} = 1;
		warn "# $fname has basename $basename\n" if $Verbose;
		return;
	} elsif ($basename =~ /\.pm$/) {
		$prov{$basename}{prov} = 1;
		warn "# $fname has basename $basename, checking version\n" if $Verbose;
	} elsif ($basename =~ /\.(al|ix)$/) {
		warn "# $fname (autoloaded file SKIP)" if $Verbose > 1;
		return;
	} elsif ($basename =~ /\.pod$/) {
		warn "# $fname (pod file SKIP)" if $Verbose > 1;
		return;
	} else {
		warn "# $fname has basename $basename (SKIP)\n";
		return;
	}
	my $cur_pkg = my $pkg0 = "main"; my $pkg1;
	my $re_pkg = qr/\b\w+(?:::\w+)*/;
	open my $fh, $fname or die "$0: $fname: $!\n";
	local $_;
	while (<$fh>) {
		chomp;
		next if /^\s*#/;
		next if /^=\w/ .. (/^=cut/ or eof);
		last if /^__(DATA|END)__\b/;
		s/\s+#\s.*//; # strip comments
		if (/^(\s*\{?\s*)package\s+($re_pkg)\s*;/) {
			if ($1) {
				# block package
				$cur_pkg = $pkg1 = $2;
			}
			else {
				# toplevel package
				$cur_pkg = $pkg0 = $2;
				undef $pkg1;
			}
			warn "# package $cur_pkg at $fname line $.\n" if $Verbose > 1;
			next;
		}
		elsif (/^}/ && $pkg1) {
			warn "# back from $pkg1 to $pkg0 at $fname line $.\n" if $Verbose > 1;
			$cur_pkg = $pkg0;
			undef $pkg1;
		}
		/\$(?:($re_pkg)::)?VERSION\s*\)?\s*=/ or next;
		# it's a version assignemnt
		my $pkg = $1;
		my $val = $';

		if ($pkg and mod2path($pkg) ne $basename) {
			warn "# saw \$$pkg\::VERSION at $fname line $. (SKIP)\n";
			next;
		}
		if (not $pkg and mod2path($cur_pkg) ne $basename) {
			warn "# saw \$VERSION in package $cur_pkg at $fname line $. (SKIP)\n";
			next;
		}

		if ($val =~ /\b\d|\d\./) { # there's a version number
			# strip e.g. use vars qw($VERSION)
			s/\buse\s+vars\b[^;]*;//;
			# check next line if it has e.g. $VERSION = eval $VERSION
			if ((my $next_line = <$fh>) =~ /\bVERSION\b.*=/) {
				chomp $next_line;
				$_ .= "\n" . $next_line;
			}
			my $v = extract_version($fname, $_, $pkg);
			$prov{$basename}{ver}{$v} = 1 if $v;
			last;
		}
		elsif ($val =~ /\$($re_pkg)::VERSION\s*;/) {
			# holy Jesus! they assign the version from another package
			warn "# saw \$VERSION reference to \$$1::VERSION at $fname line $.\n" if $Verbose;
			my $basename2 = mod2path($1);
			$prov{$basename}{ver} = $prov{$basename2}{ver} ||= {};
			$prov{$basename}{vref} = $basename2;
			last;
		}
		else {
			warn "# VERSION assignment not recognized at $fname line $. (SKIP)\n";
			warn "# $_\n";
		}
	}
}

# end
foreach my $k (sort { uc($a) cmp uc($b) } keys %prov) {
	next unless $prov{$k}{prov};
	my @v = sort keys %{$prov{$k}{ver}||{}};
	if (not @v and my $k2 = $prov{$k}{vref}) {
		my $m1 = path2mod($k);
		my $m2 = path2mod($k2);
		warn "# unresolved reference from \$$m1\::VERSION to \$$m2\::VERSION\n";
	}
	if (@v) {
		print path2dep($k) . " = $_\n" for @v;
	}
	else {
		print path2dep($k) . "\n";
	}
}

sub extract_version {
	my ($fname, $line, $pkg) = @_;
	warn "# extracting version at line $.:\n# $line\n" if $Verbose > 1;
	my $code = "$line\n; \$VERSION";
	$code =~ s/\$\Q$pkg\E::VERSION/\$VERSION/g if $pkg;

	if ($code =~ s/\buse\s+version\b[^;]*;//g or $code =~ /\bqv\b/
		or $code =~ /\bversion(?:::)?->new\b/ or $code =~ /\bnew\s+version\b/)
	{
		package Sandbox;
		eval "use version";
		warn $@ if $@;
	}

	my $version = do {
		package Sandbox;
		no strict;
		eval $code;
	};
	goto bad if not $version;

	use B qw(svref_2object);
	my $v = sv_version(svref_2object(\$version));
	if ($v) {
		$v = verf($v);
		warn "# VERSION $v at $fname line $.\n" if $Verbose;
		return $v;
	}
bad:
	warn "# WARNING: failed to extract version at $fname line $.:\n";
	warn "# $line\n";
	warn "# $@\n" if $@;
	return;
}

__END__

=head1	NAME

perl.prov - list what Perl source files provide

=head1	SYNOPSIS

B<perl.prov>
[B<-v>|B<--verbose>]
[I<FILE>...]

=head1	DESCRIPTION

This script calculates conventional name for each Perl source I<file>
specified on a command line, based on its location relative to standard
Perl library paths; alternatively, a list of files is obtained from
standard input, one file per line.  F<*.pm>, F<*.pl>, and F<*.ph> files
are processed (F<*.pm> files also suffer version extraction).  The
output of perl.prov is suitable for automatic dependency tracking (e.g.
for RPM packaging).

For example, F</usr/lib/perl5/i386-linux/DB_File.pm> provides
C<perl(DB_File.pm) = 1.810> (as of perl-5.8.6).

perl.prov is a counterpart of L<perl.req>.

=head1	OPTIONS

=over

=item	B<-v>, B<--verbose>

Increase verbosity.

=back

The RPM_PERL_LIB_PATH environment variable, if set, must contain the
list of paths, separated by colons. These paths are considered as
library paths used to determine relative names of provided perl files
(in addition to paths from C<@INC> variable).

=head1	AUTHOR

Written by Alexey Tourbin <at@altlinux.org>,
based on an earlier version by Ken Estes <kestes@staff.mail.com>,
with contributions from Mikhail Zabaluev <mhz@altlinux.org>.

=head1	COPYING

Copyright (c) 2003, 2004, 2006, 2007, 2008 Alexey Tourbin, ALT Linux Team.

This is free software; you can redistribute it and/or modify it under
the terms of the GNU Library General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.

=head1	SEE ALSO

L<perl.req>