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/;
20 CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
28 our $VERSION = '0.05';
32 cpan2dist --format=CPANPLUS::Dist::Gentoo \
33 --dist-opts overlay=/usr/local/portage \
34 --dist-opts distdir=/usr/portage/distfiles \
35 --dist-opts manifest=yes \
36 --dist-opts keywords=x86 \
37 --dist-opts header="# Copyright 1999-2008 Gentoo Foundation" \
38 --dist-opts footer="# End" \
43 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.
45 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>.
49 After installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
53 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.
57 use constant CATEGORY => 'perl-gcpanp';
75 sub format_available {
76 return $format_available if defined $format_available;
78 for my $prog (qw/emerge ebuild/) {
79 unless (can_run($prog)) {
80 error "$prog is required to write ebuilds -- aborting";
81 return $format_available = 0;
85 if (IPC::Cmd->can_capture_buffer) {
86 my ($success, $errmsg, $output) = run command => [ qw/emerge --info/ ],
89 for (@{$output || []}) {
90 if (/^PORTDIR_OVERLAY=(.*)$/m) {
91 $overlays = [ map abs_path($_), split ' ', _unquote($1) ];
93 if (/^ACCEPT_KEYWORDS=(.*)$/m) {
94 $default_keywords = [ split ' ', _unquote($1) ];
96 if (/^DISTDIR=(.*)$/m) {
97 $default_distdir = abs_path(_unquote($1));
99 if (/^PORTDIR=(.*)$/m) {
100 $main_portdir = abs_path(_unquote($1));
108 $default_keywords = [ 'x86' ] unless defined $default_keywords;
109 $default_distdir = '/usr/portage/distfiles' unless defined $default_distdir;
111 return $format_available = 1;
116 my $stat = $self->status;
117 my $conf = $self->parent->parent->configure_object;
119 $stat->mk_accessors(qw/name version author distribution desc uri src license
120 deps eb_name eb_version eb_dir eb_file fetched_arch
122 overlay distdir keywords do_manifest header footer
125 $stat->force($conf->get_conf('force'));
126 $stat->verbose($conf->get_conf('verbose'));
132 'ANSIColor' => 'Term-ANSIColor',
133 'Audio-CD' => 'Audio-CD-disc-cover',
134 'CGI-Simple' => 'Cgi-Simple',
135 'Cache-Mmap' => 'cache-mmap',
136 'Class-Loader' => 'class-loader',
137 'Class-ReturnValue' => 'class-returnvalue',
138 'Config-General' => 'config-general',
139 'Convert-ASCII-Armour' => 'convert-ascii-armour',
140 'Convert-PEM' => 'convert-pem',
141 'Crypt-CBC' => 'crypt-cbc',
142 'Crypt-DES_EDE3' => 'crypt-des-ede3',
143 'Crypt-DH' => 'crypt-dh',
144 'Crypt-DSA' => 'crypt-dsa',
145 'Crypt-IDEA' => 'crypt-idea',
146 'Crypt-Primes' => 'crypt-primes',
147 'Crypt-RSA' => 'crypt-rsa',
148 'Crypt-Random' => 'crypt-random',
149 'DBIx-SearchBuilder' => 'dbix-searchbuilder',
150 'Data-Buffer' => 'data-buffer',
151 'Digest' => 'digest-base',
152 'Digest-BubbleBabble' => 'digest-bubblebabble',
153 'Digest-MD2' => 'digest-md2',
154 'ExtUtils-Depends' => 'extutils-depends',
155 'ExtUtils-PkgConfig' => 'extutils-pkgconfig',
156 'Frontier-RPC' => 'frontier-rpc',
157 'Gimp' => 'gimp-perl',
158 'Glib' => 'glib-perl',
159 'Gnome2-Canvas' => 'gnome2-canvas',
160 'Gnome2-GConf' => 'gnome2-gconf',
161 'Gnome2-Print' => 'gnome2-print',
162 'Gnome2-VFS' => 'gnome2-vfs-perl',
163 'Gnome2-Wnck' => 'gnome2-wnck',
164 'Gtk2' => 'gtk2-perl',
165 'Gtk2-Ex-FormFactory' => 'gtk2-ex-formfactory',
166 'Gtk2-GladeXML' => 'gtk2-gladexml',
167 'Gtk2-Spell' => 'gtk2-spell',
168 'Gtk2-TrayIcon' => 'gtk2-trayicon',
169 'Gtk2-TrayManager' => 'gtk2-traymanager',
170 'Gtk2Fu' => 'gtk2-fu',
171 'I18N-LangTags' => 'i18n-langtags',
172 'Image-Info' => 'ImageInfo',
173 'Image-Size' => 'ImageSize',
174 'Inline-Files' => 'inline-files',
175 'Locale-Maketext' => 'locale-maketext',
176 'Locale-Maketext-Fuzzy' => 'locale-maketext-fuzzy',
177 'Locale-Maketext-Lexicon' => 'locale-maketext-lexicon',
178 'Log-Dispatch' => 'log-dispatch',
179 'Math-Pari' => 'math-pari',
180 'Module-Info' => 'module-info',
181 'Net-Ping' => 'net-ping',
182 'Net-SFTP' => 'net-sftp',
183 'Net-SSH-Perl' => 'net-ssh-perl',
184 'Net-Server' => 'net-server',
185 'OLE-Storage_Lite' => 'OLE-StorageLite',
186 'Ogg-Vorbis-Header' => 'ogg-vorbis-header',
187 'PathTools' => 'File-Spec',
188 'Pod-Parser' => 'PodParser',
189 'Regexp-Common' => 'regexp-common',
190 'SDL_Perl' => 'sdl-perl',
191 'Set-Scalar' => 'set-scalar',
192 'String-CRC32' => 'string-crc32',
193 'Text-Autoformat' => 'text-autoformat',
194 'Text-Reform' => 'text-reform',
195 'Text-Template' => 'text-template',
196 'Text-Wrapper' => 'text-wrapper',
197 'Tie-EncryptedHash' => 'tie-encryptedhash',
201 'gettext' => 'Locale-gettext',
202 'txt2html' => 'TextToHTML',
207 my $mod = $self->parent;
208 my $stat = $self->status;
209 my $int = $mod->parent;
210 my $conf = $int->configure_object;
216 my $keywords = delete $opts{'keywords'};
217 if (defined $keywords) {
218 $keywords = [ split ' ', $keywords ];
220 $keywords = $default_keywords;
222 $stat->keywords($keywords);
224 my $manifest = delete $opts{'manifest'};
225 $manifest = 1 unless defined $manifest;
226 $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
227 $stat->do_manifest($manifest);
229 my $header = delete $opts{'header'};
230 if (defined $header) {
231 1 while chomp $header;
236 $stat->header($header);
238 my $footer = delete $opts{'footer'};
239 if (defined $footer) {
240 $footer = "\n" . $footer;
244 $stat->footer($footer);
246 my $overlay = delete $opts{'overlay'};
247 $overlay = (defined $overlay) ? abs_path $overlay : '/usr/local/portage';
248 $stat->overlay($overlay);
250 my $distdir = delete $opts{'distdir'};
251 $distdir = (defined $distdir) ? abs_path $distdir : $default_distdir;
252 $stat->distdir($distdir);
254 if ($stat->do_manifest && !-w $stat->distdir) {
255 error 'distdir isn\'t writable -- aborting';
258 $stat->fetched_arch($mod->status->fetch);
260 my $cur = File::Spec::Functions::curdir();
263 if ($_ eq $overlay or File::Spec::Functions::abs2rel($overlay, $_) eq $cur) {
264 $portdir_overlay = [ @$overlays ];
268 $portdir_overlay = [ @$overlays, $overlay ] unless $portdir_overlay;
269 $stat->portdir_overlay($portdir_overlay);
271 my $name = $mod->package_name;
274 my $version = $mod->package_version;
275 $stat->version($version);
277 my $author = $mod->author->cpanid;
278 $stat->author($author);
280 $stat->distribution($name . '-' . $version);
282 $version =~ s/[^\d._]+//g;
283 $version =~ s/^[._]*//;
284 $version =~ s/[._]*$//;
285 $version =~ s/[._]*_[._]*/_/g;
287 ($version, my $patch, my @rest) = split /_/, $version;
288 $version .= '_p' . $patch if defined $patch;
289 $version .= join('.', '', @rest) if @rest;
291 $stat->eb_version($version);
293 $stat->eb_name($gentooism{$name} || $name);
295 $stat->eb_dir(catdir($stat->overlay, CATEGORY, $stat->eb_name));
297 my $file = catfile($stat->eb_dir,
298 $stat->eb_name . '-' . $stat->eb_version . '.ebuild');
299 $stat->eb_file($file);
303 if ($stat->force and not $forced{$file}) {
305 1 while unlink $file;
309 error "Can't force rewriting of $file -- skipping";
312 msg 'Ebuild already generated for ' . $stat->distribution . ' -- skipping';
322 $self->SUPER::prepare(%opts);
326 my $desc = $mod->description;
327 ($desc = $name) =~ s/-+/::/g unless $desc;
330 $stat->uri('http://search.cpan.org/dist/' . $name);
332 unless ($author =~ /^(.)(.)/) {
333 error 'Wrong author name -- aborting';
336 $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/"
339 $stat->license([ qw/Artistic GPL-2/ ]);
341 my $prereqs = $mod->status->prereqs;
343 for my $prereq (sort keys %$prereqs) {
344 next if $prereq =~ /^perl(?:-|\z)/;
345 my $obj = $int->module_tree($prereq);
347 error 'Wrong module object -- aborting';
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 @depends, [ $obj->package_name, $version ];
363 $stat->deps(\@depends);
371 my $stat = $self->status;
373 unless ($stat->prepared) {
374 error 'Can\'t create ' . $stat->distribution . ' since it was never prepared -- aborting';
380 if ($stat->created) {
381 msg $stat->distribution . ' was already created -- skipping';
382 $stat->dist($stat->eb_file);
389 $self->SUPER::create(@_);
394 my $dir = $stat->eb_dir;
396 eval { mkpath $dir };
398 error "mkpath($dir): $@";
405 my $d = $stat->header;
406 $d .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
407 $d .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
408 $d .= 'S="${WORKDIR}/' . $stat->distribution . "\"\n";
409 $d .= 'DESCRIPTION="' . $stat->desc . "\"\n";
410 $d .= 'HOMEPAGE="' . $stat->uri . "\"\n";
411 $d .= 'SRC_URI="' . $stat->src . "\"\n";
412 $d .= "SLOT=\"0\"\n";
413 $d .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
414 $d .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
415 $d .= 'DEPEND="' . join("\n",
416 sort grep !$seen{$_}++, 'dev-lang/perl',
417 map $self->_cpan2portage(@$_), @{$stat->deps}
419 $d .= "SRC_TEST=\"do\"\n";
422 my $file = $stat->eb_file;
423 open my $eb, '>', $file or do {
424 error "open($file): $! -- aborting";
430 if ($stat->do_manifest) {
431 unless (copy $stat->fetched_arch, $stat->distdir) {
432 error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
433 1 while unlink $file;
437 msg 'Adding Manifest entry for ' . $stat->distribution;
438 unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
439 1 while unlink $file;
450 my ($self, $name, $version) = @_;
452 $name = $gentooism{$name} || $name;
454 $ver = eval { version->new($version) } if defined $version;
456 my @portdirs = ($main_portdir, @{$self->status->portdir_overlay});
458 for my $category (qw/virtual perl-core dev-perl perl-gcpan/, CATEGORY) {
459 my $atom = ($category eq 'virtual' ? 'perl-' : '') . $name;
461 for my $portdir (@portdirs) {
462 my @ebuilds = glob catfile($portdir, $category, $atom,"$atom-*.ebuild");
463 next unless @ebuilds;
465 if (defined $ver) { # implies that $version is defined
467 next unless /\Q$atom\E-v?([\d._]+).*?\.ebuild$/;
468 my $eb_ver = eval { version->new($1) };
469 next unless defined $eb_ver and $eb_ver >= $ver;
470 return ">=$category/$atom-$version";
473 return "$category/$atom";
480 error "Couldn't find an appropriate ebuild for $name in the portage tree -- skipping";
486 my $stat = $self->status;
487 my $conf = $self->parent->parent->configure_object;
489 my $sudo = $conf->get_program('sudo');
490 my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
491 unshift @cmd, $sudo if $sudo;
493 my $success = $self->_run(\@cmd, 1);
494 $stat->installed($success);
501 my $stat = $self->status;
502 my $conf = $self->parent->parent->configure_object;
504 my $sudo = $conf->get_program('sudo');
505 my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
506 unshift @cmd, $sudo if $sudo;
508 my $success = $self->_run(\@cmd, 1);
509 $stat->uninstalled($success);
515 my ($self, $cmd, $verbose) = @_;
516 my $stat = $self->status;
518 my ($success, $errmsg, $output) = do {
519 local $ENV{PORTDIR_OVERLAY} = join ' ', @{$stat->portdir_overlay};
520 local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
521 run command => $cmd, verbose => $verbose;
525 error "$errmsg -- aborting";
526 if (not $verbose and defined $output and $self->status->verbose) {
527 my $msg = join '', @$output;
538 Gentoo (L<http://gentoo.org>).
540 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5), L<version> (since 5.009).
542 L<Cwd> (since perl 5) L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
548 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
552 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
554 You can contact me by mail or on C<irc.perl.org> (vincent).
558 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.
562 You can find documentation for this module with the perldoc command.
564 perldoc CPANPLUS::Dist::Gentoo
566 =head1 ACKNOWLEDGEMENTS
568 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
570 Kent Fredric, for testing and suggesting improvements.
572 =head1 COPYRIGHT & LICENSE
574 Copyright 2008-2009 Vincent Pit, all rights reserved.
576 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
580 1; # End of CPANPLUS::Dist::Gentoo