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/;
15 use base qw/CPANPLUS::Dist::Base/;
19 CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
27 our $VERSION = '0.04';
31 cpan2dist --format=CPANPLUS::Dist::Gentoo \
32 --dist-opts overlay=/usr/local/portage \
33 --dist-opts distdir=/usr/portage/distfiles \
34 --dist-opts manifest=yes \
35 --dist-opts keywords=x86 \
36 --dist-opts header="# Copyright 1999-2008 Gentoo Foundation" \
37 --dist-opts footer="# End" \
42 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>.
44 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>.
48 After installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
52 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.
56 use constant CATEGORY => 'perl-gcpanp';
58 sub format_available {
59 for my $prog (qw/emerge ebuild/) {
60 unless (can_run($prog)) {
61 error "$prog is required to write ebuilds -- aborting";
70 my $stat = $self->status;
71 my $conf = $self->parent->parent->configure_object;
73 $stat->mk_accessors(qw/name version author dist desc uri src license deps
74 eb_name eb_version eb_dir eb_file fetched_arch
75 overlay distdir keywords do_manifest header footer
78 $stat->force($conf->get_conf('force'));
79 $stat->verbose($conf->get_conf('verbose'));
85 'Digest' => 'digest-base',
86 'Locale-Maketext' => 'locale-maketext',
87 'Net-Ping' => 'net-ping',
88 'PathTools' => 'File-Spec',
89 'PodParser' => 'Pod-Parser',
90 'Set-Scalar' => 'set-scalar',
91 'Tie-EncryptedHash' => 'tie-encryptedhash',
96 my $mod = $self->parent;
97 my $stat = $self->status;
98 my $int = $mod->parent;
99 my $conf = $int->configure_object;
103 my $keywords = delete $opts{'keywords'};
104 $keywords = 'x86' unless defined $keywords;
105 $keywords = [ split ' ', $keywords ];
106 $stat->keywords($keywords);
108 my $manifest = delete $opts{'manifest'};
109 $manifest = 1 unless defined $manifest;
110 $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
111 $stat->do_manifest($manifest);
113 my $header = delete $opts{'header'};
114 if (defined $header) {
115 1 while chomp $header;
120 $stat->header($header);
122 my $footer = delete $opts{'footer'};
123 if (defined $footer) {
124 $footer = "\n" . $footer;
128 $stat->footer($footer);
130 my $overlay = delete $opts{'overlay'};
131 $overlay = (defined $overlay) ? abs_path $overlay : '/usr/local/portage';
132 $stat->overlay($overlay);
134 my $distdir = delete $opts{'distdir'};
135 $distdir = (defined $distdir) ? abs_path $distdir : '/usr/portage/distfiles';
136 $stat->distdir($distdir);
138 if ($stat->do_manifest && !-w $stat->distdir) {
139 error 'distdir isn\'t writable -- aborting';
142 $stat->fetched_arch($mod->status->fetch);
144 my $name = $mod->package_name;
147 my $version = $mod->package_version;
148 $stat->version($version);
150 my $author = $mod->author->cpanid;
151 $stat->author($author);
153 $stat->dist($name . '-' . $version);
155 $version =~ s/[^\d._]+//g;
156 $version =~ s/^[._]*//;
157 $version =~ s/[._]*$//;
158 $version =~ s/[._]*_[._]*/_/g;
160 ($version, my $patch, my @rest) = split /_/, $version;
161 $version .= '_p' . $patch if defined $patch;
162 $version .= join('.', '', @rest) if @rest;
164 $stat->eb_version($version);
166 $stat->eb_name($gentooism{$name} || $name);
168 $stat->eb_dir(catdir($stat->overlay, CATEGORY, $stat->eb_name));
170 my $file = catfile($stat->eb_dir,
171 $stat->eb_name . '-' . $stat->eb_version . '.ebuild');
176 1 while unlink $file;
179 error "Can't force rewriting of $file -- skipping";
182 msg 'Ebuild already generated for ' . $stat->dist . ' -- skipping';
190 $stat->eb_file($file);
192 $self->SUPER::prepare(%opts);
194 my $desc = $mod->description;
195 ($desc = $name) =~ s/-+/::/g unless $desc;
198 $stat->uri('http://search.cpan.org/dist/' . $name);
200 unless ($author =~ /^(.)(.)/) {
201 error 'Wrong author name -- aborting';
204 $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/"
207 $stat->license([ qw/Artistic GPL-2/ ]);
209 my $prereqs = $mod->status->prereqs;
210 $prereqs = { map { ($gentooism{$_} || $_) => $prereqs->{$_} } keys %$prereqs };
212 for my $prereq (sort keys %$prereqs) {
213 next if $prereq =~ /^perl(?:-|\z)/;
214 my $obj = $int->module_tree($prereq);
216 error 'Wrong module object -- aborting';
219 next if $obj->package_is_perl_core;
222 if ($prereqs->{$prereq}) {
223 if ($obj->installed_version && $obj->installed_version < $obj->version) {
224 $version = $obj->installed_version;
226 $version = $obj->package_version;
229 push @depends, [ $obj , $version ];
232 $stat->deps(\@depends);
239 my $stat = $self->status;
241 unless ($stat->prepared) {
242 error 'Can\'t create ' . $stat->dist . ' since it was never prepared -- aborting';
246 if ($stat->created) {
247 msg $stat->dist . ' was already created -- skipping';
251 $self->SUPER::create(@_);
253 my $dir = $stat->eb_dir;
255 eval { mkpath $dir };
257 error "mkpath($dir): $@";
262 my $d = $stat->header;
263 $d .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
264 $d .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
265 $d .= 'S="${WORKDIR}/' . $stat->dist . "\"\n";
266 $d .= 'DESCRIPTION="' . $stat->desc . "\"\n";
267 $d .= 'HOMEPAGE="' . $stat->uri . "\"\n";
268 $d .= 'SRC_URI="' . $stat->src . "\"\n";
269 $d .= "SLOT=\"0\"\n";
270 $d .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
271 $d .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
272 $d .= 'DEPEND="' . join "\n",
275 my $a = $_->[0]->package_name;
277 if (defined $_->[1]) {
281 '|| ( ' . join(' ', map "$x$_/$a",
282 qw/perl-core dev-perl perl-gcpan/, CATEGORY)
286 $d .= "SRC_TEST=\"do\"\n";
289 my $file = $stat->eb_file;
290 open my $eb, '>', $file or do {
291 error "open($file): $! -- aborting";
297 if ($stat->do_manifest) {
298 unless (copy $stat->fetched_arch, $stat->distdir) {
299 error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
300 1 while unlink $file;
304 msg 'Adding Manifest entry for ' . $stat->dist;
305 unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
306 1 while unlink $file;
316 my $stat = $self->status;
317 my $conf = $self->parent->parent->configure_object;
319 my $sudo = $conf->get_program('sudo');
320 my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
321 unshift @cmd, $sudo if $sudo;
323 return $self->_run(\@cmd, 1);
328 my $stat = $self->status;
329 my $conf = $self->parent->parent->configure_object;
331 my $sudo = $conf->get_program('sudo');
332 my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
333 unshift @cmd, $sudo if $sudo;
335 return $self->_run(\@cmd, 1);
339 my ($self, $cmd, $verbose) = @_;
340 my $stat = $self->status;
342 my ($success, $errmsg, $output) = do {
343 local $ENV{PORTDIR_OVERLAY} = $stat->overlay;
344 local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
345 run command => $cmd, verbose => $verbose;
349 error "$errmsg -- aborting";
350 if (not $verbose and defined $output and $self->status->verbose) {
351 my $msg = join '', @$output;
362 Gentoo (L<http://gentoo.org>).
364 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5).
366 L<Cwd> (since perl 5) L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
372 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
376 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
378 You can contact me by mail or on C<irc.perl.org> (vincent).
382 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.
386 You can find documentation for this module with the perldoc command.
388 perldoc CPANPLUS::Dist::Gentoo
390 =head1 ACKNOWLEDGEMENTS
392 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
394 Kent Fredric, for testing and suggesting improvements.
396 =head1 COPYRIGHT & LICENSE
398 Copyright 2008 Vincent Pit, all rights reserved.
400 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
404 1; # End of CPANPLUS::Dist::Gentoo