summaryrefslogtreecommitdiff
path: root/Porting/sync-with-cpan
blob: 3a1cdcb052a929d72629c072a0326d871caa9382 (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
#!/usr/bin/perl

#
# Script to help out with syncing cpan distros.
#
# Does the following:
#    - Fetches the package list from CPAN. Finds the current version of
#      the given package.
#    - Downloads the relevant tarball; unpacks the tarball;.
#    - Clean out the old directory (git clean -dfx)
#    - Moves the old directory out of the way, moves the new directory in place.
#    - Restores any .gitignore file.
#    - Removes files from @IGNORE and EXCLUDED
#    - git add any new files.
#    - git rm any files that are gone.
#    - Remove the +x bit on files in t/
#    - Remove the +x bit on files that don't have in enabled in the current dir
#    - Adds new files to MANIFEST
#    - Runs a "make" (assumes a configure has been run)
#    - Cleans up
#    - Runs tests for the package
#    - Runs the porting tests
#
# TODO:  - Restore files from CUSTOMIZED
#        - Delete files from MANIFEST
#        - Update Porting/Maintainers.pl
#        - Optional, run a full test suite
#        - Handle complicated FILES
#
# 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.
#
# Usage: perl Porting/sync-with-cpan <module>
#        where <module> is the name it appears in the %Modules hash
#        of Porting/Maintainers.pl
#

use 5.010;

use strict;
use warnings;
no  warnings 'syntax';

$| = 1;

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

package Maintainers;

our @IGNORABLE;
our %Modules;

use autodie;

require "Porting/Maintainers.pl";

chdir "cpan";

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

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

#
# Poor man's cache
#
unless (-f $package_file && -M $package_file < 1) {
    system wget => $package_url, '-qO', $package_file;
}

die "Usage: $0 module" unless @ARGV == 1;

my ($module) = @ARGV;

my  $info         = $Modules {$module} or die "Cannot find module $module";
my  $distribution = $$info {DISTRIBUTION};
my  $pkg_dir      = $$info {FILES};
    $pkg_dir      =~ s!.*/!!;

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

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

#
# Find the information from CPAN.
#
my  $new_line = `grep '^$module ' $package_file`
                 or die "Cannot find $module on CPAN\n";
chomp $new_line;
my (undef, $new_version, $new_path) = split ' ', $new_line;
my $new_file = (split '/', $new_path) [-1];

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

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



my $url = "http://search.cpan.org/CPAN/authors/id/$new_path";

say "Fetching $url";

#
# Fetch the new distro
#
system wget => $url, '-qO', $new_file;

say "Unpacking $new_file";

system tar => 'xfz', $new_file;

say "Renaming directories";
rename $pkg_dir => $old_dir;
rename $new_dir => $pkg_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;

#
# Find files that can be deleted.
#
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}
    }
}

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;
    }
    if ($EXCLUDED_QQ {$file}) {
        push @delete => $file;
        next;
    }
    foreach my $pattern (@EXCLUDED_QR) {
        if ($file =~ /$pattern/) {
            push @delete => $file;
            next FILE;
        }
    }
    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;

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;


#
# 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;


print "Now you ought to run a make; make test ...\n";

say "Do not forget to update Porting/Maintainers.pl before committing";
say "$o_module is now version $new_version";


__END__