summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/perl/Porting/sync-with-cpan
blob: b6ee246407881ffff960f72568be468d072a467f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/usr/bin/env perl

=head1 NAME

Porting/sync-with-cpan - Synchronize with CPAN distributions

=head1 SYNOPSIS

  perl Porting/sync-with-cpan <module>

where <module> is the name it appears in the C<%Modules> hash
of F<Porting/Maintainers.pl>

=head1 DESCRIPTION

Script to help out with syncing cpan distros.

Does the following:

=over 4

=item *

Fetches the package list from CPAN. Finds the current version of the given
package. [1]

=item *

Downloads the relevant tarball; unpacks the tarball. [1]

=item *

Clean out the old directory (C<git clean -dfx>)

=item *

Moves the old directory out of the way, moves the new directory in place.

=item *

Restores any F<.gitignore> file.

=item *

Removes files from C<@IGNORE> and C<EXCLUDED>

=item *

C<git add> any new files.

=item *

C<git rm> any files that are gone.

=item *

Remove the +x bit on files in F<t/>

=item *

Remove the +x bit on files that don't have it enabled in the current dir

=item *

Restore files mentioned in C<CUSTOMIZED>

=item *

Adds new files to F<MANIFEST>

=item *

Runs a C<make> (assumes a configure has been run)

=item *

Cleans up

=item *

Runs tests for the package

=item *

Runs the porting tests

=back

[1]  If the C<--tarball> option is given, then CPAN is not consulted.
C<--tarball> should be the path to the tarball; the version is extracted
from the filename -- but can be overwritten by the C<--version> option.

=head1 TODO

=over 4

=item *

Delete files from F<MANIFEST>

=item *

Update F<Porting/Maintainers.pl>

=item *

Optional, run a full test suite

=item *

Handle complicated C<FILES>

=back

This is an initial version; no attempt has been made yet to make this
portable. It shells out instead of trying to find a Perl solution.
In particular, it assumes wget, git, tar, chmod, perl, make, and rm
to be available.

=cut


package Maintainers;

use 5.010;

use strict;
use warnings;
use Getopt::Long;

$| = 1;

die "This does not look like a top level directory"
     unless -d "cpan" && -d "Porting";

our @IGNORABLE;
our %Modules;

use autodie;

require "Porting/Maintainers.pl";

my %IGNORABLE    = map {$_ => 1} @IGNORABLE;

my $package      = "02packages.details.txt";
my $package_url  = "http://www.cpan.org/modules/$package";
my $package_file = "/tmp/$package";

my @problematic = (
    'podlators', # weird CUSTOMIZED section due to .PL files
);


GetOptions ('tarball=s'  =>  \my $tarball,
            'version=s'  =>  \my $version,
             force       =>  \my $force,)
        or  die "Failed to parse arguments";

die "Usage: $0 module [args] [cpan package]" unless @ARGV == 1 || @ARGV == 2;

my ($module)  = shift;
my  $cpan_mod = @ARGV ? shift : $module;


my  $info         = $Modules {$module} or die "Cannot find module $module";
my  $distribution = $$info {DISTRIBUTION};

my @files         = glob $$info {FILES};
if (!-d $files [0] || grep { $_ eq $module } @problematic) {
    say "This looks like a setup $0 cannot handle (yet)";
    unless ($force) {
        say "Will not continue without a --force option";
        exit 1;
    }
    say "--force is in effect, so we'll soldier on. Wish me luck!";
}


chdir "cpan";

my  $pkg_dir      = $files[0];
    $pkg_dir      =~ s!.*/!!;

my ($old_version) = $distribution =~ /-([0-9.]+)\.tar\.gz/;

my  $o_module     = $module;
if ($cpan_mod =~ /-/ && $cpan_mod !~ /::/) {
    $cpan_mod =~ s/-/::/g;
}

