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 wget, git, tar, chmod, perl, make, and rm
134 die "This does not look like a top level directory"
135 unless -d "cpan" && -d "Porting";
142 require "Porting/Maintainers.pl";
144 my %IGNORABLE = map {$_ => 1} @IGNORABLE;
146 my $package = "02packages.details.txt";
147 my $package_url = "http://www.cpan.org/modules/$package";
148 my $package_file = "/tmp/$package";
151 'podlators', # weird CUSTOMIZED section due to .PL files
155 GetOptions ('tarball=s' => \my $tarball,
156 'version=s' => \my $version,
157 force => \my $force,)
158 or die "Failed to parse arguments";
160 die "Usage: $0 module [args] [cpan package]" unless @ARGV == 1 || @ARGV == 2;
162 my ($module) = shift;
163 my $cpan_mod = @ARGV ? shift : $module;
166 my $info = $Modules {$module} or die "Cannot find module $module";
167 my $distribution = $$info {DISTRIBUTION};
169 my @files = glob $$info {FILES};
170 if (!-d $files [0] || grep { $_ eq $module } @problematic) {
171 say "This looks like a setup $0 cannot handle (yet)";
173 say "Will not continue without a --force option";
176 say "--force is in effect, so we'll soldier on. Wish me luck!";
182 my $pkg_dir = $files[0];
185 my ($old_version) = $distribution =~ /-([0-9.]+)\.tar\.gz/;
187 my $o_module = $module;
188 if ($cpan_mod =~ /-/ && $cpan_mod !~ /::/) {
189 $cpan_mod =~ s/-/::/g;
193 # Find the information from CPAN.
201 unless (-f $package_file && -M $package_file < 1) {
202 system wget => $package_url, '-qO', $package_file;
205 my $new_line = `grep '^$cpan_mod ' $package_file`
206 or die "Cannot find $cpan_mod on CPAN\n";
208 (undef, $new_version, my $new_path) = split ' ', $new_line;
209 if (defined $version) {
210 $new_path =~ s/-$new_version\./-$version\./;
211 $new_version = $version;
213 $new_file = (split '/', $new_path) [-1];
215 my $url = "http://search.cpan.org/CPAN/authors/id/$new_path";
218 # Fetch the new distro
220 system wget => $url, '-qO', $new_file;
223 $new_file = $tarball;
224 $new_version = $version // ($new_file =~ /-([0-9._]+)\.tar\.gz/) [0];
227 my $old_dir = "$pkg_dir-$old_version";
229 say "Cleaning out old directory";
230 system git => 'clean', '-dfxq', $pkg_dir;
232 say "Unpacking $new_file";
233 Archive::Tar->extract_archive( $new_file );
235 (my $new_dir = $new_file) =~ s/\.tar\.gz//;
236 # ensure 'make' will update all files
237 system('find', $new_dir, '-exec', 'touch', '{}', ';');
239 say "Renaming directories";
240 rename $pkg_dir => $old_dir;
242 say "Creating new package directory";
245 say "Populating new package directory";
246 my $map = $$info {MAP};
249 if ($$info {EXCLUDED}) {
250 foreach my $entry (@{$$info {EXCLUDED}}) {
251 if (ref $entry) {push @EXCLUDED_QR => $entry}
252 else {$EXCLUDED_QQ {$entry} = 1}
256 FILE: for my $file ( `find $new_dir -type f` ) {
258 my $old_file = $file;
259 $file =~ s{^$new_dir/}{};
261 next if $EXCLUDED_QQ{$file};
262 for my $qr (@EXCLUDED_QR) {
263 next FILE if $file =~ $qr;
267 for my $key ( sort { length $b <=> length $a } keys %$map ) {
268 my $val = $map->{$key};
269 last if $file =~ s/^$key/$val/;
273 $file = $files[0] . '/' . $file;
276 if ( $file =~ m{^cpan/} ) {
277 $file =~ s{^cpan/}{};
280 $file = '../' . $file;
284 my @parts = split '/', $file;
286 for my $part (@parts) {
287 $prefix .= '/' if $prefix;
289 mkdir $prefix unless -d $prefix;
292 rename $old_file => $file;
294 system 'rm', '-rf', $new_dir;
296 if (-f "$old_dir/.gitignore") {
297 say "Restoring .gitignore";
298 system git => 'checkout', "$pkg_dir/.gitignore";
301 my @new_files = `find $pkg_dir -type f`;
303 @new_files = grep {$_ ne $pkg_dir} @new_files;
304 s!^[^/]+/!! for @new_files;
305 my %new_files = map {$_ => 1} @new_files;
307 my @old_files = `find $old_dir -type f`;
309 @old_files = grep {$_ ne $old_dir} @old_files;
310 s!^[^/]+/!! for @old_files;
311 my %old_files = map {$_ => 1} @old_files;
317 foreach 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;
324 push @commit => $file;
326 foreach my $file (@old_files) {
327 next if -d "$old_dir/$file";
328 next if $new_files {$file};
333 # Find all files with an exec bit
335 my @exec = `find $pkg_dir -type f -perm +111`;
338 foreach my $file (@exec) {
340 $file =~ s!^[^/]+/!!;
341 if ($file =~ m!^t/!) {
342 push @de_exec => $file;
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;
355 # No need to change the +x bit on files that will be deleted.
357 if (@de_exec && @delete) {
358 my %delete = map {+"$pkg_dir/$_" => 1} @delete;
359 @de_exec = grep {!$delete {$_}} @de_exec;
362 say "unlink $pkg_dir/$_" for @delete;
363 say "git add $pkg_dir/$_" for @commit;
364 say "git rm -f $pkg_dir/$_" for @gone;
365 say "chmod a-x $pkg_dir/$_" for @de_exec;
367 print "Hit return to continue; ^C to abort "; <STDIN>;
369 unlink "$pkg_dir/$_" for @delete;
370 system git => 'add', "$pkg_dir/$_" for @commit;
371 system git => 'rm', '-f', "$pkg_dir/$_" for @gone;
372 system chmod => 'a-x', "$pkg_dir/$_" for @de_exec;
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.
379 if ($$info {CUSTOMIZED}) {
380 say "Restoring customized files";
381 foreach my $file (@{$$info {CUSTOMIZED}}) {
382 system git => "checkout", "$pkg_dir/$file";
388 say "Fixing MANIFEST";
389 my $MANIFEST = "MANIFEST";
390 my $MANIFEST_SORT = "$MANIFEST.sorted";
391 open my $fh, ">>", $MANIFEST;
392 say $fh "cpan/$pkg_dir/$_" for @commit;
394 system perl => "Porting/manisort", '--output', $MANIFEST_SORT;
395 rename $MANIFEST_SORT => $MANIFEST;
399 print "Running a make ... ";
400 system "make > make.log 2>&1" and die "Running make failed, see make.log";
404 # Must clean up, or else t/porting/FindExt.t will fail.
405 # Note that we can always retrieve the original directory with a git checkout.
407 print "About to clean up; hit return or abort (^C) "; <STDIN>;
410 system rm => '-r', $old_dir;
411 unlink $new_file unless $tarball;
415 # Run the tests. First the test belonging to the module, followed by the
416 # the tests in t/porting
419 say "Running module tests";
420 my @test_files = `find ../cpan/$pkg_dir -name '*.t' -type f`;
422 my $output = `./perl TEST @test_files`;
423 unless ($output =~ /All tests successful/) {
428 print "Running tests in t/porting ";
429 my @tests = `ls porting/*.t`;
432 foreach 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;
438 say "Failed tests: @failed" if @failed;
441 say "Attempting to update Maintainers.pl";
444 open my $Maintainers_pl, '<', 'Porting/Maintainers.pl';
445 open my $new_Maintainers_pl, '>', 'Maintainers.pl';
449 while (<$Maintainers_pl>) {
451 if ($in_mod_section) {
452 if (/DISTRIBUTION/) {
453 if (s/\Q$old_version/$new_version/) {
468 print $new_Maintainers_pl $_;
472 unlink 'Porting/Maintainers.pl';
473 rename 'Maintainers.pl' => 'Porting/Maintainers.pl';
474 system chmod => 'a+x', 'Porting/Maintainers.pl';
477 say "Could not update Porting/Maintainers.pl.";
478 say "Make sure you update this by hand before committing.";
481 say "$o_module is now version $new_version";
482 say "Now you ought to run a make; make test ...";