5 Porting/sync-with-cpan - Synchronize with CPAN distributions
9 perl Porting/sync-with-cpan <module>
11 where <module> is the name it appears in the C<%Modules> hash
12 of F<Porting/Maintainers.pl>
16 Script to help out with syncing cpan distros.
24 Fetches the package list from CPAN. Finds the current version of the given
29 Downloads the relevant tarball; unpacks the tarball. [1]
33 Clean out the old directory (C<git clean -dfx>)
37 Moves the old directory out of the way, moves the new directory in place.
41 Restores any F<.gitignore> file.
45 Removes files from C<@IGNORE> and C<EXCLUDED>
49 C<git add> any new files.
53 C<git rm> any files that are gone.
57 Remove the +x bit on files in F<t/>
61 Remove the +x bit on files that don't have it enabled in the current dir
65 Restore files mentioned in C<CUSTOMIZED>
69 Adds new files to F<MANIFEST>
73 Runs a C<make> (assumes a configure has been run)
81 Runs tests for the package
85 Runs the porting tests
89 [1] If the C<--tarball> option is given, then CPAN is not consulted.
90 C<--tarball> should be the path to the tarball; the version is extracted
91 from the filename -- but can be overwritten by the C<--version> option.
99 Delete files from F<MANIFEST>
103 Update F<Porting/Maintainers.pl>
107 Optional, run a full test suite
111 Handle complicated C<FILES>
115 This is an initial version; no attempt has been made yet to make this
116 portable. It shells out instead of trying to find a Perl solution.
117 In particular, it assumes git, perl, and make
131 use File::Path qw( remove_tree );
133 use Config qw( %Config );
137 die "This does not look like a top level directory"
138 unless -d "cpan" && -d "Porting";
145 require "Porting/Maintainers.pl";
147 my %IGNORABLE = map {$_ => 1} @IGNORABLE;
149 my $tmpdir= $ENV{ TEMP } // '/tmp';
151 my $package = "02packages.details.txt";
152 my $package_url = "http://www.cpan.org/modules/$package";
153 my $package_file = "$tmpdir/$package"; # this is a cache
156 'podlators', # weird CUSTOMIZED section due to .PL files
162 my $err = shift and select STDERR;
163 print "Usage: $0 module [args] [cpan package]\n";
167 GetOptions ('tarball=s' => \my $tarball,
168 'version=s' => \my $version,
170 help => sub { usage 0; },
171 ) or die "Failed to parse arguments";
173 usage 1 unless @ARGV == 1 || @ARGV == 2;
177 find( { no_chdir => 1, wanted => sub {
178 my $file= $File::Find::name;
179 return unless -f $file;
185 # Equivalent of `chmod a-x`
187 for my $filename ( @_ ) {
188 my $mode= (stat $filename)[2] & 0777;
189 if( $mode & 0111 ) { # exec-bit set
190 chmod $mode & 0666, $filename;
197 if( $^O eq 'MSWin32') {
199 system "$Config{make} @args> ..\\make.log 2>&1" and die "Running make failed, see make.log";
202 system "$Config{make} @args> make.log 2>&1" and die "Running make failed, see make.log";
206 my ($module) = shift;
207 my $cpan_mod = @ARGV ? shift : $module;
210 my $info = $Modules {$module} or die "Cannot find module $module";
211 my $distribution = $$info {DISTRIBUTION};
213 my @files = glob $$info {FILES};
214 if (!-d $files [0] || grep { $_ eq $module } @problematic) {
215 say "This looks like a setup $0 cannot handle (yet)";
217 say "Will not continue without a --force option";
220 say "--force is in effect, so we'll soldier on. Wish me luck!";
226 my $pkg_dir = $files[0];
229 my ($old_version) = $distribution =~ /-([0-9.]+(?:-TRIAL[0-9]*)?)\.tar\.gz/;
231 my $o_module = $module;
232 if ($cpan_mod =~ /-/ && $cpan_mod !~ /::/) {
233 $cpan_mod =~ s/-/::/g;
237 # Find the information from CPAN.
245 unless (-f $package_file && -M $package_file < 1) {
248 my $http= HTTP::Tiny->new();
249 $http->mirror( $package_url => $package_file );
251 } or system wget => $package_url, '-qO', $package_file;
254 open my $fh, '<', $package_file;
255 (my $new_line) = grep {/^$cpan_mod/} <$fh> # Yes, this needs a lot of memory
256 or die "Cannot find $cpan_mod on CPAN\n";
257 (undef, $new_version, my $new_path) = split ' ', $new_line;
258 if (defined $version) {
259 $new_path =~ s/-$new_version\./-$version\./;
260 $new_version = $version;
262 $new_file = (split '/', $new_path) [-1];
264 my $url = "http://search.cpan.org/CPAN/authors/id/$new_path";
267 # Fetch the new distro
271 my $http= HTTP::Tiny->new();
272 $http->mirror( $url => $new_file );
274 } or system wget => $url, '-qO', $new_file;
277 $new_file = $tarball;
278 $new_version = $version // ($new_file =~ /-([0-9._]+(?:-TRIAL[0-9]*)?)\.tar\.gz/) [0];
281 my $old_dir = "$pkg_dir-$old_version";
283 say "Cleaning out old directory";
284 system git => 'clean', '-dfxq', $pkg_dir;
286 say "Unpacking $new_file";
287 Archive::Tar->extract_archive( $new_file );
289 (my $new_dir = $new_file) =~ s/\.tar\.gz//;
290 # ensure 'make' will update all files
292 for my $file (find_type_f($new_dir)) {
293 open(my $fh,">>$file") || die "Cannot write $file:$!";
298 say "Renaming directories";
299 rename $pkg_dir => $old_dir;
301 say "Creating new package directory";
304 say "Populating new package directory";
305 my $map = $$info {MAP};
308 if ($$info {EXCLUDED}) {
309 foreach my $entry (@{$$info {EXCLUDED}}) {
310 if (ref $entry) {push @EXCLUDED_QR => $entry}
311 else {$EXCLUDED_QQ {$entry} = 1}
315 FILE: for my $file ( find_type_f( $new_dir )) {
316 my $old_file = $file;
317 $file =~ s{^$new_dir/}{};
319 next if $EXCLUDED_QQ{$file};
320 for my $qr (@EXCLUDED_QR) {
321 next FILE if $file =~ $qr;
325 for my $key ( sort { length $b <=> length $a } keys %$map ) {
326 my $val = $map->{$key};
327 last if $file =~ s/^$key/$val/;
331 $file = $files[0] . '/' . $file;
334 if ( $file =~ m{^cpan/} ) {
335 $file =~ s{^cpan/}{};
338 $file = '../' . $file;
342 my @parts = split '/', $file;
344 for my $part (@parts) {
345 $prefix .= '/' if $prefix;
347 mkdir $prefix unless -d $prefix;
350 rename $old_file => $file;
352 remove_tree( $new_dir );
354 if (-f "$old_dir/.gitignore") {
355 say "Restoring .gitignore";
356 system git => 'checkout', "$pkg_dir/.gitignore";
359 my @new_files = find_type_f( $pkg_dir );
360 @new_files = grep {$_ ne $pkg_dir} @new_files;
361 s!^[^/]+/!! for @new_files;
362 my %new_files = map {$_ => 1} @new_files;
364 my @old_files = find_type_f( $old_dir );
365 @old_files = grep {$_ ne $old_dir} @old_files;
366 s!^[^/]+/!! for @old_files;
367 my %old_files = map {$_ => 1} @old_files;
373 foreach my $file (@new_files) {
374 next if -d "$pkg_dir/$file"; # Ignore directories.
375 next if $old_files {$file}; # It's already there.
376 if ($IGNORABLE {$file}) {
377 push @delete => $file;
380 push @commit => $file;
382 foreach my $file (@old_files) {
383 next if -d "$old_dir/$file";
384 next if $new_files {$file};
389 # Find all files with an exec bit
391 my @exec = find_type_f( $pkg_dir );
393 foreach my $file (@exec) {
395 $file =~ s!^[^/]+/!!;
396 if ($file =~ m!^t/!) {
397 push @de_exec => $file;
400 # Check to see if the file exists; if it doesn't and doesn't have
401 # the exec bit, remove it.
402 if ($old_files {$file}) {
403 unless (-x "$old_dir/$file") {
404 push @de_exec => $file;
410 # No need to change the +x bit on files that will be deleted.
412 if (@de_exec && @delete) {
413 my %delete = map {+"$pkg_dir/$_" => 1} @delete;
414 @de_exec = grep {!$delete {$_}} @de_exec;
417 say "unlink $pkg_dir/$_" for @delete;
418 say "git add $pkg_dir/$_" for @commit;
419 say "git rm -f $pkg_dir/$_" for @gone;
420 say "chmod a-x $pkg_dir/$_" for @de_exec;
422 print "Hit return to continue; ^C to abort "; <STDIN>;
424 unlink "$pkg_dir/$_" for @delete;
425 system git => 'add', "$pkg_dir/$_" for @commit;
426 system git => 'rm', '-f', "$pkg_dir/$_" for @gone;
427 de_exec( "$pkg_dir/$_" ) for @de_exec;
430 # Restore anything that is customized.
431 # We don't really care whether we've deleted the file - since we
432 # do a git restore, it's going to be resurrected if necessary.
434 if ($$info {CUSTOMIZED}) {
435 say "Restoring customized files";
436 foreach my $file (@{$$info {CUSTOMIZED}}) {
437 system git => "checkout", "$pkg_dir/$file";
443 say "Fixing MANIFEST";
444 my $MANIFEST = "MANIFEST";
445 my $MANIFEST_SORT = "$MANIFEST.sorted";
446 open my $fh, ">>", $MANIFEST;
447 say $fh "cpan/$pkg_dir/$_" for @commit;
449 system perl => "Porting/manisort", '--output', $MANIFEST_SORT;
450 rename $MANIFEST_SORT => $MANIFEST;
454 print "Running a make ... ";
455 # Prepare for running (selected) tests
460 # Must clean up, or else t/porting/FindExt.t will fail.
461 # Note that we can always retrieve the original directory with a git checkout.
463 print "About to clean up; hit return or abort (^C) "; <STDIN>;
465 remove_tree( "cpan/$old_dir" );
466 unlink "cpan/$new_file" unless $tarball;
469 # Run the tests. First the test belonging to the module, followed by the
470 # the tests in t/porting
473 say "Running module tests";
474 my @test_files = grep { /\.t$/ } find_type_f( $pkg_dir );
475 my $exe_dir= $^O =~ /MSWin/ ? "..\\" : './';
476 my $output = `${exe_dir}perl$Config{_exe} TEST @test_files`;
477 unless ($output =~ /All tests successful/) {
482 print "Running tests in t/porting ";
483 my @tests = glob 'porting/*.t';
486 foreach my $t (@tests) {
487 my @not = grep {!/# TODO/ }
489 `${exe_dir}perl -I../lib -I.. $t`;
490 print @not ? '!' : '.';
491 push @failed => $t if @not;
494 say "Failed tests: @failed" if @failed;
497 say "Attempting to update Maintainers.pl";
500 open my $Maintainers_pl, '<', 'Porting/Maintainers.pl';
501 open my $new_Maintainers_pl, '>', 'Maintainers.pl';
505 while (<$Maintainers_pl>) {
507 if ($in_mod_section) {
508 if (/DISTRIBUTION/) {
509 if (s/\Q$old_version/$new_version/) {
524 print $new_Maintainers_pl $_;
528 unlink 'Porting/Maintainers.pl';
529 rename 'Maintainers.pl' => 'Porting/Maintainers.pl';
530 chmod 0755 => 'Porting/Maintainers.pl';
533 say "Could not update Porting/Maintainers.pl.";
534 say "Make sure you update this by hand before committing.";
537 say "$o_module is now version $new_version";
538 say "Now you ought to run a make; make test ...";