]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/blobdiff - lib/CPANPLUS/Dist/Gentoo.pm
Set $stat->prepared properly in prepare()
[perl/modules/CPANPLUS-Dist-Gentoo.git] / lib / CPANPLUS / Dist / Gentoo.pm
index 4a3c275593740e271479e0777e9233b651a5e981..be152cf2fc8c883c2d2a9394e73038b96b50f8d6 100644 (file)
@@ -3,6 +3,7 @@ package CPANPLUS::Dist::Gentoo;
 use strict;
 use warnings;
 
+use Cwd qw/abs_path/;
 use File::Copy qw/copy/;
 use File::Path qw/mkpath/;
 use File::Spec::Functions qw/catdir catfile/;
@@ -19,11 +20,11 @@ CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
 
 =head1 VERSION
 
-Version 0.02_01
+Version 0.04
 
 =cut
 
-our $VERSION = '0.02_01';
+our $VERSION = '0.04';
 
 =head1 SYNOPSIS
 
@@ -32,17 +33,23 @@ our $VERSION = '0.02_01';
               --dist-opts distdir=/usr/portage/distfiles \
               --dist-opts manifest=yes \
               --dist-opts keywords=x86 \
+              --dist-opts header="# Copyright 1999-2008 Gentoo Foundation" \
+              --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 C</usr/local/portage>), update the manifest, and even emerge it (together with its dependencies) if the user requires it. You need write permissions on the directory where Gentoo fetches its source files (usually C</usr/portage/distfiles>).
+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>). You also need to specify the correct keyword for your architecture if it differs from the default C<x86>.
 
-The generated ebuilds are placed into the section C<perl-gcpanp>. They favour depending on C<perl-core> or C<dev-perl> rather than C<perl-gcpanp>.
+The generated ebuilds are placed into the C<perl-gcpanp> category. They favour depending on C<perl-core>, C<dev-perl> or C<perl-gcpan> (in that order) rather than C<perl-gcpanp>.
+
+=head1 INSTALLATION
+
+After installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
 
 =head1 METHODS
 
-All the methods are inherited from L<CPANPLUS::Dist::Base>. Please refer to its perldoc for precise information on what's done at each step.
+All the methods are inherited from L<CPANPLUS::Dist::Base>. Please refer to its documentation for precise information on what's done at each step.
 
 =cut
 
