summaryrefslogtreecommitdiffstats
path: root/misc/perlcore
blob: 6eb25ae7d07cede45671bdda708c4d07ea107376 (plain)
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
#!/usr/bin/env perl

use warnings 'FATAL' => 'all';
use strict;

package Common;

sub evalver
{
	my($path, $mod) = @_;

	open(my $fh, '<', $path) or die("open $path: $!");

	my $m = ($mod
		? qr/(?:\$${mod}::VERSION|\$VERSION)/
		: qr/\$VERSION/);

	while(<$fh>){
		next unless(/\s*$m\s*=\s*.+/);
		chomp;
		my $ver = do { no strict; eval };
		return $ver unless($@);
		die qq{$path:$. bad version string "$_"\n};
	}

	close($fh);
	return undef;
}

# ----------------------------------------

package CoreDist;

use File::Basename qw(basename);
use File::Find qw();
*findfile = *File::Find::find;

sub modname
{
	my($dist) = @_;
	$dist =~ s/-+/::/g;
	return $dist;
}

sub maindistfile
{
	my($dist, $dir) = @_;

	# libpath is the modern style, installing modules under lib/
	# with dirs matching the name components.
	my $libpath = join(q{/}, 'lib', split(/-/, "${dist}.pm"));

	# dumbpath is an old style where there's no subdirs and just
	# a .pm file.
	my $dumbpath = $dist;
	$dumbpath =~ s/\A.+-//;
	$dumbpath .= ".pm";

	my @paths = ($libpath, $dumbpath);
	# Some modules (with simple names like XSLoader, lib, etc) are
	# generated by Makefile.PL. Search through their generating code.
	push @paths, "${dist}_pm.PL" if($dist =~ tr/-/-/ == 0);

	for my $path (map { "$dir/$_" } @paths){ return $path if(-f $path); }
	return undef;
}

sub module_ver
{
	my($dist, $dir) = @_;

	my $path = maindistfile($dist, $dir) or return undef;

	my $mod = modname($dist);
	my $ver = Common::evalver($path, $mod);
	unless($ver){
		warn "failed to find version in module file $path\n";
		return undef;
	}

	return [ $mod, $ver ];
}

sub changelog_ver
{
	my($dist, $dir) = @_;

	my $path;
	for my $tmp (glob "$dir/{Changes,ChangeLog}"){
		if(-f $tmp){ $path = $tmp; last; }
	}
	return undef unless($path);

	my $mod = modname($dist);
	open my $fh, '<', $path or die"open: $!";
	while(<$fh>){
		return [ $mod, $1 ] if(/\A\s*(?:$dist[ \t]*)?([0-9._]+)/);
		return [ $mod, $1 ] if(/\A\s*version\s+([0-9._]+)/i);
	}
	close $fh;

	return undef;
}

# for some reason podlators has a VERSION file with perl code in it
sub verfile_ver
{
	my($dist, $dir) = @_;
	my $path = "$dir/VERSION";
	return undef unless(-f $path); # no warning, only podlaters has it

	my $v = Common::evalver($path);
	return ($v ? [ modname($dist), $v ] : undef);
}

# scans a directory full of nicely separated dist. directories.
sub scan_distroot
{
	my ($distroot) = @_;
	opendir my $cpand, "$distroot" or die("failed to open $distroot");
	my @dists = grep { !/^[.]/ && -d "$distroot/$_" } readdir $cpand;
	closedir $cpand;

	my @found;
	for my $ddir (map { "$distroot/$_/" } @dists){
		$ddir .= 'lib/' if(-d "$ddir/lib");
		my $finder = sub {
			return unless(/[.]pm$/);
			return if(m{/t/});
	
			my $p = $_;
			s/^\Q$ddir\E//;
			s{^lib/}{};
			s{[.]pm$}{};
			s{/}{::}g;
			my $m = $_;
	
			my $v = Common::evalver($p, $m);
			if($v){
				push @found, [ $m, $v ];
			}else{
				#warn "failed to find version in $p\n";
			}
		};
		findfile({ 'no_chdir' => 1, 'wanted' => $finder }, $ddir);
	}

	return @found;
}

sub findmods
{
	my($srcdir) = @_;
	return map { scan_distroot($_) } glob "$srcdir/{cpan,dist}";
}

# ----------------------------------------

package CoreLib;

use File::Find qw();
use File::stat;

*findfile = *File::Find::find;

sub findmods
{
	my($srcdir) = @_;
	my $libdir = "$srcdir/lib/";
	die "failed to find $libdir directory" unless(-d $libdir);

	# Find only the module files that have not changed since perl
	# was extracted. We don't want the files perl just recently
	# installed into lib/. We processed those already.
	my @modfiles;
	my $finder = sub {
		return unless(/[.]pm\z/);
		return if m{\Q$libdir\E[^/]+/t/}; # ignore testing modules
		push @modfiles, $_;
	};
	findfile({ 'no_chdir' => 1, 'wanted' => $finder }, $libdir);

	# First we have to find what the oldest ctime actually is.
	my $oldest = time;
	@modfiles = map {
		my $modfile = $_;
		my $ctime = (stat $modfile)->ctime;
		$oldest = $ctime if($ctime < $oldest);
		[ $modfile, $ctime ]; # save ctime for later
	} @modfiles;

	# Then we filter out any file that was created more than a
	# few seconds after that. Process the rest.
	my @mods;
	for my $modfile (@modfiles){
		my($mod, $ctime) = @$modfile;
		next if $ctime - $oldest > 5; # ignore newer files

		my $path = $mod;
		$mod =~ s{[.]pm\z}{};
		$mod =~ s{\A$libdir}{};
		$mod =~ s{/}{::}g;

		my $ver = Common::evalver($path, $mod);
		if($ver){
			push @mods, [ $mod, $ver ];
		}else{
			warn "failed to find version in $path\n";
		}
	}
	return @mods;
}

# ----------------------------------------

package main;

sub _delmods
{
	my $coreonly = shift;
	for(@_){
		my($m) = split;
		delete $coreonly->{$m};
	}
	return;
}

sub printmods
{
	my($mods, $mpath) = @_;
	my %coreonly = map { @$_ } @$mods;

	# Remove mods from the set which are also available from CPAN.
	{
		local $/ = qq{\n\n};
		open my $if, '<', $mpath or die "open $mpath failed: $!";
		while(<$if>){
			my($dist, @dms) = split /\n/;
			_delmods(\%coreonly, @dms);
		}
		close $if;
	}

	# Print a * in the third column for core-only modules.
	for my $mv (@$mods){
		my($m, $v) = @$mv;
		printf "%s\n", join q{ }, $m, $v, ($coreonly{$m} ? q{*} : ());
	}
}

sub main
{
	die"Usage: $0 [path to perl source] [path to cpan.mods]\n" unless(@_ == 2);
	my($perldir, $mpath) = @_;

	die "$perldir is not a valid directory.\n" unless(-d $perldir);
	die "$mpath is not a valid file.\n" unless(-f $mpath);

	my @mods = (CoreDist::findmods($perldir), CoreLib::findmods($perldir));

	## Embedded modules without any files...
	push @mods, [ 'Config' => 1 ];

	@mods = sort { $a->[0] cmp $b->[0] } @mods;
	printmods(\@mods, $mpath);

	return 0;
}

exit main(@ARGV);