#
# Find the information from CPAN.
#
my $new_file;
my $new_version;
unless ($tarball) {
    #
    # Poor man's cache
    #
    unless (-f $package_file && -M $package_file < 1) {
        system wget => $package_url, '-qO', $package_file;
    }

    my  $new_line = `grep '^$cpan_mod ' $package_file`
                     or die "Cannot find $cpan_mod on CPAN\n";
    chomp $new_line;
    (undef, $new_version, my $new_path) = split ' ', $new_line;
    if (defined $version) {
        $new_path =~ s/-$new_version\./-$version\./;
        $new_version = $version;
    }
    $new_file = (split '/', $new_path) [-1];

    my $url = "http://search.cpan.org/CPAN/authors/id/$new_path";
    say "Fetching $url";
    #
    # Fetch the new distro
    #
    system wget => $url, '-qO', $new_file;
}
else {
    $new_file     = $tarball;
    $new_version  = $version // ($new_file =~ /-([0-9._]+)\.tar\.gz/) [0];
}

my  $old_dir      = "$pkg_dir-$old_version";

say "Cleaning out old directory";
system git => 'clean', '-dfxq', $pkg_dir;

say "Unpacking $new_file";

system tar => 'xfz', $new_file;
(my $new_dir = $new_file) =~ s/\.tar\.gz//;
# ensure 'make' will update all files
system('find', $new_dir, '-exec', 'touch', '{}', ';');

say "Renaming directories";
rename $pkg_dir => $old_dir;

say "Creating new package directory";
mkdir $pkg_dir;

say "Populating new package directory";
my $map = $$info {MAP};
my @EXCLUDED_QR;
my %EXCLUDED_QQ;
if ($$info {EXCLUDED}) {
    foreach my $entry (@{$$info {EXCLUDED}}) {
        if (ref $entry) {push @EXCLUDED_QR => $entry}
        else            {$EXCLUDED_QQ {$entry} = 1}
    }
}

FILE: for my $file ( `find $new_dir -type f` ) {
    chomp $file;
    my $old_file = $file;
    $file =~ s{^$new_dir/}{};

    next if $EXCLUDED_QQ{$file};
    for my $qr (@EXCLUDED_QR) {
        next FILE if $file =~ $qr;
    }

    if ( $map ) {
        for my $key ( sort { length $b <=> length $a } keys %$map ) {
            my $val = $map->{$key};
            last if $file =~ s/^$key/$val/;
        }
    }
    else {
        $file = $files[0] . '/' . $file;
    }

    if ( $file =~ m{^cpan/} ) {
        $file =~ s{^cpan/}{};
    }
    else {
        $file = '../' . $file;
    }

    my $prefix = '';
    my @parts = split '/', $file;
    pop @parts;
    for my $part (@parts) {
        $prefix .= '/' if $prefix;
        $prefix .= $part;
        mkdir $prefix unless -d $prefix;
    }

    rename $old_file => $file;
}
system 'rm', '-rf', $new_dir;

if (-f "$old_dir/.gitignore") {
    say "Restoring .gitignore";
    system git => 'checkout', "$pkg_dir/.gitignore";
}

my @new_files = `find $pkg_dir -type f`;
chomp @new_files;
@new_files = grep {$_ ne $pkg_dir} @new_files;
s!^[^/]+/!! for @new_files;
my %new_files = map {$_ => 1} @new_files;

my @old_files = `find $old_dir -type f`;
chomp @old_files;
@old_files = grep {$_ ne $old_dir} @old_files;
s!^[^/]+/!! for @old_files;
my %old_files = map {$_ => 1} @old_files;

my @delete;
my @commit;
my @gone;
FILE:
foreach my $file (@new_files) {
    next if -d "$pkg_dir/$file";   # Ignore directories.
    next if $old_files {$file};    # It's already there.
    if ($IGNORABLE {$file}) {
        push @delete => $file;
        next;
    }
    push @commit => $file;
}
foreach my $file (@old_files) {
    next if -d "$old_dir/$file";
    next if $new_files {$file};
    push @gone => $file;
}

#
# Find all files with an exec bit
#
my @exec = `find $pkg_dir -type f -perm +111`;
chomp @exec;
my @de_exec;
foreach my $file (@exec) {
    # Remove leading dir
    $file =~ s!^[^/]+/!!;
    if ($file =~ m!^t/!) {
        push @de_exec => $file;
        next;
    }
    # Check to see if the file exists; if it doesn't and doesn't have
    # the exec bit, remove it.
    if ($old_files {$file}) {
        unless (-x "$old_dir/$file") {
            push @de_exec => $file;
        }
    }
}

