File: perl.req

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 (425 lines) | stat: -rwxr-xr-x 10,725 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

use strict;

use Config qw(%Config);
use PerlReq::Utils qw(argv inc explode mod2dep path2dep);

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

use Getopt::Long 2.24 qw(GetOptions :config gnu_getopt);
GetOptions
	"m|method=s"	=> \my $Method,
	"v|verbose+"	=> \my $Verbose,
	"h|help"	=> sub { pod2usage("00") }
		or pod2usage(2);
$Verbose = 2 if $ENV{RPM_SCRIPTS_DEBUG};
$Method ||= $ENV{RPM_PERL_REQ_METHOD} || "normal";
$Method =~ s/\s//g;
$Method eq "strict" || $Method eq "normal" || $Method eq "relaxed" ||
	pod2usage("$0: invalid method $Method");
$| = 1;

my @Skip = (
#	qr(/usr/share/doc/),
#	qr(/[Dd]emos?/),
#	qr(/examples?/),
	qr(\bOS2|OS2\b),
	qr(\bMacPerl|\bMac\b),
	qr(\bMacOS|MacOS\b),
	qr(\bMacOSX|MacOSX\b),
	qr(\bvmsish\b),
	qr(\bVMS|VMS\b),
	qr(\bWin32|Win32\b),
	qr(\bCygwin|Cygwin\b),
);

