This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
ensure 'make' updates all necessary 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};
165if (@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
175chdir "cpan";
176
418f4069
A
177my $pkg_dir = $$info {FILES};
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;
236rename $new_dir => $pkg_dir;
237
238
239if (-f "$old_dir/.gitignore") {
240 say "Restoring .gitignore";
241 system git => 'checkout', "$pkg_dir/.gitignore";
242}
243
ad9b4e6f 244my @new_files = `find $pkg_dir -type f`;
418f4069
A
245chomp @new_files;
246@new_files = grep {$_ ne $pkg_dir} @new_files;
247s!^[^/]+/!! for @new_files;
248my %new_files = map {$_ => 1} @new_files;
249
ad9b4e6f 250my @old_files = `find $old_dir -type f`;
418f4069
A
251chomp @old_files;
252@old_files = grep {$_ ne $old_dir} @old_files;
253s!^[^/]+/!! for @old_files;
254my %old_files = map {$_ => 1} @old_files;
255
256#
257# Find files that can be deleted.
258#
259my @EXCLUDED_QR;
260my %EXCLUDED_QQ;
261if ($$info {EXCLUDED}) {
262 foreach my $entry (@{$$info {EXCLUDED}}) {
263 if (ref $entry) {push @EXCLUDED_QR => $entry}
264 else {$EXCLUDED_QQ {$entry} = 1}
265 }
266}
267
268my @delete;
269my @commit;
270my @gone;
418f4069
A
271FILE:
272foreach my $file (@new_files) {
273 next if -d "$pkg_dir/$file"; # Ignore directories.
274 next if $old_files {$file}; # It's already there.
275 if ($IGNORABLE {$file}) {
276 push @delete => $file;
277 next;
278 }
279 if ($EXCLUDED_QQ {$file}) {
280 push @delete => $file;
281 next;
282 }
283 foreach my $pattern (@EXCLUDED_QR) {
284 if ($file =~ /$pattern/) {
285 push @delete => $file;
286 next FILE;
287 }
288 }
289 push @commit => $file;
290}
291foreach my $file (@old_files) {
292 next if -d "$old_dir/$file";
293 next if $new_files {$file};
294 push @gone => $file;
295}
ad9b4e6f
A
296
297#
298# Find all files with an exec bit
299#
300my @exec = `find $pkg_dir -type f -perm +111`;
418f4069 301chomp @exec;
ad9b4e6f
A
302my @de_exec;
303foreach my $file (@exec) {
304 # Remove leading dir
305 $file =~ s!^[^/]+/!!;
306 if ($file =~ m!^t/!) {
307 push @de_exec => $file;
308 next;
309 }
310 # Check to see if the file exists; if it doesn't and doesn't have
311 # the exec bit, remove it.
312 if ($old_files {$file}) {
313 unless (-x "$old_dir/$file") {
314 push @de_exec => $file;
315 }
316 }
317}
418f4069
A
318
319#
320# No need to change the +x bit on files that will be deleted.
321#
ad9b4e6f 322if (@de_exec && @delete) {
a9f5d1d4 323 my %delete = map {+"$pkg_dir/$_" => 1} @delete;
ad9b4e6f 324 @de_exec = grep {!$delete {$_}} @de_exec;
418f4069
A
325}
326
327say "unlink $pkg_dir/$_" for @delete;
328say "git add $pkg_dir/$_" for @commit;
329say "git rm -f $pkg_dir/$_" for @gone;
ad9b4e6f 330say "chmod a-x $pkg_dir/$_" for @de_exec;
418f4069
A
331
332print "Hit return to continue; ^C to abort "; <STDIN>;
333
334unlink "$pkg_dir/$_" for @delete;
335system git => 'add', "$pkg_dir/$_" for @commit;
336system git => 'rm', '-f', "$pkg_dir/$_" for @gone;
ad9b4e6f 337system chmod => 'a-x', "$pkg_dir/$_" for @de_exec;
418f4069 338
9c259538
A
339#
340# Restore anything that is customized.
341# We don't really care whether we've deleted the file - since we
342# do a git restore, it's going to be resurrected if necessary.
343#
344if ($$info {CUSTOMIZED}) {
345 say "Restoring customized files";
346 foreach my $file (@{$$info {CUSTOMIZED}}) {
347 system git => "checkout", "$pkg_dir/$file";
348 }
349}
350
a8121781 351chdir "..";
418f4069
A
352if (@commit) {
353 say "Fixing MANIFEST";
a8121781 354 my $MANIFEST = "MANIFEST";
418f4069
A
355 my $MANIFEST_SORT = "$MANIFEST.sorted";
356 open my $fh, ">>", $MANIFEST;
a8121781 357 say $fh "cpan/$pkg_dir/$_" for @commit;
418f4069 358 close $fh;
a8121781 359 system perl => "Porting/manisort", '--output', $MANIFEST_SORT;
418f4069
A
360 rename $MANIFEST_SORT => $MANIFEST;
361}
362
363
418f4069
A
364print "Running a make ... ";
365system "make > make.log 2>&1" and die "Running make failed, see make.log";
366print "done\n";
367
368#
369# Must clean up, or else t/porting/FindExt.t will fail.
370# Note that we can always retrieve the orginal directory with a git checkout.
371#
372print "About to clean up; hit return or abort (^C) "; <STDIN>;
373
374chdir "cpan";
375system rm => '-r', $old_dir;
e81fec2c 376unlink $new_file unless $tarball;
418f4069
A
377
378
ad9b4e6f
A
379#
380# Run the tests. First the test belonging to the module, followed by the
381# the tests in t/porting
382#
418f4069 383chdir "../t";
ad9b4e6f
A
384say "Running module tests";
385my @test_files = `find ../cpan/$pkg_dir -name '*.t' -type f`;
386chomp @test_files;
387my $output = `./perl TEST @test_files`;
388unless ($output =~ /All tests successful/) {
389 say $output;
390 exit 1;
391}
392
418f4069
A
393print "Running tests in t/porting ";
394my @tests = `ls porting/*.t`;
395chomp @tests;
396my @failed;
397foreach my $t (@tests) {
398 my @not = `./perl -I../lib -I.. $t | grep ^not | grep -v "# TODO"`;
399 print @not ? '!' : '.';
400 push @failed => $t if @not;
401}
402print "\n";
403say "Failed tests: @failed" if @failed;
404
405
406print "Now you ought to run a make; make test ...\n";
407
408say "Do not forget to update Porting/Maintainers.pl before committing";
409say "$o_module is now version $new_version";
410
411
412__END__