fix Math::BigInt, PathTools entries in Maintainers.pl
[p5sagit/p5-mst-13.2.git] / Porting / Maintainers.pm
1 #
2 # Maintainers.pm - show information about maintainers
3 #
4
5 package Maintainers;
6
7 use strict;
8
9 use lib "Porting";
10 # Please don't use post 5.008 features as this module is used by
11 # Porting/makemeta, and that in turn has to be run by the perl just built.
12 use 5.008;
13
14 require "Maintainers.pl";
15 use vars qw(%Modules %Maintainers);
16
17 use vars qw(@ISA @EXPORT_OK $VERSION);
18 @ISA = qw(Exporter);
19 @EXPORT_OK = qw(%Modules %Maintainers
20                 get_module_files get_module_pat
21                 show_results process_options files_to_modules
22                 reload_manifest);
23 $VERSION = 0.03;
24 require Exporter;
25
26 use File::Find;
27 use Getopt::Long;
28
29 my %MANIFEST;
30
31 # (re)read the MANIFEST file, blowing away any previous effort
32
33 sub reload_manifest {
34     %MANIFEST = ();
35     if (open(MANIFEST, "MANIFEST")) {
36         while (<MANIFEST>) {
37             if (/^(\S+)/) {
38                 $MANIFEST{$1}++;
39             }
40             else {
41                 warn "MANIFEST:$.: malformed line: $_\n";
42             }
43         }
44         close MANIFEST;
45     } else {
46         die "$0: Failed to open MANIFEST for reading: $!\n";
47     }
48 }
49
50 reload_manifest;
51
52
53 sub get_module_pat {
54     my $m = shift;
55     split ' ', $Modules{$m}{FILES};
56 }
57
58 # exand dir/ or foo* into a full list of files
59 #
60 sub expand_glob {
61     sort { lc $a cmp lc $b }
62         map {
63             -f $_ ? # File as-is.
64                 $_ :
65                 -d _ ? # Recurse into directories.
66                 do {
67                     my @files;
68                     find(
69                          sub {
70                              push @files, $File::Find::name
71                                  if -f $_ && exists $MANIFEST{$File::Find::name};
72                          }, $_);
73                     @files;
74                 }
75             # The rest are globbable patterns; expand the glob, then
76             # recurively perform directory expansion on any results
77             : expand_glob(grep -e $_,glob($_))
78             } @_;
79 }
80
81 sub get_module_files {
82     my $m = shift;
83     my %exclude;
84     my @files;
85     for (get_module_pat($m)) {
86         if (s/^!//) {
87             $exclude{$_}=1 for expand_glob($_);
88         }
89         else {
90             push @files, expand_glob($_);
91         }
92     }
93     return grep !$exclude{$_}, @files;
94 }
95
96
97 sub get_maintainer_modules {
98     my $m = shift;
99     sort { lc $a cmp lc $b }
100     grep { $Modules{$_}{MAINTAINER} eq $m }
101     keys %Modules;
102 }
103
104 sub usage {
105     print <<__EOF__;
106 $0: Usage: $0 [[--maintainer M --module M --files]|[--check] [commit] | [file ...]
107 --maintainer M  list all maintainers matching M
108 --module M      list all modules matching M
109 --files         list all files
110 --check         check consistency of Maintainers.pl
111                         with a file     checks if it has a maintainer
112                         with a dir      checks all files have a maintainer
113                         otherwise       checks for multiple maintainers
114 --checkmani     like --check, but only reports on unclaimed files if they
115                     are in MANIFEST
116 --opened        list all modules of modified files
117 Matching is case-ignoring regexp, author matching is both by
118 the short id and by the full name and email.  A "module" may
119 not be just a module, it may be a file or files or a subdirectory.
120 The options may be abbreviated to their unique prefixes
121 __EOF__
122     exit(0);
123 }
124
125 my $Maintainer;
126 my $Module;
127 my $Files;
128 my $Check;
129 my $Checkmani;
130 my $Opened;
131
132 sub process_options {
133     usage()
134         unless
135             GetOptions(
136                        'maintainer=s'   => \$Maintainer,
137                        'module=s'       => \$Module,
138                        'files'          => \$Files,
139                        'check'          => \$Check,
140                        'checkmani'      => \$Checkmani,
141                        'opened'         => \$Opened,
142                       );
143
144     my @Files;
145
146     if ($Opened) {
147         chomp (@Files = `git ls-files -m --full-name`);
148         die if $?;
149     } elsif (@ARGV == 1 &&
150              $ARGV[0] =~ /^(?:HEAD|[0-9a-f]{4,40})(?:~\d+)?\^*$/) {
151         my $command = "git diff --name-only $ARGV[0]^ $ARGV[0]";
152         chomp (@Files = `$command`);
153         die "'$command' failed: $?" if $?;
154     } else {
155         @Files = @ARGV;
156     }
157
158     usage() if @Files && ($Maintainer || $Module || $Files);
159
160     for my $mean ($Maintainer, $Module) {
161         warn "$0: Did you mean '$0 $mean'?\n"
162             if $mean && -e $mean && $mean ne '.' && !$Files;
163     }
164
165     warn "$0: Did you mean '$0 -mo $Maintainer'?\n"
166         if defined $Maintainer && exists $Modules{$Maintainer};
167
168     warn "$0: Did you mean '$0 -ma $Module'?\n"
169         if defined $Module     && exists $Maintainers{$Module};
170
171     return ($Maintainer, $Module, $Files, @Files);
172 }
173
174 sub files_to_modules {
175     my @Files = @_;
176     my %ModuleByFile;
177
178     for (@Files) { s:^\./:: }
179
180     @ModuleByFile{@Files} = ();
181
182     # First try fast match.
183
184     my %ModuleByPat;
185     for my $module (keys %Modules) {
186         for my $pat (get_module_pat($module)) {
187             $ModuleByPat{$pat} = $module;
188         }
189     }
190     # Expand any globs.
191     my %ExpModuleByPat;
192     for my $pat (keys %ModuleByPat) {
193         if (-e $pat) {
194             $ExpModuleByPat{$pat} = $ModuleByPat{$pat};
195         } else {
196             for my $exp (glob($pat)) {
197                 $ExpModuleByPat{$exp} = $ModuleByPat{$pat};
198             }
199         }
200     }
201     %ModuleByPat = %ExpModuleByPat;
202     for my $file (@Files) {
203         $ModuleByFile{$file} = $ModuleByPat{$file}
204             if exists $ModuleByPat{$file};
205     }
206
207     # If still unresolved files...
208     if (my @ToDo = grep { !defined $ModuleByFile{$_} } keys %ModuleByFile) {
209
210         # Cannot match what isn't there.
211         @ToDo = grep { -e $_ } @ToDo;
212
213         if (@ToDo) {
214             # Try prefix matching.
215
216             # Remove trailing slashes.
217             for (@ToDo) { s|/$|| }
218
219             my %ToDo;
220             @ToDo{@ToDo} = ();
221
222             for my $pat (keys %ModuleByPat) {
223                 last unless keys %ToDo;
224                 if (-d $pat) {
225                     my @Done;
226                     for my $file (keys %ToDo) {
227                         if ($file =~ m|^$pat|i) {
228                             $ModuleByFile{$file} = $ModuleByPat{$pat};
229                             push @Done, $file;
230                         }
231                     }
232                     delete @ToDo{@Done};
233                 }
234             }
235         }
236     }
237     \%ModuleByFile;
238 }
239 sub show_results {
240     my ($Maintainer, $Module, $Files, @Files) = @_;
241
242     if ($Maintainer) {
243         for my $m (sort keys %Maintainers) {
244             if ($m =~ /$Maintainer/io || $Maintainers{$m} =~ /$Maintainer/io) {
245                 my @modules = get_maintainer_modules($m);
246                 if ($Module) {
247                     @modules = grep { /$Module/io } @modules;
248                 }
249                 if ($Files) {
250                     my @files;
251                     for my $module (@modules) {
252                         push @files, get_module_files($module);
253                     }
254                     printf "%-15s @files\n", $m;
255                 } else {
256                     if ($Module) {
257                         printf "%-15s @modules\n", $m;
258                     } else {
259                         printf "%-15s $Maintainers{$m}\n", $m;
260                     }
261                 }
262             }
263         }
264     } elsif ($Module) {
265         for my $m (sort { lc $a cmp lc $b } keys %Modules) {
266             if ($m =~ /$Module/io) {
267                 if ($Files) {
268                     my @files = get_module_files($m);
269                     printf "%-15s @files\n", $m;
270                 } else {
271                     printf "%-15s %-12s %s\n", $m, $Modules{$m}{MAINTAINER}, $Modules{$m}{UPSTREAM}||'unknown';
272                 }
273             }
274         }
275     } elsif ($Check or $Checkmani) {
276         if( @Files ) {
277             missing_maintainers(
278                 $Checkmani
279                     ? sub { -f $_ and exists $MANIFEST{$File::Find::name} }
280                     : sub { /\.(?:[chty]|p[lm]|xs)\z/msx },
281                 @Files
282             );
283         }
284         else { 
285             duplicated_maintainers();
286         }
287     } elsif (@Files) {
288         my $ModuleByFile = files_to_modules(@Files);
289         for my $file (@Files) {
290             if (defined $ModuleByFile->{$file}) {
291                 my $module     = $ModuleByFile->{$file};
292                 my $maintainer = $Modules{$ModuleByFile->{$file}}{MAINTAINER};
293                 my $upstream   = $Modules{$module}{UPSTREAM}||'unknown';
294                 printf "%-15s [%-7s] $module $maintainer $Maintainers{$maintainer}\n", $file, $upstream;
295             } else {
296                 printf "%-15s ?\n", $file;
297             }
298         }
299     }
300     elsif ($Opened) {
301         print STDERR "(No files are modified)\n";
302     }
303     else {
304         usage();
305     }
306 }
307
308 my %files;
309
310 sub maintainers_files {
311     %files = ();
312     for my $k (keys %Modules) {
313         for my $f (get_module_files($k)) {
314             ++$files{$f};
315         }
316     }
317 }
318
319 sub duplicated_maintainers {
320     maintainers_files();
321     for my $f (keys %files) {
322         if ($files{$f} > 1) {
323             warn "File $f appears $files{$f} times in Maintainers.pl\n";
324         }
325     }
326 }
327
328 sub warn_maintainer {
329     my $name = shift;
330     warn "File $name has no maintainer\n" if not $files{$name};
331 }
332
333 sub missing_maintainers {
334     my($check, @path) = @_;
335     maintainers_files();
336     my @dir;
337     for my $d (@path) {
338         if( -d $d ) { push @dir, $d } else { warn_maintainer($d) }
339     }
340     find sub { warn_maintainer($File::Find::name) if $check->() }, @dir
341         if @dir;
342 }
343
344 1;
345