This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Digest-SHA to CPAN version 6.00
[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
190c1b3b 69Updates the contents of F<MANIFEST>
c5e3e317
JL
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
b7078f1e
AC
93=head1 OPTIONS
94
95=over 4
96
97=item C<--jobs> I<N>
98
99When running C<make>, pass a C<< -jI<N> >> option to it.
100
101=back
102
c5e3e317
JL
103=head1 TODO
104
105=over 4
106
107=item *
108
c5e3e317
JL
109Update F<Porting/Maintainers.pl>
110
111=item *
112
113Optional, run a full test suite
114
115=item *
116
117Handle complicated C<FILES>
118
119=back
120
121This is an initial version; no attempt has been made yet to make this
122portable. It shells out instead of trying to find a Perl solution.
cd9a1714 123In particular, it assumes git, perl, and make
c5e3e317
JL
124to be available.
125
126=cut
127
418f4069 128
4d18e0a2
A
129package Maintainers;
130
418f4069
A
131use 5.010;
132
133use strict;
134use warnings;
4d18e0a2 135use Getopt::Long;
a1450e8b 136use Archive::Tar;
bd4de633 137use File::Basename qw( basename );
192f56b0 138use File::Path qw( remove_tree );
fc134225 139use File::Find;
af8c53c3 140use File::Spec::Functions qw( tmpdir );
160daab8 141use Config qw( %Config );
418f4069
A
142
143$| = 1;
144
215d9c65
AC
145use constant WIN32 => $^O eq 'MSWin32';
146
07a826df 147die "This does not look like a top level directory"
418f4069
A
148 unless -d "cpan" && -d "Porting";
149
4b760246
AC
150# Check that there's a Makefile, if needed; otherwise, we'll do most of our
151# work only to fail when we try to run make, and the user will have to
152# either unpick everything we've done, or do the rest manually.
153die "Please run Configure before using $0\n"
154 if !WIN32 && !-f "Makefile";
155
418f4069
A
156our @IGNORABLE;
157our %Modules;
158
159use autodie;
160
b27c755c 161require "./Porting/Maintainers.pl";
418f4069 162
e6e4cae9
AC
163my $MAKE_LOG = 'make.log';
164
418f4069
A
165my %IGNORABLE = map {$_ => 1} @IGNORABLE;
166
af8c53c3 167my $tmpdir = tmpdir();
fc134225 168
418f4069
A
169my $package = "02packages.details.txt";
170my $package_url = "http://www.cpan.org/modules/$package";
fc134225 171my $package_file = "$tmpdir/$package"; # this is a cache
418f4069 172
b7e2b692
JL
173my @problematic = (
174 'podlators', # weird CUSTOMIZED section due to .PL files
175);
176
418f4069 177
311454c0
MB
178sub usage
179{
180 my $err = shift and select STDERR;
181 print "Usage: $0 module [args] [cpan package]\n";
182 exit $err;
183}
184
4d18e0a2 185GetOptions ('tarball=s' => \my $tarball,
b5bf278a 186 'version=s' => \my $version,
b7078f1e 187 'jobs=i' => \my $make_jobs,
311454c0
MB
188 force => \my $force,
189 help => sub { usage 0; },
190 ) or die "Failed to parse arguments";
4d18e0a2 191
311454c0 192usage 1 unless @ARGV == 1 || @ARGV == 2;
418f4069 193
fc134225
MM
194sub find_type_f {
195 my @res;
196 find( { no_chdir => 1, wanted => sub {
197 my $file= $File::Find::name;
198 return unless -f $file;
199 push @res, $file
200 }}, @_ );
201 @res
202};
203
cd9a1714
MM
204# Equivalent of `chmod a-x`
205sub de_exec {
746bc9e1
AC
206 my ($filename) = @_;
207 my $mode = (stat $filename)[2] & 0777;
208 if ($mode & 0111) { # exec-bit set
209 chmod $mode & 0666, $filename;
cd9a1714
MM
210 }
211}
212
b6574671
AC
213# Equivalent of `chmod +w`
214sub make_writable {
215 my ($filename) = @_;
216 my $mode = (stat $filename)[2] & 0777;
217 if (!($mode & 0222)) { # not writable
218 chmod $mode | (0222 & ~umask), $filename;
219 }
220}
221
cd9a1714 222sub make {
160daab8 223 my @args= @_;
b7078f1e 224 unshift @args, "-j$make_jobs" if defined $make_jobs;
215d9c65 225 if (WIN32) {
160daab8 226 chdir "Win32";
e6e4cae9
AC
227 system "$Config{make} @args> ..\\$MAKE_LOG 2>&1"
228 and die "Running make failed, see $MAKE_LOG";
160daab8
MM
229 chdir '..';
230 } else {
e6e4cae9
AC
231 system "$Config{make} @args> $MAKE_LOG 2>&1"
232 and die "Running make failed, see $MAKE_LOG";
160daab8
MM
233 };
234};
235
5b73aae5 236my ($module) = shift;
418f4069 237
24c7e242
AC
238my $info = $Modules{$module};
239if (!$info) {
469f7948
AC
240 # Maybe the user said "Test-Simple" instead of "Test::Simple", or
241 # "IO::Compress" instead of "IO-Compress". See if we can fix it up.
242 my $guess = $module;
243 s/-/::/g or s/::/-/g for $guess;
244 $info = $Modules{$guess} or die <<"EOF";
245Cannot find module $module.
246The available options are listed in the %Modules hash in Porting/Maintainers.pl
247EOF
24c7e242
AC
248 say "Guessing you meant $guess instead of $module";
249 $module = $guess;
250}
251
7035e4d3
AC
252if ($info->{CUSTOMIZED}) {
253 print <<"EOF";
254$module has a CUSTOMIZED entry in Porting/Maintainers.pl.
255
256This program's behaviour is to copy every CUSTOMIZED file into the version
257of the module being imported. But that might not be the right thing: in some
258cases, the new CPAN version will supersede whatever changes had previously
259been made in blead, so it would be better to import the new CPAN files.
260
261If you've checked that the CUSTOMIZED versions are still correct, you can
262proceed now. Otherwise, you should abort and investigate the situation. If
263the blead customizations are no longer needed, delete the CUSTOMIZED entry
264for $module in Porting/Maintainers.pl (and you'll also need to regenerate
265t/porting/customized.dat in that case; see t/porting/customized.t).
266
267EOF
268 print "Hit return to continue; ^C to abort "; <STDIN>;
269}
270
24c7e242 271my $cpan_mod = @ARGV ? shift : $module;
4d18e0a2 272
418f4069 273my $distribution = $$info {DISTRIBUTION};
b5bf278a
A
274
275my @files = glob $$info {FILES};
b7e2b692 276if (!-d $files [0] || grep { $_ eq $module } @problematic) {
b5bf278a
A
277 say "This looks like a setup $0 cannot handle (yet)";
278 unless ($force) {
279 say "Will not continue without a --force option";
280 exit 1;
281 }
282 say "--force is in effect, so we'll soldier on. Wish me luck!";
283}
284
285
286chdir "cpan";
287
83d3dd1d 288my $pkg_dir = $files[0];
418f4069
A
289 $pkg_dir =~ s!.*/!!;
290
87a7cbad 291my ($old_version) = $distribution =~ /-([0-9.]+(?:-TRIAL[0-9]*)?)\.tar\.gz/;
418f4069
A
292
293my $o_module = $module;
5b73aae5
A
294if ($cpan_mod =~ /-/ && $cpan_mod !~ /::/) {
295 $cpan_mod =~ s/-/::/g;
418f4069
A
296}
297
fea97686
FC
298sub wget {
299 my ($url, $saveas) = @_;
300 eval {
301 require HTTP::Tiny;
302 my $http= HTTP::Tiny->new();
303 $http->mirror( $url => $saveas );
304 1
305 } or
306 # Some system do not have wget. Fall back to curl if we do not
307 # have it. On Windows, `which wget` is not going to work, so
308 # just use wget, as this script has always done.
309 WIN32 || -x substr(`which wget`, 0, -1)
310 ? system wget => $url, '-qO', $saveas
311 : system curl => $url, '-sSo', $saveas;
312}
313
418f4069
A
314#
315# Find the information from CPAN.
316#
4d18e0a2
A
317my $new_file;
318my $new_version;
d11e2991 319if (defined $tarball) {
861c6796
AC
320 die "Tarball $tarball does not exist\n" if !-e $tarball;
321 die "Tarball $tarball is not a plain file\n" if !-f _;
d11e2991
AC
322 $new_file = $tarball;
323 $new_version = $version // ($new_file =~ /-([0-9._]+(?:-TRIAL[0-9]*)?)\.tar\.gz/) [0];
08ee4cf2
AC
324 die "Blead and that tarball both have version $new_version of $module\n"
325 if $new_version eq $old_version;
d11e2991
AC
326}
327else {
4d18e0a2
A
328 #
329 # Poor man's cache
330 #
331 unless (-f $package_file && -M $package_file < 1) {
fea97686 332 wget $package_url, $package_file;
4d18e0a2
A
333 }
334
cefd15c2
MM
335 open my $fh, '<', $package_file;
336 (my $new_line) = grep {/^$cpan_mod/} <$fh> # Yes, this needs a lot of memory
4d18e0a2 337 or die "Cannot find $cpan_mod on CPAN\n";
4d18e0a2 338 (undef, $new_version, my $new_path) = split ' ', $new_line;
3a4316cc
JL
339 if (defined $version) {
340 $new_path =~ s/-$new_version\./-$version\./;
341 $new_version = $version;
342 }
4d18e0a2
A
343 $new_file = (split '/', $new_path) [-1];
344
08ee4cf2
AC
345 die "The latest version of $module is $new_version, but blead already has it\n"
346 if $new_version eq $old_version;
347
499e37eb 348 my $url = "https://cpan.metacpan.org/authors/id/$new_path";
4d18e0a2
A
349 say "Fetching $url";
350 #
351 # Fetch the new distro
352 #
fea97686 353 wget $url, $new_file;
4d18e0a2 354}
418f4069
A
355
356my $old_dir = "$pkg_dir-$old_version";
418f4069
A
357
358say "Cleaning out old directory";
359system git => 'clean', '-dfxq', $pkg_dir;
360
418f4069 361say "Unpacking $new_file";
5fb91d48 362Archive::Tar->extract_archive( $new_file );
418f4069 363
bd4de633 364(my $new_dir = basename($new_file)) =~ s/\.tar\.gz//;
3f7808eb 365# ensure 'make' will update all files
fc134225
MM
366my $t= time;
367for my $file (find_type_f($new_dir)) {
b6574671 368 make_writable($file); # for convenience if the user later edits it
fc134225
MM
369 utime($t,$t,$file);
370};
418f4069
A
371
372say "Renaming directories";
373rename $pkg_dir => $old_dir;
418f4069 374
83d3dd1d
JL
375say "Creating new package directory";
376mkdir $pkg_dir;
377
378say "Populating new package directory";
379my $map = $$info {MAP};
380my @EXCLUDED_QR;
381my %EXCLUDED_QQ;
382if ($$info {EXCLUDED}) {
383 foreach my $entry (@{$$info {EXCLUDED}}) {
384 if (ref $entry) {push @EXCLUDED_QR => $entry}
385 else {$EXCLUDED_QQ {$entry} = 1}
386 }
387}
388
fc134225 389FILE: for my $file ( find_type_f( $new_dir )) {
83d3dd1d
JL
390 my $old_file = $file;
391 $file =~ s{^$new_dir/}{};
392
393 next if $EXCLUDED_QQ{$file};
394 for my $qr (@EXCLUDED_QR) {
395 next FILE if $file =~ $qr;
396 }
397
398 if ( $map ) {
399 for my $key ( sort { length $b <=> length $a } keys %$map ) {
400 my $val = $map->{$key};
401 last if $file =~ s/^$key/$val/;
402 }
403 }
7bbb137d
JL
404 else {
405 $file = $files[0] . '/' . $file;
406 }
83d3dd1d
JL
407
408 if ( $file =~ m{^cpan/} ) {
409 $file =~ s{^cpan/}{};
410 }
411 else {
412 $file = '../' . $file;
413 }
414
415 my $prefix = '';
416 my @parts = split '/', $file;
417 pop @parts;
418 for my $part (@parts) {
419 $prefix .= '/' if $prefix;
420 $prefix .= $part;
421 mkdir $prefix unless -d $prefix;
422 }
423
424 rename $old_file => $file;
425}
192f56b0 426remove_tree( $new_dir );
418f4069
A
427
428if (-f "$old_dir/.gitignore") {
429 say "Restoring .gitignore";
430 system git => 'checkout', "$pkg_dir/.gitignore";
431}
432
fc134225 433my @new_files = find_type_f( $pkg_dir );
418f4069
A
434@new_files = grep {$_ ne $pkg_dir} @new_files;
435s!^[^/]+/!! for @new_files;
436my %new_files = map {$_ => 1} @new_files;
437
fc134225 438my @old_files = find_type_f( $old_dir );
418f4069
A
439@old_files = grep {$_ ne $old_dir} @old_files;
440s!^[^/]+/!! for @old_files;
441my %old_files = map {$_ => 1} @old_files;
442
418f4069
A
443my @delete;
444my @commit;
445my @gone;
418f4069
A
446FILE:
447foreach my $file (@new_files) {
448 next if -d "$pkg_dir/$file"; # Ignore directories.
449 next if $old_files {$file}; # It's already there.
450 if ($IGNORABLE {$file}) {
451 push @delete => $file;
452 next;
453 }
418f4069
A
454 push @commit => $file;
455}
456foreach my $file (@old_files) {
457 next if -d "$old_dir/$file";
458 next if $new_files {$file};
459 push @gone => $file;
460}
ad9b4e6f
A
461
462#
463# Find all files with an exec bit
464#
fc134225 465my @exec = find_type_f( $pkg_dir );
ad9b4e6f
A
466my @de_exec;
467foreach my $file (@exec) {
468 # Remove leading dir
469 $file =~ s!^[^/]+/!!;
470 if ($file =~ m!^t/!) {
471 push @de_exec => $file;
472 next;
473 }
474 # Check to see if the file exists; if it doesn't and doesn't have
475 # the exec bit, remove it.
476 if ($old_files {$file}) {
477 unless (-x "$old_dir/$file") {
478 push @de_exec => $file;
479 }
480 }
481}
418f4069
A
482
483#
484# No need to change the +x bit on files that will be deleted.
485#
ad9b4e6f 486if (@de_exec && @delete) {
a9f5d1d4 487 my %delete = map {+"$pkg_dir/$_" => 1} @delete;
ad9b4e6f 488 @de_exec = grep {!$delete {$_}} @de_exec;
418f4069
A
489}
490
e42bf9ad
AC
491#
492# Mustn't change the +x bit on files that are whitelisted
493#
494if (@de_exec) {
54ed4dc4 495 my %permitted = map { (my $x = $_) =~ tr/\n//d; $x => 1 } grep !/^#/,
e42bf9ad
AC
496 do { local @ARGV = '../Porting/exec-bit.txt'; <> };
497 @de_exec = grep !$permitted{"cpan/$pkg_dir/$_"}, @de_exec;
498}
499
418f4069
A
500say "unlink $pkg_dir/$_" for @delete;
501say "git add $pkg_dir/$_" for @commit;
502say "git rm -f $pkg_dir/$_" for @gone;
ad9b4e6f 503say "chmod a-x $pkg_dir/$_" for @de_exec;
418f4069
A
504
505print "Hit return to continue; ^C to abort "; <STDIN>;
506
507unlink "$pkg_dir/$_" for @delete;
508system git => 'add', "$pkg_dir/$_" for @commit;
509system git => 'rm', '-f', "$pkg_dir/$_" for @gone;
cd9a1714 510de_exec( "$pkg_dir/$_" ) for @de_exec;
418f4069 511
9c259538
A
512#
513# Restore anything that is customized.
514# We don't really care whether we've deleted the file - since we
515# do a git restore, it's going to be resurrected if necessary.
516#
517if ($$info {CUSTOMIZED}) {
518 say "Restoring customized files";
519 foreach my $file (@{$$info {CUSTOMIZED}}) {
520 system git => "checkout", "$pkg_dir/$file";
521 }
522}
523
a8121781 524chdir "..";
190c1b3b 525if (@commit || @gone) {
418f4069 526 say "Fixing MANIFEST";
190c1b3b
AC
527 my $MANIFEST = "MANIFEST";
528 my $MANIFEST_NEW = "$MANIFEST.new";
529
530 open my $orig, "<", $MANIFEST
531 or die "Failed to open $MANIFEST for reading: $!\n";
532 open my $new, ">", $MANIFEST_NEW
533 or die "Failed to open $MANIFEST_NEW for writing: $!\n";
534 my %gone = map +("cpan/$pkg_dir/$_" => 1), @gone;
535 while (my $line = <$orig>) {
536 my ($file) = $line =~ /^(\S+)/
537 or die "Can't parse MANIFEST line: $line";
538 print $new $line if !$gone{$file};
539 }
540
541 say $new "cpan/$pkg_dir/$_" for @commit;
542
543 close $new or die "Can't close $MANIFEST: $!\n";
544
545 system $^X => "Porting/manisort", '--quiet', "--output=$MANIFEST", $MANIFEST_NEW;
546 unlink $MANIFEST_NEW
547 or die "Can't delete temporary $MANIFEST_NEW: $!\n";
418f4069
A
548}
549
550
e6e4cae9 551print "Running a make and saving its output to $MAKE_LOG ... ";
160daab8
MM
552# Prepare for running (selected) tests
553make 'test-prep';
418f4069
A
554print "done\n";
555
d8a823f4
AC
556# The build system installs code from CPAN dists into the lib/ directory,
557# creating directories as needed. This means that the cleaning-related rules
558# in the Makefile need to know which directories to clean up. The Makefile
559# is generated by Configure from Makefile.SH, so *that* file needs the list
560# of directories. regen/lib_cleanup.pl is capable of automatically updating
561# the contents of Makefile.SH (and win32/Makefile, which needs similar but
562# not identical lists of directories), so we can just run that (using the
563# newly-built Perl, as is done with the regen programs run by "make regen").
564#
565# We do this if any files at all have been added or deleted, regardless of
566# whether those changes result in any directories being added or deleted,
567# because the alternative would be to replicate the regen/lib_cleanup.pl
568# logic here. That's fine, because regen/lib_cleanup.pl is idempotent if run
569# repeatedly.
570if (@commit || @gone) {
571 say "Running regen/lib_cleanup.pl to handle potential added/deleted dirs";
215d9c65 572 my $exe_dir = WIN32 ? ".\\" : './';
d8a823f4
AC
573 system "${exe_dir}perl$Config{_exe}", "-Ilib", "regen/lib_cleanup.pl"
574 and die "regen/lib_cleanup.pl failed\n";
575}
576
418f4069
A
577#
578# Must clean up, or else t/porting/FindExt.t will fail.
730ad6b9 579# Note that we can always retrieve the original directory with a git checkout.
418f4069
A
580#
581print "About to clean up; hit return or abort (^C) "; <STDIN>;
582
192f56b0
MM
583remove_tree( "cpan/$old_dir" );
584unlink "cpan/$new_file" unless $tarball;
418f4069 585
ad9b4e6f
A
586#
587# Run the tests. First the test belonging to the module, followed by the
588# the tests in t/porting
589#
192f56b0 590chdir "t";
ad9b4e6f 591say "Running module tests";
57b5d6e1 592my @test_files = grep { /\.t$/ } find_type_f( "../cpan/$pkg_dir" );
215d9c65 593my $exe_dir = WIN32 ? "..\\" : './';
60f5272c 594my $output = `${exe_dir}perl$Config{_exe} TEST @test_files`;
ad9b4e6f
A
595unless ($output =~ /All tests successful/) {
596 say $output;
597 exit 1;
598}
599
418f4069 600print "Running tests in t/porting ";
fc134225 601my @tests = glob 'porting/*.t';
418f4069
A
602chomp @tests;
603my @failed;
604foreach my $t (@tests) {
cefd15c2
MM
605 my @not = grep {!/# TODO/ }
606 grep { /^not/ }
607 `${exe_dir}perl -I../lib -I.. $t`;
418f4069
A
608 print @not ? '!' : '.';
609 push @failed => $t if @not;
610}
611print "\n";
612say "Failed tests: @failed" if @failed;
613
614
9807c17b
JL
615chdir '..';
616
617open my $Maintainers_pl, '<', 'Porting/Maintainers.pl';
618open my $new_Maintainers_pl, '>', 'Maintainers.pl';
619
620my $found;
621my $in_mod_section;
622while (<$Maintainers_pl>) {
623 if (!$found) {
624 if ($in_mod_section) {
625 if (/DISTRIBUTION/) {
33c6567b 626 if (s/\Q$old_version/$new_version/) {
9807c17b
JL
627 $found = 1;
628 }
629 }
630
92e8e650 631 if (/^ \}/) {
9807c17b
JL
632 $in_mod_section = 0;
633 }
634 }
635
ec1d1ba0 636 if (/\Q$module/) {
9807c17b
JL
637 $in_mod_section = 1;
638 }
639 }
640
641 print $new_Maintainers_pl $_;
642}
643
644if ($found) {
d9d83ea5 645 say "Successfully updated Maintainers.pl";
9807c17b
JL
646 unlink 'Porting/Maintainers.pl';
647 rename 'Maintainers.pl' => 'Porting/Maintainers.pl';
cd9a1714 648 chmod 0755 => 'Porting/Maintainers.pl';
9807c17b
JL
649}
650else {
651 say "Could not update Porting/Maintainers.pl.";
652 say "Make sure you update this by hand before committing.";
653}
418f4069 654
592f3827 655print <<"EOF";
418f4069 656
592f3827
AC
657=======================================================================
658
659$o_module is now at version $new_version
660Next, you should run a "make test".
661
662Hopefully that will complete successfully, but if not, you can make any
663changes you need to get the tests to pass. Don't forget that you'll need
664a "CUSTOMIZED" entry in Porting/Maintainers.pl if you change any of the
665files under cpan/$pkg_dir.
666
667Once all tests pass, you can "git add -u" and "git commit" the changes.
668
669EOF
418f4069
A
670
671__END__