CPANPLUS-Dist-Gentoo

 view release on metacpan or  search on metacpan

lib/CPANPLUS/Dist/Gentoo.pm  view on Meta::CPAN

package CPANPLUS::Dist::Gentoo;

use strict;
use warnings;

use Cwd        ();
use List::Util qw<reduce>;
use File::Copy ();
use File::Path ();
use File::Spec;
use POSIX      ();

use IPC::Cmd          ();
use Parse::CPAN::Meta ();

use CPANPLUS::Error ();

use base qw<CPANPLUS::Dist::Base>;

use CPANPLUS::Dist::Gentoo::Atom;
use CPANPLUS::Dist::Gentoo::Guard;
use CPANPLUS::Dist::Gentoo::Maps;

=head1 NAME

CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.

=head1 VERSION

Version 0.12

=cut

our $VERSION = '0.12';

=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="# Begin" \
              --dist-opts footer="# End" \
              Any::Module You::Like

=head1 DESCRIPTION

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>.
Existing ebuilds will be searched into the main C<PORTDIR> portage tree and then into the overlays listed in C<PORTDIR_OVERLAY>.

=head1 OPTIONS

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.

The valid option C<key>s are :

=over 4

=item *

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 *

C<overlay>

The path of the overlay in which the generated ebuilds will be written.

Defaults to the first overlay listed in C<PORTDIR_OVERLAY> (as returned by C<emerge --info>) or F</usr/local/portage> if this variable is empty.

=item *

C<distdir>

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>) or F</usr/portage/distfiles> if this variable is empty.

=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>) or C<'x86'> if this variable is empty.

=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.

=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

Please refer to L<cpan2dist> documentation for a complete coverage of its abilities.

=head1 INSTALLATION

Before installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.

You have two ways for installing this module :

=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> which will most likely be up-to-date given the reactivity of Gentoo's Perl herd.

=item *

Bootstrap an ebuild for L<CPANPLUS::Dist::Gentoo> using itself.

First, make sure your system C<perl> is C<5.10> or greater, so that the L<CPANPLUS> toolchain is available.

    $ perl -v
    This is perl 5, version 12, subversion 2 (v5.12.2)...

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 :

    # emerge -tv ">=dev-lang/perl-5.10"
    # perl-cleaner --all

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.12.tar.gz

Log in as root and unpack it in e.g. your home directory :

    # cd
    # tar xzf /tmp/CPANPLUS-Dist-Gentoo-0.12.tar.gz
    # cd CPANPLUS-Dist-Gentoo-0.12

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 :

    # emerge -tv CPANPLUS-Dist-Gentoo

=back

=head1 METHODS

This module inherits all the methods from L<CPANPLUS::Dist::Base>.
Please refer to its documentation for precise information on what's done at each step.

=cut

use constant CATEGORY => 'perl-gcpanp';

my $overlays;
my $default_keywords;
my $default_distdir;
my $main_portdir;

my %dependencies;
my %forced;

