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
133 die "This does not look like a top level directory"
134 unless -d "cpan" && -d "Porting";
141 require "Porting/Maintainers.pl";
143 my %IGNORABLE = map {$_ => 1} @IGNORABLE;
145 my $package = "02packages.details.txt";
146 my $package_url = "http://www.cpan.org/modules/$package";
147 my $package_file = "/tmp/$package";
150 'podlators', # weird CUSTOMIZED section due to .PL files
154 GetOptions ('tarball=s' => \my $tarball,
155 'version=s' => \my $version,
156 force => \my $force,)
157 or die "Failed to parse arguments";
159 die "Usage: $0 module [args] [cpan package]" unless @ARGV == 1 || @ARGV == 2;
161 my ($module) = shift;
162 my $cpan_mod = @ARGV ? shift : $module;
165 my $info = $Modules {$module} or die "Cannot find module $module";
166 my $distribution = $$info {DISTRIBUTION};
168 my @files = glob $$info {FILES};
169 if (!-d $files [0] || grep { $_ eq $module } @problematic) {
170 say "This looks like a setup $0 cannot handle (yet)";
172 say "Will not continue without a --force option";
175 say "--force is in effect, so we'll soldier on. Wish me luck!";
181 my $pkg_dir = $files[0];
184 my ($old_version) = $distribution =~ /-([0-9.]+)\.tar\.gz/;
186 my $o_module = $module;
187 if ($cpan_mod =~ /-/ && $cpan_mod !~ /::/) {
188 $cpan_mod =~ s/-/::/g;
192 # Find the information from CPAN.
200 unless (-f $package_file && -M $package_file < 1) {
201 system wget => $package_url, '-qO', $package_file;
204 my $new_line = `grep '^$cpan_mod ' $package_file`
205 or die "Cannot find $cpan_mod on CPAN\n";
207 (undef, $new_version, my $new_path) = split ' ', $new_line;
208 if (defined $version) {
209 $new_path =~ s/-$new_version\./-$version\./;
210 $new_version = $version;
212 $new_file = (split '/', $new_path) [-1];
214 my $url = "http://search.cpan.org/CPAN/authors/id/$new_path";
217 # Fetch the new distro
219 system wget => $url, '-qO', $new_file;
222 $new_file = $tarball;
223 $new_version = $version // ($new_file =~ /-([0-9._]+)\.tar\.gz/) [0];
226 my $old_dir = "$pkg_dir-$old_version";
228 say "Cleaning out old directory";
229 system git => 'clean', '-dfxq', $pkg_dir;
231 say "Unpacking $new_file";
233 system tar => 'xfz', $new_file;
234 (my $new_dir = $new_file) =~ s/\.tar\.gz//;
235 # ensure 'make' will update all files
236 system('find', $new_dir, '-exec', 'touch', '{}', ';');
238 say "Renaming directories";
239 rename $pkg_dir => $old_dir;
241 say "Creating new package directory";
244 say "Populating new package directory";
245 my $map = $$info {MAP};
248 if ($$info {EXCLUDED}) {
249 foreach my $entry (@{$$info {EXCLUDED}}) {
250 if (ref $entry) {push @EXCLUDED_QR => $entry}
251 else {$EXCLUDED_QQ {$entry} = 1}
255 FILE: for my $file ( `find $new_dir -type f` ) {
257 my $old_file = $file;
258 $file =~ s{^$new_dir/}{};
260 next if $EXCLUDED_QQ{$file};
261 for my $qr (@EXCLUDED_QR) {
262 next FILE if $file =~ $qr;
266 for my $key ( sort { length $b <=> length $a } keys %$map ) {
267 my $val = $map->{$key};
268 last if $file =~ s/^$key/$val/;
272 $file = $files[0] . '/' . $file;
275 if ( $file =~ m{^cpan/} ) {
276 $file =~ s{^cpan/}{};
279 $file = '../' . $file;
283 my @parts = split '/', $file;
285 for my $part (@parts) {
286 $prefix .= '/' if $prefix;
288 mkdir $prefix unless -d $prefix;
291 rename $old_file => $file;
293 system 'rm', '-rf', $new_dir;
295 if (-f "$old_dir/.gitignore") {
296 say "Restoring .gitignore";
297 system git => 'checkout', "$pkg_dir/.gitignore";
300 my @new_files = `find $pkg_dir -type f`;
302 @new_files = grep {$_ ne $pkg_dir} @new_files;
303 s!^[^/]+/!! for @new_files;
304 my %new_files = map {$_ => 1} @new_files;
306 my @old_files = `find $old_dir -type f`;
308 @old_files = grep {$_ ne $old_dir} @old_files;
309 s!^[^/]+/!! for @old_files;
310 my %old_files = map {$_ => 1} @old_files;
316 foreach my $file (@new_files) {
317 next if -d "$pkg_dir/$file"; # Ignore directories.
318 next if $old_files {$file}; # It's already there.
319 if ($IGNORABLE {$file}) {
320 push @delete => $file;
323 push @commit => $file;
325 foreach my $file (@old_files) {
326 next if -d "$old_dir/$file";
327 next if $new_files {$file};
332 # Find all files with an exec bit
334 my @exec = `find $pkg_dir -type f -perm +111`;
337 foreach my $file (@exec) {
339 $file =~ s!^[^/]+/!!;
340 if ($file =~ m!^t/!) {
341 push @de_exec => $file;
344 # Check to see if the file exists; if it doesn't and doesn't have
345 # the exec bit, remove it.
346 if ($old_files {$file}) {
347 unless (-x "$old_dir/$file") {
348 push @de_exec => $file;
354 # No need to change the +x bit on files that will be deleted.
356 if (@de_exec && @delete) {
357 my %delete = map {+"$pkg_dir/$_" => 1} @delete;
358 @de_exec = grep {!$delete {$_}} @de_exec;
361 say "unlink $pkg_dir/$_" for @delete;
362 say "git add $pkg_dir/$_" for @commit;
363 say "git rm -f $pkg_dir/$_" for @gone;
364 say "chmod a-x $pkg_dir/$_" for @de_exec;
366 print "Hit return to continue; ^C to abort "; <STDIN>;
368 unlink "$pkg_dir/$_" for @delete;
369 system git => 'add', "$pkg_dir/$_" for @commit;
370 system git => 'rm', '-f', "$pkg_dir/$_" for @gone;
371 system chmod => 'a-x', "$pkg_dir/$_" for @de_exec;
374 # Restore anything that is customized.
375 # We don't really care whether we've deleted the file - since we
376 # do a git restore, it's going to be resurrected if necessary.
378 if ($$info {CUSTOMIZED}) {
379 say "Restoring customized files";
380 foreach my $file (@{$$info {CUSTOMIZED}}) {
381 system git => "checkout", "$pkg_dir/$file";
387 say "Fixing MANIFEST";
388 my $MANIFEST = "MANIFEST";
389 my $MANIFEST_SORT = "$MANIFEST.sorted";
390 open my $fh, ">>", $MANIFEST;
391 say $fh "cpan/$pkg_dir/$_" for @commit;
393 system perl => "Porting/manisort", '--output', $MANIFEST_SORT;
394 rename $MANIFEST_SORT => $MANIFEST;
398 print "Running a make ... ";
399 system "make > make.log 2>&1" and die "Running make failed, see make.log";
403 # Must clean up, or else t/porting/FindExt.t will fail.
404 # Note that we can always retrieve the orginal directory with a git checkout.
406 print "About to clean up; hit return or abort (^C) "; <STDIN>;
409 system rm => '-r', $old_dir;
410 unlink $new_file unless $tarball;
414 # Run the tests. First the test belonging to the module, followed by the
415 # the tests in t/porting
418 say "Running module tests";
419 my @test_files = `find ../cpan/$pkg_dir -name '*.t' -type f`;
421 my $output = `./perl TEST @test_files`;
422 unless ($output =~ /All tests successful/) {
427 print "Running tests in t/porting ";
428 my @tests = `ls porting/*.t`;
431 foreach my $t (@tests) {
432 my @not = `./perl -I../lib -I.. $t | grep ^not | grep -v "# TODO"`;
433 print @not ? '!' : '.';
434 push @failed => $t if @not;
437 say "Failed tests: @failed" if @failed;
440 say "Attempting to update Maintainers.pl";
443 open my $Maintainers_pl, '<', 'Porting/Maintainers.pl';
444 open my $new_Maintainers_pl, '>', 'Maintainers.pl';
448 while (<$Maintainers_pl>) {
450 if ($in_mod_section) {
451 if (/DISTRIBUTION/) {
452 if (s/\Q$old_version/$new_version/) {
467 print $new_Maintainers_pl $_;
471 unlink 'Porting/Maintainers.pl';
472 rename 'Maintainers.pl' => 'Porting/Maintainers.pl';
473 system chmod => 'a+x', 'Porting/Maintainers.pl';
476 say "Could not update Porting/Maintainers.pl.";
477 say "Make sure you update this by hand before committing.";
480 say "$o_module is now version $new_version";
481 say "Now you ought to run a make; make test ...";