1 package CPANPLUS::Dist::Gentoo;
7 use File::Copy qw/copy/;
8 use File::Path qw/mkpath/;
9 use File::Spec::Functions qw/catdir catfile/;
11 use IPC::Cmd qw/run can_run/;
16 use base qw/CPANPLUS::Dist::Base/;
18 use CPANPLUS::Dist::Gentoo::Maps;
22 CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
30 our $VERSION = '0.05';
34 cpan2dist --format=CPANPLUS::Dist::Gentoo \
35 --dist-opts overlay=/usr/local/portage \
36 --dist-opts distdir=/usr/portage/distfiles \
37 --dist-opts manifest=yes \
38 --dist-opts keywords=x86 \
39 --dist-opts header="# Copyright 1999-2008 Gentoo Foundation" \
40 --dist-opts footer="# End" \
45 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.
47 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>.
51 After installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
55 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.
59 use constant CATEGORY => 'perl-gcpanp';
77 sub format_available {
78 return $format_available if defined $format_available;
80 for my $prog (qw/emerge ebuild/) {
81 unless (can_run($prog)) {
82 error "$prog is required to write ebuilds -- aborting";
83 return $format_available = 0;
87 if (IPC::Cmd->can_capture_buffer) {
88 my ($success, $errmsg, $output) = run command => [ qw/emerge --info/ ],
91 for (@{$output || []}) {
92 if (/^PORTDIR_OVERLAY=(.*)$/m) {
93 $overlays = [ map abs_path($_), split ' ', _unquote($1) ];
95 if (/^ACCEPT_KEYWORDS=(.*)$/m) {
96 $default_keywords = [ split ' ', _unquote($1) ];
98 if (/^DISTDIR=(.*)$/m) {
99 $default_distdir = abs_path(_unquote($1));
101 if (/^PORTDIR=(.*)$/m) {
102 $main_portdir = abs_path(_unquote($1));
110 $default_keywords = [ 'x86' ] unless defined $default_keywords;
111 $default_distdir = '/usr/portage/distfiles' unless defined $default_distdir;
113 return $format_available = 1;
118 my $stat = $self->status;
119 my $conf = $self->parent->parent->configure_object;
121 $stat->mk_accessors(qw/name version author distribution desc uri src license
122 deps eb_name eb_version eb_dir eb_file fetched_arch
124 overlay distdir keywords do_manifest header footer
127 $stat->force($conf->get_conf('force'));
128 $stat->verbose($conf->get_conf('verbose'));
135 my $mod = $self->parent;
136 my $stat = $self->status;
137 my $int = $mod->parent;
138 my $conf = $int->configure_object;
144 my $keywords = delete $opts{'keywords'};
145 if (defined $keywords) {
146 $keywords = [ split ' ', $keywords ];
148 $keywords = $default_keywords;
150 $stat->keywords($keywords);
152 my $manifest = delete $opts{'manifest'};
153 $manifest = 1 unless defined $manifest;
154 $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
155 $stat->do_manifest($manifest);
157 my $header = delete $opts{'header'};
158 if (defined $header) {
159 1 while chomp $header;
164 $stat->header($header);
166 my $footer = delete $opts{'footer'};
167 if (defined $footer) {
168 $footer = "\n" . $footer;
172 $stat->footer($footer);
174 my $overlay = delete $opts{'overlay'};
175 $overlay = (defined $overlay) ? abs_path $overlay : '/usr/local/portage';
176 $stat->overlay($overlay);
178 my $distdir = delete $opts{'distdir'};
179 $distdir = (defined $distdir) ? abs_path $distdir : $default_distdir;
180 $stat->distdir($distdir);
182 if ($stat->do_manifest && !-w $stat->distdir) {
183 error 'distdir isn\'t writable -- aborting';
186 $stat->fetched_arch($mod->status->fetch);
188 my $cur = File::Spec::Functions::curdir();
191 if ($_ eq $overlay or File::Spec::Functions::abs2rel($overlay, $_) eq $cur) {
192 $portdir_overlay = [ @$overlays ];
196 $portdir_overlay = [ @$overlays, $overlay ] unless $portdir_overlay;
197 $stat->portdir_overlay($portdir_overlay);
199 my $name = $mod->package_name;
202 my $version = $mod->package_version;
203 $stat->version($version);
205 my $author = $mod->author->cpanid;
206 $stat->author($author);
208 $stat->distribution($name . '-' . $version);
210 $version =~ s/[^\d._]+//g;
211 $version =~ s/^[._]*//;
212 $version =~ s/[._]*$//;
213 $version =~ s/[._]*_[._]*/_/g;
215 ($version, my $patch, my @rest) = split /_/, $version;
216 $version .= '_p' . $patch if defined $patch;
217 $version .= join('.', '', @rest) if @rest;
219 $stat->eb_version($version);
221 $stat->eb_name(CPANPLUS::Dist::Gentoo::Maps::name_c2g($name));
223 $stat->eb_dir(catdir($stat->overlay, CATEGORY, $stat->eb_name));
225 my $file = catfile($stat->eb_dir,
226 $stat->eb_name . '-' . $stat->eb_version . '.ebuild');
227 $stat->eb_file($file);
231 if ($stat->force and not $forced{$file}) {
233 1 while unlink $file;
237 error "Can't force rewriting of $file -- skipping";
240 msg 'Ebuild already generated for ' . $stat->distribution . ' -- skipping';
250 $self->SUPER::prepare(%opts);
254 my $desc = $mod->description;
255 ($desc = $name) =~ s/-+/::/g unless $desc;
258 $stat->uri('http://search.cpan.org/dist/' . $name);
260 unless ($author =~ /^(.)(.)/) {
261 error 'Wrong author name -- aborting';
264 $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/"
267 $stat->license([ qw/Artistic GPL-2/ ]);
269 my $prereqs = $mod->status->prereqs;
271 for my $prereq (sort keys %$prereqs) {
272 next if $prereq =~ /^perl(?:-|\z)/;
273 my $obj = $int->module_tree($prereq);
275 error 'Wrong module object -- aborting';
278 next if $obj->package_is_perl_core;
281 if ($prereqs->{$prereq}) {
282 if ($obj->installed_version && $obj->installed_version < $obj->version) {
283 $version = $obj->installed_version;
285 $version = $obj->package_version;
288 push @depends, [ $obj->package_name, $version ];
291 $stat->deps(\@depends);
299 my $stat = $self->status;
301 unless ($stat->prepared) {
302 error 'Can\'t create ' . $stat->distribution . ' since it was never prepared -- aborting';
308 if ($stat->created) {
309 msg $stat->distribution . ' was already created -- skipping';
310 $stat->dist($stat->eb_file);
314 my $dir = $stat->eb_dir;
316 eval { mkpath $dir };
318 error "mkpath($dir): $@";
325 my $d = $stat->header;
326 $d .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
327 $d .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
328 $d .= 'S="${WORKDIR}/' . $stat->distribution . "\"\n";
329 $d .= 'DESCRIPTION="' . $stat->desc . "\"\n";
330 $d .= 'HOMEPAGE="' . $stat->uri . "\"\n";
331 $d .= 'SRC_URI="' . $stat->src . "\"\n";
332 $d .= "SLOT=\"0\"\n";
333 $d .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
334 $d .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
335 $d .= 'DEPEND="' . join("\n",
336 sort grep !$seen{$_}++, 'dev-lang/perl',
337 map $self->_cpan2portage(@$_), @{$stat->deps}
339 $d .= "SRC_TEST=\"do\"\n";
342 my $file = $stat->eb_file;
343 open my $eb, '>', $file or do {
344 error "open($file): $! -- aborting";
353 $self->SUPER::create(@_);
358 if ($stat->do_manifest) {
359 unless (copy $stat->fetched_arch, $stat->distdir) {
360 error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
361 1 while unlink $file;
365 msg 'Adding Manifest entry for ' . $stat->distribution;
366 unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
367 1 while unlink $file;
378 my ($self, $name, $version) = @_;
380 $name = CPANPLUS::Dist::Gentoo::Maps::name_c2g($name);
382 $ver = eval { version->new($version) } if defined $version;
384 my @portdirs = ($main_portdir, @{$self->status->portdir_overlay});
386 for my $category (qw/virtual perl-core dev-perl perl-gcpan/, CATEGORY) {
387 my $atom = ($category eq 'virtual' ? 'perl-' : '') . $name;
389 for my $portdir (@portdirs) {
390 my @ebuilds = glob catfile($portdir, $category, $atom,"$atom-*.ebuild");
391 next unless @ebuilds;
393 if (defined $ver) { # implies that $version is defined
395 next unless /\Q$atom\E-v?([\d._]+).*?\.ebuild$/;
396 my $eb_ver = eval { version->new($1) };
397 next unless defined $eb_ver and $eb_ver >= $ver;
398 return ">=$category/$atom-$version";
401 return "$category/$atom";
408 error "Couldn't find an appropriate ebuild for $name in the portage tree -- skipping";
414 my $stat = $self->status;
415 my $conf = $self->parent->parent->configure_object;
417 my $sudo = $conf->get_program('sudo');
418 my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
419 unshift @cmd, $sudo if $sudo;
421 my $success = $self->_run(\@cmd, 1);
422 $stat->installed($success);
429 my $stat = $self->status;
430 my $conf = $self->parent->parent->configure_object;
432 my $sudo = $conf->get_program('sudo');
433 my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
434 unshift @cmd, $sudo if $sudo;
436 my $success = $self->_run(\@cmd, 1);
437 $stat->uninstalled($success);
443 my ($self, $cmd, $verbose) = @_;
444 my $stat = $self->status;
446 my ($success, $errmsg, $output) = do {
447 local $ENV{PORTDIR_OVERLAY} = join ' ', @{$stat->portdir_overlay};
448 local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
449 run command => $cmd, verbose => $verbose;
453 error "$errmsg -- aborting";
454 if (not $verbose and defined $output and $self->status->verbose) {
455 my $msg = join '', @$output;
466 Gentoo (L<http://gentoo.org>).
468 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5), L<version> (since 5.009).
470 L<Cwd> (since perl 5) L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
476 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
480 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
482 You can contact me by mail or on C<irc.perl.org> (vincent).
486 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.
490 You can find documentation for this module with the perldoc command.
492 perldoc CPANPLUS::Dist::Gentoo
494 =head1 ACKNOWLEDGEMENTS
496 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
498 Kent Fredric, for testing and suggesting improvements.
500 =head1 COPYRIGHT & LICENSE
502 Copyright 2008-2009 Vincent Pit, all rights reserved.
504 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
508 1; # End of CPANPLUS::Dist::Gentoo