sub prereq_pm {
	my %prereq;
	my $dir = $ENV{RPM_BUILD_ROOT} || ".";
	open my $fh, "$dir/.perl.req"
		or return;
	warn "# processing $dir/.perl.req\n" if $Verbose;
	local $_ = join "" => grep /^perl[(]/ => <$fh>;
	while (s/\bperl[(]([\w:]+)[)]>=([v\d._]+)//) {
		my $dep = mod2dep($1);
		my $ver = $2;
		if ($ver) {
			use B qw(svref_2object);
			use PerlReq::Utils qw(sv_version);
			$ver = sv_version(svref_2object(\$ver));
		}
		if ($ver) {
			use PerlReq::Utils qw(verf);
			$ver = verf($ver);
			warn "#\t$dep >= $ver\n" if $Verbose;
			$prereq{$dep}{$ver} = undef;
		} else {
			warn "#\t$dep\n" if $Verbose;
			$prereq{$dep} ||= undef;
		}
	}
	return %prereq;
}

# list of requires
my %req;

# modules outside established module path
my %weak_prov;

# process PRINT_PREREQ output
my %prereq = prereq_pm();

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

sub process_file {
	my $fname = shift;
	my ($prefix, $basename) = explode($fname);

	if (not $prefix and $fname =~ /\.p[lmh]$/) {
		local $_ = $fname;
		s#^\Q$ENV{RPM_BUILD_ROOT}\E/*##g if $ENV{RPM_BUILD_ROOT};
		$weak_prov{path2dep($_)} = $fname while s#.+?/##;
	}

	if ($Method ne "strict" and $basename and grep { $basename =~ $_ } @Skip) {
		warn "# $fname (builtin SKIP)\n";
		return;
	}
	warn "# processing $fname\n" if $Verbose > 1;

	do_deparse($fname) and return;

# deparse failed, handle errors
	if ($Method eq "relaxed") {
		warn "# $fname: deparse failed, but I don't care.\n";
		return;
	} elsif ($Method eq "strict") {
		die "# $fname: deparse failed.\n";
	}

# we are not quite sure this is perl file
	unless ($prefix) {
		my $v = isPerl($fname);
		die  "# $fname: deparse failed. isPerl=$v.\n" if $v > 0;
		warn "# $fname: deparse failed, isPerl=$v, ok.\n";
		return;
	}

# it's a module, try to recover
	goto bad if $Method eq "strict";
# find out a `superclass' and try to use it
# examples:
# 	Math::BigInt::CalcEmu	implies	Math::BigInt	loaded
#	Pod::Perldoc::ToMan	implies Pod::Perldoc	loaded
#	Tk::Event::IO		implies	Tk::Event	loaded
#	...
#	bytes_heavy.pl          implies bytes.pm        loaded
#
	my $super = $basename;
	$super =~ s/\//::/g and $super =~ s/(.+)::.*/$1/
		or
	$super =~ s/(.+)_\w+\.pl$/$1/
		or goto bad;
	warn "# $fname: deparse failed, trying to recover with -M$super\n";
	my $ok2 = do_deparse($fname, "-M$super");
	goto bad unless $ok2;
	return;
bad:
	die "# $fname: deparse failed. prefix=$prefix\n";
}

sub shebang_options {
	my $fname = shift;
	open my $fh, $fname or die "$0: $fname: $!\n";
	local $_ = <$fh>;
	my @argv;
	if (s/^#!\s*\S*perl\S*//) {
		foreach my $arg (split) {
			last if $arg =~ /^#/;
			next if $arg eq "--";
			push @argv, $arg;
		}
	}
	elsif (m[^#!\s*/bin/sh(\s|$)]) {
		# check for "perl -x" re-exec hack
		my $maybe_x;
		while (<$fh>) {
			# this is just a standard way to re-exec perl:
			last if /^eval\s+'exec\s/;
			if (/\bexec\s.*\bperl.*\s-x/) {
				$maybe_x = 1;
			}
			elsif ($maybe_x and s/^#!\s*\S*perl\S*//) {
				push @argv, "-x";
				foreach my $arg (split) {
					last if $arg =~ /^#/;
					next if $arg eq "--";
					push @argv, $arg;
				}
				last;
			}
		}
	}
	return @argv;
}

sub do_deparse {
	my ($fname, @add_arguments) = @_;

# skip "syntax OK" messages
#	use Fcntl qw(F_SETFD);
#	fcntl(STDERR, F_SETFD, 1) if !$Debug && $Method eq 'relaxed';

# construct pipe command
	my $X = $^X;
	if ($ENV{RPM_BUILD_ROOT}) {
# what if we build perl itself?
# find deps with newer perl in order to avoid incompatible changes
		for my $perl ($^X, $Config{perlpath}, "/usr/bin/perl") {
			next unless $perl and -x "$ENV{RPM_BUILD_ROOT}$perl";
			$X = "$ENV{RPM_BUILD_ROOT}$perl";
			last;
		}
# adjust LD_LIBRARY_PATH if there are libraries inside buildroot
# spotted by Yury Konovalov
		for my $libdir ("/usr/lib64", "/usr/lib") {
			next unless glob "$ENV{RPM_BUILD_ROOT}$libdir/lib*.so*";
			$ENV{LD_LIBRARY_PATH} .= ":" if $ENV{LD_LIBRARY_PATH};
			$ENV{LD_LIBRARY_PATH} .= "$ENV{RPM_BUILD_ROOT}$libdir";
		}
	}
	my @pipe = ($X, shebang_options($fname));

# known problems and workarounds:
# - /usr/lib/rpm/base.pm apparently fixes possible dependency loops with base.pm
#   that make syntax check impossible; affected packages: perl-Tk, perl-Video-DVDRip
#   See also:
#   http://www.google.com/search?q="base.pm+and+eval"&filter=0
#   http://www.google.com/search?q="base.pm+import+stuff"&filter=0
# - /usr/lib/rpm/fake.pm (preloaded with `use') rearranges @INC entries so that
#   fake %buildroot-dependent paths takes precedence at INIT stage;
#   affected packages: autoconf
	push @pipe, "-I/usr/lib/rpm", "-Mfake" if $Method ne "strict";
	push @pipe, map { "-I$_" } inc();
	push @pipe, "-MO=ConstOptree";
	my $MO = "-MO=PerlReq";
	$MO .= ",-$Method" if $Method ne "normal";
	$MO .= ",-verbose" if $Verbose;
	$MO .= ",-debug" if $Verbose > 1;

	push @pipe, @add_arguments, $MO, "--", $fname;
	warn "# pipe: @pipe\n" if $Verbose > 1;

# do deparse
	use 5.007_001; # the list form of open() for pipes
	open my $pipe, "-|", @pipe or die "$0: @pipe: $!\n";
	local $_;
	while (<$pipe>) {
		my ($dep, undef, $v) = split;
		unless ($dep =~ /^perl\b/) {
			warn "# invalid dep: $_\n";
			next;
		}
		if ($v) {
			$req{$dep}{$v} = undef;
		} else {
			$req{$dep} ||= undef;
		}
	}
# flush buffers
	1 while <$pipe>;
	return close $pipe;
}

# end
foreach my $k (sort { uc($a) cmp uc($b) } keys %req) {
	if ($weak_prov{$k}) {
		warn "# $k internally povided by $weak_prov{$k}\n";
		next;
	}
	my %ver = map { $_ ? %$_ : () } $req{$k}, $prereq{$k};
	if (%ver) {
		print "$k >= $_\n" foreach sort { $a <=> $b } keys %ver;
	} else {
		print "$k\n";
	}
}


# auxiliary stuff
sub count($$) {
	warn "# @_\n" if $Verbose > 1;
}

sub isPerl {
	my $fname = shift;
	chomp $fname;
	open(FILE, $fname) || die "$0: $fname: $!\n";
	warn "# checking if $fname is perl source\n" if $Verbose;
# shortcut for non-text files
	return -1 unless -T FILE;
	local $_ = join "" => <FILE>;
	close FILE;
	my ($n, @n);

# POSITIVE
# variables
	@n = /\W[\$\%\@](?!Id[\$:])\w+/g;
	count @n, "variables";
	$n += @n;
# comments
	@n = /^\s*#/gm;
	count @n, "comments";
	$n += @n;
# blocks
	@n = /[}{]$|^\s*[}{]/gm;
	count @n, "blocks";
	$n += @n;
# keywords
	@n = /\b(unless|foreach|package|sub|use|strict)\b/gm;
	count @n, "keywords";
	$n += @n;
# pod
	@n = /^=(?:back|begin|cut|end|for|head|item|over|pod)/gm;
	count @n, "pod sections";
	$n += @n;
# modules
	@n = /^1;$/gm;
	count @n, "`1;'";
	$n += @n;

# NEGATIVE
# prolog
	@n = /:-/g;
	count @n, "prolog :- operators";
	$n -= @n;
# prolog
	@n = /\![.,]$/gm;
	count @n, "prolog ! operators";
	$n -= @n;
# prolog
	@n = /\[\]/g;
	count @n, "prolog [] empty lists";
	$n -= @n;
# prolog
	@n = /(?:^|\s)%\s/gm;
	count @n, "prolog % comments";
	$n -= @n;
# prolog
	@n = /\(.*\)\.$/gm;
	count @n, "prolog ). EOF";
	$n -= @n;
# overall density
	$n /= (-s $fname) + 1;
}

__END__

=head1	NAME

perl.req - list requirements for Perl scripts and libraries

=head1	SYNOPSIS

B<perl.req>
[B<-h>|B<--help>]
[B<-v>|B<--verbose>]
[B<-m>|B<--method>=I<strict>|I<normal>|I<relaxed>]
[I<FILE>...]

=head1	DESCRIPTION

B<perl.req> calculates prerequisites for each Perl source I<file>
specified on a command line; alternatively, a list of files is obtained
from standard input, one file per line.  C<use>, C<require> and C<do>
statements are processed.  The output of perl.req is suitable for
automatic dependency tracking (e.g. for RPM packaging).

For example, F</usr/lib/perl5/File/Temp.pm> requires, in particular,
C<< perl(Fcntl.pm) >= 1.030 >> (as of perl-5.8.6).

B<perl.req> is basically a wrapper for L<B::PerlReq> Perl compiler backend.

=head1	OPTIONS

=over

=item	B<-m>, B<--method>=I<method>

Use particular I<method> for dependency tracking.  Alternatively,
RPM_PERL_REQ_METHOD environment variable can be used to set the method.

The following methods are available:

=over

=item	B<strict>

Search thoroughly and list all requirements.  In particular, list
platform-specific (non-UNIX) requirements and requirements found inside
C<eval> blocks.

=item	B<normal> (default)

Enable moderate search most acceptable for RPM packaging.  That is,
skip files known to be platform-specific; skip platform-specific
requirements and those found inside C<eval> blocks; skip most common
requirements (e.g. C<strict.pm>).

=item	B<relaxed>

Enable relaxed mode.  That is, tolerate B::PerlReq failures; in addition
to normal method, skip conditional requirements (e.g. C<require>
statements inside subroutines); skip C<do FILE> statements; list only
essential dependencies.

=back

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

Increase verbosity.

=back

=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	HISTORY

Initial version of perl.req (part of RPM 3.0) done by Ken Estes
in 1999.  Regular expressions were used to extract dependencies.
(Later a part of ALT Linux Master 2.0, with modifications from Mikhail
Zabaluev.)

Reworked in November 2002: complicated regular expressions were added to
enhance search; methods added.  (Later a part of ALT Linux Master 2.2.)

Reworked in September 2003: L<B::Deparse> was utilized to re-format Perl
code before dependency extraction; hence more simple and accurate.
Decoupled from rpm-build package into rpm-build-perl.  (Later a part of
ALT Linux Master 2.4.)

Reworked in December 2004: L<B::PerlReq> was developed.  Released on
CPAN, see L<http://search.cpan.org/dist/rpm-build-perl/>.

=head1	COPYING

Copyright (c) 2003, 2004 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<B::PerlReq>, L<perl.prov>