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';
73 sub format_available {
74 for my $prog (qw/emerge ebuild/) {
75 unless (can_run($prog)) {
76 error "$prog is required to write ebuilds -- aborting";
81 if (IPC::Cmd->can_capture_buffer) {
82 my ($success, $errmsg, $output) = run command => [ qw/emerge --info/ ],
85 for (@{$output || []}) {
86 if (/^PORTDIR_OVERLAY=(.*)$/m) {
87 $overlays = [ map abs_path($_), split ' ', _unquote($1) ];
89 if (/^ACCEPT_KEYWORDS=(.*)$/m) {
90 $default_keywords = [ split ' ', _unquote($1) ];
92 if (/^DISTDIR=(.*)$/m) {
93 $default_distdir = abs_path(_unquote($1));
95 if (/^PORTDIR=(.*)$/m) {
96 $main_portdir = abs_path(_unquote($1));
104 $default_keywords = [ 'x86' ] unless defined $default_keywords;
105 $default_distdir = '/usr/portage/distfiles' unless defined $default_distdir;
112 my $stat = $self->status;
113 my $conf = $self->parent->parent->configure_object;
115 $stat->mk_accessors(qw/name version author distribution desc uri src license
116 deps eb_name eb_version eb_dir eb_file fetched_arch
118 overlay distdir keywords do_manifest header footer
121 $stat->force($conf->get_conf('force'));
122 $stat->verbose($conf->get_conf('verbose'));
128 'ANSIColor' => 'Term-ANSIColor',
129 'Audio-CD' => 'Audio-CD-disc-cover',
130 'CGI-Simple' => 'Cgi-Simple',
131 'Cache-Mmap' => 'cache-mmap',
132 'Class-Loader' => 'class-loader',
133 'Class-ReturnValue' => 'class-returnvalue',
134 'Config-General' => 'config-general',
135 'Convert-ASCII-Armour' => 'convert-ascii-armour',
136 'Convert-PEM' => 'convert-pem',
137 'Crypt-CBC' => 'crypt-cbc',
138 'Crypt-DES_EDE3' => 'crypt-des-ede3',
139 'Crypt-DH' => 'crypt-dh',
140 'Crypt-DSA' => 'crypt-dsa',
141 'Crypt-IDEA' => 'crypt-idea',
142 'Crypt-Primes' => 'crypt-primes',
143 'Crypt-RSA' => 'crypt-rsa',
144 'Crypt-Random' => 'crypt-random',
145 'DBIx-SearchBuilder' => 'dbix-searchbuilder',
146 'Data-Buffer' => 'data-buffer',
147 'Digest' => 'digest-base',
148 'Digest-BubbleBabble' => 'digest-bubblebabble',
149 'Digest-MD2' => 'digest-md2',
150 'ExtUtils-Depends' => 'extutils-depends',
151 'ExtUtils-PkgConfig' => 'extutils-pkgconfig',
152 'Frontier-RPC' => 'frontier-rpc',
153 'Gimp' => 'gimp-perl',
154 'Glib' => 'glib-perl',
155 'Gnome2-Canvas' => 'gnome2-canvas',
156 'Gnome2-GConf' => 'gnome2-gconf',
157 'Gnome2-Print' => 'gnome2-print',
158 'Gnome2-VFS' => 'gnome2-vfs-perl',
159 'Gnome2-Wnck' => 'gnome2-wnck',
160 'Gtk2' => 'gtk2-perl',
161 'Gtk2-Ex-FormFactory' => 'gtk2-ex-formfactory',
162 'Gtk2-GladeXML' => 'gtk2-gladexml',
163 'Gtk2-Spell' => 'gtk2-spell',
164 'Gtk2-TrayIcon' => 'gtk2-trayicon',
165 'Gtk2-TrayManager' => 'gtk2-traymanager',
166 'Gtk2Fu' => 'gtk2-fu',
167 'I18N-LangTags' => 'i18n-langtags',
168 'Image-Info' => 'ImageInfo',
169 'Image-Size' => 'ImageSize',
170 'Inline-Files' => 'inline-files',
171 'Locale-Maketext' => 'locale-maketext',
172 'Locale-Maketext-Fuzzy' => 'locale-maketext-fuzzy',
173 'Locale-Maketext-Lexicon' => 'locale-maketext-lexicon',
174 'Log-Dispatch' => 'log-dispatch',
175 'Math-Pari' => 'math-pari',
176 'Module-Info' => 'module-info',
177 'Net-Ping' => 'net-ping',
178 'Net-SFTP' => 'net-sftp',
179 'Net-SSH-Perl' => 'net-ssh-perl',
180 'Net-Server' => 'net-server',
181 'OLE-Storage_Lite' => 'OLE-StorageLite',
182 'Ogg-Vorbis-Header' => 'ogg-vorbis-header',
183 'PathTools' => 'File-Spec',
184 'Pod-Parser' => 'PodParser',
185 'Regexp-Common' => 'regexp-common',
186 'SDL_Perl' => 'sdl-perl',
187 'Set-Scalar' => 'set-scalar',
188 'String-CRC32' => 'string-crc32',
189 'Text-Autoformat' => 'text-autoformat',
190 'Text-Reform' => 'text-reform',
191 'Text-Template' => 'text-template',
192 'Text-Wrapper' => 'text-wrapper',
193 'Tie-EncryptedHash' => 'tie-encryptedhash',
197 'gettext' => 'Locale-gettext',
198 'txt2html' => 'TextToHTML',
203 my $mod = $self->parent;
204 my $stat = $self->status;
205 my $int = $mod->parent;
206 my $conf = $int->configure_object;
212 my $keywords = delete $opts{'keywords'};
213 if (defined $keywords) {
214 $keywords = [ split ' ', $keywords ];
216 $keywords = $default_keywords;
218 $stat->keywords($keywords);
220 my $manifest = delete $opts{'manifest'};
221 $manifest = 1 unless defined $manifest;
222 $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
223 $stat->do_manifest($manifest);
225 my $header = delete $opts{'header'};
226 if (defined $header) {
227 1 while chomp $header;
232 $stat->header($header);
234 my $footer = delete $opts{'footer'};
235 if (defined $footer) {
236 $footer = "\n" . $footer;
240 $stat->footer($footer);
242 my $overlay = delete $opts{'overlay'};
243 $overlay = (defined $overlay) ? abs_path $overlay : '/usr/local/portage';
244 $stat->overlay($overlay);
246 my $distdir = delete $opts{'distdir'};
247 $distdir = (defined $distdir) ? abs_path $distdir : $default_distdir;
248 $stat->distdir($distdir);
250 if ($stat->do_manifest && !-w $stat->distdir) {
251 error 'distdir isn\'t writable -- aborting';
254 $stat->fetched_arch($mod->status->fetch);
256 my $cur = File::Spec::Functions::curdir();
259 if ($_ eq $overlay or File::Spec::Functions::abs2rel($overlay, $_) eq $cur) {
260 $portdir_overlay = [ @$overlays ];
264 $portdir_overlay = [ @$overlays, $overlay ] unless $portdir_overlay;
265 $stat->portdir_overlay($portdir_overlay);
267 my $name = $mod->package_name;
270 my $version = $mod->package_version;
271 $stat->version($version);
273 my $author = $mod->author->cpanid;
274 $stat->author($author);
276 $stat->distribution($name . '-' . $version);
278 $version =~ s/[^\d._]+//g;
279 $version =~ s/^[._]*//;
280 $version =~ s/[._]*$//;
281 $version =~ s/[._]*_[._]*/_/g;
283 ($version, my $patch, my @rest) = split /_/, $version;
284 $version .= '_p' . $patch if defined $patch;
285 $version .= join('.', '', @rest) if @rest;
287 $stat->eb_version($version);
289 $stat->eb_name($gentooism{$name} || $name);
291 $stat->eb_dir(catdir($stat->overlay, CATEGORY, $stat->eb_name));
293 my $file = catfile($stat->eb_dir,
294 $stat->eb_name . '-' . $stat->eb_version . '.ebuild');
295 $stat->eb_file($file);
299 if ($stat->force and not $forced{$file}) {
301 1 while unlink $file;
305 error "Can't force rewriting of $file -- skipping";
308 msg 'Ebuild already generated for ' . $stat->distribution . ' -- skipping';
318 $self->SUPER::prepare(%opts);
322 my $desc = $mod->description;
323 ($desc = $name) =~ s/-+/::/g unless $desc;
326 $stat->uri('http://search.cpan.org/dist/' . $name);
328 unless ($author =~ /^(.)(.)/) {
329 error 'Wrong author name -- aborting';
332 $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/"
335 $stat->license([ qw/Artistic GPL-2/ ]);
337 my $prereqs = $mod->status->prereqs;
339 for my $prereq (sort keys %$prereqs) {
340 next if $prereq =~ /^perl(?:-|\z)/;
341 my $obj = $int->module_tree($prereq);
343 error 'Wrong module object -- aborting';
346 next if $obj->package_is_perl_core;
349 if ($prereqs->{$prereq}) {
350 if ($obj->installed_version && $obj->installed_version < $obj->version) {
351 $version = $obj->installed_version;
353 $version = $obj->package_version;
356 push @depends, [ $obj->package_name, $version ];
359 $stat->deps(\@depends);
367 my $stat = $self->status;
369 unless ($stat->prepared) {
370 error 'Can\'t create ' . $stat->distribution . ' since it was never prepared -- aborting';
376 if ($stat->created) {
377 msg $stat->distribution . ' was already created -- skipping';
378 $stat->dist($stat->eb_file);
385 $self->SUPER::create(@_);
390 my $dir = $stat->eb_dir;
392 eval { mkpath $dir };
394 error "mkpath($dir): $@";
399 my $d = $stat->header;
400 $d .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
401 $d .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
402 $d .= 'S="${WORKDIR}/' . $stat->distribution . "\"\n";
403 $d .= 'DESCRIPTION="' . $stat->desc . "\"\n";
404 $d .= 'HOMEPAGE="' . $stat->uri . "\"\n";
405 $d .= 'SRC_URI="' . $stat->src . "\"\n";
406 $d .= "SLOT=\"0\"\n";
407 $d .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
408 $d .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
409 $d .= 'DEPEND="' . join("\n",
411 map $self->_cpan2portage(@$_), @{$stat->deps}
413 $d .= "SRC_TEST=\"do\"\n";
416 my $file = $stat->eb_file;
417 open my $eb, '>', $file or do {
418 error "open($file): $! -- aborting";
424 if ($stat->do_manifest) {
425 unless (copy $stat->fetched_arch, $stat->distdir) {
426 error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
427 1 while unlink $file;
431 msg 'Adding Manifest entry for ' . $stat->distribution;
432 unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
433 1 while unlink $file;
444 my ($self, $name, $version) = @_;
446 $name = $gentooism{$name} || $name;
448 $ver = eval { version->new($version) } if defined $version;
450 my @portdirs = ($main_portdir, @{$self->status->portdir_overlay});
452 for my $category (qw/virtual perl-core dev-perl perl-gcpan/, CATEGORY) {
453 my $atom = ($category eq 'virtual' ? 'perl-' : '') . $name;
455 for my $portdir (@portdirs) {
456 my @ebuilds = glob catfile($portdir, $category, $atom,"$atom-*.ebuild");
457 next unless @ebuilds;
459 if (defined $ver) { # implies that $version is defined
461 next unless /\Q$atom\E-v?([\d._]+).*?\.ebuild$/;
462 my $eb_ver = eval { version->new($1) };
463 next unless defined $eb_ver and $eb_ver >= $ver;
464 return ">=$category/$atom-$version";
467 return "$category/$atom";
474 error "Couldn't find an appropriate ebuild for $name in the portage tree -- skipping";
480 my $stat = $self->status;
481 my $conf = $self->parent->parent->configure_object;
483 my $sudo = $conf->get_program('sudo');
484 my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
485 unshift @cmd, $sudo if $sudo;
487 my $success = $self->_run(\@cmd, 1);
488 $stat->installed($success);
495 my $stat = $self->status;
496 my $conf = $self->parent->parent->configure_object;
498 my $sudo = $conf->get_program('sudo');
499 my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
500 unshift @cmd, $sudo if $sudo;
502 my $success = $self->_run(\@cmd, 1);
503 $stat->uninstalled($success);
509 my ($self, $cmd, $verbose) = @_;
510 my $stat = $self->status;
512 my ($success, $errmsg, $output) = do {
513 local $ENV{PORTDIR_OVERLAY} = join ' ', @{$stat->portdir_overlay};
514 local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
515 run command => $cmd, verbose => $verbose;
519 error "$errmsg -- aborting";
520 if (not $verbose and defined $output and $self->status->verbose) {
521 my $msg = join '', @$output;
532 Gentoo (L<http://gentoo.org>).
534 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5), L<version> (since 5.009).
536 L<Cwd> (since perl 5) L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
542 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
546 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
548 You can contact me by mail or on C<irc.perl.org> (vincent).
552 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.
556 You can find documentation for this module with the perldoc command.
558 perldoc CPANPLUS::Dist::Gentoo
560 =head1 ACKNOWLEDGEMENTS
562 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
564 Kent Fredric, for testing and suggesting improvements.
566 =head1 COPYRIGHT & LICENSE
568 Copyright 2008-2009 Vincent Pit, all rights reserved.
570 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
574 1; # End of CPANPLUS::Dist::Gentoo