use strict;
use warnings;
-use Cwd qw/abs_path/;
-use List::Util qw/reduce/;
+use Cwd ();
+use List::Util qw<reduce>;
use File::Copy ();
use File::Path ();
use File::Spec;
-use IPC::Cmd qw/run can_run/;
+use IPC::Cmd ();
use Parse::CPAN::Meta ();
use CPANPLUS::Error ();
-use base qw/CPANPLUS::Dist::Base/;
+use base qw<CPANPLUS::Dist::Base>;
+use CPANPLUS::Dist::Gentoo::Atom;
+use CPANPLUS::Dist::Gentoo::Guard;
use CPANPLUS::Dist::Gentoo::Maps;
=head1 NAME
=head1 VERSION
-Version 0.08
+Version 0.11
=cut
-our $VERSION = '0.08';
+our $VERSION = '0.11';
=head1 SYNOPSIS
+ # Using default values from your make.conf
+ cpan2dist --format=CPANPLUS::Dist::Gentoo --buildprereq Some::Module
+
+ # Specifying your own options
cpan2dist --format=CPANPLUS::Dist::Gentoo \
--dist-opts overlay=/usr/local/portage \
--dist-opts distdir=/usr/portage/distfiles \
--dist-opts manifest=yes \
--dist-opts keywords=x86 \
- --dist-opts header="# Copyright 1999-2008 Gentoo Foundation" \
+ --dist-opts header="# Begin" \
--dist-opts footer="# End" \
Any::Module You::Like
=head1 DESCRPITON
-This module is a CPANPLUS backend that recursively generates Gentoo ebuilds for a given package in the specified overlay (defaults to F</usr/local/portage>), updates the manifest, and even emerges it (together with its dependencies) if the user requires it.
-You need write permissions on the directory where Gentoo fetches its source files (usually F</usr/portage/distfiles>).
-The valid C<KEYWORDS> for the generated ebuilds are by default those given in C<ACCEPT_KEYWORDS>, but you can specify your own with the C<keywords> dist-option.
+This module is a CPANPLUS backend that recursively generates Gentoo ebuilds for a given package in the default overlay, updates the manifest, and even emerges it (together with its dependencies) if the user requires it.
The generated ebuilds are placed into the C<perl-gcpanp> category.
They favour depending on a C<virtual>, on C<perl-core>, C<dev-perl> or C<perl-gcpan> (in that order) rather than C<perl-gcpanp>.
-=head1 INSTALLATION
+=head1 OPTIONS
-Before installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
+You can pass specific options to L<cpan2dist> by using the C<--dist-opts> command-line argument followed by a C<key=value> pair, where C<key> is the option name and C<value> is what it is set to.
+C<--dist-opts> can be used several times.
-You have two ways for installing this module :
+The valid option C<key>s are :
=over 4
=item *
-Use the perl overlay located at L<http://git.overlays.gentoo.org/gitweb/?p=proj/perl-overlay.git>.
-It contains an ebuild for L<CPANPLUS::Dist::Gentoo>.
+C<manifest>
+
+A boolean that indicates whether the F<Manifest> file should be generated by running C<ebuild manifest> onto the generated ebuilds.
+
+Defaults to C<yes>.
=item *
-Bootstrap an ebuild for L<CPANPLUS::Dist::Gentoo> using itself.
-Note that if your Gentoo system C<perl> is C<5.8.x>, L<CPANPLUS> and its dependencies are not installed and not even available in the main portage tree.
-So you need to bootstrap them as well.
+C<overlay>
-First, fetch tarballs for L<CPANPLUS> and L<CPANPLUS::Dist::Gentoo> :
+A string formatted as a space-delimited sequence of paths, that lists the different overlays in which existent ebuilds will be looked for.
- $ cd /tmp
- $ wget http://search.cpan.org/CPAN/authors/id/K/KA/KANE/CPANPLUS-0.88.tar.gz
- $ wget http://search.cpan.org/CPAN/authors/id/V/VP/VPIT/CPANPLUS-Dist-Gentoo-0.08.tar.gz
+Defaults to the value of C<PORTDIR_OVERLAY> as returned by C<emerge --info> (usually F</usr/local/portage>).
+
+=item *
-Log in as root and unpack them in e.g. your home directory :
+C<distdir>
- # cd
- # tar xzf /tmp/CPANPLUS-0.88.tar.gz
- # tar xzf /tmp/CPANPLUS-Dist-Gentoo-0.08.tar.gz
+The directory where C<ebuild> expects to find the source tarballs.
+You need write permissions on this directory.
+
+Defaults to the value of C<DISTDIR> as returned by C<emerge --info> (usually F</usr/portage/distfiles>).
+
+=item *
+
+C<keywords>
+
+The valid C<KEYWORDS> for the generated ebuilds.
+
+Defaults to the value of C<ACCEPT_KEYWORDS> as returned by C<emerge --info>.
+
+=item *
+
+C<header>
+
+A chunk of text that is prepended to every ebuild.
+
+Defaults to the generic Gentoo Foundation header.
+
+=item *
+
+C<footer>
+
+A chunk of text that is appended to every ebuild.
+
+Defaults to nothing.
+
+=back
+
+L<cpan2dist> itself takes other options, most notably :
+
+=over 4
+
+=item *
+
+C<--buildprereq> generates an ebuild for every dependency, even for those that are already up-to-date.
+Setting this option is recommended.
+
+=item *
+
+C<--force> forcefully regenerates ebuilds even if they already exist.
-Set up environment variables so that the toolchain is temporarily available :
+=item *
+
+C<--install> installs the ebuilds after generating them.
+
+=item *
+
+C<--skiptest> skips tests while building, which speeds up the building process.
+
+=item *
+
+C<--verbose> shows a lot more information.
+
+=back
- # export OLDPATH=$PATH
- # export PATH=/root/CPANPLUS-Dist-Gentoo-0.08/bin:$PATH
- # export PERL5LIB=/root/CPANPLUS-Dist-Gentoo-0.08/blib/lib:/root/CPANPLUS-0.88/lib:/root/CPANPLUS-0.88/inc/bundle
+Please refer to L<cpan2dist> documentation for a complete coverage of its abilities.
-Make sure you don't have an old C<.cpanplus> configuration visible :
+=head1 INSTALLATION
- # [ -d /root/.cpanplus ] && mv /root/.cpanplus{,.bak}
+Before installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
-Bootstrap L<CPANPLUS> :
+You have two ways for installing this module :
+
+=over 4
- # cd /root/CPANPLUS-Dist-Gentoo-0.08
- # samples/g-cpanp CPANPLUS
+=item *
-Reset the environment :
+Use the perl overlay located at L<http://git.overlays.gentoo.org/gitweb/?p=proj/perl-overlay.git>.
+It contains an ebuild for L<CPANPLUS::Dist::Gentoo> which will most likely be up-to-date given the reactivity of Gentoo's Perl herd.
- # export PATH=$OLDPATH
- # unset PERL5LIB OLDPATH
+=item *
-Emerge L<CPANPLUS> with the ebuilds you've just generated :
+Bootstrap an ebuild for L<CPANPLUS::Dist::Gentoo> using itself.
- # emerge -tv CPANPLUS
+First, make sure your system C<perl> is C<5.10> or greater, so that the L<CPANPLUS> toolchain is available.
-As of september 2009, C<podlators> and C<ExtUtils-MakeMaker> may fail to emerge due to collisions.
-You can work around this by disabling the C<protect-owned> C<FEATURE> for them :
+ $ perl -v
+ This is perl 5, version 12, subversion 2 (v5.12.2)...
- # FEATURES="-protect-owned" emerge podlators
- # FEATURES="-protect-owned" emerge ExtUtils-MakeMaker
+C<perl> C<5.12> is the current stable Perl version in Gentoo.
+If you still have C<perl> C<5.8.x>, you can upgrade it by running the following commands as root :
-You may need to run each of these commands two times for them to succeed.
+ # emerge -tv ">=dev-lang/perl-5.10"
+ # perl-cleaner --all
-At this point, you can bootstrap L<CPANPLUS::Dist::Gentoo> using the system L<CPANPLUS> :
+Then, fetch the L<CPANPLUS::Dist::Gentoo> tarball :
+
+ $ cd /tmp
+ $ wget http://search.cpan.org/CPAN/authors/id/V/VP/VPIT/CPANPLUS-Dist-Gentoo-0.11.tar.gz
+
+Log in as root and unpack it in e.g. your home directory :
+
+ # cd
+ # tar xzf /tmp/CPANPLUS-Dist-Gentoo-0.11.tar.gz
+ # cd CPANPLUS-Dist-Gentoo-0.11
+
+Bootstrap L<CPANPLUS::Dist::Gentoo> using the bundled shell script C<g-cpanp> :
+
+ # perl Makefile.PL
+ # make
+ # PERL5LIB=blib/lib samples/g-cpanp CPANPLUS::Dist::Gentoo
+
+Finally, emerge the C<CPANPLUS-Dist-Gentoo> ebuild you've just generated :
- # PERL5LIB=/root/CPANPLUS-Dist-Gentoo-0.08/blib/lib samples/g-cpanp CPANPLUS::Dist::Gentoo
# emerge -tv CPANPLUS-Dist-Gentoo
=back
my $default_distdir;
my $main_portdir;
+my %dependencies;
my %forced;
my $unquote = sub {
sub format_available {
return $format_available if defined $format_available;
- for my $prog (qw/emerge ebuild/) {
- unless (can_run($prog)) {
+ unless (IPC::Cmd->can_capture_buffer) {
+ my $msg = 'IPC::Cmd must be able to capture buffers.';
+ unless (do { local $@; eval { require IPC::Run; 1 } }) {
+ $msg .= ' Try installing IPC::Run (dev-perl/IPC-Run on Gentoo).';
+ }
+ __PACKAGE__->_abort($msg);
+ return $format_available = 0;
+ }
+
+ for my $prog (qw<emerge ebuild>) {
+ unless (IPC::Cmd::can_run($prog)) {
__PACKAGE__->_abort("$prog is required to write ebuilds");
return $format_available = 0;
}
}
- if (IPC::Cmd->can_capture_buffer) {
+ {
my $buffers;
- my ($success, $errmsg) = run command => [ qw/emerge --info/ ],
- verbose => 0,
- buffer => \$buffers;
+ my ($success, $errmsg) = IPC::Cmd::run(
+ command => [ qw<emerge --info> ],
+ verbose => 0,
+ buffer => \$buffers,
+ );
if ($success) {
if ($buffers =~ /^PORTDIR_OVERLAY=(.*)$/m) {
- $overlays = [ map abs_path($_), split ' ', $unquote->($1) ];
+ $overlays = [ map Cwd::abs_path($_), split ' ', $unquote->($1) ];
}
if ($buffers =~ /^ACCEPT_KEYWORDS=(.*)$/m) {
$default_keywords = [ split ' ', $unquote->($1) ];
}
if ($buffers =~ /^DISTDIR=(.*)$/m) {
- $default_distdir = abs_path($unquote->($1));
+ $default_distdir = Cwd::abs_path($unquote->($1));
}
if ($buffers =~ /^PORTDIR=(.*)$/m) {
- $main_portdir = abs_path($unquote->($1));
+ $main_portdir = Cwd::abs_path($unquote->($1));
}
} else {
__PACKAGE__->_abort($errmsg);
+ return $format_available = 0;
}
}
my $stat = $self->status;
my $conf = $self->parent->parent->configure_object;
- $stat->mk_accessors(qw/name version author distribution desc uri src license
- fetched_arch deps
- ebuild_name ebuild_version ebuild_dir ebuild_file
- portdir_overlay
- overlay distdir keywords do_manifest header footer
- force verbose/);
+ $stat->mk_accessors(qw<
+ name version author distribution desc uri src license
+ meta min_perl
+ fetched_arch
+ requires configure_requires recursive_requires
+ ebuild_name ebuild_version ebuild_dir ebuild_file
+ portdir_overlay overlay distdir keywords do_manifest header footer
+ force verbose
+ >);
$stat->force($conf->get_conf('force'));
$stat->verbose($conf->get_conf('verbose'));
return 1;
}
+my $filter_prereqs = sub {
+ my ($int, $prereqs) = @_;
+
+ my @requires;
+ for my $prereq (sort keys %$prereqs) {
+ next if $prereq =~ /^perl(?:-|\z)/;
+
+ my $obj = $int->module_tree($prereq);
+ next unless $obj; # Not in the module tree (e.g. Config)
+ next if $obj->package_is_perl_core;
+
+ my $version = $prereqs->{$prereq} || undef;
+
+ push @requires, [ $obj->package_name, $version ];
+ }
+
+ return \@requires;
+};
+
sub prepare {
my $self = shift;
my $mod = $self->parent;
$stat->footer($footer);
my $overlay = delete $opts{overlay};
- $overlay = (defined $overlay) ? abs_path $overlay : '/usr/local/portage';
+ $overlay = (defined $overlay) ? Cwd::abs_path($overlay) : '/usr/local/portage';
$stat->overlay($overlay);
my $distdir = delete $opts{distdir};
- $distdir = (defined $distdir) ? abs_path $distdir : $default_distdir;
+ $distdir = (defined $distdir) ? Cwd::abs_path($distdir) : $default_distdir;
$stat->distdir($distdir);
return $FAIL->("distdir isn't writable") if $stat->do_manifest && !-w $distdir;
$stat->distribution($name . '-' . $version);
- $stat->ebuild_version(CPANPLUS::Dist::Gentoo::Maps::version_c2g($version));
+ $stat->ebuild_version(CPANPLUS::Dist::Gentoo::Maps::version_c2g($name, $version));
$stat->ebuild_name(CPANPLUS::Dist::Gentoo::Maps::name_c2g($name));
1 while unlink $file;
}
} else {
- if (my @match = $self->_cpan2portage($name, $version)) {
- $stat->dist($match[1]);
+ if (my $atom = $self->_cpan2portage($name, $version)) {
+ $stat->dist($atom->ebuild);
return $SKIP->('Ebuild already generated for', $stat->distribution);
}
}
$stat->prepared(0);
- $self->SUPER::prepare(%opts);
+ $self->SUPER::prepare(@_);
return $FAIL->() unless $stat->prepared;
my $desc = $mod->description;
- ($desc = $name) =~ s/-+/::/g unless $desc;
+ $desc = $mod->comment unless $desc;
+ $desc = "$name Perl distribution (provides " . $mod->module . ')'
+ unless $desc;
+ $desc = substr($desc, 0, 77) . '...' if length $desc > 80;
$stat->desc($desc);
$stat->uri('http://search.cpan.org/dist/' . $name);
$stat->license($self->intuit_license);
- my $prereqs = $mod->status->prereqs;
- my @depends;
- for my $prereq (sort keys %$prereqs) {
- next if $prereq =~ /^perl(?:-|\z)/;
- my $obj = $int->module_tree($prereq);
- next unless $obj; # Not in the module tree (e.g. Config)
- next if $obj->package_is_perl_core;
- {
- my $version;
- if ($prereqs->{$prereq}) {
- if ($obj->installed_version && $obj->installed_version < $obj->version) {
- $version = $obj->installed_version;
- } else {
- $version = $obj->package_version;
- }
- }
- push @depends, [ $obj->package_name, $version ];
+ my $mstat = $mod->status;
+ $stat->configure_requires($int->$filter_prereqs($mstat->configure_requires));
+ $stat->requires($int->$filter_prereqs($mstat->requires));
+ $stat->recursive_requires([ ]);
+
+ $dependencies{$name} = [ map $_->[0], @{ $stat->requires } ];
+
+ my $meta = $self->meta;
+ $stat->min_perl(CPANPLUS::Dist::Gentoo::Maps::perl_version_c2g(
+ $meta->{requires}->{perl},
+ ));
+
+ return $OK->();
+}
+
+=head2 C<meta>
+
+Returns the contents of the F<META.yml> or F<META.json> files as parsed by L<Parse::CPAN::Meta>.
+
+=cut
+
+sub meta {
+ my $self = shift;
+ my $mod = $self->parent;
+ my $stat = $self->status;
+
+ my $meta = $stat->meta;
+ return $meta if defined $meta;
+
+ my $extract_dir = $mod->status->extract;
+
+ for my $name (qw<META.json META.yml>) {
+ my $meta_file = File::Spec->catdir($extract_dir, $name);
+ next unless -e $meta_file;
+
+ local $@;
+ my $meta = eval { Parse::CPAN::Meta::LoadFile($meta_file) };
+ if (defined $meta) {
+ $stat->meta($meta);
+ return $meta;
}
}
- $stat->deps(\@depends);
- return $OK->();
+ return;
}
=head2 C<intuit_license>
return \@licenses if @licenses;
}
- my $extract_dir = $mod->status->extract;
-
- for my $meta_file (qw/META.json META.yml/) {
- my $meta = eval {
- Parse::CPAN::Meta::LoadFile(File::Spec->catdir(
- $extract_dir,
- $meta_file,
- ));
- } or next;
- my $license = $meta->{license};
- if (defined $license) {
- my @licenses = CPANPLUS::Dist::Gentoo::Maps::license_c2g($license);
- return \@licenses if @licenses;
- }
+ my $meta = $self->meta;
+ my $license = $meta->{license};
+ if (defined $license) {
+ my @licenses = CPANPLUS::Dist::Gentoo::Maps::license_c2g($license);
+ return \@licenses if @licenses;
}
return [ CPANPLUS::Dist::Gentoo::Maps::license_c2g('perl') ];
my $file;
+ my $guard = CPANPLUS::Dist::Gentoo::Guard->new(sub {
+ if (defined $file and -e $file and -w _) {
+ 1 while unlink $file;
+ }
+ });
+
+ my $SIG_INT = $SIG{INT};
+ local $SIG{INT} = sub {
+ if ($SIG_INT) {
+ local $@;
+ eval { $SIG_INT->() };
+ die $@ if $@;
+ }
+ die 'Caught SIGINT';
+ };
+
my $OK = sub {
+ $guard->unarm;
$stat->created(1);
$stat->dist($file) if defined $file;
1;
$stat->created(0);
$stat->dist(undef);
$self->_abort(@_) if @_;
- if (defined $file and -f $file) {
- 1 while unlink $file;
- }
0;
};
=cut
+my $dep_tree_contains;
+{
+ my %seen;
+
+ $dep_tree_contains = sub {
+ my ($dist, $target) = @_;
+
+ return 0 if $seen{$dist};
+ local $seen{$dist} = 1;
+
+ for my $kid (@{ $dependencies{$dist} }) {
+ return 1 if $kid eq $target
+ or $dep_tree_contains->($kid, $target);
+ }
+
+ return 0;
+ }
+}
+
sub ebuild_source {
my $self = shift;
my $stat = $self->status;
+ {
+ my $name = $stat->name;
+ my %recursive_kids = map { $_ => 1 }
+ grep $dep_tree_contains->($_, $name),
+ @{ $dependencies{$name} };
+ if (%recursive_kids) {
+ my (@requires, @recursive_requires);
+ for (@{ $stat->requires }) {
+ if ($recursive_kids{$_->[0]}) {
+ push @recursive_requires, $_;
+ } else {
+ push @requires, $_;
+ }
+ }
+ $stat->requires(\@requires);
+ $stat->recursive_requires(\@recursive_requires);
+ }
+ }
+
# We must resolve the deps now and not inside prepare because _cpan2portage
# has to see the ebuilds already generated for the dependencies of the current
# dist.
- my @deps;
- for (@{$stat->deps}) {
- my $dep = $self->_cpan2portage(@$_);
- unless (defined $dep) {
- $self->_abort(
- "Couldn't find an appropriate ebuild for $_->[0] in the portage tree"
- );
- return;
+
+ my (@configure_requires, @requires, @recursive_requires);
+
+ my @phases = (
+ [ configure_requires => \@configure_requires ],
+ [ requires => \@requires ],
+ [ recursive_requires => \@recursive_requires ],
+ );
+
+ push @requires, CPANPLUS::Dist::Gentoo::Atom->new(
+ category => 'dev-lang',
+ name => 'perl',
+ version => $stat->min_perl,
+ );
+
+ for (@phases) {
+ my ($phase, $list) = @$_;
+
+ for (@{ $stat->$phase }) {
+ my $atom = $self->_cpan2portage(@$_);
+ unless (defined $atom) {
+ $self->_abort(
+ "Couldn't find an appropriate ebuild for $_->[0] in the portage tree"
+ );
+ return;
+ }
+
+ push @$list, $atom;
}
- push @deps, $dep;
- }
- @deps = do { my %seen; sort grep !$seen{$_}++, 'dev-lang/perl', @deps };
+ @$list = CPANPLUS::Dist::Gentoo::Atom->fold(@$list);
+ }
my $d = $stat->header;
$d .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
$d .= "SLOT=\"0\"\n";
$d .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
$d .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
- $d .= 'RDEPEND="' . join("\n", @deps) . "\"\n";
- $d .= "DEPEND=\"\${RDEPEND}\"\n";
+ $d .= 'RDEPEND="' . join("\n", sort @requires) . "\"\n" if @requires;
+ $d .= 'PDEPEND="' . join("\n", sort @recursive_requires) . "\"\n"
+ if @recursive_requires;
+ $d .= 'DEPEND="' . join("\n", '${RDEPEND}', sort @configure_requires) . "\"\n";
$d .= "SRC_TEST=\"do\"\n";
$d .= $stat->footer;
}
sub _cpan2portage {
- my ($self, $name, $version) = @_;
+ my ($self, $dist_name, $dist_version) = @_;
- $name = CPANPLUS::Dist::Gentoo::Maps::name_c2g($name);
- my $ver;
- $ver = CPANPLUS::Dist::Gentoo::Maps::version_c2g($version) if defined $version;
+ my $name = CPANPLUS::Dist::Gentoo::Maps::name_c2g($dist_name);
+ my $version = CPANPLUS::Dist::Gentoo::Maps::version_c2g($dist_name, $dist_version);
my @portdirs = ($main_portdir, @{$self->status->portdir_overlay});
- for my $category (qw/virtual perl-core dev-perl perl-gcpan/, CATEGORY) {
- my $atom = ($category eq 'virtual' ? 'perl-' : '') . $name;
+ for my $category (qw<virtual perl-core dev-perl perl-gcpan>, CATEGORY) {
+ my $name = ($category eq 'virtual' ? 'perl-' : '') . $name;
for my $portdir (@portdirs) {
my @ebuilds = glob File::Spec->catfile(
$portdir,
$category,
- $atom,
- "$atom-*.ebuild",
+ $name,
+ "$name-*.ebuild",
) or next;
- my $last = reduce {
- CPANPLUS::Dist::Gentoo::Maps::version_gcmp($b->[1], $a->[1]) >= 0 ? $b : $a
- } map [ $_, /\Q$atom\E-v?([\d._pr-]+).*?\.ebuild$/ ? $1 : 0 ], @ebuilds;
-
- my $dep;
- if (defined $ver) { # implies that $version is defined
- next unless
- CPANPLUS::Dist::Gentoo::Maps::version_gcmp($last->[1], $ver) >= 0;
- $dep = ">=$category/$atom-$ver";
- } else {
- $dep = "$category/$atom";
- }
+ my $last = reduce { $a < $b ? $b : $a } # handles overloading
+ map CPANPLUS::Dist::Gentoo::Atom->new_from_ebuild($_),
+ @ebuilds;
+ next if defined $version and $last < $version;
- return wantarray ? ($dep, $last->[0]) : $dep;
+ return CPANPLUS::Dist::Gentoo::Atom->new(
+ category => $last->category,
+ name => $last->name,
+ version => $version,
+ ebuild => $last->ebuild,
+ );
}
}
my ($success, $errmsg, $output) = do {
local $ENV{PORTDIR_OVERLAY} = join ' ', @{$stat->portdir_overlay};
local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
- run command => $cmd, verbose => $verbose;
+ IPC::Cmd::run(
+ command => $cmd,
+ verbose => $verbose,
+ );
};
unless ($success) {
=head1 COPYRIGHT & LICENSE
-Copyright 2008-2009 Vincent Pit, all rights reserved.
+Copyright 2008,2009,2010 Vincent Pit, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.