17 use CPAN::Perl::Releases;
24 my $old_fh = select STDOUT;
30 getopts('ft:m:', \%opts);
32 my $cpan_mirror = 'cpan.cpantesters.org';
39 local $SIG{'INT'} = sub { exit 1 };
48 my ($path, $indent, $keep) = @args{qw<path indent keep>};
50 die "Path $path already exists" if -e $path;
51 File::Path::mkpath($path);
55 indent => $indent || 0,
62 eval "sub $_ { \$_[0]->{$_} }; 1" or die $@ for qw<path indent>;
65 sub keep { @_ > 1 ? $_[0]->{keep} = $_[1] : $_[0]->{keep} }
70 return if $self->keep;
72 my $path = $self->path;
73 return unless -e $path;
75 my $indent = $self->indent;
76 $indent = ' ' x (2 * $indent);
78 print "${indent}Cleaning up path $path... ";
79 File::Path::remove_tree($path);
85 my $num_version = shift;
87 my $obj = version->parse($num_version);
88 my $pretty_version = $obj->normal;
89 $pretty_version =~ s/^v?//;
91 my ($int, $frac) = split /\./, $num_version, 2;
93 die 'Wrong fractional part' if length $frac > 6;
94 $frac .= '0' x (6 - length $frac);
96 "$int$frac" => [ $num_version, $pretty_version ];
99 my %perls = map key_version($_),
100 grep "$_" >= '5.010001',
101 keys %Module::CoreList::released;
104 package LWP::MyUserAgent;
107 BEGIN { push @ISA, 'LWP::UserAgent' }
113 my ($class, $cb) = @_;
115 my $ua = bless LWP::UserAgent->new, $class;
117 $ua->show_progress(1) if $cb;
119 my $tag = Scalar::Util::refaddr($ua);
127 my ($ua, $stat, $r) = @_;
129 my $tag = Scalar::Util::refaddr($ua);
134 if ($stat eq 'begin') {
136 } elsif ($stat eq 'end') {
138 } elsif ($stat eq 'tick') {
139 my $tick = ++$ticks{$tag};
140 $desc = qw<- \ | />[$tick % 4];
142 $desc = sprintf "%.01f%%", 100 * $stat;
153 my $tag = Scalar::Util::refaddr($ua);
163 my ($uri, $to, $cb) = @_;
165 my $start = [ Time::HiRes::gettimeofday ];
167 my $ua = LWP::MyUserAgent->new($cb);
168 my $res = $ua->get($uri);
169 die "Could not fetch $uri: " . $res->status_line unless $res->is_success;
172 my $file = (File::Spec::Unix->splitpath(URI->new($uri)->path))[2];
173 my $vol = (File::Spec->splitpath($here))[0];
174 $to = File::Spec->catdir($here, $to)
175 unless File::Spec->file_name_is_absolute($to);
176 $file = File::Spec->catpath($vol, $to, $file);
178 open my $fh, '>', $file or die "Can't open $file for writing: $!";
179 print $fh $res->content;
180 close $fh or die "Can't close $file: $!";
182 my $elapsed = Time::HiRes::tv_interval($start);
184 return $file, $elapsed;
187 sub perl_archive_for {
190 my $tarballs = CPAN::Perl::Releases::perl_tarballs($version);
191 my $path = $tarballs->{'tar.gz'};
192 die "Could not find the archive for perl $version" unless defined $path;
194 my ($file) = ($path =~ m{([^/]*)$});
196 return "http://$cpan_mirror/authors/id/$path", $file;
200 my ($size, $seconds) = @_;
202 my $speed = $size / $seconds;
205 while ($speed >= 1024) {
210 $speed = sprintf '%.02f', $speed;
212 my $unit = ('', 'K', 'M', 'G', 'T', 'P')[$order] . 'B/s';
214 return $speed, $unit;
220 open my $fh, '>', $file or die "Can't open $file for writing: $!";
223 File::Path::mkpath($target) unless -e $target;
225 my $tmp_dir = File::Spec->catdir($target, 'tmp');
227 sub fetch_source_file {
228 my ($file, $version, $dir) = @_;
230 my $INDENT = ' ' x 4;
232 print "${INDENT}Looking for the full name of the perl archive... ";
233 my ($archive_uri, $archive_file) = perl_archive_for($version);
234 print "$archive_uri\n";
236 if (-e File::Spec->catfile($tmp_dir, $archive_file)) {
237 print "${INDENT}$archive_file was already fetched\n";
239 print "${INDENT}Fetching $archive_uri...\n";
242 my ($stat, $desc) = @_;
243 $desc = '0%' if $stat eq 'begin';
244 my $len = length $desc;
246 if ($len > $maxlen) {
249 $extra = ' ' x ($maxlen - $len);
251 print "\r${INDENT} In progress... $desc$extra";
252 print "$extra\n" if $stat eq 'end';
254 ($archive_file, my $elapsed) = fetch_uri($archive_uri => $tmp_dir, $cb);
255 my ($speed, $unit) = bandwidth(-s $archive_file, $elapsed);
256 print "${INDENT} File downloaded at $speed$unit\n";
259 my $extract_path = File::Spec->catfile($tmp_dir, "perl-$version");
260 if (-e $extract_path) {
261 print "${INDENT}$archive_file was already extracted\n";
263 print "${INDENT}Extracting $archive_file... ";
264 my $ae = Archive::Extract->new(archive => $archive_file);
265 $ae->extract(to => $tmp_dir)
266 or die "Could not extract $archive_file: " . $ae->error;
267 $extract_path = $ae->extract_path;
271 File::Path::mkpath($dir) unless -e $dir;
272 print "${INDENT}Copying $file to $dir... ";
273 my $src = File::Spec->catfile($extract_path, $file);
274 my $dst = File::Spec->catfile($dir, $file);
276 File::Copy::copy($src => $dst) or die "Can't copy $src to $dst: $!";
281 print "not needed\n";
287 my %expected_chunks = (
292 ('COMP_NODE_HOOK') x 3,
301 my ($line, $file) = @_;
303 if ($line =~ /#\s*include\s+"INTERN\.h"/) {
304 push @{$patched_chunks{$file}}, 're_defs';
305 return "#include \"re_defs.h\"\n";
306 } elsif ($line =~ /^(\s*)RExC_rxi\s*=\s*ri\s*;\s*$/) {
307 push @{$patched_chunks{$file}}, 'COMP_BEGIN_HOOK';
308 return $line, "$1REH_CALL_COMP_BEGIN_HOOK(pRExC_state->rx);\n";
309 } elsif ($line =~ /FILL_ADVANCE_NODE(_ARG)?\(\s*([^\s,\)]+)/) {
310 my $shift = $1 ? 2 : 1;
311 push @{$patched_chunks{$file}}, 'COMP_NODE_HOOK';
312 return $line, " REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, ($2) - $shift);\n"
313 } elsif ($line =~ /end node insert/) {
314 push @{$patched_chunks{$file}}, 'COMP_NODE_HOOK';
315 return $line, " REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, convert);\n";
316 } elsif ($line =~ /&PL_core_reg_engine/) {
317 $line =~ s/&PL_core_reg_engine\b/&reh_regexp_engine/g;
325 my ($line, $file) = @_;
327 if ($line =~ /#\s*include\s+"perl\.h"/) {
328 push @{$patched_chunks{$file}}, 're_defs';
329 return $line, "#include \"re_defs.h\"\n";
330 } elsif ($line =~ /^\s*reenter_switch:\s*$/) {
331 push @{$patched_chunks{$file}}, 'EXEC_NODE_HOOK';
332 return "\tREH_CALL_EXEC_NODE_HOOK(rex, scan, reginfo, st);\n", $line;
339 'regcomp.c' => \&patch_regcomp,
340 'regexec.c' => \&patch_regexec,
343 sub patch_source_file {
344 my ($src, $dst) = @_;
346 my $file = (File::Spec->splitpath($src))[2];
348 $dst = File::Spec->catfile($dst, $file);
351 my $mangler = $manglers{$file};
353 File::Copy::copy($src => $dst) or die "Can't copy $src to $dst: $!";
357 open my $in, '<', $src or die "Can't open $src for reading: $!";
358 open my $out, '>', $dst or die "Can't open $dst for writing: $!";
360 while (defined(my $line = <$in>)) {
361 print $out $mangler->($line, $dst);
364 my $patched_chunks = join ' ', @{$patched_chunks{$dst}};
365 my $expected_chunks = join ' ', @{$expected_chunks{$file}};
366 unless ($patched_chunks eq $expected_chunks) {
367 die "File $dst was not properly patched (got \"$patched_chunks\", expected \"$expected_chunks\")\n";
373 for my $tag (sort { $a <=> $b } keys %perls) {
374 my ($num_version, $pretty_version) = @{$perls{$tag}};
376 my $dir = File::Spec->catdir($target, $tag);
378 print "Working on perl $pretty_version\n";
380 my $tmp_guard = Guard::Path->new(path => $tmp_dir);
382 my $orig_dir = File::Spec->catdir($dir, 'orig');
384 my @files = qw<regcomp.c regexec.c>;
385 push @files, 'dquote_static.c' if $num_version >= 5.013_006;
386 push @files, 'inline_invlist.c' if $num_version >= 5.017_004;
387 for my $file (@files) {
388 my $orig_file = File::Spec->catfile($orig_dir, $file);
390 print " Already have original $file\n";
392 print " Need to get original $file\n";
393 fetch_source_file($file, $pretty_version => $orig_dir);
397 if (not $opts{f} and -e File::Spec->catfile($dir, $file)) {
398 print " Already have patched $file\n";
400 print " Need to patch $file... ";
401 my $res = patch_source_file($orig_file => $dir);
402 print $res ? "done\n" : "nothing to do\n";
409 print 'Updating MANIFEST... ';
413 open my $in, '<', 'MANIFEST' or die "Can't open MANIFEST for reading: $!";
414 @manifest_files = grep !m{^src/.*\.c$}, <$in>;
417 my @source_files = map "$_\n", glob 'src/*/*.c';
419 open my $out, '>', 'MANIFEST' or die "Can't open MANIFEST for writing: $!";
420 print $out sort @manifest_files, @source_files;