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::Guard;
21 use CPANPLUS::Dist::Gentoo::Maps;
25 CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
33 our $VERSION = '0.11';
37 # Using default values from your make.conf
38 cpan2dist --format=CPANPLUS::Dist::Gentoo --buildprereq Some::Module
40 # Specifying your own options
41 cpan2dist --format=CPANPLUS::Dist::Gentoo \
42 --dist-opts overlay=/usr/local/portage \
43 --dist-opts distdir=/usr/portage/distfiles \
44 --dist-opts manifest=yes \
45 --dist-opts keywords=x86 \
46 --dist-opts header="# Begin" \
47 --dist-opts footer="# End" \
52 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.
54 The generated ebuilds are placed into the C<perl-gcpanp> category.
55 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>.
59 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.
60 C<--dist-opts> can be used several times.
62 The valid option C<key>s are :
70 A boolean that indicates whether the F<Manifest> file should be generated by running C<ebuild manifest> onto the generated ebuilds.
78 A string formatted as a space-delimited sequence of paths, that lists the different overlays in which existent ebuilds will be looked for.
80 Defaults to the value of C<PORTDIR_OVERLAY> as returned by C<emerge --info> (usually F</usr/local/portage>).
86 The directory where C<ebuild> expects to find the source tarballs.
87 You need write permissions on this directory.
89 Defaults to the value of C<DISTDIR> as returned by C<emerge --info> (usually F</usr/portage/distfiles>).
95 The valid C<KEYWORDS> for the generated ebuilds.
97 Defaults to the value of C<ACCEPT_KEYWORDS> as returned by C<emerge --info>.
103 A chunk of text that is prepended to every ebuild.
105 Defaults to the generic Gentoo Foundation header.
111 A chunk of text that is appended to every ebuild.
117 L<cpan2dist> itself takes other options, most notably :
123 C<--buildprereq> generates an ebuild for every dependency, even for those that are already up-to-date.
124 Setting this option is recommended.
128 C<--force> forcefully regenerates ebuilds even if they already exist.
132 C<--install> installs the ebuilds after generating them.
136 C<--skiptest> skips tests while building, which speeds up the building process.
140 C<--verbose> shows a lot more information.
144 Please refer to L<cpan2dist> documentation for a complete coverage of its abilities.
148 Before installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
150 You have two ways for installing this module :
156 Use the perl overlay located at L<http://git.overlays.gentoo.org/gitweb/?p=proj/perl-overlay.git>.
157 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.
161 Bootstrap an ebuild for L<CPANPLUS::Dist::Gentoo> using itself.
163 First, make sure your system C<perl> is C<5.10> or greater, so that the L<CPANPLUS> toolchain is available.
166 This is perl 5, version 12, subversion 2 (v5.12.2)...
168 C<perl> C<5.12> is the current stable Perl version in Gentoo.
169 If you still have C<perl> C<5.8.x>, you can upgrade it by running the following commands as root :
171 # emerge -tv ">=dev-lang/perl-5.10"
174 Then, fetch the L<CPANPLUS::Dist::Gentoo> tarball :
177 $ wget http://search.cpan.org/CPAN/authors/id/V/VP/VPIT/CPANPLUS-Dist-Gentoo-0.11.tar.gz
179 Log in as root and unpack it in e.g. your home directory :
182 # tar xzf /tmp/CPANPLUS-Dist-Gentoo-0.11.tar.gz
183 # cd CPANPLUS-Dist-Gentoo-0.11
185 Bootstrap L<CPANPLUS::Dist::Gentoo> using the bundled shell script C<g-cpanp> :
189 # PERL5LIB=blib/lib samples/g-cpanp CPANPLUS::Dist::Gentoo
191 Finally, emerge the C<CPANPLUS-Dist-Gentoo> ebuild you've just generated :
193 # emerge -tv CPANPLUS-Dist-Gentoo
199 This module inherits all the methods from L<CPANPLUS::Dist::Base>.
200 Please refer to its documentation for precise information on what's done at each step.
204 use constant CATEGORY => 'perl-gcpanp';
207 my $default_keywords;
221 my $format_available;
223 sub format_available {
224 return $format_available if defined $format_available;
226 unless (IPC::Cmd->can_capture_buffer) {
227 my $msg = 'IPC::Cmd must be able to capture buffers.';
228 unless (do { local $@; eval { require IPC::Run; 1 } }) {
229 $msg .= ' Try installing IPC::Run (dev-perl/IPC-Run on Gentoo).';
231 __PACKAGE__->_abort($msg);
232 return $format_available = 0;
235 for my $prog (qw<emerge ebuild>) {
236 unless (IPC::Cmd::can_run($prog)) {
237 __PACKAGE__->_abort("$prog is required to write ebuilds");
238 return $format_available = 0;
244 my ($success, $errmsg) = IPC::Cmd::run(
245 command => [ qw<emerge --info> ],
250 if ($buffers =~ /^PORTDIR_OVERLAY=(.*)$/m) {
251 $overlays = [ map Cwd::abs_path($_), split ' ', $unquote->($1) ];
253 if ($buffers =~ /^ACCEPT_KEYWORDS=(.*)$/m) {
254 $default_keywords = [ split ' ', $unquote->($1) ];
256 if ($buffers =~ /^DISTDIR=(.*)$/m) {
257 $default_distdir = Cwd::abs_path($unquote->($1));
259 if ($buffers =~ /^PORTDIR=(.*)$/m) {
260 $main_portdir = Cwd::abs_path($unquote->($1));
263 __PACKAGE__->_abort($errmsg);
264 return $format_available = 0;
268 $default_keywords = [ 'x86' ] unless defined $default_keywords;
269 $default_distdir = '/usr/portage/distfiles' unless defined $default_distdir;
271 return $format_available = 1;
276 my $stat = $self->status;
277 my $conf = $self->parent->parent->configure_object;
279 $stat->mk_accessors(qw<
280 name version author distribution desc uri src license
283 requires configure_requires recursive_requires
284 ebuild_name ebuild_version ebuild_dir ebuild_file
285 portdir_overlay overlay distdir keywords do_manifest header footer
289 $stat->force($conf->get_conf('force'));
290 $stat->verbose($conf->get_conf('verbose'));
295 my $filter_prereqs = sub {
296 my ($int, $prereqs) = @_;
299 for my $prereq (sort keys %$prereqs) {
300 next if $prereq =~ /^perl(?:-|\z)/;
302 my $obj = $int->module_tree($prereq);
303 next unless $obj; # Not in the module tree (e.g. Config)
304 next if $obj->package_is_perl_core;
306 my $version = $prereqs->{$prereq} || undef;
308 push @requires, [ $obj->package_name, $version ];
316 my $mod = $self->parent;
317 my $stat = $self->status;
318 my $int = $mod->parent;
319 my $conf = $int->configure_object;
323 my $OK = sub { $stat->prepared(1); 1 };
324 my $FAIL = sub { $stat->prepared(0); $self->_abort(@_) if @_; 0 };
325 my $SKIP = sub { $stat->prepared(1); $stat->created(1); $self->_skip(@_) if @_; 1 };
327 my $keywords = delete $opts{keywords};
328 if (defined $keywords) {
329 $keywords = [ split ' ', $keywords ];
331 $keywords = $default_keywords;
333 $stat->keywords($keywords);
335 my $manifest = delete $opts{manifest};
336 $manifest = 1 unless defined $manifest;
337 $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
338 $stat->do_manifest($manifest);
340 my $header = delete $opts{header};
341 if (defined $header) {
342 1 while chomp $header;
345 my $year = (localtime)[5] + 1900;
346 $header = <<" DEF_HEADER";
347 # Copyright 1999-$year Gentoo Foundation
348 # Distributed under the terms of the GNU General Public License v2
352 $stat->header($header);
354 my $footer = delete $opts{footer};
355 if (defined $footer) {
356 $footer = "\n" . $footer;
360 $stat->footer($footer);
362 my $overlay = delete $opts{overlay};
363 $overlay = (defined $overlay) ? Cwd::abs_path($overlay) : '/usr/local/portage';
364 $stat->overlay($overlay);
366 my $distdir = delete $opts{distdir};
367 $distdir = (defined $distdir) ? Cwd::abs_path($distdir) : $default_distdir;
368 $stat->distdir($distdir);
370 return $FAIL->("distdir isn't writable") if $stat->do_manifest && !-w $distdir;
372 $stat->fetched_arch($mod->status->fetch);
374 my $cur = File::Spec->curdir();
377 if ($_ eq $overlay or File::Spec->abs2rel($overlay, $_) eq $cur) {
378 $portdir_overlay = [ @$overlays ];
382 $portdir_overlay = [ @$overlays, $overlay ] unless $portdir_overlay;
383 $stat->portdir_overlay($portdir_overlay);
385 my $name = $mod->package_name;
388 my $version = $mod->package_version;
389 $stat->version($version);
391 my $author = $mod->author->cpanid;
392 $stat->author($author);
394 $stat->distribution($name . '-' . $version);
396 $stat->ebuild_version(CPANPLUS::Dist::Gentoo::Maps::version_c2g($name, $version));
398 $stat->ebuild_name(CPANPLUS::Dist::Gentoo::Maps::name_c2g($name));
400 $stat->ebuild_dir(File::Spec->catdir(
406 my $file = File::Spec->catfile(
408 $stat->ebuild_name . '-' . $stat->ebuild_version . '.ebuild',
410 $stat->ebuild_file($file);
413 # Always generate an ebuild in our category when forcing
414 if ($forced{$file}) {
416 return $SKIP->('Ebuild already forced for', $stat->distribution);
422 return $SKIP->("Can't force rewriting of $file");
424 1 while unlink $file;
427 if (my $atom = $self->_cpan2portage($name, $version)) {
428 $stat->dist($atom->ebuild);
429 return $SKIP->('Ebuild already generated for', $stat->distribution);
435 $self->SUPER::prepare(@_);
437 return $FAIL->() unless $stat->prepared;
439 my $desc = $mod->description;
440 $desc = $mod->comment unless $desc;
441 $desc = "$name Perl distribution (provides " . $mod->module . ')'
443 $desc = substr($desc, 0, 77) . '...' if length $desc > 80;
446 $stat->uri('http://search.cpan.org/dist/' . $name);
448 $author =~ /^(.)(.)/ or return $FAIL->('Wrong author name');
449 $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/" . $mod->package);
451 $stat->license($self->intuit_license);
453 my $mstat = $mod->status;
454 $stat->configure_requires($int->$filter_prereqs($mstat->configure_requires));
455 $stat->requires($int->$filter_prereqs($mstat->requires));
456 $stat->recursive_requires([ ]);
458 $dependencies{$name} = [ map $_->[0], @{ $stat->requires } ];
460 my $meta = $self->meta;
461 $stat->min_perl(CPANPLUS::Dist::Gentoo::Maps::perl_version_c2g(
462 $meta->{requires}->{perl},
470 Returns the contents of the F<META.yml> or F<META.json> files as parsed by L<Parse::CPAN::Meta>.
476 my $mod = $self->parent;
477 my $stat = $self->status;
479 my $meta = $stat->meta;
480 return $meta if defined $meta;
482 my $extract_dir = $mod->status->extract;
484 for my $name (qw<META.json META.yml>) {
485 my $meta_file = File::Spec->catdir($extract_dir, $name);
486 next unless -e $meta_file;
489 my $meta = eval { Parse::CPAN::Meta::LoadFile($meta_file) };
499 =head2 C<intuit_license>
501 Returns an array reference to a list of Gentoo licences identifiers under which the current distribution is released.
505 my %dslip_license = (
516 my $mod = $self->parent;
518 my $dslip = $mod->dslip;
519 if (defined $dslip and $dslip =~ /\S{4}(\S)/) {
520 my @licenses = CPANPLUS::Dist::Gentoo::Maps::license_c2g($dslip_license{$1});
521 return \@licenses if @licenses;
524 my $meta = $self->meta;
525 my $license = $meta->{license};
526 if (defined $license) {
527 my @licenses = CPANPLUS::Dist::Gentoo::Maps::license_c2g($license);
528 return \@licenses if @licenses;
531 return [ CPANPLUS::Dist::Gentoo::Maps::license_c2g('perl') ];
536 my $stat = $self->status;
540 my $guard = CPANPLUS::Dist::Gentoo::Guard->new(sub {
541 if (defined $file and -e $file and -w _) {
542 1 while unlink $file;
546 my $SIG_INT = $SIG{INT};
547 local $SIG{INT} = sub {
550 eval { $SIG_INT->() };
559 $stat->dist($file) if defined $file;
566 $self->_abort(@_) if @_;
570 unless ($stat->prepared) {
572 'Can\'t create', $stat->distribution, 'since it was never prepared'
576 if ($stat->created) {
577 $self->_skip($stat->distribution, 'was already created');
578 $file = $stat->dist; # Keep the existing one.
582 my $dir = $stat->ebuild_dir;
584 eval { File::Path::mkpath($dir) };
585 return $FAIL->("mkpath($dir): $@") if $@;
588 $file = $stat->ebuild_file;
590 # Create a placeholder ebuild to prevent recursion with circular dependencies.
592 open my $eb, '>', $file or return $FAIL->("open($file): $!");
593 print $eb "PLACEHOLDER\n";
599 $self->SUPER::create(@_);
601 return $FAIL->() unless $stat->created;
604 open my $eb, '>', $file or return $FAIL->("open($file): $!");
605 my $source = $self->ebuild_source;
606 return $FAIL->() unless defined $source;
610 return $FAIL->() if $stat->do_manifest and not $self->update_manifest;
615 =head2 C<update_manifest>
617 Updates the F<Manifest> file for the ebuild associated to the current dist object.
621 sub update_manifest {
623 my $stat = $self->status;
625 my $file = $stat->ebuild_file;
626 unless (defined $file and -e $file) {
627 return $self->_abort('The ebuild file is invalid or does not exist');
630 unless (File::Copy::copy($stat->fetched_arch => $stat->distdir)) {
631 return $self->_abort("Couldn\'t copy the distribution file to distdir ($!)");
634 $self->_notify('Adding Manifest entry for', $stat->distribution);
636 return $self->_run([ 'ebuild', $file, 'manifest' ], 0);
639 =head2 C<ebuild_source>
641 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.
645 my $dep_tree_contains;
649 $dep_tree_contains = sub {
650 my ($dist, $target) = @_;
652 return 0 if $seen{$dist};
653 local $seen{$dist} = 1;
655 for my $kid (@{ $dependencies{$dist} }) {
656 return 1 if $kid eq $target
657 or $dep_tree_contains->($kid, $target);
666 my $stat = $self->status;
669 my $name = $stat->name;
670 my %recursive_kids = map { $_ => 1 }
671 grep $dep_tree_contains->($_, $name),
672 @{ $dependencies{$name} };
673 if (%recursive_kids) {
674 my (@requires, @recursive_requires);
675 for (@{ $stat->requires }) {
676 if ($recursive_kids{$_->[0]}) {
677 push @recursive_requires, $_;
682 $stat->requires(\@requires);
683 $stat->recursive_requires(\@recursive_requires);
687 # We must resolve the deps now and not inside prepare because _cpan2portage
688 # has to see the ebuilds already generated for the dependencies of the current
691 my (@configure_requires, @requires, @recursive_requires);
694 [ configure_requires => \@configure_requires ],
695 [ requires => \@requires ],
696 [ recursive_requires => \@recursive_requires ],
699 push @requires, CPANPLUS::Dist::Gentoo::Atom->new(
700 category => 'dev-lang',
702 version => $stat->min_perl,
706 my ($phase, $list) = @$_;
708 for (@{ $stat->$phase }) {
709 my $atom = $self->_cpan2portage(@$_);
710 unless (defined $atom) {
712 "Couldn't find an appropriate ebuild for $_->[0] in the portage tree"
720 @$list = CPANPLUS::Dist::Gentoo::Atom->fold(@$list);
723 my $d = $stat->header;
724 $d .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
725 $d .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
726 $d .= 'S="${WORKDIR}/' . $stat->distribution . "\"\n";
727 $d .= 'DESCRIPTION="' . $stat->desc . "\"\n";
728 $d .= 'HOMEPAGE="' . $stat->uri . "\"\n";
729 $d .= 'SRC_URI="' . $stat->src . "\"\n";
730 $d .= "SLOT=\"0\"\n";
731 $d .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
732 $d .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
733 $d .= 'RDEPEND="' . join("\n", sort @requires) . "\"\n" if @requires;
734 $d .= 'PDEPEND="' . join("\n", sort @recursive_requires) . "\"\n"
735 if @recursive_requires;
736 $d .= 'DEPEND="' . join("\n", '${RDEPEND}', sort @configure_requires) . "\"\n";
737 $d .= "SRC_TEST=\"do\"\n";
744 my ($self, $dist_name, $dist_version) = @_;
746 my $name = CPANPLUS::Dist::Gentoo::Maps::name_c2g($dist_name);
747 my $version = CPANPLUS::Dist::Gentoo::Maps::version_c2g($dist_name, $dist_version);
749 my @portdirs = ($main_portdir, @{$self->status->portdir_overlay});
751 for my $category (qw<virtual perl-core dev-perl perl-gcpan>, CATEGORY) {
752 my $name = ($category eq 'virtual' ? 'perl-' : '') . $name;
754 for my $portdir (@portdirs) {
755 my @ebuilds = glob File::Spec->catfile(
762 my $last = reduce { $a < $b ? $b : $a } # handles overloading
763 map CPANPLUS::Dist::Gentoo::Atom->new_from_ebuild($_),
765 next if defined $version and $last < $version;
767 return CPANPLUS::Dist::Gentoo::Atom->new(
768 category => $last->category,
771 ebuild => $last->ebuild,
782 my $stat = $self->status;
783 my $conf = $self->parent->parent->configure_object;
785 my $sudo = $conf->get_program('sudo');
786 my @cmd = ('emerge', '=' . $stat->ebuild_name . '-' . $stat->ebuild_version);
787 unshift @cmd, $sudo if $sudo;
789 my $success = $self->_run(\@cmd, 1);
790 $stat->installed($success);
797 my $stat = $self->status;
798 my $conf = $self->parent->parent->configure_object;
800 my $sudo = $conf->get_program('sudo');
801 my @cmd = ('emerge', '-C', '=' . $stat->ebuild_name . '-' . $stat->ebuild_version);
802 unshift @cmd, $sudo if $sudo;
804 my $success = $self->_run(\@cmd, 1);
805 $stat->uninstalled($success);
811 my ($self, $cmd, $verbose) = @_;
812 my $stat = $self->status;
814 my ($success, $errmsg, $output) = do {
815 local $ENV{PORTDIR_OVERLAY} = join ' ', @{$stat->portdir_overlay};
816 local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
824 $self->_abort($errmsg);
825 if (not $verbose and defined $output and $stat->verbose) {
826 my $msg = join '', @$output;
828 CPANPLUS::Error::error($msg);
838 CPANPLUS::Error::error("@_ -- aborting");
846 CPANPLUS::Error::msg("@_");
851 sub _skip { shift->_notify(@_, '-- skipping') }
855 Gentoo (L<http://gentoo.org>).
857 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5), L<Parse::CPAN::Meta> (since 5.10.1).
859 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).
865 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
869 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
871 You can contact me by mail or on C<irc.perl.org> (vincent).
875 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>.
876 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
880 You can find documentation for this module with the perldoc command.
882 perldoc CPANPLUS::Dist::Gentoo
884 =head1 ACKNOWLEDGEMENTS
886 The module was inspired by L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
888 Kent Fredric, for testing and suggesting improvements.
890 =head1 COPYRIGHT & LICENSE
892 Copyright 2008,2009,2010 Vincent Pit, all rights reserved.
894 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
898 1; # End of CPANPLUS::Dist::Gentoo