This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
allow overriding version in this branch too
[perl5.git] / Porting / sync-with-cpan
1 #!/usr/bin/env perl
2
3 =head1 NAME
4
5 Porting/sync-with-cpan
6
7 =head1 SYNOPSIS
8
9   perl Porting/sync-with-cpan <module>
10
11 where <module> is the name it appears in the C<%Modules> hash
12 of F<Porting/Maintainers.pl>
13
14 =head1 DESCRIPTION
15
16 Script to help out with syncing cpan distros.
17
18 Does the following:
19
20 =over 4
21
22 =item *
23
24 Fetches the package list from CPAN. Finds the current version of the given
25 package. [1]
26
27 =item *
28
29 Downloads the relevant tarball; unpacks the tarball. [1]
30
31 =item *
32
33 Clean out the old directory (C<git clean -dfx>)
34
35 =item *
36
37 Moves the old directory out of the way, moves the new directory in place.
38
39 =item *
40
41 Restores any F<.gitignore> file.
42
43 =item *
44
45 Removes files from C<@IGNORE> and C<EXCLUDED>
46
47 =item *
48
49 C<git add> any new files.
50
51 =item *
52
53 C<git rm> any files that are gone.
54
55 =item *
56
57 Remove the +x bit on files in F<t/>
58
59 =item *
60
61 Remove the +x bit on files that don't have it enabled in the current dir
62
63 =item *
64
65 Restore files mentioned in C<CUSTOMIZED>
66
67 =item *
68
69 Adds new files to F<MANIFEST>
70
71 =item *
72
73 Runs a C<make> (assumes a configure has been run)
74
75 =item *
76
77 Cleans up
78
79 =item *
80
81 Runs tests for the package
82
83 =item *
84
85 Runs the porting tests
86
87 =back
88
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.
92
93 =head1 TODO
94
95 =over 4
96
97 =item *
98
99 Delete files from F<MANIFEST>
100
101 =item *
102
103 Update F<Porting/Maintainers.pl>
104
105 =item *
106
107 Optional, run a full test suite
108
109 =item *
110
111 Handle complicated C<FILES>
112
113 =back
114
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
118 to be available.
119
120 =cut
121
122
123 package Maintainers;
124
125 use 5.010;
126
127 use strict;
128 use warnings;
129 use Getopt::Long;
130
131 $| = 1;
132
133 die "This does not look like a top level directory"
134      unless -d "cpan" && -d "Porting";
135
136 our @IGNORABLE;
137 our %Modules;
138
139 use autodie;
140
141 require "Porting/Maintainers.pl";
142
143 my %IGNORABLE    = map {$_ => 1} @IGNORABLE;
144
145 my $package      = "02packages.details.txt";
146 my $package_url  = "http://www.cpan.org/modules/$package";
147 my $package_file = "/tmp/$package";
148
149
150 GetOptions ('tarball=s'  =>  \my $tarball,
151             'version=s'  =>  \my $version,
152              force       =>  \my $force,)
153         or  die "Failed to parse arguments";
154
155 die "Usage: $0 module [args] [cpan package]" unless @ARGV == 1 || @ARGV == 2;
156
157 my ($module)  = shift;
158 my  $cpan_mod = @ARGV ? shift : $module;
159
160
161 my  $info         = $Modules {$module} or die "Cannot find module $module";
162 my  $distribution = $$info {DISTRIBUTION};
163
164 my @files         = glob $$info {FILES};
165 if (@files != 1 || !-d $files [0] || $$info {MAP}) {
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
175 chdir "cpan";
176
177 my  $pkg_dir      = $$info {FILES};
178     $pkg_dir      =~ s!.*/!!;
179
180 my ($old_version) = $distribution =~ /-([0-9.]+)\.tar\.gz/;
181
182 my  $o_module     = $module;
183 if ($cpan_mod =~ /-/ && $cpan_mod !~ /::/) {
184     $cpan_mod =~ s/-/::/g;
185 }
186
187 #
188 # Find the information from CPAN.
189 #
190 my $new_file;
191 my $new_version;
192 unless ($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;
204     if (defined $version) {
205         $new_path =~ s/-$new_version\./-$version\./;
206         $new_version = $version;
207     }
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 }
217 else {
218     $new_file     = $tarball;
219     $new_version  = $version // ($new_file =~ /-([0-9._]+)\.tar\.gz/) [0];
220 }
221
222 my  $old_dir      = "$pkg_dir-$old_version";
223 my  $new_dir      = "$pkg_dir-$new_version";
224
225 say "Cleaning out old directory";
226 system git => 'clean', '-dfxq', $pkg_dir;
227
228 say "Unpacking $new_file";
229
230 system tar => 'xfz', $new_file;
231
232 say "Renaming directories";
233 rename $pkg_dir => $old_dir;
234 rename $new_dir => $pkg_dir;
235
236
237 if (-f "$old_dir/.gitignore") {
238     say "Restoring .gitignore";
239     system git => 'checkout', "$pkg_dir/.gitignore";
240 }
241
242 my @new_files = `find $pkg_dir -type f`;
243 chomp @new_files;
244 @new_files = grep {$_ ne $pkg_dir} @new_files;
245 s!^[^/]+/!! for @new_files;
246 my %new_files = map {$_ => 1} @new_files;
247
248 my @old_files = `find $old_dir -type f`;
249 chomp @old_files;
250 @old_files = grep {$_ ne $old_dir} @old_files;
251 s!^[^/]+/!! for @old_files;
252 my %old_files = map {$_ => 1} @old_files;
253
254 #
255 # Find files that can be deleted.
256 #
257 my @EXCLUDED_QR;
258 my %EXCLUDED_QQ;
259 if ($$info {EXCLUDED}) {
260     foreach my $entry (@{$$info {EXCLUDED}}) {
261         if (ref $entry) {push @EXCLUDED_QR => $entry}
262         else            {$EXCLUDED_QQ {$entry} = 1}
263     }
264 }
265
266 my @delete;
267 my @commit;
268 my @gone;
269 FILE:
270 foreach my $file (@new_files) {
271     next if -d "$pkg_dir/$file";   # Ignore directories.
272     next if $old_files {$file};    # It's already there.
273     if ($IGNORABLE {$file}) {
274         push @delete => $file;
275         next;
276     }
277     if ($EXCLUDED_QQ {$file}) {
278         push @delete => $file;
279         next;
280     }
281     foreach my $pattern (@EXCLUDED_QR) {
282         if ($file =~ /$pattern/) {
283             push @delete => $file;
284             next FILE;
285         }
286     }
287     push @commit => $file;
288 }
289 foreach my $file (@old_files) {
290     next if -d "$old_dir/$file";
291     next if $new_files {$file};
292     push @gone => $file;
293 }
294
295 #
296 # Find all files with an exec bit
297 #
298 my @exec = `find $pkg_dir -type f -perm +111`;
299 chomp @exec;
300 my @de_exec;
301 foreach my $file (@exec) {
302     # Remove leading dir
303     $file =~ s!^[^/]+/!!;
304     if ($file =~ m!^t/!) {
305         push @de_exec => $file;
306         next;
307     }
308     # Check to see if the file exists; if it doesn't and doesn't have
309     # the exec bit, remove it.
310     if ($old_files {$file}) {
311         unless (-x "$old_dir/$file") {
312             push @de_exec => $file;
313         }
314     }
315 }
316
317 #
318 # No need to change the +x bit on files that will be deleted.
319 #
320 if (@de_exec && @delete) {
321     my %delete = map {+"$pkg_dir/$_" => 1} @delete;
322     @de_exec = grep {!$delete {$_}} @de_exec;
323 }
324
325 say "unlink $pkg_dir/$_" for @delete;
326 say "git add $pkg_dir/$_" for @commit;
327 say "git rm -f $pkg_dir/$_" for @gone;
328 say "chmod a-x $pkg_dir/$_" for @de_exec;
329
330 print "Hit return to continue; ^C to abort "; <STDIN>;
331
332 unlink "$pkg_dir/$_"                      for @delete;
333 system git   => 'add', "$pkg_dir/$_"      for @commit;
334 system git   => 'rm', '-f', "$pkg_dir/$_" for @gone;
335 system chmod => 'a-x', "$pkg_dir/$_"      for @de_exec;
336
337 #
338 # Restore anything that is customized.
339 # We don't really care whether we've deleted the file - since we
340 # do a git restore, it's going to be resurrected if necessary.
341 #
342 if ($$info {CUSTOMIZED}) {
343     say "Restoring customized files";
344     foreach my $file (@{$$info {CUSTOMIZED}}) {
345         system git => "checkout", "$pkg_dir/$file";
346     }
347 }
348
349 chdir "..";
350 if (@commit) {
351     say "Fixing MANIFEST";
352     my $MANIFEST      = "MANIFEST";
353     my $MANIFEST_SORT = "$MANIFEST.sorted";
354     open my $fh, ">>", $MANIFEST;
355     say $fh "cpan/$pkg_dir/$_" for @commit;
356     close $fh;
357     system perl => "Porting/manisort", '--output', $MANIFEST_SORT;
358     rename $MANIFEST_SORT => $MANIFEST;
359 }
360
361
362 print "Running a make ... ";
363 system "make > make.log 2>&1" and die "Running make failed, see make.log";
364 print "done\n";
365
366 #
367 # Must clean up, or else t/porting/FindExt.t will fail.
368 # Note that we can always retrieve the orginal directory with a git checkout.
369 #
370 print "About to clean up; hit return or abort (^C) "; <STDIN>;
371
372 chdir "cpan";
373 system rm => '-r', $old_dir;
374 unlink $new_file unless $tarball;
375
376
377 #
378 # Run the tests. First the test belonging to the module, followed by the
379 # the tests in t/porting
380 #
381 chdir "../t";
382 say "Running module tests";
383 my @test_files = `find ../cpan/$pkg_dir -name '*.t' -type f`;
384 chomp @test_files;
385 my $output = `./perl TEST @test_files`;
386 unless ($output =~ /All tests successful/) {
387     say $output;
388     exit 1;
389 }
390
391 print "Running tests in t/porting ";
392 my @tests = `ls porting/*.t`;
393 chomp @tests;
394 my @failed;
395 foreach my $t (@tests) {
396     my @not = `./perl -I../lib -I.. $t | grep ^not | grep -v "# TODO"`;
397     print @not ? '!' : '.';
398     push @failed => $t if @not;
399 }
400 print "\n";
401 say "Failed tests: @failed" if @failed;
402
403
404 print "Now you ought to run a make; make test ...\n";
405
406 say "Do not forget to update Porting/Maintainers.pl before committing";
407 say "$o_module is now version $new_version";
408
409
410 __END__