]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/blobdiff - lib/CPANPLUS/Dist/Gentoo.pm
Write the ebuild only after the dependencies were handled
[perl/modules/CPANPLUS-Dist-Gentoo.git] / lib / CPANPLUS / Dist / Gentoo.pm
index 5b113d812609dbe06ce28532a1e0ce0281d0279b..49de89288dce24c55a4f6dbf9b5da1eb899bd76e 100644 (file)
@@ -9,6 +9,7 @@ use File::Path ();
 use File::Spec;
 
 use IPC::Cmd qw/run can_run/;
+use Parse::CPAN::Meta ();
 
 use CPANPLUS::Error ();
 
@@ -41,9 +42,12 @@ our $VERSION = '0.07';
 
 =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 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.
 
-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>.
+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
 
@@ -51,7 +55,8 @@ After installing this module, you should append C<perl-gcpanp> to your F</etc/po
 
 =head1 METHODS
 
-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.
+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
 
@@ -142,7 +147,7 @@ sub prepare {
  my $OK   = sub { $stat->prepared(1); 1 };
  my $FAIL = sub { $stat->prepared(0); $self->_abort(@_) if @_; 0 };
 
- my $keywords = delete $opts{'keywords'};
+ my $keywords = delete $opts{keywords};
  if (defined $keywords) {
   $keywords = [ split ' ', $keywords ];
  } else {
@@ -150,12 +155,12 @@ sub prepare {
  }
  $stat->keywords($keywords);
 
- my $manifest = delete $opts{'manifest'};
+ 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'};
+ my $header = delete $opts{header};
  if (defined $header) {
   1 while chomp $header;
   $header .= "\n\n";
@@ -164,7 +169,7 @@ sub prepare {
  }
  $stat->header($header);
 
- my $footer = delete $opts{'footer'};
+ my $footer = delete $opts{footer};
  if (defined $footer) {
   $footer = "\n" . $footer;
  } else {
@@ -172,17 +177,16 @@ sub prepare {
  }
  $stat->footer($footer);
 
- my $overlay = delete $opts{'overlay'};
+ my $overlay = delete $opts{overlay};
  $overlay = (defined $overlay) ? abs_path $overlay : '/usr/local/portage';
  $stat->overlay($overlay);
 
- my $distdir = delete $opts{'distdir'};
+ my $distdir = delete $opts{distdir};
  $distdir = (defined $distdir) ? abs_path $distdir : $default_distdir;
  $stat->distdir($distdir);
 
- if ($stat->do_manifest && !-w $stat->distdir) {
-  return $FAIL->('distdir isn\'t writable');
- }
+ return $FAIL->("distdir isn't writable") if $stat->do_manifest && !-w $distdir;
+
  $stat->fetched_arch($mod->status->fetch);
 
  my $cur = File::Spec->curdir();
@@ -244,9 +248,11 @@ sub prepare {
   }
  }
 
+ $stat->prepared(0);
+
  $self->SUPER::prepare(%opts);
 
$stat->prepared(0);
return $FAIL->() unless $stat->prepared;
 
  my $desc = $mod->description;
  ($desc = $name) =~ s/-+/::/g unless $desc;
@@ -254,20 +260,17 @@ sub prepare {
 
  $stat->uri('http://search.cpan.org/dist/' . $name);
 
- unless ($author =~ /^(.)(.)/) {
-  return $FAIL->('Wrong author name');
- }
- $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/"
-            . $mod->package);
+ $author =~ /^(.)(.)/ or return $FAIL->('Wrong author name');
+ $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/" . $mod->package);
 
- $stat->license([ qw/Artistic GPL-2/ ]);
+ $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);
-  return $FAIL->('Wrong module object') unless $obj;
+  next unless $obj; # Not in the module tree (e.g. Config)
   next if $obj->package_is_perl_core;
   {
    my $version;
@@ -286,12 +289,71 @@ sub prepare {
  return $OK->();
 }
 
+=head2 C<intuit_license>
+
+Returns an array reference to a list of Gentoo licences identifiers under which the current distribution is released.
+
+=cut
+
+my %dslip_license = (
+ p => 'perl',
+ g => 'gpl',
+ l => 'lgpl',
+ b => 'bsd',
+ a => 'artistic',
+ 2 => 'artistic_2',
+);
+
+sub intuit_license {
+ my $self = shift;
+ my $mod  = $self->parent;
+
+ my $dslip = $mod->dslip;
+ if (defined $dslip and $dslip =~ /\S{4}(\S)/) {
+  my @licenses = CPANPLUS::Dist::Gentoo::Maps::license_c2g($dslip_license{$1});
+  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;
+  }
+ }
+
+ return [ CPANPLUS::Dist::Gentoo::Maps::license_c2g('perl') ];
+}
+
 sub create {
  my $self = shift;
  my $stat = $self->status;
 
- my $OK   = sub { $stat->created(1); $stat->dist($stat->ebuild_file); 1 };
- my $FAIL = sub { $stat->created(0); $stat->dist(undef); $self->_abort(@_) if @_; 0 };
+ my $file;
+
+ my $OK   = sub {
+  $stat->created(1);
+  $stat->dist($file);
+  1;
+ };
+
+ my $FAIL = sub {
+  $stat->created(0);
+  $stat->dist(undef);
+  $self->_abort(@_) if @_;
+  if ($file and -f $file) {
+   1 while unlink $file;
+  }
+  0;
+ };
 
  unless ($stat->prepared) {
   return $FAIL->(
@@ -310,27 +372,34 @@ sub create {
   return $FAIL->("mkpath($dir): $@") if $@;
  }
 
- my $file = $stat->ebuild_file;
- open my $eb, '>', $file or return $FAIL->("open($file): $!");
- print $eb $self->ebuild_source;
- close $eb;
+ $file = $stat->ebuild_file;
+
+ # Create a placeholder ebuild to prevent recursion with circular dependencies.
+ {
+  open my $eb, '>', $file or return $FAIL->("open($file): $!");
+  print $eb "PLACEHOLDER\n";
+ }
 
  $stat->created(0);
  $stat->dist(undef);
 
  $self->SUPER::create(@_);
 
- if ($stat->do_manifest and not $self->update_manifest) {
-  1 while unlink $file;
-  return $FAIL->();
+ return $FAIL->() unless $stat->created;
+
+ {
+  open my $eb, '>', $file or return $FAIL->("open($file): $!");
+  print $eb $self->ebuild_source;
  }
 
+ return $FAIL->() if $stat->do_manifest and not $self->update_manifest;
+
  return $OK->();
 }
 
 =head2 C<update_manifest>
 
-Update the F<Manifest> file for the ebuild associated to the current dist object.
+Updates the F<Manifest> file for the ebuild associated to the current dist object.
 
 =cut
 
@@ -413,7 +482,7 @@ sub _cpan2portage {
      my ($eb_ver) = /\Q$atom\E-v?([\d._pr-]+).*?\.ebuild$/;
      return ">=$category/$atom-$ver"
             if  defined $eb_ver
-            and CPANPLUS::Dist::Gentoo::Maps::version_gcmp($eb_ver, $ver) > 0;
+            and CPANPLUS::Dist::Gentoo::Maps::version_gcmp($eb_ver, $ver) >= 0;
     }
    } else {
     return "$category/$atom";
@@ -504,7 +573,7 @@ sub _skip { shift->_notify(@_, '-- skipping') }
 
 Gentoo (L<http://gentoo.org>).
 
-L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5).
+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).
 
@@ -522,7 +591,8 @@ 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.
+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.
 
 =head1 SUPPORT
 
@@ -532,7 +602,7 @@ You can find documentation for this module with the perldoc command.
 
 =head1 ACKNOWLEDGEMENTS
 
-The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
+The module was inspired by L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
 
 Kent Fredric, for testing and suggesting improvements.