1 package CPANPLUS::Dist::Gentoo;
11 use IPC::Cmd qw/run can_run/;
13 use CPANPLUS::Error ();
15 use base qw/CPANPLUS::Dist::Base/;
17 use CPANPLUS::Dist::Gentoo::Maps;
21 CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
29 our $VERSION = '0.07';
33 cpan2dist --format=CPANPLUS::Dist::Gentoo \
34 --dist-opts overlay=/usr/local/portage \
35 --dist-opts distdir=/usr/portage/distfiles \
36 --dist-opts manifest=yes \
37 --dist-opts keywords=x86 \
38 --dist-opts header="# Copyright 1999-2008 Gentoo Foundation" \
39 --dist-opts footer="# End" \
44 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.
46 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>.
50 After installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
54 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.
58 use constant CATEGORY => 'perl-gcpanp';
76 sub format_available {
77 return $format_available if defined $format_available;
79 for my $prog (qw/emerge ebuild/) {
80 unless (can_run($prog)) {
81 __PACKAGE__->_abort("$prog is required to write ebuilds");
82 return $format_available = 0;
86 if (IPC::Cmd->can_capture_buffer) {
88 my ($success, $errmsg) = run command => [ qw/emerge --info/ ],
92 if ($buffers =~ /^PORTDIR_OVERLAY=(.*)$/m) {
93 $overlays = [ map abs_path($_), split ' ', $unquote->($1) ];
95 if ($buffers =~ /^ACCEPT_KEYWORDS=(.*)$/m) {
96 $default_keywords = [ split ' ', $unquote->($1) ];
98 if ($buffers =~ /^DISTDIR=(.*)$/m) {
99 $default_distdir = abs_path($unquote->($1));
101 if ($buffers =~ /^PORTDIR=(.*)$/m) {
102 $main_portdir = abs_path($unquote->($1));
105 __PACKAGE__->_abort($errmsg);
109 $default_keywords = [ 'x86' ] unless defined $default_keywords;
110 $default_distdir = '/usr/portage/distfiles' unless defined $default_distdir;
112 return $format_available = 1;
117 my $stat = $self->status;
118 my $conf = $self->parent->parent->configure_object;
120 $stat->mk_accessors(qw/name version author distribution desc uri src license
121 deps eb_name eb_version eb_dir eb_file fetched_arch
123 overlay distdir keywords do_manifest header footer
126 $stat->force($conf->get_conf('force'));
127 $stat->verbose($conf->get_conf('verbose'));
134 my $mod = $self->parent;
135 my $stat = $self->status;
136 my $int = $mod->parent;
137 my $conf = $int->configure_object;
141 my $OK = sub { $stat->prepared(1); 1 };
142 my $FAIL = sub { $stat->prepared(0); $self->_abort(@_) if @_; 0 };
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 return $FAIL->('distdir isn\'t writable');
185 $stat->fetched_arch($mod->status->fetch);
187 my $cur = File::Spec->curdir();
190 if ($_ eq $overlay or File::Spec->abs2rel($overlay, $_) eq $cur) {
191 $portdir_overlay = [ @$overlays ];
195 $portdir_overlay = [ @$overlays, $overlay ] unless $portdir_overlay;
196 $stat->portdir_overlay($portdir_overlay);
198 my $name = $mod->package_name;
201 my $version = $mod->package_version;
202 $stat->version($version);
204 my $author = $mod->author->cpanid;
205 $stat->author($author);
207 $stat->distribution($name . '-' . $version);
209 $stat->eb_version(CPANPLUS::Dist::Gentoo::Maps::version_c2g($version));
211 $stat->eb_name(CPANPLUS::Dist::Gentoo::Maps::name_c2g($name));
213 $stat->eb_dir(File::Spec->catdir(
219 my $file = File::Spec->catfile(
221 $stat->eb_name . '-' . $stat->eb_version . '.ebuild',
223 $stat->eb_file($file);
227 if ($stat->force and not $forced{$file}) {
229 1 while unlink $file;
233 $self->_skip("Can't force rewriting of $file");
236 $self->_skip('Ebuild already generated for', $stat->distribution);
246 $self->SUPER::prepare(%opts);
250 my $desc = $mod->description;
251 ($desc = $name) =~ s/-+/::/g unless $desc;
254 $stat->uri('http://search.cpan.org/dist/' . $name);
256 unless ($author =~ /^(.)(.)/) {
257 return $FAIL->('Wrong author name');
259 $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/"
262 $stat->license([ qw/Artistic GPL-2/ ]);
264 my $prereqs = $mod->status->prereqs;
266 for my $prereq (sort keys %$prereqs) {
267 next if $prereq =~ /^perl(?:-|\z)/;
268 my $obj = $int->module_tree($prereq);
269 return $FAIL->('Wrong module object') unless $obj;
270 next if $obj->package_is_perl_core;
273 if ($prereqs->{$prereq}) {
274 if ($obj->installed_version && $obj->installed_version < $obj->version) {
275 $version = $obj->installed_version;
277 $version = $obj->package_version;
280 push @depends, [ $obj->package_name, $version ];
283 $stat->deps(\@depends);
290 my $stat = $self->status;
292 my $OK = sub { $stat->created(1); $stat->dist($stat->eb_file); 1 };
293 my $FAIL = sub { $stat->created(0); $stat->dist(undef); $self->_abort(@_) if @_; 0 };
295 unless ($stat->prepared) {
297 'Can\'t create', $stat->distribution, 'since it was never prepared'
301 if ($stat->created) {
302 $self->_skip($stat->distribution, 'was already created');
306 my $dir = $stat->eb_dir;
308 eval { File::Path::mkpath($dir) };
309 return $FAIL->("mkpath($dir): $@") if $@;
314 my $d = $stat->header;
315 $d .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
316 $d .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
317 $d .= 'S="${WORKDIR}/' . $stat->distribution . "\"\n";
318 $d .= 'DESCRIPTION="' . $stat->desc . "\"\n";
319 $d .= 'HOMEPAGE="' . $stat->uri . "\"\n";
320 $d .= 'SRC_URI="' . $stat->src . "\"\n";
321 $d .= "SLOT=\"0\"\n";
322 $d .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
323 $d .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
324 $d .= 'DEPEND="' . join("\n",
325 sort grep !$seen{$_}++, 'dev-lang/perl',
326 map $self->_cpan2portage(@$_), @{$stat->deps}
328 $d .= "SRC_TEST=\"do\"\n";
331 my $file = $stat->eb_file;
332 open my $eb, '>', $file or return $FAIL->("open($file): $!");
339 $self->SUPER::create(@_);
341 if ($stat->do_manifest and not $self->update_manifest) {
342 1 while unlink $file;
349 =head2 C<update_manifest>
351 Update the F<Manifest> file for the ebuild associated to the current dist object.
355 sub update_manifest {
357 my $stat = $self->status;
359 my $file = $stat->eb_file;
360 unless ($file and -e $file) {
361 return $self->_abort('The ebuild file is invalid or does not exist');
364 unless (File::Copy::copy($stat->fetched_arch => $stat->distdir)) {
365 return $self->_abort("Couldn\'t copy the distribution file to distdir ($!)");
368 $self->_notify('Adding Manifest entry for', $stat->distribution);
370 return $self->_run([ 'ebuild', $stat->eb_file, 'manifest' ], 0);
374 my ($self, $name, $version) = @_;
376 $name = CPANPLUS::Dist::Gentoo::Maps::name_c2g($name);
378 $ver = CPANPLUS::Dist::Gentoo::Maps::version_c2g($version) if defined $version;
380 my @portdirs = ($main_portdir, @{$self->status->portdir_overlay});
382 for my $category (qw/virtual perl-core dev-perl perl-gcpan/, CATEGORY) {
383 my $atom = ($category eq 'virtual' ? 'perl-' : '') . $name;
385 for my $portdir (@portdirs) {
386 my @ebuilds = glob File::Spec->catfile(
393 if (defined $ver) { # implies that $version is defined
395 my ($eb_ver) = /\Q$atom\E-v?([\d._pr-]+).*?\.ebuild$/;
396 return ">=$category/$atom-$ver"
398 and CPANPLUS::Dist::Gentoo::Maps::version_gcmp($eb_ver, $ver) > 0;
401 return "$category/$atom";
409 "Couldn't find an appropriate ebuild for $name in the portage tree"
417 my $stat = $self->status;
418 my $conf = $self->parent->parent->configure_object;
420 my $sudo = $conf->get_program('sudo');
421 my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
422 unshift @cmd, $sudo if $sudo;
424 my $success = $self->_run(\@cmd, 1);
425 $stat->installed($success);
432 my $stat = $self->status;
433 my $conf = $self->parent->parent->configure_object;
435 my $sudo = $conf->get_program('sudo');
436 my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
437 unshift @cmd, $sudo if $sudo;
439 my $success = $self->_run(\@cmd, 1);
440 $stat->uninstalled($success);
446 my ($self, $cmd, $verbose) = @_;
447 my $stat = $self->status;
449 my ($success, $errmsg, $output) = do {
450 local $ENV{PORTDIR_OVERLAY} = join ' ', @{$stat->portdir_overlay};
451 local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
452 run command => $cmd, verbose => $verbose;
456 $self->_abort($errmsg);
457 if (not $verbose and defined $output and $stat->verbose) {
458 my $msg = join '', @$output;
460 CPANPLUS::Error::error($msg);
470 CPANPLUS::Error::error("@_ -- aborting");
478 CPANPLUS::Error::msg("@_");
483 sub _skip { shift->_notify(@_, '-- skipping') }
487 Gentoo (L<http://gentoo.org>).
489 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5).
491 L<Cwd>, L<Carp> (since perl 5), L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec> (5.00405).
497 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
501 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
503 You can contact me by mail or on C<irc.perl.org> (vincent).
507 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.
511 You can find documentation for this module with the perldoc command.
513 perldoc CPANPLUS::Dist::Gentoo
515 =head1 ACKNOWLEDGEMENTS
517 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
519 Kent Fredric, for testing and suggesting improvements.
521 =head1 COPYRIGHT & LICENSE
523 Copyright 2008-2009 Vincent Pit, all rights reserved.
525 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
529 1; # End of CPANPLUS::Dist::Gentoo