#
# No need to change the +x bit on files that will be deleted.
#
if (@de_exec && @delete) {
    my %delete = map {+"$pkg_dir/$_" => 1} @delete;
    @de_exec = grep {!$delete {$_}} @de_exec;
}

say "unlink $pkg_dir/$_" for @delete;
say "git add $pkg_dir/$_" for @commit;
say "git rm -f $pkg_dir/$_" for @gone;
say "chmod a-x $pkg_dir/$_" for @de_exec;

print "Hit return to continue; ^C to abort "; <STDIN>;

unlink "$pkg_dir/$_"                      for @delete;
system git   => 'add', "$pkg_dir/$_"      for @commit;
system git   => 'rm', '-f', "$pkg_dir/$_" for @gone;
system chmod => 'a-x', "$pkg_dir/$_"      for @de_exec;

#
# Restore anything that is customized.
# We don't really care whether we've deleted the file - since we
# do a git restore, it's going to be resurrected if necessary.
#
if ($$info {CUSTOMIZED}) {
    say "Restoring customized files";
    foreach my $file (@{$$info {CUSTOMIZED}}) {
        system git => "checkout", "$pkg_dir/$file";
    }
}

chdir "..";
if (@commit) {
    say "Fixing MANIFEST";
    my $MANIFEST      = "MANIFEST";
    my $MANIFEST_SORT = "$MANIFEST.sorted";
    open my $fh, ">>", $MANIFEST;
    say $fh "cpan/$pkg_dir/$_" for @commit;
    close $fh;
    system perl => "Porting/manisort", '--output', $MANIFEST_SORT;
    rename $MANIFEST_SORT => $MANIFEST;
}


print "Running a make ... ";
system "make > make.log 2>&1" and die "Running make failed, see make.log";
print "done\n";

#
# Must clean up, or else t/porting/FindExt.t will fail.
# Note that we can always retrieve the orginal directory with a git checkout.
#
print "About to clean up; hit return or abort (^C) "; <STDIN>;

chdir "cpan";
system rm => '-r', $old_dir;
unlink $new_file unless $tarball;


#
# Run the tests. First the test belonging to the module, followed by the
# the tests in t/porting
#
chdir "../t";
say "Running module tests";
my @test_files = `find ../cpan/$pkg_dir -name '*.t' -type f`;
chomp @test_files;
my $output = `./perl TEST @test_files`;
unless ($output =~ /All tests successful/) {
    say $output;
    exit 1;
}

print "Running tests in t/porting ";
my @tests = `ls porting/*.t`;
chomp @tests;
my @failed;
foreach my $t (@tests) {
    my @not = `./perl -I../lib -I.. $t | grep ^not | grep -v "# TODO"`;
    print @not ? '!' : '.';
    push @failed => $t if @not;
}
print "\n";
say "Failed tests: @failed" if @failed;


say "Attempting to update Maintainers.pl";
chdir '..';

open my $Maintainers_pl, '<', 'Porting/Maintainers.pl';
open my $new_Maintainers_pl, '>', 'Maintainers.pl';

my $found;
my $in_mod_section;
while (<$Maintainers_pl>) {
    if (!$found) {
        if ($in_mod_section) {
            if (/DISTRIBUTION/) {
                if (s/\Q$old_version/$new_version/) {
                    $found = 1;
                }
            }

            if (/^    }/) {
                $in_mod_section = 0;
            }
        }

        if (/\Q$cpan_mod/) {
            $in_mod_section = 1;
        }
    }

    print $new_Maintainers_pl $_;
}

if ($found) {
    unlink 'Porting/Maintainers.pl';
    rename 'Maintainers.pl' => 'Porting/Maintainers.pl';
    system chmod => 'a+x', 'Porting/Maintainers.pl';
}
else {
    say "Could not update Porting/Maintainers.pl.";
    say "Make sure you update this by hand before committing.";
}

say "$o_module is now version $new_version";
say "Now you ought to run a make; make test ...";


__END__