@@ -61,19 +68,30 @@ sub format_available {
 sub init {
  my ($self) = @_;
  my $stat = $self->status;
- $stat->mk_accessors(qw/name version dist desc uri src license deps
-                        eb_name eb_version eb_dir eb_file distdir fetched_arch
-                        keywords do_manifest/);
+ my $conf = $self->parent->parent->configure_object;
+
+ $stat->mk_accessors(qw/name version author dist desc uri src license deps
+                        eb_name eb_version eb_dir eb_file fetched_arch
+                        overlay distdir keywords do_manifest header footer
+                        force verbose/);
+
+ $stat->force($conf->get_conf('force'));
+ $stat->verbose($conf->get_conf('verbose'));
 
  return 1;
 }
 
 my %gentooism = (
- 'Digest'          => 'digest-base',
- 'Locale-Maketext' => 'locale-maketext',
- 'Net-Ping'        => 'net-ping',
- 'PathTools'       => 'File-Spec',
- 'PodParser'       => 'Pod-Parser',
+ 'Crypt-RSA'         => 'crypt-rsa',
+ 'Digest'            => 'digest-base',
+ 'Locale-Maketext'   => 'locale-maketext',
+ 'Math-Pari'         => 'math-pari',
+ 'Net-Ping'          => 'net-ping',
+ 'PathTools'         => 'File-Spec',
+ 'PodParser'         => 'Pod-Parser',
+ 'Set-Scalar'        => 'set-scalar',
+ 'Tie-EncryptedHash' => 'tie-encryptedhash',
+ 'YAML'              => 'yaml',
 );
 
 sub prepare {
@@ -85,6 +103,8 @@ sub prepare {
 
  my %opts = @_;
 
+ $stat->prepared(0);
+
  my $keywords = delete $opts{'keywords'};
  $keywords = 'x86' unless defined $keywords;
  $keywords = [ split ' ', $keywords ];
@@ -95,10 +115,31 @@ sub prepare {
  $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
  $stat->do_manifest($manifest);
 
- my $overlay = catdir(delete($opts{'overlay'}) || '/usr/local/portage',
-                      CATEGORY);
+ my $header = delete $opts{'header'};
+ if (defined $header) {
+  1 while chomp $header;
+  $header .= "\n\n";
+ } else {
+  $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'};
+ $overlay = (defined $overlay) ? abs_path $overlay : '/usr/local/portage';
+ $stat->overlay($overlay);
+
+ my $distdir = delete $opts{'distdir'};
+ $distdir = (defined $distdir) ? abs_path $distdir : '/usr/portage/distfiles';
+ $stat->distdir($distdir);
 
- $stat->distdir(delete($opts{'distdir'}) || '/usr/portage/distfiles');
  if ($stat->do_manifest && !-w $stat->distdir) {
   error 'distdir isn\'t writable -- aborting';
   return 0;
@@ -110,39 +151,70 @@ sub prepare {
 
  my $version = $mod->package_version;
  $stat->version($version);
+
+ my $author = $mod->author->cpanid;
+ $stat->author($author);
+
  $stat->dist($name . '-' . $version);
- my $f = 1;
- $version =~ s/_+/$f ? do { $f = 0; '_p' } : ''/ge;
- 1 while $version =~ s/(_p[^.]*)\.+/$1/;
+
+ $version =~ s/[^\d._]+//g;
+ $version =~ s/^[._]*//;
+ $version =~ s/[._]*$//;
+ $version =~ s/[._]*_[._]*/_/g;
+ {
+  ($version, my $patch, my @rest) = split /_/, $version;
+  $version .= '_p' . $patch if defined $patch;
+  $version .= join('.', '', @rest) if @rest;
+ }
  $stat->eb_version($version);
 
- $stat->eb_name($gentooism{$stat->name} || $stat->name);
- $stat->eb_dir(catdir($overlay, $stat->eb_name));
- $stat->eb_file(catfile($stat->eb_dir,
-                        $stat->eb_name . '-' . $stat->eb_version . '.ebuild'));
- if (-r $stat->eb_file) {
-  msg 'Ebuild already generated for ' . $stat->dist . ' -- skipping';
-  $stat->prepared(1);
-  $stat->created(1);
-  return 1;
+ $stat->eb_name($gentooism{$name} || $name);
+
+ $stat->eb_dir(catdir($stat->overlay, CATEGORY, $stat->eb_name));
+
+ my $file = catfile($stat->eb_dir,
+                    $stat->eb_name . '-' . $stat->eb_version . '.ebuild');
+ if (-e $file) {
+  my $skip = 1;
+  if ($stat->force) {
+   if (-w $file) {
+    1 while unlink $file;
+    $skip = 0;
+   } else {
+    error "Can't force rewriting of $file -- skipping";
+   }
+  } else {
+   msg 'Ebuild already generated for ' . $stat->dist . ' -- skipping';
+  }
+  if ($skip) {
+   $stat->prepared(1);
+   $stat->created(1);
+   return 1;
+  }
  }
+ $stat->eb_file($file);
 
  $self->SUPER::prepare(%opts);
 
  my $desc = $mod->description;
  ($desc = $name) =~ s/-+/::/g unless $desc;
  $stat->desc($desc);
+
  $stat->uri('http://search.cpan.org/dist/' . $name);
- unless ($name =~ /^([^-]+)/) {
-  error 'Wrong distribution name -- aborting';
+
+ unless ($author =~ /^(.)(.)/) {
+  error 'Wrong author name -- aborting';
   return 0;
  }
- $stat->src('mirror://cpan/modules/by-module/' . $1 . '/' . $mod->package);
+ $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/"
+            . $mod->package);
+
  $stat->license([ qw/Artistic GPL-2/ ]);
 
  my $prereqs = $mod->status->prereqs;
  my @depends;
  for my $prereq (sort keys %$prereqs) {
+  next if $prereq =~ /^perl(?:-|\z)/;
   my $obj = $int->module_tree($prereq);
   unless ($obj) {
    error 'Wrong module object -- aborting';
@@ -163,6 +235,7 @@ sub prepare {
  }
  $stat->deps(\@depends);
 
+ $stat->prepared(1);
  return 1;
 }
 
@@ -191,7 +264,9 @@ sub create {
   }
  }
 
- my $d = "# Generated by CPANPLUS::Dist::Gentoo\n\ninherit perl-module\n\n";
+ 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->dist . "\"\n";
  $d   .= 'DESCRIPTION="' . $stat->desc . "\"\n";
  $d   .= 'HOMEPAGE="' . $stat->uri . "\"\n";
@@ -203,16 +278,19 @@ sub create {
   'dev-lang/perl',
   map {
    my $a = $_->[0]->package_name;
+   $a = $gentooism{$a} || $a;
    my $x = '';
    if (defined $_->[1]) {
     $x  = '>=';
     $a .= '-' . $_->[1];
    }
    '|| ( ' . join(' ', map "$x$_/$a",
-                           qw/perl-core dev-perl/, CATEGORY) # perl-gcpan ?
+                           qw/perl-core dev-perl perl-gcpan/, CATEGORY)
            . ' )';
   } @{$stat->deps};
  $d   .= "\"\n";
+ $d   .= "SRC_TEST=\"do\"\n";
+ $d   .= $stat->footer;
 
  my $file = $stat->eb_file;
  open my $eb, '>', $file or do {
@@ -230,8 +308,7 @@ sub create {
   }
 
   msg 'Adding Manifest entry for ' . $stat->dist;
-  unless (scalar run command => [ 'ebuild', $file, 'manifest' ], verbose => 0) {
-   error 'ebuild manifest failed -- aborting';
+  unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
    1 while unlink $file;
    return 0;
   }
@@ -249,12 +326,7 @@ sub install {
  my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
  unshift @cmd, $sudo if $sudo;
 
- unless (run command => \@cmd, verbose => 1) {
-  error 'emerge failed -- aborting';
-  return 0;
- }
-
- return 1;
+ return $self->_run(\@cmd, 1);
 }
 
 sub uninstall {
@@ -266,12 +338,29 @@ sub uninstall {
  my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
  unshift @cmd, $sudo if $sudo;
 
- unless (run command => \@cmd, verbose => 1) {
-  error 'emerge -C failed -- aborting';
-  return 0;
+ return $self->_run(\@cmd, 1);
+}
+
+sub _run {
+ my ($self, $cmd, $verbose) = @_;
+ my $stat = $self->status;
+
+ my ($success, $errmsg, $output) = do {
+  local $ENV{PORTDIR_OVERLAY}     = $stat->overlay;
+  local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
+  run command => $cmd, verbose => $verbose;
+ };
+
+ unless ($success) {
+  error "$errmsg -- aborting";
+  if (not $verbose and defined $output and $self->status->verbose) {
+   my $msg = join '', @$output;
+   1 while chomp $msg;
+   error $msg;
+  }
  }
 
- return 1;
+ return $success;
 }
 
 =head1 DEPENDENCIES
@@ -280,7 +369,7 @@ Gentoo (L<http://gentoo.org>).
 
 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5).
 
-L<File::Path> (since 5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
+L<Cwd> (since perl 5) L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
 
 =head1 SEE ALSO
 
@@ -292,6 +381,8 @@ L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
 
 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>.  I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
@@ -306,6 +397,8 @@ You can find documentation for this module with the perldoc command.
 
 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
 
+Kent Fredric, for testing and suggesting improvements.
+
 =head1 COPYRIGHT & LICENSE
 
 Copyright 2008 Vincent Pit, all rights reserved.