#!/usr/bin/env perl
use
5.010;
$| = 1;
die
"This does not look like a top level directory"
unless
-d
"cpan"
&& -d
"Porting"
;
die
"Please run Configure before using $0\n"
if
!WIN32 && !-f
"Makefile"
;
our
@IGNORABLE
;
our
%Modules
;
require
"./Porting/Maintainers.pl"
;
my
$MAKE_LOG
=
'make.log'
;
my
%IGNORABLE
=
map
{
$_
=> 1}
@IGNORABLE
;
my
$tmpdir
= tmpdir();
my
$package
=
"02packages.details.txt"
;
my
$package_file
=
"$tmpdir/$package"
;
my
@problematic
= (
'podlators'
,
);
sub
usage
{
my
$err
=
shift
and
select
STDERR;
print
"Usage: $0 <module-or-dist> [args]\n"
;
exit
$err
;
}
GetOptions (
'tarball=s'
=> \
my
$tarball
,
'version=s'
=> \
my
$version
,
'jobs=i'
=> \
my
$make_jobs
,
force
=> \
my
$force
,
help
=>
sub
{ usage 0; },
) or
die
"Failed to parse arguments"
;
usage 1
unless
@ARGV
== 1 ||
@ARGV
== 2;
sub
find_type_f {
my
@res
;
find( {
no_chdir
=> 1,
wanted
=>
sub
{
my
$file
=
$File::Find::name
;
return
unless
-f
$file
;
push
@res
,
$file
}},
@_
);
@res
};
sub
de_exec {
my
(
$filename
) =
@_
;
my
$mode
= (
stat
$filename
)[2] & 0777;
if
(
$mode
& 0111) {
chmod
$mode
& 0666,
$filename
;
}
}
sub
make_writable {
my
(
$filename
) =
@_
;
my
$mode
= (
stat
$filename
)[2] & 0777;
if
(!(
$mode
& 0222)) {
chmod
$mode
| (0222 & ~
umask
),
$filename
;
}
}
sub
make {
my
@args
=
@_
;
unshift
@args
,
"-j$make_jobs"
if
defined
$make_jobs
;
if
(WIN32) {
chdir
"Win32"
;
system
"$Config{make} @args> ..\\$MAKE_LOG 2>&1"
and
die
"Running make failed, see $MAKE_LOG"
;
chdir
'..'
;
}
else
{
system
"$Config{make} @args> $MAKE_LOG 2>&1"
and
die
"Running make failed, see $MAKE_LOG"
;
};
};
my
(
$module
) =
shift
;
my
$info
=
$Modules
{
$module
};
if
(!
$info
) {
my
$guess
=
$module
;
s/-/::/g or s/::/-/g
for
$guess
;
$info
=
$Modules
{
$guess
} or
die
<<"EOF";
Cannot find module $module.
The available options are listed in the %Modules hash in Porting/Maintainers.pl
EOF
say
"Guessing you meant $guess instead of $module"
;
$module
=
$guess
;
}
if
(
$info
->{CUSTOMIZED}) {
print
<<"EOF";
$module has a CUSTOMIZED entry in Porting/Maintainers.pl.
This program's behaviour is to copy every CUSTOMIZED file into the version
of the module being imported. But that might not be the right thing: in some
cases, the new CPAN version will supersede whatever changes had previously
been made in blead, so it would be better to import the new CPAN files.
If you've checked that the CUSTOMIZED versions are still correct, you can
proceed now. Otherwise, you should abort and investigate the situation. If
the blead customizations are no longer needed, delete the CUSTOMIZED entry
for $module in Porting/Maintainers.pl (and you'll also need to regenerate
t/porting/customized.dat in that case; see t/porting/customized.t).
EOF
print
"Hit return to continue; ^C to abort "
; <STDIN>;
}
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!"
;
}
my
$orig_pwd
= cwd();
chdir
"cpan"
;
my
$pkg_dir
=
$files
[0];
$pkg_dir
=~ s!.*/!!;
my
(
$old_version
) =
$distribution
=~ /-([0-9.]+(?:-TRIAL[0-9]*)?)\.tar\.gz/;
sub
wget {
my
(
$url
,
$saveas
) =
@_
;
my
$ht_res
;
eval
{
my
$http
= HTTP::Tiny->new();
$ht_res
=
$http
->mirror(
$url
=>
$saveas
);
1;
} or
WIN32 || -x
substr
(`which wget`, 0, -1)
?
system
wget
=>
$url
,
'-qO'
,
$saveas
:
system
curl
=>
$url
,
'-sSo'
,
$saveas
;
if
(
$ht_res
&& !
$ht_res
->{
'success'
} ) {
die
"Cannot retrieve file: $url\n"
.
sprintf
"Status: %s\nReason: %s\nContent: %s\n"
,
map
$_
//
'(unavailable)'
, @{
$ht_res
}{
qw< status reason content >
};
}
}
my
$new_file
;
my
$new_version
;
if
(
defined
$tarball
) {
$tarball
= rel2abs(
$tarball
,
$orig_pwd
) ;
die
"Tarball $tarball does not exist\n"
if
!-e
$tarball
;
die
"Tarball $tarball is not a plain file\n"
if
!-f _;
$new_file
=
$tarball
;
$new_version
=
$version
// (
$new_file
=~ /-([0-9._]+(?:-TRIAL[0-9]*)?)\.tar\.gz/) [0];
die
"Blead and that tarball both have version $new_version of $module\n"
if
$new_version
eq
$old_version
;
}
else
{
unless
(-f
$package_file
&& -M
$package_file
< 1) {
wget
$package_url
,
$package_file
;
}
my
$cpan_mod
=
$info
->{MAIN_MODULE} //
$module
;
open
my
$fh
,
'<'
,
$package_file
;
(
my
$new_line
) =
grep
{/^\Q
$cpan_mod
\E /} <
$fh
>
or
die
"Cannot find $cpan_mod on CPAN\n"
;
(
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];
die
"The latest version of $module is $new_version, but blead already has it\n"
if
$new_version
eq
$old_version
;
say
"Fetching $url"
;
wget
$url
,
$new_file
;
}
my
$old_dir
=
"$pkg_dir-$old_version"
;
say
"Cleaning out old directory"
;
system
git
=>
'clean'
,
'-dfxq'
,
$pkg_dir
;
say
"Unpacking $new_file"
;
Archive::Tar->extract_archive(
$new_file
);
(
my
$new_dir
= basename(
$new_file
)) =~ s/\.tar\.gz//;
my
$t
=
time
;
for
my
$file
(find_type_f(
$new_dir
)) {
make_writable(
$file
);
utime
(
$t
,
$t
,
$file
);
};
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_type_f(
$new_dir
)) {
my
$old_file
=
$file
;
$file
=~ s{^\Q
$new_dir
\E/}{};
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
;
}
remove_tree(
$new_dir
);
if
(-f
"$old_dir/.gitignore"
) {
say
"Restoring .gitignore"
;
system
git
=>
'checkout'
,
"$pkg_dir/.gitignore"
;
}
my
@new_files
= find_type_f(
$pkg_dir
);
@new_files
=
grep
{
$_
ne
$pkg_dir
}
@new_files
;
s!^[^/]+/!!
for
@new_files
;
my
%new_files
=
map
{
$_
=> 1}
@new_files
;
my
@old_files
= find_type_f(
$old_dir
);
@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"
;
next
if
$old_files
{
$file
};
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
;
}
my
@exec
= find_type_f(
$pkg_dir
);
my
@de_exec
;
foreach
my
$file
(
@exec
) {
$file
=~ s!^[^/]+/!!;
if
(
$file
=~ m!^t/!) {
push
@de_exec
=>
$file
;
next
;
}
if
(
$old_files
{
$file
}) {
unless
(-x
"$old_dir/$file"
) {
push
@de_exec
=>
$file
;
}
}
}
if
(
@de_exec
&&
@delete
) {
my
%delete
=
map
{+
"$pkg_dir/$_"
=> 1}
@delete
;
@de_exec
=
grep
{!
$delete
{
$_
}}
@de_exec
;
}
if
(
@de_exec
) {
my
%permitted
=
map
{ (
my
$x
=
$_
) =~
tr
/\n//d;
$x
=> 1 }
grep
!/^
do
{
local
@ARGV
=
'../Porting/exec-bit.txt'
; <> };
@de_exec
=
grep
!
$permitted
{
"cpan/$pkg_dir/$_"
},
@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
;
de_exec(
"$pkg_dir/$_"
)
for
@de_exec
;
if
(
$$info
{CUSTOMIZED}) {
say
"Restoring customized files"
;
foreach
my
$file
(@{
$$info
{CUSTOMIZED}}) {
system
git
=>
"checkout"
,
"$pkg_dir/$file"
;
}
}
chdir
".."
;
if
(
@commit
||
@gone
) {
say
"Fixing MANIFEST"
;
my
$MANIFEST
=
"MANIFEST"
;
my
$MANIFEST_NEW
=
"$MANIFEST.new"
;
open
my
$orig
,
"<"
,
$MANIFEST
or
die
"Failed to open $MANIFEST for reading: $!\n"
;
open
my
$new
,
">"
,
$MANIFEST_NEW
or
die
"Failed to open $MANIFEST_NEW for writing: $!\n"
;
my
%gone
=
map
+(
"cpan/$pkg_dir/$_"
=> 1),
@gone
;
while
(
my
$line
= <
$orig
>) {
my
(
$file
) =
$line
=~ /^(\S+)/
or
die
"Can't parse MANIFEST line: $line"
;
print
$new
$line
if
!
$gone
{
$file
};
}
say
$new
"cpan/$pkg_dir/$_"
for
@commit
;
close
$new
or
die
"Can't close $MANIFEST: $!\n"
;
system
$^
X
=>
"Porting/manisort"
,
'--quiet'
,
"--output=$MANIFEST"
,
$MANIFEST_NEW
;
unlink
$MANIFEST_NEW
or
die
"Can't delete temporary $MANIFEST_NEW: $!\n"
;
}
print
"Running a make and saving its output to $MAKE_LOG ... "
;
make
'test-prep'
;
print
"done\n"
;
if
(
@commit
||
@gone
) {
say
"Running regen/lib_cleanup.pl to handle potential added/deleted dirs"
;
my
$exe_dir
= WIN32 ?
".\\"
:
'./'
;
system
"${exe_dir}perl$Config{_exe}"
,
"-Ilib"
,
"regen/lib_cleanup.pl"
and
die
"regen/lib_cleanup.pl failed\n"
;
}
print
"About to clean up; hit return or abort (^C) "
; <STDIN>;
remove_tree(
"cpan/$old_dir"
);
unlink
"cpan/$new_file"
unless
$tarball
;
chdir
"t"
;
say
"Running module tests"
;
my
@test_files
=
grep
{ /\.t$/ } find_type_f(
"../cpan/$pkg_dir"
);
my
$exe_dir
= WIN32 ?
"..\\"
:
'./'
;
my
$output
= `${exe_dir}perl
$Config
{_exe} TEST
@test_files
`;
unless
(
$output
=~ /All tests successful/) {
say
$output
;
exit
1;
}
print
"Running tests in t/porting "
;
my
@tests
=
glob
'porting/*.t'
;
chomp
@tests
;
my
@failed
;
foreach
my
$t
(
@tests
) {
my
@not
=
grep
{!/
grep
{ /^not/ }
`${exe_dir}perl -I../lib -I..
$t
`;
print
@not
?
'!'
:
'.'
;
push
@failed
=>
$t
if
@not
;
}
print
"\n"
;
say
"Failed tests: @failed"
if
@failed
;
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
$module
/) {
$in_mod_section
= 1;
}
}
print
$new_Maintainers_pl
$_
;
}
if
(
$found
) {
say
"Successfully updated Maintainers.pl"
;
unlink
'Porting/Maintainers.pl'
;
rename
'Maintainers.pl'
=>
'Porting/Maintainers.pl'
;
chmod
0755
=>
'Porting/Maintainers.pl'
;
}
else
{
say
"Could not update Porting/Maintainers.pl."
;
say
"Make sure you update this by hand before committing."
;
}
print
<<"EOF";
=======================================================================
$module is now at version $new_version
Next, you should run "make minitest" and then "make test".
Minitest uses miniperl, which does not support XS modules. The full test
suite uses perl, which does. Minitest can fail - e.g. if a cpan module
has added an XS dependency - even if the full test suite passes just fine.
Hopefully all will complete successfully, but if not, you can make any
changes you need to get the tests to pass. Don't forget that you'll need
a "CUSTOMIZED" entry in Porting/Maintainers.pl if you change any of the
files under cpan/$pkg_dir.
Once all tests pass, you can "git add -u" and "git commit" the changes
with a message along the lines of "Update Foo::Bar to v1.234".
EOF
__END__