1 package CPANPLUS::Dist::Gentoo;
6 use File::Copy qw/copy/;
7 use File::Path qw/mkpath/;
8 use File::Spec::Functions qw/catdir catfile/;
10 use IPC::Cmd qw/run can_run/;
14 use base qw/CPANPLUS::Dist::Base/;
18 CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
26 our $VERSION = '0.03';
30 cpan2dist --format=CPANPLUS::Dist::Gentoo \
31 --dist-opts overlay=/usr/local/portage \
32 --dist-opts distdir=/usr/portage/distfiles \
33 --dist-opts manifest=yes \
34 --dist-opts keywords=x86 \
39 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>). You also need to specify the correct keyword for your architecture if it differs from the default C<x86>.
41 The generated ebuilds are placed into the C<perl-gcpanp> category. They favour depending on C<perl-core>, C<dev-perl> or C<perl-gcpan> (in that order) rather than C<perl-gcpanp>.
45 After installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
49 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.
53 use constant CATEGORY => 'perl-gcpanp';
55 sub format_available {
56 for my $prog (qw/emerge ebuild/) {
57 unless (can_run($prog)) {
58 error "$prog is required to write ebuilds -- aborting";
67 my $stat = $self->status;
68 my $conf = $self->parent->parent->configure_object;
70 $stat->mk_accessors(qw/name version author dist desc uri src license deps
71 eb_name eb_version eb_dir eb_file fetched_arch
72 overlay distdir keywords do_manifest
75 $stat->force($conf->get_conf('force'));
76 $stat->verbose($conf->get_conf('verbose'));
82 'Digest' => 'digest-base',
83 'Locale-Maketext' => 'locale-maketext',
84 'Net-Ping' => 'net-ping',
85 'PathTools' => 'File-Spec',
86 'PodParser' => 'Pod-Parser',
87 'Set-Scalar' => 'set-scalar',
88 'Tie-EncryptedHash' => 'tie-encryptedhash',
93 my $mod = $self->parent;
94 my $stat = $self->status;
95 my $int = $mod->parent;
96 my $conf = $int->configure_object;
100 my $keywords = delete $opts{'keywords'};
101 $keywords = 'x86' unless defined $keywords;
102 $keywords = [ split ' ', $keywords ];
103 $stat->keywords($keywords);
105 my $manifest = delete $opts{'manifest'};
106 $manifest = 1 unless defined $manifest;
107 $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
108 $stat->do_manifest($manifest);
110 $stat->overlay(delete($opts{'overlay'}) || '/usr/local/portage');
112 $stat->distdir(delete($opts{'distdir'}) || '/usr/portage/distfiles');
114 if ($stat->do_manifest && !-w $stat->distdir) {
115 error 'distdir isn\'t writable -- aborting';
118 $stat->fetched_arch($mod->status->fetch);
120 my $name = $mod->package_name;
123 my $version = $mod->package_version;
124 $stat->version($version);
126 my $author = $mod->author->cpanid;
127 $stat->author($author);
129 $stat->dist($name . '-' . $version);
131 $version =~ s/[^\d._]+//g;
132 $version =~ s/^[._]*//;
133 $version =~ s/[._]*$//;
134 $version =~ s/[._]*_[._]*/_/g;
136 ($version, my $patch, my @rest) = split /_/, $version;
137 $version .= '_p' . $patch if defined $patch;
138 $version .= join('.', '', @rest) if @rest;
140 $stat->eb_version($version);
142 $stat->eb_name($gentooism{$name} || $name);
144 $stat->eb_dir(catdir($stat->overlay, CATEGORY, $stat->eb_name));
146 my $file = catfile($stat->eb_dir,
147 $stat->eb_name . '-' . $stat->eb_version . '.ebuild');
152 1 while unlink $file;
155 error "Can't force rewriting of $file -- skipping";
158 msg 'Ebuild already generated for ' . $stat->dist . ' -- skipping';
166 $stat->eb_file($file);
168 $self->SUPER::prepare(%opts);
170 my $desc = $mod->description;
171 ($desc = $name) =~ s/-+/::/g unless $desc;
174 $stat->uri('http://search.cpan.org/dist/' . $name);
176 unless ($name =~ /^([^-]+)/) {
177 error 'Wrong distribution name -- aborting';
180 $stat->src('mirror://cpan/modules/by-module/' . $1 . '/' . $mod->package);
182 $stat->license([ qw/Artistic GPL-2/ ]);
184 my $prereqs = $mod->status->prereqs;
185 $prereqs = { map { ($gentooism{$_} || $_) => $prereqs->{$_} } keys %$prereqs };
187 for my $prereq (sort keys %$prereqs) {
188 next if $prereq =~ /^perl(?:-|\z)/;
189 my $obj = $int->module_tree($prereq);
191 error 'Wrong module object -- aborting';
194 next if $obj->package_is_perl_core;
197 if ($prereqs->{$prereq}) {
198 if ($obj->installed_version && $obj->installed_version < $obj->version) {
199 $version = $obj->installed_version;
201 $version = $obj->package_version;
204 push @depends, [ $obj , $version ];
207 $stat->deps(\@depends);
214 my $stat = $self->status;
216 unless ($stat->prepared) {
217 error 'Can\'t create ' . $stat->dist . ' since it was never prepared -- aborting';
221 if ($stat->created) {
222 msg $stat->dist . ' was already created -- skipping';
226 $self->SUPER::create(@_);
228 my $dir = $stat->eb_dir;
230 eval { mkpath $dir };
232 error "mkpath($dir): $@";
237 my $d = "# Generated by CPANPLUS::Dist::Gentoo\n\n";
238 $d .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
239 $d .= 'S="${WORKDIR}/' . $stat->dist . "\"\n";
240 $d .= 'DESCRIPTION="' . $stat->desc . "\"\n";
241 $d .= 'HOMEPAGE="' . $stat->uri . "\"\n";
242 $d .= 'SRC_URI="' . $stat->src . "\"\n";
243 $d .= "SLOT=\"0\"\n";
244 $d .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
245 $d .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
246 $d .= 'DEPEND="' . join "\n",
249 my $a = $_->[0]->package_name;
251 if (defined $_->[1]) {
255 '|| ( ' . join(' ', map "$x$_/$a",
256 qw/perl-core dev-perl perl-gcpan/, CATEGORY)
260 $d .= "SRC_TEST=\"do\"\n";
262 my $file = $stat->eb_file;
263 open my $eb, '>', $file or do {
264 error "open($file): $! -- aborting";
270 if ($stat->do_manifest) {
271 unless (copy $stat->fetched_arch, $stat->distdir) {
272 error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
273 1 while unlink $file;
277 msg 'Adding Manifest entry for ' . $stat->dist;
278 unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
279 1 while unlink $file;
289 my $stat = $self->status;
290 my $conf = $self->parent->parent->configure_object;
292 my $sudo = $conf->get_program('sudo');
293 my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
294 unshift @cmd, $sudo if $sudo;
296 return $self->_run(\@cmd, 1);
301 my $stat = $self->status;
302 my $conf = $self->parent->parent->configure_object;
304 my $sudo = $conf->get_program('sudo');
305 my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
306 unshift @cmd, $sudo if $sudo;
308 return $self->_run(\@cmd, 1);
312 my ($self, $cmd, $verbose) = @_;
313 my $stat = $self->status;
315 my ($success, $errmsg, $output) = do {
316 local $ENV{PORTDIR_OVERLAY} = $stat->overlay;
317 local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
318 run command => $cmd, verbose => $verbose;
322 error "$errmsg -- aborting";
323 if (not $verbose and defined $output and $self->status->verbose) {
324 my $msg = join '', @$output;
335 Gentoo (L<http://gentoo.org>).
337 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5).
339 L<File::Path> (since 5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
345 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
349 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
351 You can contact me by mail or on C<irc.perl.org> (vincent).
355 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.
359 You can find documentation for this module with the perldoc command.
361 perldoc CPANPLUS::Dist::Gentoo
363 =head1 ACKNOWLEDGEMENTS
365 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
367 =head1 COPYRIGHT & LICENSE
369 Copyright 2008 Vincent Pit, all rights reserved.
371 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
375 1; # End of CPANPLUS::Dist::Gentoo