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.04';
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';
71 sub format_available {
72 for my $prog (qw/emerge ebuild/) {
73 unless (can_run($prog)) {
74 error "$prog is required to write ebuilds -- aborting";
79 if (IPC::Cmd->can_capture_buffer) {
80 my ($success, $errmsg, $output) = run command => [ qw/emerge --info/ ],
83 for (@{$output || []}) {
84 if (/^PORTDIR_OVERLAY=(.*)$/m) {
85 $overlays = [ map abs_path($_), split ' ', _unquote($1) ];
87 if (/^ACCEPT_KEYWORDS=(.*)$/m) {
88 $default_keywords = [ split ' ', _unquote($1) ];
90 if (/^DISTDIR=(.*)$/m) {
91 $default_distdir = abs_path(_unquote($1));
93 if (/^PORTDIR=(.*)$/m) {
94 $main_portdir = abs_path(_unquote($1));
102 $default_keywords = [ 'x86' ] unless defined $default_keywords;
103 $default_distdir = '/usr/portage/distfiles' unless defined $default_distdir;
110 my $stat = $self->status;
111 my $conf = $self->parent->parent->configure_object;
113 $stat->mk_accessors(qw/name version author distribution desc uri license
114 deps eb_name eb_version eb_dir eb_file fetched_arch
116 overlay distdir keywords do_manifest header footer
119 $stat->force($conf->get_conf('force'));
120 $stat->verbose($conf->get_conf('verbose'));
126 'ANSIColor' => 'Term-ANSIColor',
127 'Audio-CD' => 'Audio-CD-disc-cover',
128 'CGI-Simple' => 'Cgi-Simple',
129 'Cache-Mmap' => 'cache-mmap',
130 'Class-Loader' => 'class-loader',
131 'Class-ReturnValue' => 'class-returnvalue',
132 'Config-General' => 'config-general',
133 'Convert-ASCII-Armour' => 'convert-ascii-armour',
134 'Convert-PEM' => 'convert-pem',
135 'Crypt-CBC' => 'crypt-cbc',
136 'Crypt-DES_EDE3' => 'crypt-des-ede3',
137 'Crypt-DH' => 'crypt-dh',
138 'Crypt-DSA' => 'crypt-dsa',
139 'Crypt-IDEA' => 'crypt-idea',
140 'Crypt-Primes' => 'crypt-primes',
141 'Crypt-RSA' => 'crypt-rsa',
142 'Crypt-Random' => 'crypt-random',
143 'DBIx-SearchBuilder' => 'dbix-searchbuilder',
144 'Data-Buffer' => 'data-buffer',
145 'Digest' => 'digest-base',
146 'Digest-BubbleBabble' => 'digest-bubblebabble',
147 'Digest-MD2' => 'digest-md2',
148 'ExtUtils-Depends' => 'extutils-depends',
149 'ExtUtils-PkgConfig' => 'extutils-pkgconfig',
150 'Frontier-RPC' => 'frontier-rpc',
151 'Gimp' => 'gimp-perl',
152 'Glib' => 'glib-perl',
153 'Gnome2-Canvas' => 'gnome2-canvas',
154 'Gnome2-GConf' => 'gnome2-gconf',
155 'Gnome2-Print' => 'gnome2-print',
156 'Gnome2-VFS' => 'gnome2-vfs-perl',
157 'Gnome2-Wnck' => 'gnome2-wnck',
158 'Gtk2' => 'gtk2-perl',
159 'Gtk2-Ex-FormFactory' => 'gtk2-ex-formfactory',
160 'Gtk2-GladeXML' => 'gtk2-gladexml',
161 'Gtk2-Spell' => 'gtk2-spell',
162 'Gtk2-TrayIcon' => 'gtk2-trayicon',
163 'Gtk2-TrayManager' => 'gtk2-traymanager',
164 'Gtk2Fu' => 'gtk2-fu',
165 'I18N-LangTags' => 'i18n-langtags',
166 'Image-Info' => 'ImageInfo',
167 'Image-Size' => 'ImageSize',
168 'Inline-Files' => 'inline-files',
169 'Locale-Maketext' => 'locale-maketext',
170 'Locale-Maketext-Fuzzy' => 'locale-maketext-fuzzy',
171 'Locale-Maketext-Lexicon' => 'locale-maketext-lexicon',
172 'Log-Dispatch' => 'log-dispatch',
173 'Math-Pari' => 'math-pari',
174 'Module-Info' => 'module-info',
175 'Net-Ping' => 'net-ping',
176 'Net-SFTP' => 'net-sftp',
177 'Net-SSH-Perl' => 'net-ssh-perl',
178 'Net-Server' => 'net-server',
179 'OLE-Storage_Lite' => 'OLE-StorageLite',
180 'Ogg-Vorbis-Header' => 'ogg-vorbis-header',
181 'PathTools' => 'File-Spec',
182 'Pod-Parser' => 'PodParser',
183 'Regexp-Common' => 'regexp-common',
184 'SDL_Perl' => 'sdl-perl',
185 'Set-Scalar' => 'set-scalar',
186 'String-CRC32' => 'string-crc32',
187 'Text-Autoformat' => 'text-autoformat',
188 'Text-Reform' => 'text-reform',
189 'Text-Template' => 'text-template',
190 'Text-Wrapper' => 'text-wrapper',
191 'Tie-EncryptedHash' => 'tie-encryptedhash',
195 'gettext' => 'Locale-gettext',
196 'txt2html' => 'TextToHTML',
201 my $mod = $self->parent;
202 my $stat = $self->status;
203 my $int = $mod->parent;
204 my $conf = $int->configure_object;
210 my $keywords = delete $opts{'keywords'};
211 if (defined $keywords) {
212 $keywords = [ split ' ', $keywords ];
214 $keywords = $default_keywords;
216 $stat->keywords($keywords);
218 my $manifest = delete $opts{'manifest'};
219 $manifest = 1 unless defined $manifest;
220 $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
221 $stat->do_manifest($manifest);
223 my $header = delete $opts{'header'};
224 if (defined $header) {
225 1 while chomp $header;
230 $stat->header($header);
232 my $footer = delete $opts{'footer'};
233 if (defined $footer) {
234 $footer = "\n" . $footer;
238 $stat->footer($footer);
240 my $overlay = delete $opts{'overlay'};
241 $overlay = (defined $overlay) ? abs_path $overlay : '/usr/local/portage';
242 $stat->overlay($overlay);
244 my $distdir = delete $opts{'distdir'};
245 $distdir = (defined $distdir) ? abs_path $distdir : $default_distdir;
246 $stat->distdir($distdir);
248 if ($stat->do_manifest && !-w $stat->distdir) {
249 error 'distdir isn\'t writable -- aborting';
252 $stat->fetched_arch($mod->status->fetch);
254 my $cur = File::Spec::Functions::curdir();
257 if ($_ eq $overlay or File::Spec::Functions::abs2rel($overlay, $_) eq $cur) {
258 $portdir_overlay = [ @$overlays ];
262 $portdir_overlay = [ @$overlays, $overlay ] unless $portdir_overlay;
263 $stat->portdir_overlay($portdir_overlay);
265 my $name = $mod->package_name;
268 my $version = $mod->package_version;
269 $stat->version($version);
271 my $author = $mod->author->cpanid;
272 $stat->author($author);
274 $stat->distribution($name . '-' . $version);
276 $version =~ s/[^\d._]+//g;
277 $version =~ s/^[._]*//;
278 $version =~ s/[._]*$//;
279 $version =~ s/[._]*_[._]*/_/g;
281 ($version, my $patch, my @rest) = split /_/, $version;
282 $version .= '_p' . $patch if defined $patch;
283 $version .= join('.', '', @rest) if @rest;
285 $stat->eb_version($version);
287 $stat->eb_name($gentooism{$name} || $name);
289 $stat->eb_dir(catdir($stat->overlay, CATEGORY, $stat->eb_name));
291 my $file = catfile($stat->eb_dir,
292 $stat->eb_name . '-' . $stat->eb_version . '.ebuild');
293 $stat->eb_file($file);
299 1 while unlink $file;
302 error "Can't force rewriting of $file -- skipping";
305 msg 'Ebuild already generated for ' . $stat->distribution . ' -- skipping';
315 $self->SUPER::prepare(%opts);
319 my $desc = $mod->description;
320 ($desc = $name) =~ s/-+/::/g unless $desc;
323 $stat->uri('http://search.cpan.org/dist/' . $name);
325 $stat->license([ qw/Artistic GPL-2/ ]);
327 my $prereqs = $mod->status->prereqs;
329 for my $prereq (sort keys %$prereqs) {
330 next if $prereq =~ /^perl(?:-|\z)/;
331 my $obj = $int->module_tree($prereq);
333 error 'Wrong module object -- aborting';
336 next if $obj->package_is_perl_core;
339 if ($prereqs->{$prereq}) {
340 if ($obj->installed_version && $obj->installed_version < $obj->version) {
341 $version = $obj->installed_version;
343 $version = $obj->package_version;
346 push @depends, [ $obj->package_name, $version ];
349 $stat->deps(\@depends);
357 my $stat = $self->status;
359 unless ($stat->prepared) {
360 error 'Can\'t create ' . $stat->distribution . ' since it was never prepared -- aborting';
366 if ($stat->created) {
367 msg $stat->distribution . ' was already created -- skipping';
368 $stat->dist($stat->eb_file);
375 $self->SUPER::create(@_);
380 my $dir = $stat->eb_dir;
382 eval { mkpath $dir };
384 error "mkpath($dir): $@";
389 my $d = $stat->header;
390 $d .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
391 $d .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
392 $d .= 'DESCRIPTION="' . $stat->desc . "\"\n";
393 $d .= 'HOMEPAGE="' . $stat->uri . "\"\n\n";
394 $d .= "SLOT=\"0\"\n";
395 $d .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
396 $d .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
397 $d .= 'DEPEND="' . join("\n",
399 map $self->_cpan2portage(@$_), @{$stat->deps}
401 $d .= "SRC_TEST=\"do\"\n";
404 my $file = $stat->eb_file;
405 open my $eb, '>', $file or do {
406 error "open($file): $! -- aborting";
412 if ($stat->do_manifest) {
413 unless (copy $stat->fetched_arch, $stat->distdir) {
414 error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
415 1 while unlink $file;
419 msg 'Adding Manifest entry for ' . $stat->distribution;
420 unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
421 1 while unlink $file;
432 my ($self, $name, $version) = @_;
434 $name = $gentooism{$name} || $name;
436 $ver = eval { version->new($version) } if defined $version;
438 my @portdirs = ($main_portdir, @{$self->status->portdir_overlay});
440 for my $category (qw/virtual perl-core dev-perl perl-gcpan/, CATEGORY) {
441 my $atom = ($category eq 'virtual' ? 'perl-' : '') . $name;
443 for my $portdir (@portdirs) {
444 my @ebuilds = glob catfile($portdir, $category, $atom,"$atom-*.ebuild");
445 next unless @ebuilds;
447 if (defined $version) {
449 next unless /\Q$atom\E-v?([\d._]+).*?\.ebuild$/;
450 my $eb_ver = eval { version->new($1) };
451 next unless defined $eb_ver and $eb_ver >= $ver;
452 return ">=$category/$atom-$version";
455 return "$category/$atom";
462 error "Couldn't find an appropriate ebuild for $name in the portage tree -- skipping";
468 my $stat = $self->status;
469 my $conf = $self->parent->parent->configure_object;
471 my $sudo = $conf->get_program('sudo');
472 my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
473 unshift @cmd, $sudo if $sudo;
475 my $success = $self->_run(\@cmd, 1);
476 $stat->installed($success);
483 my $stat = $self->status;
484 my $conf = $self->parent->parent->configure_object;
486 my $sudo = $conf->get_program('sudo');
487 my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
488 unshift @cmd, $sudo if $sudo;
490 my $success = $self->_run(\@cmd, 1);
491 $stat->uninstalled($success);
497 my ($self, $cmd, $verbose) = @_;
498 my $stat = $self->status;
500 my ($success, $errmsg, $output) = do {
501 local $ENV{PORTDIR_OVERLAY} = join ' ', @{$stat->portdir_overlay};
502 local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
503 run command => $cmd, verbose => $verbose;
507 error "$errmsg -- aborting";
508 if (not $verbose and defined $output and $self->status->verbose) {
509 my $msg = join '', @$output;
520 Gentoo (L<http://gentoo.org>).
522 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5), L<version> (since 5.009).
524 L<Cwd> (since perl 5) L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
530 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
534 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
536 You can contact me by mail or on C<irc.perl.org> (vincent).
540 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.
544 You can find documentation for this module with the perldoc command.
546 perldoc CPANPLUS::Dist::Gentoo
548 =head1 ACKNOWLEDGEMENTS
550 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
552 Kent Fredric, for testing and suggesting improvements.
554 =head1 COPYRIGHT & LICENSE
556 Copyright 2008 Vincent Pit, all rights reserved.
558 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
562 1; # End of CPANPLUS::Dist::Gentoo