This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
update sync-with-cpan to handle dists with MAPped files
[perl5.git] / Porting / sync-with-cpan
CommitLineData
33e80a47 1#!/usr/bin/env perl
418f4069 2
c5e3e317
JL
3=head1 NAME
4
0c41dfff 5Porting/sync-with-cpan
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;
418f4069
A
130
131$| = 1;
132
07a826df 133die "This does not look like a top level directory"
418f4069
A
134 unless -d "cpan" && -d "Porting";
135
418f4069
A
136our @IGNORABLE;
137our %Modules;
138
139use autodie;
140
141require "Porting/Maintainers.pl";
142
418f4069
A
143my %IGNORABLE = map {$_ => 1} @IGNORABLE;
144
145my $package = "02packages.details.txt";
146my $package_url = "http://www.cpan.org/modules/$package";
147my $package_file = "/tmp/$package";
148
418f4069 149
4d18e0a2 150GetOptions ('tarball=s' => \my $tarball,
b5bf278a
A
151 'version=s' => \my $version,
152 force => \my $force,)
4d18e0a2
A
153 or die "Failed to parse arguments";
154
155die "Usage: $0 module [args] [cpan package]" unless @ARGV == 1 || @ARGV == 2;
418f4069 156
5b73aae5
A
157my ($module) = shift;
158my $cpan_mod = @ARGV ? shift : $module;
418f4069 159
4d18e0a2 160
418f4069
A
161my $info = $Modules {$module} or die "Cannot find module $module";
162my $distribution = $$info {DISTRIBUTION};
b5bf278a
A
163
164my @files = glob $$info {FILES};
83d3dd1d 165if (!-d $files [0]) {
b5bf278a
A
166 say "This looks like a setup $0 cannot handle (yet)";
167 unless ($force) {
168 say "Will not continue without a --force option";
169 exit 1;
170 }
171 say "--force is in effect, so we'll soldier on. Wish me luck!";
172}
173
174
175chdir "cpan";
176
83d3dd1d 177my $pkg_dir = $files[0];
418f4069
A
178 $pkg_dir =~ s!.*/!!;
179
180my ($old_version) = $distribution =~ /-([0-9.]+)\.tar\.gz/;
181
182my $o_module = $module;
5b73aae5
A
183if ($cpan_mod =~ /-/ && $cpan_mod !~ /::/) {
184 $cpan_mod =~ s/-/::/g;
418f4069
A
185}
186
187#
188# Find the information from CPAN.
189#
4d18e0a2
A
190my $new_file;
191my $new_version;
192unless ($tarball) {
193 #
194 # Poor man's cache
195 #
196 unless (-f $package_file && -M $package_file < 1) {
197 system wget => $package_url, '-qO', $package_file;
198 }
199
200 my $new_line = `grep '^$cpan_mod ' $package_file`
201 or die "Cannot find $cpan_mod on CPAN\n";
202 chomp $new_line;
203 (undef, $new_version, my $new_path) = split ' ', $new_line;
3a4316cc
JL
204 if (defined $version) {
205 $new_path =~ s/-$new_version\./-$version\./;
206 $new_version = $version;
207 }
4d18e0a2
A
208 $new_file = (split '/', $new_path) [-1];
209
210 my $url = "http://search.cpan.org/CPAN/authors/id/$new_path";
211 say "Fetching $url";
212 #
213 # Fetch the new distro
214 #
215 system wget => $url, '-qO', $new_file;
216}
217else {
218 $new_file = $tarball;
219 $new_version = $version // ($new_file =~ /-([0-9._]+)\.tar\.gz/) [0];
220}
418f4069
A
221
222my $old_dir = "$pkg_dir-$old_version";
418f4069
A
223
224say "Cleaning out old directory";
225system git => 'clean', '-dfxq', $pkg_dir;
226
418f4069
A
227say "Unpacking $new_file";
228
229system tar => 'xfz', $new_file;
618ac2f6 230(my $new_dir = $new_file) =~ s/\.tar\.gz//;
3f7808eb
JL
231# ensure 'make' will update all files
232system('find', $new_dir, '-exec', 'touch', '{}', ';');
418f4069
A
233
234say "Renaming directories";
235rename $pkg_dir => $old_dir;
418f4069 236
83d3dd1d
JL
237say "Creating new package directory";
238mkdir $pkg_dir;
239
240say "Populating new package directory";
241my $map = $$info {MAP};
242my @EXCLUDED_QR;
243my %EXCLUDED_QQ;
244if ($$info {EXCLUDED}) {
245 foreach my $entry (@{$$info {EXCLUDED}}) {
246 if (ref $entry) {push @EXCLUDED_QR => $entry}
247 else {$EXCLUDED_QQ {$entry} = 1}
248 }
249}
250
251FILE: for my $file ( `find $new_dir -type f` ) {
252 chomp $file;
253 my $old_file = $file;
254 $file =~ s{^$new_dir/}{};
255
256 next if $EXCLUDED_QQ{$file};
257 for my $qr (@EXCLUDED_QR) {
258 next FILE if $file =~ $qr;
259 }
260
261 if ( $map ) {
262 for my $key ( sort { length $b <=> length $a } keys %$map ) {
263 my $val = $map->{$key};
264 last if $file =~ s/^$key/$val/;
265 }
266 }
267
268 if ( $file =~ m{^cpan/} ) {
269 $file =~ s{^cpan/}{};
270 }
271 else {
272 $file = '../' . $file;
273 }
274
275 my $prefix = '';
276 my @parts = split '/', $file;
277 pop @parts;
278 for my $part (@parts) {
279 $prefix .= '/' if $prefix;
280 $prefix .= $part;
281 mkdir $prefix unless -d $prefix;
282 }
283
284 rename $old_file => $file;
285}
286system 'rm', '-rf', $new_dir;
418f4069
A
287
288if (-f "$old_dir/.gitignore") {
289 say "Restoring .gitignore";
290 system git => 'checkout', "$pkg_dir/.gitignore";
291}
292
ad9b4e6f 293my @new_files = `find $pkg_dir -type f`;
418f4069
A
294chomp @new_files;
295@new_files = grep {$_ ne $pkg_dir} @new_files;
296s!^[^/]+/!! for @new_files;
297my %new_files = map {$_ => 1} @new_files;
298
ad9b4e6f 299my @old_files = `find $old_dir -type f`;
418f4069
A
300chomp @old_files;
301@old_files = grep {$_ ne $old_dir} @old_files;
302s!^[^/]+/!! for @old_files;
303my %old_files = map {$_ => 1} @old_files;
304
418f4069
A
305my @delete;
306my @commit;
307my @gone;
418f4069
A
308FILE:
309foreach my $file (@new_files) {
310 next if -d "$pkg_dir/$file"; # Ignore directories.
311 next if $old_files {$file}; # It's already there.
312 if ($IGNORABLE {$file}) {
313 push @delete => $file;
314 next;
315 }
418f4069
A
316 push @commit => $file;
317}
318foreach my $file (@old_files) {
319 next if -d "$old_dir/$file";
320 next if $new_files {$file};
321 push @gone => $file;
322}
ad9b4e6f
A
323
324#
325# Find all files with an exec bit
326#
327my @exec = `find $pkg_dir -type f -perm +111`;
418f4069 328chomp @exec;
ad9b4e6f
A
329my @de_exec;
330foreach my $file (@exec) {
331 # Remove leading dir
332 $file =~ s!^[^/]+/!!;
333 if ($file =~ m!^t/!) {
334 push @de_exec => $file;
335 next;
336 }
337 # Check to see if the file exists; if it doesn't and doesn't have
338 # the exec bit, remove it.
339 if ($old_files {$file}) {
340 unless (-x "$old_dir/$file") {
341 push @de_exec => $file;
342 }
343 }
344}
418f4069
A
345
346#
347# No need to change the +x bit on files that will be deleted.
348#
ad9b4e6f 349if (@de_exec && @delete) {
a9f5d1d4 350 my %delete = map {+"$pkg_dir/$_" => 1} @delete;
ad9b4e6f 351 @de_exec = grep {!$delete {$_}} @de_exec;
418f4069
A
352}
353
354say "unlink $pkg_dir/$_" for @delete;
355say "git add $pkg_dir/$_" for @commit;
356say "git rm -f $pkg_dir/$_" for @gone;
ad9b4e6f 357say "chmod a-x $pkg_dir/$_" for @de_exec;
418f4069
A
358
359print "Hit return to continue; ^C to abort "; <STDIN>;
360
361unlink "$pkg_dir/$_" for @delete;
362system git => 'add', "$pkg_dir/$_" for @commit;
363system git => 'rm', '-f', "$pkg_dir/$_" for @gone;
ad9b4e6f 364system chmod => 'a-x', "$pkg_dir/$_" for @de_exec;
418f4069 365
9c259538
A
366#
367# Restore anything that is customized.
368# We don't really care whether we've deleted the file - since we
369# do a git restore, it's going to be resurrected if necessary.
370#
371if ($$info {CUSTOMIZED}) {
372 say "Restoring customized files";
373 foreach my $file (@{$$info {CUSTOMIZED}}) {
374 system git => "checkout", "$pkg_dir/$file";
375 }
376}
377
a8121781 378chdir "..";
418f4069
A
379if (@commit) {
380 say "Fixing MANIFEST";
a8121781 381 my $MANIFEST = "MANIFEST";
418f4069
A
382 my $MANIFEST_SORT = "$MANIFEST.sorted";
383 open my $fh, ">>", $MANIFEST;
a8121781 384 say $fh "cpan/$pkg_dir/$_" for @commit;
418f4069 385 close $fh;
a8121781 386 system perl => "Porting/manisort", '--output', $MANIFEST_SORT;
418f4069
A
387 rename $MANIFEST_SORT => $MANIFEST;
388}
389
390
418f4069
A
391print "Running a make ... ";
392system "make > make.log 2>&1" and die "Running make failed, see make.log";
393print "done\n";
394
395#
396# Must clean up, or else t/porting/FindExt.t will fail.
397# Note that we can always retrieve the orginal directory with a git checkout.
398#
399print "About to clean up; hit return or abort (^C) "; <STDIN>;
400
401chdir "cpan";
402system rm => '-r', $old_dir;
e81fec2c 403unlink $new_file unless $tarball;
418f4069
A
404
405
ad9b4e6f
A
406#
407# Run the tests. First the test belonging to the module, followed by the
408# the tests in t/porting
409#
418f4069 410chdir "../t";
ad9b4e6f
A
411say "Running module tests";
412my @test_files = `find ../cpan/$pkg_dir -name '*.t' -type f`;
413chomp @test_files;
414my $output = `./perl TEST @test_files`;
415unless ($output =~ /All tests successful/) {
416 say $output;
417 exit 1;
418}
419
418f4069
A
420print "Running tests in t/porting ";
421my @tests = `ls porting/*.t`;
422chomp @tests;
423my @failed;
424foreach my $t (@tests) {
425 my @not = `./perl -I../lib -I.. $t | grep ^not | grep -v "# TODO"`;
426 print @not ? '!' : '.';
427 push @failed => $t if @not;
428}
429print "\n";
430say "Failed tests: @failed" if @failed;
431
432
433print "Now you ought to run a make; make test ...\n";
434
435say "Do not forget to update Porting/Maintainers.pl before committing";
436say "$o_module is now version $new_version";
437
438
439__END__