This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Params-Check to CPAN version 0.38
[perl5.git] / Porting / sync-with-cpan
CommitLineData
33e80a47 1#!/usr/bin/env perl
418f4069 2
c5e3e317
JL
3=head1 NAME
4
f703fc96 5Porting/sync-with-cpan - Synchronize with CPAN distributions
c5e3e317
JL
6
7=head1 SYNOPSIS
8
9 perl Porting/sync-with-cpan <module>
10
11where <module> is the name it appears in the C<%Modules> hash
12of F<Porting/Maintainers.pl>
13
14=head1 DESCRIPTION
15
16Script to help out with syncing cpan distros.
17
18Does the following:
19
20=over 4
21
22=item *
23
24Fetches the package list from CPAN. Finds the current version of the given
25package. [1]
26
27=item *
28
29Downloads the relevant tarball; unpacks the tarball. [1]
30
31=item *
32
33Clean out the old directory (C<git clean -dfx>)
34
35=item *
36
37Moves the old directory out of the way, moves the new directory in place.
38
39=item *
40
41Restores any F<.gitignore> file.
42
43=item *
44
45Removes files from C<@IGNORE> and C<EXCLUDED>
46
47=item *
48
49C<git add> any new files.
50
51=item *
52
53C<git rm> any files that are gone.
54
55=item *
56
57Remove the +x bit on files in F<t/>
58
59=item *
60
61Remove the +x bit on files that don't have it enabled in the current dir
62
63=item *
64
65Restore files mentioned in C<CUSTOMIZED>
66
67=item *
68
69Adds new files to F<MANIFEST>
70
71=item *
72
73Runs a C<make> (assumes a configure has been run)
74
75=item *
76
77Cleans up
78
79=item *
80
81Runs tests for the package
82
83=item *
84
85Runs the porting tests
86
87=back
88
89[1] If the C<--tarball> option is given, then CPAN is not consulted.
90C<--tarball> should be the path to the tarball; the version is extracted
91from the filename -- but can be overwritten by the C<--version> option.
92
93=head1 TODO
94
95=over 4
96
97=item *
98
99Delete files from F<MANIFEST>
100
101=item *
102
103Update F<Porting/Maintainers.pl>
104
105=item *
106
107Optional, run a full test suite
108
109=item *
110
111Handle complicated C<FILES>
112
113=back
114
115This is an initial version; no attempt has been made yet to make this
116portable. It shells out instead of trying to find a Perl solution.
117In particular, it assumes wget, git, tar, chmod, perl, make, and rm
118to be available.
119
120=cut
121
418f4069 122
4d18e0a2
A
123package Maintainers;
124
418f4069
A
125use 5.010;
126
127use strict;
128use warnings;
4d18e0a2 129use Getopt::Long;
a1450e8b 130use Archive::Tar;
418f4069
A
131
132$| = 1;
133
07a826df 134die "This does not look like a top level directory"
418f4069
A
135 unless -d "cpan" && -d "Porting";
136
418f4069
A
137our @IGNORABLE;
138our %Modules;
139
140use autodie;
141
142require "Porting/Maintainers.pl";
143
418f4069
A
144my %IGNORABLE = map {$_ => 1} @IGNORABLE;
145
146my $package = "02packages.details.txt";
147my $package_url = "http://www.cpan.org/modules/$package";
148my $package_file = "/tmp/$package";
149
b7e2b692
JL
150my @problematic = (
151 'podlators', # weird CUSTOMIZED section due to .PL files
152);
153
418f4069 154
4d18e0a2 155GetOptions ('tarball=s' => \my $tarball,
b5bf278a
A
156 'version=s' => \my $version,
157 force => \my $force,)
4d18e0a2
A
158 or die "Failed to parse arguments";
159
160die "Usage: $0 module [args] [cpan package]" unless @ARGV == 1 || @ARGV == 2;
418f4069 161
5b73aae5
A
162my ($module) = shift;
163my $cpan_mod = @ARGV ? shift : $module;
418f4069 164
4d18e0a2 165
418f4069
A
166my $info = $Modules {$module} or die "Cannot find module $module";
167my $distribution = $$info {DISTRIBUTION};
b5bf278a
A
168
169my @files = glob $$info {FILES};
b7e2b692 170if (!-d $files [0] || grep { $_ eq $module } @problematic) {
b5bf278a
A
171 say "This looks like a setup $0 cannot handle (yet)";
172 unless ($force) {
173 say "Will not continue without a --force option";
174 exit 1;
175 }
176 say "--force is in effect, so we'll soldier on. Wish me luck!";
177}
178
179
180chdir "cpan";
181
83d3dd1d 182my $pkg_dir = $files[0];
418f4069
A
183 $pkg_dir =~ s!.*/!!;
184
185my ($old_version) = $distribution =~ /-([0-9.]+)\.tar\.gz/;
186
187my $o_module = $module;
5b73aae5
A
188if ($cpan_mod =~ /-/ && $cpan_mod !~ /::/) {
189 $cpan_mod =~ s/-/::/g;
418f4069
A
190}
191
192#
193# Find the information from CPAN.
194#
4d18e0a2
A
195my $new_file;
196my $new_version;
197unless ($tarball) {
198 #
199 # Poor man's cache
200 #
201 unless (-f $package_file && -M $package_file < 1) {
202 system wget => $package_url, '-qO', $package_file;
203 }
204
205 my $new_line = `grep '^$cpan_mod ' $package_file`
206 or die "Cannot find $cpan_mod on CPAN\n";
207 chomp $new_line;
208 (undef, $new_version, my $new_path) = split ' ', $new_line;
3a4316cc
JL
209 if (defined $version) {
210 $new_path =~ s/-$new_version\./-$version\./;
211 $new_version = $version;
212 }
4d18e0a2
A
213 $new_file = (split '/', $new_path) [-1];
214
215 my $url = "http://search.cpan.org/CPAN/authors/id/$new_path";
216 say "Fetching $url";
217 #
218 # Fetch the new distro
219 #
220 system wget => $url, '-qO', $new_file;
221}
222else {
223 $new_file = $tarball;
224 $new_version = $version // ($new_file =~ /-([0-9._]+)\.tar\.gz/) [0];
225}
418f4069
A
226
227my $old_dir = "$pkg_dir-$old_version";
418f4069
A
228
229say "Cleaning out old directory";
230system git => 'clean', '-dfxq', $pkg_dir;
231
418f4069 232say "Unpacking $new_file";
5fb91d48 233Archive::Tar->extract_archive( $new_file );
418f4069 234
618ac2f6 235(my $new_dir = $new_file) =~ s/\.tar\.gz//;
3f7808eb
JL
236# ensure 'make' will update all files
237system('find', $new_dir, '-exec', 'touch', '{}', ';');
418f4069
A
238
239say "Renaming directories";
240rename $pkg_dir => $old_dir;
418f4069 241
83d3dd1d
JL
242say "Creating new package directory";
243mkdir $pkg_dir;
244
245say "Populating new package directory";
246my $map = $$info {MAP};
247my @EXCLUDED_QR;
248my %EXCLUDED_QQ;
249if ($$info {EXCLUDED}) {
250 foreach my $entry (@{$$info {EXCLUDED}}) {
251 if (ref $entry) {push @EXCLUDED_QR => $entry}
252 else {$EXCLUDED_QQ {$entry} = 1}
253 }
254}
255
256FILE: for my $file ( `find $new_dir -type f` ) {
257 chomp $file;
258 my $old_file = $file;
259 $file =~ s{^$new_dir/}{};
260
261 next if $EXCLUDED_QQ{$file};
262 for my $qr (@EXCLUDED_QR) {
263 next FILE if $file =~ $qr;
264 }
265
266 if ( $map ) {
267 for my $key ( sort { length $b <=> length $a } keys %$map ) {
268 my $val = $map->{$key};
269 last if $file =~ s/^$key/$val/;
270 }
271 }
7bbb137d
JL
272 else {
273 $file = $files[0] . '/' . $file;
274 }
83d3dd1d
JL
275
276 if ( $file =~ m{^cpan/} ) {
277 $file =~ s{^cpan/}{};
278 }
279 else {
280 $file = '../' . $file;
281 }
282
283 my $prefix = '';
284 my @parts = split '/', $file;
285 pop @parts;
286 for my $part (@parts) {
287 $prefix .= '/' if $prefix;
288 $prefix .= $part;
289 mkdir $prefix unless -d $prefix;
290 }
291
292 rename $old_file => $file;
293}
294system 'rm', '-rf', $new_dir;
418f4069
A
295
296if (-f "$old_dir/.gitignore") {
297 say "Restoring .gitignore";
298 system git => 'checkout', "$pkg_dir/.gitignore";
299}
300
ad9b4e6f 301my @new_files = `find $pkg_dir -type f`;
418f4069
A
302chomp @new_files;
303@new_files = grep {$_ ne $pkg_dir} @new_files;
304s!^[^/]+/!! for @new_files;
305my %new_files = map {$_ => 1} @new_files;
306
ad9b4e6f 307my @old_files = `find $old_dir -type f`;
418f4069
A
308chomp @old_files;
309@old_files = grep {$_ ne $old_dir} @old_files;
310s!^[^/]+/!! for @old_files;
311my %old_files = map {$_ => 1} @old_files;
312
418f4069
A
313my @delete;
314my @commit;
315my @gone;
418f4069
A
316FILE:
317foreach my $file (@new_files) {
318 next if -d "$pkg_dir/$file"; # Ignore directories.
319 next if $old_files {$file}; # It's already there.
320 if ($IGNORABLE {$file}) {
321 push @delete => $file;
322 next;
323 }
418f4069
A
324 push @commit => $file;
325}
326foreach my $file (@old_files) {
327 next if -d "$old_dir/$file";
328 next if $new_files {$file};
329 push @gone => $file;
330}
ad9b4e6f
A
331
332#
333# Find all files with an exec bit
334#
335my @exec = `find $pkg_dir -type f -perm +111`;
418f4069 336chomp @exec;
ad9b4e6f
A
337my @de_exec;
338foreach my $file (@exec) {
339 # Remove leading dir
340 $file =~ s!^[^/]+/!!;
341 if ($file =~ m!^t/!) {
342 push @de_exec => $file;
343 next;
344 }
345 # Check to see if the file exists; if it doesn't and doesn't have
346 # the exec bit, remove it.
347 if ($old_files {$file}) {
348 unless (-x "$old_dir/$file") {
349 push @de_exec => $file;
350 }
351 }
352}
418f4069
A
353
354#
355# No need to change the +x bit on files that will be deleted.
356#
ad9b4e6f 357if (@de_exec && @delete) {
a9f5d1d4 358 my %delete = map {+"$pkg_dir/$_" => 1} @delete;
ad9b4e6f 359 @de_exec = grep {!$delete {$_}} @de_exec;
418f4069
A
360}
361
362say "unlink $pkg_dir/$_" for @delete;
363say "git add $pkg_dir/$_" for @commit;
364say "git rm -f $pkg_dir/$_" for @gone;
ad9b4e6f 365say "chmod a-x $pkg_dir/$_" for @de_exec;
418f4069
A
366
367print "Hit return to continue; ^C to abort "; <STDIN>;
368
369unlink "$pkg_dir/$_" for @delete;
370system git => 'add', "$pkg_dir/$_" for @commit;
371system git => 'rm', '-f', "$pkg_dir/$_" for @gone;
ad9b4e6f 372system chmod => 'a-x', "$pkg_dir/$_" for @de_exec;
418f4069 373
9c259538
A
374#
375# Restore anything that is customized.
376# We don't really care whether we've deleted the file - since we
377# do a git restore, it's going to be resurrected if necessary.
378#
379if ($$info {CUSTOMIZED}) {
380 say "Restoring customized files";
381 foreach my $file (@{$$info {CUSTOMIZED}}) {
382 system git => "checkout", "$pkg_dir/$file";
383 }
384}
385
a8121781 386chdir "..";
418f4069
A
387if (@commit) {
388 say "Fixing MANIFEST";
a8121781 389 my $MANIFEST = "MANIFEST";
418f4069
A
390 my $MANIFEST_SORT = "$MANIFEST.sorted";
391 open my $fh, ">>", $MANIFEST;
a8121781 392 say $fh "cpan/$pkg_dir/$_" for @commit;
418f4069 393 close $fh;
a8121781 394 system perl => "Porting/manisort", '--output', $MANIFEST_SORT;
418f4069
A
395 rename $MANIFEST_SORT => $MANIFEST;
396}
397
398
418f4069
A
399print "Running a make ... ";
400system "make > make.log 2>&1" and die "Running make failed, see make.log";
401print "done\n";
402
403#
404# Must clean up, or else t/porting/FindExt.t will fail.
730ad6b9 405# Note that we can always retrieve the original directory with a git checkout.
418f4069
A
406#
407print "About to clean up; hit return or abort (^C) "; <STDIN>;
408
409chdir "cpan";
410system rm => '-r', $old_dir;
e81fec2c 411unlink $new_file unless $tarball;
418f4069
A
412
413
ad9b4e6f
A
414#
415# Run the tests. First the test belonging to the module, followed by the
416# the tests in t/porting
417#
418f4069 418chdir "../t";
ad9b4e6f
A
419say "Running module tests";
420my @test_files = `find ../cpan/$pkg_dir -name '*.t' -type f`;
421chomp @test_files;
422my $output = `./perl TEST @test_files`;
423unless ($output =~ /All tests successful/) {
424 say $output;
425 exit 1;
426}
427
418f4069
A
428print "Running tests in t/porting ";
429my @tests = `ls porting/*.t`;
430chomp @tests;
431my @failed;
432foreach my $t (@tests) {
433 my @not = `./perl -I../lib -I.. $t | grep ^not | grep -v "# TODO"`;
434 print @not ? '!' : '.';
435 push @failed => $t if @not;
436}
437print "\n";
438say "Failed tests: @failed" if @failed;
439
440
9807c17b
JL
441say "Attempting to update Maintainers.pl";
442chdir '..';
443
444open my $Maintainers_pl, '<', 'Porting/Maintainers.pl';
445open my $new_Maintainers_pl, '>', 'Maintainers.pl';
446
447my $found;
448my $in_mod_section;
449while (<$Maintainers_pl>) {
450 if (!$found) {
451 if ($in_mod_section) {
452 if (/DISTRIBUTION/) {
33c6567b 453 if (s/\Q$old_version/$new_version/) {
9807c17b
JL
454 $found = 1;
455 }
456 }
457
458 if (/^ }/) {
459 $in_mod_section = 0;
460 }
461 }
462
463 if (/\Q$cpan_mod/) {
464 $in_mod_section = 1;
465 }
466 }
467
468 print $new_Maintainers_pl $_;
469}
470
471if ($found) {
472 unlink 'Porting/Maintainers.pl';
473 rename 'Maintainers.pl' => 'Porting/Maintainers.pl';
474 system chmod => 'a+x', 'Porting/Maintainers.pl';
475}
476else {
477 say "Could not update Porting/Maintainers.pl.";
478 say "Make sure you update this by hand before committing.";
479}
418f4069 480
418f4069 481say "$o_module is now version $new_version";
9807c17b 482say "Now you ought to run a make; make test ...";
418f4069
A
483
484
485__END__