]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/blob - lib/CPANPLUS/Dist/Gentoo.pm
The dists available in cpan/modules/by-module are only those registered in the module...
[perl/modules/CPANPLUS-Dist-Gentoo.git] / lib / CPANPLUS / Dist / Gentoo.pm
1 package CPANPLUS::Dist::Gentoo;
2
3 use strict;
4 use warnings;
5
6 use File::Copy qw/copy/;
7 use File::Path qw/mkpath/;
8 use File::Spec::Functions qw/catdir catfile/;
9
10 use IPC::Cmd qw/run can_run/;
11
12 use CPANPLUS::Error;
13
14 use base qw/CPANPLUS::Dist::Base/;
15
16 =head1 NAME
17
18 CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
19
20 =head1 VERSION
21
22 Version 0.03
23
24 =cut
25
26 our $VERSION = '0.03';
27
28 =head1 SYNOPSIS
29
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 \
35               Any::Module You::Like
36
37 =head1 DESCRPITON
38
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>.
40
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>.
42
43 =head1 INSTALLATION
44
45 After installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
46
47 =head1 METHODS
48
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.
50
51 =cut
52
53 use constant CATEGORY => 'perl-gcpanp';
54
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";
59    return 0;
60   }
61  }
62  return 1;
63 }
64
65 sub init {
66  my ($self) = @_;
67  my $stat = $self->status;
68  my $conf = $self->parent->parent->configure_object;
69
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
73                         force verbose/);
74
75  $stat->force($conf->get_conf('force'));
76  $stat->verbose($conf->get_conf('verbose'));
77
78  return 1;
79 }
80
81 my %gentooism = (
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',
89 );
90
91 sub prepare {
92  my $self = shift;
93  my $mod  = $self->parent;
94  my $stat = $self->status;
95  my $int  = $mod->parent;
96  my $conf = $int->configure_object;
97
98  my %opts = @_;
99
100  my $keywords = delete $opts{'keywords'};
101  $keywords = 'x86' unless defined $keywords;
102  $keywords = [ split ' ', $keywords ];
103  $stat->keywords($keywords);
104
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);
109
110  $stat->overlay(delete($opts{'overlay'}) || '/usr/local/portage');
111
112  $stat->distdir(delete($opts{'distdir'}) || '/usr/portage/distfiles');
113
114  if ($stat->do_manifest && !-w $stat->distdir) {
115   error 'distdir isn\'t writable -- aborting';
116   return 0;
117  }
118  $stat->fetched_arch($mod->status->fetch);
119
120  my $name = $mod->package_name;
121  $stat->name($name);
122
123  my $version = $mod->package_version;
124  $stat->version($version);
125
126  my $author = $mod->author->cpanid;
127  $stat->author($author);
128
129  $stat->dist($name . '-' . $version);
130
131  $version =~ s/[^\d._]+//g;
132  $version =~ s/^[._]*//;
133  $version =~ s/[._]*$//;
134  $version =~ s/[._]*_[._]*/_/g;
135  {
136   ($version, my $patch, my @rest) = split /_/, $version;
137   $version .= '_p' . $patch if defined $patch;
138   $version .= join('.', '', @rest) if @rest;
139  }
140  $stat->eb_version($version);
141
142  $stat->eb_name($gentooism{$name} || $name);
143
144  $stat->eb_dir(catdir($stat->overlay, CATEGORY, $stat->eb_name));
145
146  my $file = catfile($stat->eb_dir,
147                     $stat->eb_name . '-' . $stat->eb_version . '.ebuild');
148  if (-e $file) {
149   my $skip = 1;
150   if ($stat->force) {
151    if (-w $file) {
152     1 while unlink $file;
153     $skip = 0;
154    } else {
155     error "Can't force rewriting of $file -- skipping";
156    }
157   } else {
158    msg 'Ebuild already generated for ' . $stat->dist . ' -- skipping';
159   }
160   if ($skip) {
161    $stat->prepared(1);
162    $stat->created(1);
163    return 1;
164   }
165  }
166  $stat->eb_file($file);
167
168  $self->SUPER::prepare(%opts);
169
170  my $desc = $mod->description;
171  ($desc = $name) =~ s/-+/::/g unless $desc;
172  $stat->desc($desc);
173
174  $stat->uri('http://search.cpan.org/dist/' . $name);
175
176  unless ($author =~ /^(.)(.)/) {
177   error 'Wrong author name -- aborting';
178   return 0;
179  }
180  $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/"
181             . $mod->package);
182
183  $stat->license([ qw/Artistic GPL-2/ ]);
184
185  my $prereqs = $mod->status->prereqs;
186  $prereqs = { map { ($gentooism{$_} || $_) => $prereqs->{$_} } keys %$prereqs };
187  my @depends;
188  for my $prereq (sort keys %$prereqs) {
189   next if $prereq =~ /^perl(?:-|\z)/;
190   my $obj = $int->module_tree($prereq);
191   unless ($obj) {
192    error 'Wrong module object -- aborting';
193    return 0;
194   }
195   next if $obj->package_is_perl_core;
196   {
197    my $version;
198    if ($prereqs->{$prereq}) {
199     if ($obj->installed_version && $obj->installed_version < $obj->version) {
200      $version = $obj->installed_version;
201     } else {
202      $version = $obj->package_version;
203     }
204    }
205    push @depends, [ $obj , $version ];
206   }
207  }
208  $stat->deps(\@depends);
209
210  return 1;
211 }
212
213 sub create {
214  my $self = shift;
215  my $stat = $self->status;
216
217  unless ($stat->prepared) {
218   error 'Can\'t create ' . $stat->dist . ' since it was never prepared -- aborting';
219   return 0;
220  }
221
222  if ($stat->created) {
223   msg $stat->dist . ' was already created -- skipping';
224   return 1;
225  }
226
227  $self->SUPER::create(@_);
228
229  my $dir = $stat->eb_dir;
230  unless (-d $dir) {
231   eval { mkpath $dir };
232   if ($@) {
233    error "mkpath($dir): $@";
234    return 0;
235   }
236  }
237
238  my $d = "# Generated by CPANPLUS::Dist::Gentoo\n\n";
239  $d   .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
240  $d   .= 'S="${WORKDIR}/' . $stat->dist . "\"\n";
241  $d   .= 'DESCRIPTION="' . $stat->desc . "\"\n";
242  $d   .= 'HOMEPAGE="' . $stat->uri . "\"\n";
243  $d   .= 'SRC_URI="' . $stat->src . "\"\n";
244  $d   .= "SLOT=\"0\"\n";
245  $d   .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
246  $d   .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
247  $d   .= 'DEPEND="' . join "\n",
248   'dev-lang/perl',
249   map {
250    my $a = $_->[0]->package_name;
251    my $x = '';
252    if (defined $_->[1]) {
253     $x  = '>=';
254     $a .= '-' . $_->[1];
255    }
256    '|| ( ' . join(' ', map "$x$_/$a",
257                            qw/perl-core dev-perl perl-gcpan/, CATEGORY)
258            . ' )';
259   } @{$stat->deps};
260  $d   .= "\"\n";
261  $d   .= "SRC_TEST=\"do\"\n";
262
263  my $file = $stat->eb_file;
264  open my $eb, '>', $file or do {
265   error "open($file): $! -- aborting";
266   return 0;
267  };
268  print $eb $d;
269  close $eb;
270
271  if ($stat->do_manifest) {
272   unless (copy $stat->fetched_arch, $stat->distdir) {
273    error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
274    1 while unlink $file;
275    return 0;
276   }
277
278   msg 'Adding Manifest entry for ' . $stat->dist;
279   unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
280    1 while unlink $file;
281    return 0;
282   }
283  }
284
285  return 1;
286 }
287
288 sub install {
289  my $self = shift;
290  my $stat = $self->status;
291  my $conf = $self->parent->parent->configure_object;
292
293  my $sudo = $conf->get_program('sudo');
294  my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
295  unshift @cmd, $sudo if $sudo;
296
297  return $self->_run(\@cmd, 1);
298 }
299
300 sub uninstall {
301  my $self = shift;
302  my $stat = $self->status;
303  my $conf = $self->parent->parent->configure_object;
304
305  my $sudo = $conf->get_program('sudo');
306  my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
307  unshift @cmd, $sudo if $sudo;
308
309  return $self->_run(\@cmd, 1);
310 }
311
312 sub _run {
313  my ($self, $cmd, $verbose) = @_;
314  my $stat = $self->status;
315
316  my ($success, $errmsg, $output) = do {
317   local $ENV{PORTDIR_OVERLAY}     = $stat->overlay;
318   local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
319   run command => $cmd, verbose => $verbose;
320  };
321
322  unless ($success) {
323   error "$errmsg -- aborting";
324   if (not $verbose and defined $output and $self->status->verbose) {
325    my $msg = join '', @$output;
326    1 while chomp $msg;
327    error $msg;
328   }
329  }
330
331  return $success;
332 }
333
334 =head1 DEPENDENCIES
335
336 Gentoo (L<http://gentoo.org>).
337
338 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5).
339
340 L<File::Path> (since 5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
341
342 =head1 SEE ALSO
343
344 L<cpan2dist>.
345
346 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
347
348 =head1 AUTHOR
349
350 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
351
352 You can contact me by mail or on C<irc.perl.org> (vincent).
353
354 =head1 BUGS
355
356 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.
357
358 =head1 SUPPORT
359
360 You can find documentation for this module with the perldoc command.
361
362     perldoc CPANPLUS::Dist::Gentoo
363
364 =head1 ACKNOWLEDGEMENTS
365
366 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
367
368 =head1 COPYRIGHT & LICENSE
369
370 Copyright 2008 Vincent Pit, all rights reserved.
371
372 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
373
374 =cut
375
376 1; # End of CPANPLUS::Dist::Gentoo