my $unquote = sub {
 my $s = shift;
 $s =~ s/^["']*//;
 $s =~ s/["']*$//;
 return $s;
};

my $format_available;

sub format_available {
 return $format_available if defined $format_available;

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

 {
  my $buffers;
  my ($success, $errmsg) = IPC::Cmd::run(
   command => [ qw<emerge --info> ],
   verbose => 0,
   buffer  => \$buffers,
  );
  if ($success) {
   if ($buffers =~ /^PORTDIR_OVERLAY=(.*)$/m) {
    $overlays = [ map Cwd::abs_path($_), split ' ', $unquote->($1) ];
   }
   if ($buffers =~ /^ACCEPT_KEYWORDS=(.*)$/m) {
    $default_keywords = [ split ' ', $unquote->($1) ];
   }
   if ($buffers =~ /^DISTDIR=(.*)$/m) {
    $default_distdir = Cwd::abs_path($unquote->($1));
   }
   if ($buffers =~ /^PORTDIR=(.*)$/m) {
    $main_portdir = Cwd::abs_path($unquote->($1));
   }
  } else {
   __PACKAGE__->_abort($errmsg);
   return $format_available = 0;
  }
 }

 $default_keywords = [ 'x86' ] unless defined $default_keywords;
 $default_distdir  = '/usr/portage/distfiles' unless defined $default_distdir;

 my $timestamp = CPANPLUS::Dist::Gentoo::Maps::get_portage_timestamp(
  $main_portdir
 );
 if (defined $timestamp) {
  __PACKAGE__->_notify("Portage tree $main_portdir dates back from UNIX timestamp $timestamp");
 } else {
  __PACKAGE__->_notify("Unable to get timestamp for portage tree $main_portdir, using gmtime instead");
  $timestamp = POSIX::mktime(gmtime);
 }
 if ($timestamp < CPANPLUS::Dist::Gentoo::Maps::TIMESTAMP) {
  __PACKAGE__->_abort("Portage tree too old (please run emerge --sync and retry)");
  return $format_available = 0;
 }

 return $format_available = 1;
}

sub init {
 my ($self) = @_;
 my $stat = $self->status;
 my $conf = $self->parent->parent->configure_object;

 $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;
 my $stat = $self->status;
 my $int  = $mod->parent;
 my $conf = $int->configure_object;

 my %opts = @_;

 my $OK   = sub { $stat->prepared(1); 1 };
 my $FAIL = sub { $stat->prepared(0); $self->_abort(@_) if @_; 0 };
 my $SKIP = sub { $stat->prepared(1); $stat->created(1); $self->_skip(@_) if @_; 1 };

 my $keywords = delete $opts{keywords};
 if (defined $keywords) {
  $keywords = [ split ' ', $keywords ];
 } else {
  $keywords = $default_keywords;
 }
 $stat->keywords($keywords);

 my $manifest = delete $opts{manifest};
 $manifest = 1 unless defined $manifest;
 $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
 $stat->do_manifest($manifest);

 my $header = delete $opts{header};
 if (defined $header) {
  1 while chomp $header;
  $header .= "\n\n";
 } else {
  my $year = (localtime)[5] + 1900;
  $header = <<"  DEF_HEADER";
# Copyright 1999-$year Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# \$Header: \$
  DEF_HEADER
 }
 $stat->header($header);

 my $footer = delete $opts{footer};
 if (defined $footer) {
  $footer = "\n" . $footer;
 } else {
  $footer = '';
 }
 $stat->footer($footer);

 my $overlay = delete $opts{overlay};
 if (defined $overlay) {
  $overlay   = Cwd::abs_path($overlay);
 } else {
  $overlay   = $overlays->[0];
  $overlay   = '/usr/local/portage' unless defined $overlay;
 }
 $stat->overlay($overlay);

 my $distdir = delete $opts{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->fetched_arch($mod->status->fetch);

 my $cur = File::Spec->curdir();
 my $portdir_overlay;
 for (@$overlays) {
  if ($_ eq $overlay or File::Spec->abs2rel($overlay, $_) eq $cur) {
   $portdir_overlay = [ @$overlays ];
   last;
  }
 }
 $portdir_overlay = [ @$overlays, $overlay ] unless $portdir_overlay;
 $stat->portdir_overlay($portdir_overlay);

 my $name = $mod->package_name;
 $stat->name($name);

 my $version = $mod->package_version;
 $stat->version($version);

 my $author = $mod->author->cpanid;
 $stat->author($author);

 $stat->distribution($name . '-' . $version);

 $stat->ebuild_version(CPANPLUS::Dist::Gentoo::Maps::version_c2g($name, $version));

 $stat->ebuild_name(CPANPLUS::Dist::Gentoo::Maps::name_c2g($name));

 $stat->ebuild_dir(File::Spec->catdir(
  $stat->overlay,
  CATEGORY,
  $stat->ebuild_name,
 ));

 my $file = File::Spec->catfile(
  $stat->ebuild_dir,
  $stat->ebuild_name . '-' . $stat->ebuild_version . '.ebuild',
 );
 $stat->ebuild_file($file);

 if ($stat->force) {
  # Always generate an ebuild in our category when forcing
  if ($forced{$file}) {
   $stat->dist($file);
   return $SKIP->('Ebuild already forced for', $stat->distribution);
  }
  ++$forced{$file};
  if (-e $file) {
   unless (-w $file) {
    $stat->dist($file);
    return $SKIP->("Can't force rewriting of $file");
   }
   1 while unlink $file;
  }
 } else {
  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(@_);

 return $FAIL->() unless $stat->prepared;

 my $desc = $mod->description;
 $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);

 $author =~ /^(.)(.)/ or return $FAIL->('Wrong author name');
 $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/" . $mod->package);

 $stat->license($self->intuit_license);

 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(

lib/CPANPLUS/Dist/Gentoo.pm  view on Meta::CPAN

 # dist.

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

  @$list = CPANPLUS::Dist::Gentoo::Atom->fold(@$list);
 }

 my $d = $stat->header;
 $d   .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
 $d   .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
 $d   .= 'S="${WORKDIR}/' . $stat->distribution . "\"\n";
 $d   .= 'DESCRIPTION="' . $stat->desc . "\"\n";
 $d   .= 'HOMEPAGE="' . $stat->uri . "\"\n";
 $d   .= 'SRC_URI="' . $stat->src . "\"\n";
 $d   .= "SLOT=\"0\"\n";
 $d   .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
 $d   .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\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;

 return $d;
}

sub _cpan2portage {
 my ($self, $dist_name, $dist_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 $name = ($category eq 'virtual' ? 'perl-' : '') . $name;

  for my $portdir (@portdirs) {
   my @ebuilds = glob File::Spec->catfile(
    $portdir,
    $category,
    $name,
    "$name-*.ebuild",
   ) or next;

   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 CPANPLUS::Dist::Gentoo::Atom->new(
    category => $last->category,
    name     => $last->name,
    version  => $version,
    ebuild   => $last->ebuild,
   );
  }

 }

 return;
}

sub install {
 my $self = shift;
 my $stat = $self->status;
 my $conf = $self->parent->parent->configure_object;

 my $sudo = $conf->get_program('sudo');
 my @cmd = ('emerge', '=' . $stat->ebuild_name . '-' . $stat->ebuild_version);
 unshift @cmd, $sudo if $sudo;

 my $success = $self->_run(\@cmd, 1);
 $stat->installed($success);

 return $success;
}

sub uninstall {
 my $self = shift;
 my $stat = $self->status;
 my $conf = $self->parent->parent->configure_object;

 my $sudo = $conf->get_program('sudo');
 my @cmd = ('emerge', '-C', '=' . $stat->ebuild_name . '-' . $stat->ebuild_version);
 unshift @cmd, $sudo if $sudo;

 my $success = $self->_run(\@cmd, 1);
 $stat->uninstalled($success);

 return $success;
}

sub _run {
 my ($self, $cmd, $verbose) = @_;
 my $stat = $self->status;

 my ($success, $errmsg, $output) = do {
  local $ENV{PORTDIR_OVERLAY}     = join ' ', @{$stat->portdir_overlay};
  local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
  IPC::Cmd::run(
   command => $cmd,
   verbose => $verbose,
  );
 };

 unless ($success) {
  $self->_abort($errmsg);
  if (not $verbose and defined $output and $stat->verbose) {
   my $msg = join '', @$output;
   1 while chomp $msg;
   CPANPLUS::Error::error($msg);
  }
 }

 return $success;
}

sub _abort {
 my $self = shift;

 CPANPLUS::Error::error("@_ -- aborting");

 return 0;
}

sub _notify {
 my $self = shift;

 CPANPLUS::Error::msg("@_");

 return 1;
}

sub _skip { shift->_notify(@_, '-- skipping') }

=head1 DEPENDENCIES

Gentoo (L<http://gentoo.org>).

L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5), L<Parse::CPAN::Meta> (since 5.10.1).

L<Cwd>, L<Carp> (since perl 5), L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec> (5.00405), L<List::Util> (5.007003).

=head1 SEE ALSO

L<cpan2dist>.

L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.

=head1 AUTHOR

Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.

You can contact me by mail or on C<irc.perl.org> (vincent).

=head1 BUGS

Please report any bugs or feature requests to C<bug-cpanplus-dist-gentoo at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CPANPLUS-Dist-Gentoo>.



( run in 1.982 second using v1.01-cache-2.11-cpan-7fcb06a456a )