1 package CPANPLUS::Dist::Gentoo;
7 use List::Util qw/reduce/;
13 use Parse::CPAN::Meta ();
15 use CPANPLUS::Error ();
17 use base qw/CPANPLUS::Dist::Base/;
19 use CPANPLUS::Dist::Gentoo::Atom;
20 use CPANPLUS::Dist::Gentoo::Maps;
24 CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
32 our $VERSION = '0.09';
36 cpan2dist --format=CPANPLUS::Dist::Gentoo \
37 --dist-opts overlay=/usr/local/portage \
38 --dist-opts distdir=/usr/portage/distfiles \
39 --dist-opts manifest=yes \
40 --dist-opts keywords=x86 \
41 --dist-opts header="# Copyright 1999-2008 Gentoo Foundation" \
42 --dist-opts footer="# End" \
47 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.
48 You need write permissions on the directory where Gentoo fetches its source files (usually F</usr/portage/distfiles>).
49 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.
51 The generated ebuilds are placed into the C<perl-gcpanp> category.
52 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>.
56 Before installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
58 You have two ways for installing this module :
64 Use the perl overlay located at L<http://git.overlays.gentoo.org/gitweb/?p=proj/perl-overlay.git>.
65 It contains an ebuild for L<CPANPLUS::Dist::Gentoo>.
69 Bootstrap an ebuild for L<CPANPLUS::Dist::Gentoo> using itself.
70 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.
71 So you need to bootstrap them as well.
73 First, fetch tarballs for L<CPANPLUS> and L<CPANPLUS::Dist::Gentoo> :
76 $ wget http://search.cpan.org/CPAN/authors/id/B/BI/BINGOS/CPANPLUS-0.9003.tar.gz
77 $ wget http://search.cpan.org/CPAN/authors/id/V/VP/VPIT/CPANPLUS-Dist-Gentoo-0.09.tar.gz
79 Log in as root and unpack them in e.g. your home directory :
82 # tar xzf /tmp/CPANPLUS-0.9003.tar.gz
83 # tar xzf /tmp/CPANPLUS-Dist-Gentoo-0.09.tar.gz
85 Set up environment variables so that the toolchain is temporarily available :
87 # export OLDPATH=$PATH
88 # export PATH=/root/CPANPLUS-0.9003/bin:$PATH
89 # export PERL5LIB=/root/CPANPLUS-Dist-Gentoo-0.09/blib/lib:/root/CPANPLUS-0.9003/lib:/root/CPANPLUS-0.9003/inc/bundle
91 Make sure you don't have an old C<.cpanplus> configuration visible :
93 # [ -d /root/.cpanplus ] && mv /root/.cpanplus{,.bak}
95 Bootstrap L<CPANPLUS> :
97 # cd /root/CPANPLUS-Dist-Gentoo-0.09
98 # samples/g-cpanp CPANPLUS
100 Reset the environment :
102 # export PATH=$OLDPATH
103 # unset PERL5LIB OLDPATH
105 Emerge L<CPANPLUS> with the ebuilds you've just generated :
107 # emerge -tv CPANPLUS
109 As of september 2009, C<podlators> and C<ExtUtils-MakeMaker> may fail to emerge due to collisions.
110 You can work around this by disabling the C<protect-owned> C<FEATURE> for them :
112 # FEATURES="-protect-owned" emerge podlators
113 # FEATURES="-protect-owned" emerge ExtUtils-MakeMaker
115 You may need to run each of these commands two times for them to succeed.
117 At this point, you can bootstrap L<CPANPLUS::Dist::Gentoo> using the system L<CPANPLUS> :
119 # PERL5LIB=/root/CPANPLUS-Dist-Gentoo-0.09/blib/lib samples/g-cpanp CPANPLUS::Dist::Gentoo
120 # emerge -tv CPANPLUS-Dist-Gentoo
126 This module inherits all the methods from L<CPANPLUS::Dist::Base>.
127 Please refer to its documentation for precise information on what's done at each step.
131 use constant CATEGORY => 'perl-gcpanp';
134 my $default_keywords;
147 my $format_available;
149 sub format_available {
150 return $format_available if defined $format_available;
152 for my $prog (qw/emerge ebuild/) {
153 unless (IPC::Cmd::can_run($prog)) {
154 __PACKAGE__->_abort("$prog is required to write ebuilds");
155 return $format_available = 0;
159 if (IPC::Cmd->can_capture_buffer) {
161 my ($success, $errmsg) = IPC::Cmd::run(
162 command => [ qw/emerge --info/ ],
167 if ($buffers =~ /^PORTDIR_OVERLAY=(.*)$/m) {
168 $overlays = [ map Cwd::abs_path($_), split ' ', $unquote->($1) ];
170 if ($buffers =~ /^ACCEPT_KEYWORDS=(.*)$/m) {
171 $default_keywords = [ split ' ', $unquote->($1) ];
173 if ($buffers =~ /^DISTDIR=(.*)$/m) {
174 $default_distdir = Cwd::abs_path($unquote->($1));
176 if ($buffers =~ /^PORTDIR=(.*)$/m) {
177 $main_portdir = Cwd::abs_path($unquote->($1));
180 __PACKAGE__->_abort($errmsg);
184 $default_keywords = [ 'x86' ] unless defined $default_keywords;
185 $default_distdir = '/usr/portage/distfiles' unless defined $default_distdir;
187 return $format_available = 1;
192 my $stat = $self->status;
193 my $conf = $self->parent->parent->configure_object;
195 $stat->mk_accessors(qw/name version author distribution desc uri src license
196 fetched_arch requires
197 ebuild_name ebuild_version ebuild_dir ebuild_file
199 overlay distdir keywords do_manifest header footer
202 $stat->force($conf->get_conf('force'));
203 $stat->verbose($conf->get_conf('verbose'));
210 my $mod = $self->parent;
211 my $stat = $self->status;
212 my $int = $mod->parent;
213 my $conf = $int->configure_object;
217 my $OK = sub { $stat->prepared(1); 1 };
218 my $FAIL = sub { $stat->prepared(0); $self->_abort(@_) if @_; 0 };
219 my $SKIP = sub { $stat->prepared(1); $stat->created(1); $self->_skip(@_) if @_; 1 };
221 my $keywords = delete $opts{keywords};
222 if (defined $keywords) {
223 $keywords = [ split ' ', $keywords ];
225 $keywords = $default_keywords;
227 $stat->keywords($keywords);
229 my $manifest = delete $opts{manifest};
230 $manifest = 1 unless defined $manifest;
231 $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
232 $stat->do_manifest($manifest);
234 my $header = delete $opts{header};
235 if (defined $header) {
236 1 while chomp $header;
239 my $year = (localtime)[5] + 1900;
240 $header = <<" DEF_HEADER";
241 # Copyright 1999-$year Gentoo Foundation
242 # Distributed under the terms of the GNU General Public License v2
246 $stat->header($header);
248 my $footer = delete $opts{footer};
249 if (defined $footer) {
250 $footer = "\n" . $footer;
254 $stat->footer($footer);
256 my $overlay = delete $opts{overlay};
257 $overlay = (defined $overlay) ? Cwd::abs_path($overlay) : '/usr/local/portage';
258 $stat->overlay($overlay);
260 my $distdir = delete $opts{distdir};
261 $distdir = (defined $distdir) ? Cwd::abs_path($distdir) : $default_distdir;
262 $stat->distdir($distdir);
264 return $FAIL->("distdir isn't writable") if $stat->do_manifest && !-w $distdir;
266 $stat->fetched_arch($mod->status->fetch);
268 my $cur = File::Spec->curdir();
271 if ($_ eq $overlay or File::Spec->abs2rel($overlay, $_) eq $cur) {
272 $portdir_overlay = [ @$overlays ];
276 $portdir_overlay = [ @$overlays, $overlay ] unless $portdir_overlay;
277 $stat->portdir_overlay($portdir_overlay);
279 my $name = $mod->package_name;
282 my $version = $mod->package_version;
283 $stat->version($version);
285 my $author = $mod->author->cpanid;
286 $stat->author($author);
288 $stat->distribution($name . '-' . $version);
290 $stat->ebuild_version(CPANPLUS::Dist::Gentoo::Maps::version_c2g($version));
292 $stat->ebuild_name(CPANPLUS::Dist::Gentoo::Maps::name_c2g($name));
294 $stat->ebuild_dir(File::Spec->catdir(
300 my $file = File::Spec->catfile(
302 $stat->ebuild_name . '-' . $stat->ebuild_version . '.ebuild',
304 $stat->ebuild_file($file);
307 # Always generate an ebuild in our category when forcing
308 if ($forced{$file}) {
310 return $SKIP->('Ebuild already forced for', $stat->distribution);
316 return $SKIP->("Can't force rewriting of $file");
318 1 while unlink $file;
321 if (my $atom = $self->_cpan2portage($name, $version)) {
322 $stat->dist($atom->ebuild);
323 return $SKIP->('Ebuild already generated for', $stat->distribution);
329 $self->SUPER::prepare(@_);
331 return $FAIL->() unless $stat->prepared;
333 my $desc = $mod->description;
334 ($desc = $name) =~ s/-+/::/g unless $desc;
337 $stat->uri('http://search.cpan.org/dist/' . $name);
339 $author =~ /^(.)(.)/ or return $FAIL->('Wrong author name');
340 $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/" . $mod->package);
342 $stat->license($self->intuit_license);
344 my $prereqs = $mod->status->requires;
346 for my $prereq (sort keys %$prereqs) {
347 next if $prereq =~ /^perl(?:-|\z)/;
348 my $obj = $int->module_tree($prereq);
349 next unless $obj; # Not in the module tree (e.g. Config)
350 next if $obj->package_is_perl_core;
353 if ($prereqs->{$prereq}) {
354 if ($obj->installed_version && $obj->installed_version < $obj->version) {
355 $version = $obj->installed_version;
357 $version = $obj->package_version;
360 push @requires, [ $obj->package_name, $version ];
363 $stat->requires(\@requires);
368 =head2 C<intuit_license>
370 Returns an array reference to a list of Gentoo licences identifiers under which the current distribution is released.
374 my %dslip_license = (
385 my $mod = $self->parent;
387 my $dslip = $mod->dslip;
388 if (defined $dslip and $dslip =~ /\S{4}(\S)/) {
389 my @licenses = CPANPLUS::Dist::Gentoo::Maps::license_c2g($dslip_license{$1});
390 return \@licenses if @licenses;
393 my $extract_dir = $mod->status->extract;
395 for my $meta_file (qw/META.json META.yml/) {
397 Parse::CPAN::Meta::LoadFile(File::Spec->catdir(
402 my $license = $meta->{license};
403 if (defined $license) {
404 my @licenses = CPANPLUS::Dist::Gentoo::Maps::license_c2g($license);
405 return \@licenses if @licenses;
409 return [ CPANPLUS::Dist::Gentoo::Maps::license_c2g('perl') ];
414 my $stat = $self->status;
420 $stat->dist($file) if defined $file;
427 $self->_abort(@_) if @_;
428 if (defined $file and -f $file) {
429 1 while unlink $file;
434 unless ($stat->prepared) {
436 'Can\'t create', $stat->distribution, 'since it was never prepared'
440 if ($stat->created) {
441 $self->_skip($stat->distribution, 'was already created');
442 $file = $stat->dist; # Keep the existing one.
446 my $dir = $stat->ebuild_dir;
448 eval { File::Path::mkpath($dir) };
449 return $FAIL->("mkpath($dir): $@") if $@;
452 $file = $stat->ebuild_file;
454 # Create a placeholder ebuild to prevent recursion with circular dependencies.
456 open my $eb, '>', $file or return $FAIL->("open($file): $!");
457 print $eb "PLACEHOLDER\n";
463 $self->SUPER::create(@_);
465 return $FAIL->() unless $stat->created;
468 open my $eb, '>', $file or return $FAIL->("open($file): $!");
469 my $source = $self->ebuild_source;
470 return $FAIL->() unless defined $source;
474 return $FAIL->() if $stat->do_manifest and not $self->update_manifest;
479 =head2 C<update_manifest>
481 Updates the F<Manifest> file for the ebuild associated to the current dist object.
485 sub update_manifest {
487 my $stat = $self->status;
489 my $file = $stat->ebuild_file;
490 unless (defined $file and -e $file) {
491 return $self->_abort('The ebuild file is invalid or does not exist');
494 unless (File::Copy::copy($stat->fetched_arch => $stat->distdir)) {
495 return $self->_abort("Couldn\'t copy the distribution file to distdir ($!)");
498 $self->_notify('Adding Manifest entry for', $stat->distribution);
500 return $self->_run([ 'ebuild', $file, 'manifest' ], 0);
503 =head2 C<ebuild_source>
505 Returns the source of the ebuild for the current dist object, or C<undef> when one of the dependencies couldn't be mapped to an existing ebuild.
511 my $stat = $self->status;
513 # We must resolve the deps now and not inside prepare because _cpan2portage
514 # has to see the ebuilds already generated for the dependencies of the current
517 for (@{$stat->requires}) {
518 my $atom = $self->_cpan2portage(@$_);
519 unless (defined $atom) {
521 "Couldn't find an appropriate ebuild for $_->[0] in the portage tree"
525 push @requires, $atom;
528 my $perl = CPANPLUS::Dist::Gentoo::Atom->new(
529 category => 'dev-lang',
532 @requires = CPANPLUS::Dist::Gentoo::Atom->fold($perl, @requires);
534 my $d = $stat->header;
535 $d .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
536 $d .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
537 $d .= 'S="${WORKDIR}/' . $stat->distribution . "\"\n";
538 $d .= 'DESCRIPTION="' . $stat->desc . "\"\n";
539 $d .= 'HOMEPAGE="' . $stat->uri . "\"\n";
540 $d .= 'SRC_URI="' . $stat->src . "\"\n";
541 $d .= "SLOT=\"0\"\n";
542 $d .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
543 $d .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
544 $d .= 'RDEPEND="' . join("\n", sort @requires) . "\"\n";
545 $d .= "DEPEND=\"\${RDEPEND}\"\n";
546 $d .= "SRC_TEST=\"do\"\n";
553 my ($self, $name, $version) = @_;
555 $name = CPANPLUS::Dist::Gentoo::Maps::name_c2g($name);
556 $version = CPANPLUS::Dist::Gentoo::Maps::version_c2g($version);
558 my @portdirs = ($main_portdir, @{$self->status->portdir_overlay});
560 for my $category (qw/virtual perl-core dev-perl perl-gcpan/, CATEGORY) {
561 my $name = ($category eq 'virtual' ? 'perl-' : '') . $name;
563 for my $portdir (@portdirs) {
564 my @ebuilds = glob File::Spec->catfile(
571 my $last = reduce { $a < $b ? $b : $a } # handles overloading
572 map CPANPLUS::Dist::Gentoo::Atom->new_from_ebuild($_),
574 next if defined $version and $last < $version;
576 return CPANPLUS::Dist::Gentoo::Atom->new(
577 category => $last->category,
579 (defined $version ? (version => $version, range => '>=') : ()),
580 ebuild => $last->ebuild,
591 my $stat = $self->status;
592 my $conf = $self->parent->parent->configure_object;
594 my $sudo = $conf->get_program('sudo');
595 my @cmd = ('emerge', '=' . $stat->ebuild_name . '-' . $stat->ebuild_version);
596 unshift @cmd, $sudo if $sudo;
598 my $success = $self->_run(\@cmd, 1);
599 $stat->installed($success);
606 my $stat = $self->status;
607 my $conf = $self->parent->parent->configure_object;
609 my $sudo = $conf->get_program('sudo');
610 my @cmd = ('emerge', '-C', '=' . $stat->ebuild_name . '-' . $stat->ebuild_version);
611 unshift @cmd, $sudo if $sudo;
613 my $success = $self->_run(\@cmd, 1);
614 $stat->uninstalled($success);
620 my ($self, $cmd, $verbose) = @_;
621 my $stat = $self->status;
623 my ($success, $errmsg, $output) = do {
624 local $ENV{PORTDIR_OVERLAY} = join ' ', @{$stat->portdir_overlay};
625 local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
633 $self->_abort($errmsg);
634 if (not $verbose and defined $output and $stat->verbose) {
635 my $msg = join '', @$output;
637 CPANPLUS::Error::error($msg);
647 CPANPLUS::Error::error("@_ -- aborting");
655 CPANPLUS::Error::msg("@_");
660 sub _skip { shift->_notify(@_, '-- skipping') }
664 Gentoo (L<http://gentoo.org>).
666 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5), L<Parse::CPAN::Meta> (since 5.10.1).
668 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).
674 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
678 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
680 You can contact me by mail or on C<irc.perl.org> (vincent).
684 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>.
685 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
689 You can find documentation for this module with the perldoc command.
691 perldoc CPANPLUS::Dist::Gentoo
693 =head1 ACKNOWLEDGEMENTS
695 The module was inspired by L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
697 Kent Fredric, for testing and suggesting improvements.
699 =head1 COPYRIGHT & LICENSE
701 Copyright 2008,2009,2010 Vincent Pit, all rights reserved.
703 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
707 1; # End of CPANPLUS::Dist::Gentoo