1 package CPANPLUS::Dist::Gentoo;
7 use List::Util qw<reduce>;
14 use Parse::CPAN::Meta ();
16 use CPANPLUS::Error ();
18 use base qw<CPANPLUS::Dist::Base>;
20 use CPANPLUS::Dist::Gentoo::Atom;
21 use CPANPLUS::Dist::Gentoo::Guard;
22 use CPANPLUS::Dist::Gentoo::Maps;
26 CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
34 our $VERSION = '0.11';
38 # Using default values from your make.conf
39 cpan2dist --format=CPANPLUS::Dist::Gentoo --buildprereq Some::Module
41 # Specifying your own options
42 cpan2dist --format=CPANPLUS::Dist::Gentoo \
43 --dist-opts overlay=/usr/local/portage \
44 --dist-opts distdir=/usr/portage/distfiles \
45 --dist-opts manifest=yes \
46 --dist-opts keywords=x86 \
47 --dist-opts header="# Begin" \
48 --dist-opts footer="# End" \
53 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.
55 The generated ebuilds are placed into the C<perl-gcpanp> category.
56 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>.
57 Existing ebuilds will be searched into the main C<PORTDIR> portage tree and then into the overlays listed in C<PORTDIR_OVERLAY>.
61 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.
62 C<--dist-opts> can be used several times.
64 The valid option C<key>s are :
72 A boolean that indicates whether the F<Manifest> file should be generated by running C<ebuild manifest> onto the generated ebuilds.
80 The path of the overlay in which the generated ebuilds will be written.
82 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.
88 The directory where C<ebuild> expects to find the source tarballs.
89 You need write permissions on this directory.
91 Defaults to the value of C<DISTDIR> (as returned by C<emerge --info>) or F</usr/portage/distfiles> if this variable is empty.
97 The valid C<KEYWORDS> for the generated ebuilds.
99 Defaults to the value of C<ACCEPT_KEYWORDS> (as returned by C<emerge --info>) or C<'x86'> if this variable is empty.
105 A chunk of text that is prepended to every ebuild.
107 Defaults to the generic Gentoo Foundation header.
113 A chunk of text that is appended to every ebuild.
119 L<cpan2dist> itself takes other options, most notably :
125 C<--buildprereq> generates an ebuild for every dependency, even for those that are already up-to-date.
126 Setting this option is recommended.
130 C<--force> forcefully regenerates ebuilds even if they already exist.
134 C<--install> installs the ebuilds after generating them.
138 C<--skiptest> skips tests while building, which speeds up the building process.
142 C<--verbose> shows a lot more information.
146 Please refer to L<cpan2dist> documentation for a complete coverage of its abilities.
150 Before installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
152 You have two ways for installing this module :
158 Use the perl overlay located at L<http://git.overlays.gentoo.org/gitweb/?p=proj/perl-overlay.git>.
159 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.
163 Bootstrap an ebuild for L<CPANPLUS::Dist::Gentoo> using itself.
165 First, make sure your system C<perl> is C<5.10> or greater, so that the L<CPANPLUS> toolchain is available.
168 This is perl 5, version 12, subversion 2 (v5.12.2)...
170 C<perl> C<5.12> is the current stable Perl version in Gentoo.
171 If you still have C<perl> C<5.8.x>, you can upgrade it by running the following commands as root :
173 # emerge -tv ">=dev-lang/perl-5.10"
176 Then, fetch the L<CPANPLUS::Dist::Gentoo> tarball :
179 $ wget http://search.cpan.org/CPAN/authors/id/V/VP/VPIT/CPANPLUS-Dist-Gentoo-0.11.tar.gz
181 Log in as root and unpack it in e.g. your home directory :
184 # tar xzf /tmp/CPANPLUS-Dist-Gentoo-0.11.tar.gz
185 # cd CPANPLUS-Dist-Gentoo-0.11
187 Bootstrap L<CPANPLUS::Dist::Gentoo> using the bundled shell script C<g-cpanp> :
191 # PERL5LIB=blib/lib samples/g-cpanp CPANPLUS::Dist::Gentoo
193 Finally, emerge the C<CPANPLUS-Dist-Gentoo> ebuild you've just generated :
195 # emerge -tv CPANPLUS-Dist-Gentoo
201 This module inherits all the methods from L<CPANPLUS::Dist::Base>.
202 Please refer to its documentation for precise information on what's done at each step.
206 use constant CATEGORY => 'perl-gcpanp';
209 my $default_keywords;
223 my $format_available;
225 sub format_available {
226 return $format_available if defined $format_available;
228 unless (IPC::Cmd->can_capture_buffer) {
229 my $msg = 'IPC::Cmd must be able to capture buffers.';
230 unless (do { local $@; eval { require IPC::Run; 1 } }) {
231 $msg .= ' Try installing IPC::Run (dev-perl/IPC-Run on Gentoo).';
233 __PACKAGE__->_abort($msg);
234 return $format_available = 0;
237 for my $prog (qw<emerge ebuild>) {
238 unless (IPC::Cmd::can_run($prog)) {
239 __PACKAGE__->_abort("$prog is required to write ebuilds");
240 return $format_available = 0;
246 my ($success, $errmsg) = IPC::Cmd::run(
247 command => [ qw<emerge --info> ],
252 if ($buffers =~ /^PORTDIR_OVERLAY=(.*)$/m) {
253 $overlays = [ map Cwd::abs_path($_), split ' ', $unquote->($1) ];
255 if ($buffers =~ /^ACCEPT_KEYWORDS=(.*)$/m) {
256 $default_keywords = [ split ' ', $unquote->($1) ];
258 if ($buffers =~ /^DISTDIR=(.*)$/m) {
259 $default_distdir = Cwd::abs_path($unquote->($1));
261 if ($buffers =~ /^PORTDIR=(.*)$/m) {
262 $main_portdir = Cwd::abs_path($unquote->($1));
265 __PACKAGE__->_abort($errmsg);
266 return $format_available = 0;
270 $default_keywords = [ 'x86' ] unless defined $default_keywords;
271 $default_distdir = '/usr/portage/distfiles' unless defined $default_distdir;
273 my $timestamp = CPANPLUS::Dist::Gentoo::Maps::get_portage_timestamp(
276 if (defined $timestamp) {
277 __PACKAGE__->_notify("Portage tree $main_portdir dates back from UNIX timestamp $timestamp");
279 __PACKAGE__->_notify("Unable to get timestamp for portage tree $main_portdir, using gmtime instead");
280 $timestamp = POSIX::mktime(gmtime);
282 if ($timestamp < CPANPLUS::Dist::Gentoo::Maps::TIMESTAMP) {
283 __PACKAGE__->_abort("Portage tree too old (please run emerge --sync and retry)");
284 return $format_available = 0;
287 return $format_available = 1;
292 my $stat = $self->status;
293 my $conf = $self->parent->parent->configure_object;
295 $stat->mk_accessors(qw<
296 name version author distribution desc uri src license
299 requires configure_requires recursive_requires
300 ebuild_name ebuild_version ebuild_dir ebuild_file
301 portdir_overlay overlay distdir keywords do_manifest header footer
305 $stat->force($conf->get_conf('force'));
306 $stat->verbose($conf->get_conf('verbose'));
311 my $filter_prereqs = sub {
312 my ($int, $prereqs) = @_;
315 for my $prereq (sort keys %$prereqs) {
316 next if $prereq =~ /^perl(?:-|\z)/;
318 my $obj = $int->module_tree($prereq);
319 next unless $obj; # Not in the module tree (e.g. Config)
320 next if $obj->package_is_perl_core;
322 my $version = $prereqs->{$prereq} || undef;
324 push @requires, [ $obj->package_name, $version ];
332 my $mod = $self->parent;
333 my $stat = $self->status;
334 my $int = $mod->parent;
335 my $conf = $int->configure_object;
339 my $OK = sub { $stat->prepared(1); 1 };
340 my $FAIL = sub { $stat->prepared(0); $self->_abort(@_) if @_; 0 };
341 my $SKIP = sub { $stat->prepared(1); $stat->created(1); $self->_skip(@_) if @_; 1 };
343 my $keywords = delete $opts{keywords};
344 if (defined $keywords) {
345 $keywords = [ split ' ', $keywords ];
347 $keywords = $default_keywords;
349 $stat->keywords($keywords);
351 my $manifest = delete $opts{manifest};
352 $manifest = 1 unless defined $manifest;
353 $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
354 $stat->do_manifest($manifest);
356 my $header = delete $opts{header};
357 if (defined $header) {
358 1 while chomp $header;
361 my $year = (localtime)[5] + 1900;
362 $header = <<" DEF_HEADER";
363 # Copyright 1999-$year Gentoo Foundation
364 # Distributed under the terms of the GNU General Public License v2
368 $stat->header($header);
370 my $footer = delete $opts{footer};
371 if (defined $footer) {
372 $footer = "\n" . $footer;
376 $stat->footer($footer);
378 my $overlay = delete $opts{overlay};
379 if (defined $overlay) {
380 $overlay = Cwd::abs_path($overlay);
382 $overlay = $overlays->[0];
383 $overlay = '/usr/local/portage' unless defined $overlay;
385 $stat->overlay($overlay);
387 my $distdir = delete $opts{distdir};
388 $distdir = (defined $distdir) ? Cwd::abs_path($distdir) : $default_distdir;
389 $stat->distdir($distdir);
391 return $FAIL->("distdir isn't writable") if $stat->do_manifest && !-w $distdir;
393 $stat->fetched_arch($mod->status->fetch);
395 my $cur = File::Spec->curdir();
398 if ($_ eq $overlay or File::Spec->abs2rel($overlay, $_) eq $cur) {
399 $portdir_overlay = [ @$overlays ];
403 $portdir_overlay = [ @$overlays, $overlay ] unless $portdir_overlay;
404 $stat->portdir_overlay($portdir_overlay);
406 my $name = $mod->package_name;
409 my $version = $mod->package_version;
410 $stat->version($version);
412 my $author = $mod->author->cpanid;
413 $stat->author($author);
415 $stat->distribution($name . '-' . $version);
417 $stat->ebuild_version(CPANPLUS::Dist::Gentoo::Maps::version_c2g($name, $version));
419 $stat->ebuild_name(CPANPLUS::Dist::Gentoo::Maps::name_c2g($name));
421 $stat->ebuild_dir(File::Spec->catdir(
427 my $file = File::Spec->catfile(
429 $stat->ebuild_name . '-' . $stat->ebuild_version . '.ebuild',
431 $stat->ebuild_file($file);
434 # Always generate an ebuild in our category when forcing
435 if ($forced{$file}) {
437 return $SKIP->('Ebuild already forced for', $stat->distribution);
443 return $SKIP->("Can't force rewriting of $file");
445 1 while unlink $file;
448 if (my $atom = $self->_cpan2portage($name, $version)) {
449 $stat->dist($atom->ebuild);
450 return $SKIP->('Ebuild already generated for', $stat->distribution);
456 $self->SUPER::prepare(@_);
458 return $FAIL->() unless $stat->prepared;
460 my $desc = $mod->description;
461 $desc = $mod->comment unless $desc;
462 $desc = "$name Perl distribution (provides " . $mod->module . ')'
464 $desc = substr($desc, 0, 77) . '...' if length $desc > 80;
467 $stat->uri('http://search.cpan.org/dist/' . $name);
469 $author =~ /^(.)(.)/ or return $FAIL->('Wrong author name');
470 $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/" . $mod->package);
472 $stat->license($self->intuit_license);
474 my $mstat = $mod->status;
475 $stat->configure_requires($int->$filter_prereqs($mstat->configure_requires));
476 $stat->requires($int->$filter_prereqs($mstat->requires));
477 $stat->recursive_requires([ ]);
479 $dependencies{$name} = [ map $_->[0], @{ $stat->requires } ];
481 my $meta = $self->meta;
482 $stat->min_perl(CPANPLUS::Dist::Gentoo::Maps::perl_version_c2g(
483 $meta->{requires}->{perl},
491 Returns the contents of the F<META.yml> or F<META.json> files as parsed by L<Parse::CPAN::Meta>.
497 my $mod = $self->parent;
498 my $stat = $self->status;
500 my $meta = $stat->meta;
501 return $meta if defined $meta;
503 my $extract_dir = $mod->status->extract;
505 for my $name (qw<META.json META.yml>) {
506 my $meta_file = File::Spec->catdir($extract_dir, $name);
507 next unless -e $meta_file;
510 my $meta = eval { Parse::CPAN::Meta::LoadFile($meta_file) };
520 =head2 C<intuit_license>
522 Returns an array reference to a list of Gentoo licences identifiers under which the current distribution is released.
526 my %dslip_license = (
537 my $mod = $self->parent;
539 my $dslip = $mod->dslip;
540 if (defined $dslip and $dslip =~ /\S{4}(\S)/) {
541 my @licenses = CPANPLUS::Dist::Gentoo::Maps::license_c2g($dslip_license{$1});
542 return \@licenses if @licenses;
545 my $meta = $self->meta;
546 my $license = $meta->{license};
547 if (defined $license) {
548 my @licenses = CPANPLUS::Dist::Gentoo::Maps::license_c2g($license);
549 return \@licenses if @licenses;
552 return [ CPANPLUS::Dist::Gentoo::Maps::license_c2g('perl') ];
557 my $stat = $self->status;
561 my $guard = CPANPLUS::Dist::Gentoo::Guard->new(sub {
562 if (defined $file and -e $file and -w _) {
563 1 while unlink $file;
567 my $SIG_INT = $SIG{INT};
568 local $SIG{INT} = sub {
571 eval { $SIG_INT->() };
580 $stat->dist($file) if defined $file;
587 $self->_abort(@_) if @_;
591 unless ($stat->prepared) {
593 'Can\'t create', $stat->distribution, 'since it was never prepared'
597 if ($stat->created) {
598 $self->_skip($stat->distribution, 'was already created');
599 $file = $stat->dist; # Keep the existing one.
603 my $dir = $stat->ebuild_dir;
605 eval { File::Path::mkpath($dir) };
606 return $FAIL->("mkpath($dir): $@") if $@;
609 $file = $stat->ebuild_file;
611 # Create a placeholder ebuild to prevent recursion with circular dependencies.
613 open my $eb, '>', $file or return $FAIL->("open($file): $!");
614 print $eb "PLACEHOLDER\n";
620 $self->SUPER::create(@_);
622 return $FAIL->() unless $stat->created;
625 open my $eb, '>', $file or return $FAIL->("open($file): $!");
626 my $source = $self->ebuild_source;
627 return $FAIL->() unless defined $source;
631 return $FAIL->() if $stat->do_manifest and not $self->update_manifest;
636 =head2 C<update_manifest>
638 Updates the F<Manifest> file for the ebuild associated to the current dist object.
642 sub update_manifest {
644 my $stat = $self->status;
646 my $file = $stat->ebuild_file;
647 unless (defined $file and -e $file) {
648 return $self->_abort('The ebuild file is invalid or does not exist');
651 unless (File::Copy::copy($stat->fetched_arch => $stat->distdir)) {
652 return $self->_abort("Couldn\'t copy the distribution file to distdir ($!)");
655 $self->_notify('Adding Manifest entry for', $stat->distribution);
657 return $self->_run([ 'ebuild', $file, 'manifest' ], 0);
660 =head2 C<ebuild_source>
662 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.
666 my $dep_tree_contains;
670 $dep_tree_contains = sub {
671 my ($dist, $target) = @_;
673 return 0 if $seen{$dist};
674 local $seen{$dist} = 1;
676 for my $kid (@{ $dependencies{$dist} }) {
677 return 1 if $kid eq $target
678 or $dep_tree_contains->($kid, $target);
687 my $stat = $self->status;
690 my $name = $stat->name;
691 my %recursive_kids = map { $_ => 1 }
692 grep $dep_tree_contains->($_, $name),
693 @{ $dependencies{$name} };
694 if (%recursive_kids) {
695 my (@requires, @recursive_requires);
696 for (@{ $stat->requires }) {
697 if ($recursive_kids{$_->[0]}) {
698 push @recursive_requires, $_;
703 $stat->requires(\@requires);
704 $stat->recursive_requires(\@recursive_requires);
708 # We must resolve the deps now and not inside prepare because _cpan2portage
709 # has to see the ebuilds already generated for the dependencies of the current
712 my (@configure_requires, @requires, @recursive_requires);
715 [ configure_requires => \@configure_requires ],
716 [ requires => \@requires ],
717 [ recursive_requires => \@recursive_requires ],
720 push @requires, CPANPLUS::Dist::Gentoo::Atom->new(
721 category => 'dev-lang',
723 version => $stat->min_perl,
727 my ($phase, $list) = @$_;
729 for (@{ $stat->$phase }) {
730 my $atom = $self->_cpan2portage(@$_);
731 unless (defined $atom) {
733 "Couldn't find an appropriate ebuild for $_->[0] in the portage tree"
741 @$list = CPANPLUS::Dist::Gentoo::Atom->fold(@$list);
744 my $d = $stat->header;
745 $d .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
746 $d .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
747 $d .= 'S="${WORKDIR}/' . $stat->distribution . "\"\n";
748 $d .= 'DESCRIPTION="' . $stat->desc . "\"\n";
749 $d .= 'HOMEPAGE="' . $stat->uri . "\"\n";
750 $d .= 'SRC_URI="' . $stat->src . "\"\n";
751 $d .= "SLOT=\"0\"\n";
752 $d .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
753 $d .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
754 $d .= 'RDEPEND="' . join("\n", sort @requires) . "\"\n" if @requires;
755 $d .= 'PDEPEND="' . join("\n", sort @recursive_requires) . "\"\n"
756 if @recursive_requires;
757 $d .= 'DEPEND="' . join("\n", '${RDEPEND}', sort @configure_requires) . "\"\n";
758 $d .= "SRC_TEST=\"do\"\n";
765 my ($self, $dist_name, $dist_version) = @_;
767 my $name = CPANPLUS::Dist::Gentoo::Maps::name_c2g($dist_name);
768 my $version = CPANPLUS::Dist::Gentoo::Maps::version_c2g($dist_name, $dist_version);
770 my @portdirs = ($main_portdir, @{$self->status->portdir_overlay});
772 for my $category (qw<virtual perl-core dev-perl perl-gcpan>, CATEGORY) {
773 my $name = ($category eq 'virtual' ? 'perl-' : '') . $name;
775 for my $portdir (@portdirs) {
776 my @ebuilds = glob File::Spec->catfile(
783 my $last = reduce { $a < $b ? $b : $a } # handles overloading
784 map CPANPLUS::Dist::Gentoo::Atom->new_from_ebuild($_),
786 next if defined $version and $last < $version;
788 return CPANPLUS::Dist::Gentoo::Atom->new(
789 category => $last->category,
792 ebuild => $last->ebuild,
803 my $stat = $self->status;
804 my $conf = $self->parent->parent->configure_object;
806 my $sudo = $conf->get_program('sudo');
807 my @cmd = ('emerge', '=' . $stat->ebuild_name . '-' . $stat->ebuild_version);
808 unshift @cmd, $sudo if $sudo;
810 my $success = $self->_run(\@cmd, 1);
811 $stat->installed($success);
818 my $stat = $self->status;
819 my $conf = $self->parent->parent->configure_object;
821 my $sudo = $conf->get_program('sudo');
822 my @cmd = ('emerge', '-C', '=' . $stat->ebuild_name . '-' . $stat->ebuild_version);
823 unshift @cmd, $sudo if $sudo;
825 my $success = $self->_run(\@cmd, 1);
826 $stat->uninstalled($success);
832 my ($self, $cmd, $verbose) = @_;
833 my $stat = $self->status;
835 my ($success, $errmsg, $output) = do {
836 local $ENV{PORTDIR_OVERLAY} = join ' ', @{$stat->portdir_overlay};
837 local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
845 $self->_abort($errmsg);
846 if (not $verbose and defined $output and $stat->verbose) {
847 my $msg = join '', @$output;
849 CPANPLUS::Error::error($msg);
859 CPANPLUS::Error::error("@_ -- aborting");
867 CPANPLUS::Error::msg("@_");
872 sub _skip { shift->_notify(@_, '-- skipping') }
876 Gentoo (L<http://gentoo.org>).
878 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5), L<Parse::CPAN::Meta> (since 5.10.1).
880 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).
886 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
890 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
892 You can contact me by mail or on C<irc.perl.org> (vincent).
896 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>.
897 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
901 You can find documentation for this module with the perldoc command.
903 perldoc CPANPLUS::Dist::Gentoo
905 =head1 ACKNOWLEDGEMENTS
907 The module was inspired by L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
909 Kent Fredric, for testing and suggesting improvements.
911 =head1 COPYRIGHT & LICENSE
913 Copyright 2008,2009,2010,2011 Vincent Pit, all rights reserved.
915 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
919 1; # End of CPANPLUS::Dist::Gentoo