]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/blob - lib/CPANPLUS/Dist/Gentoo.pm
Better handling of the run of emerge --info
[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 Cwd qw/abs_path/;
7 use File::Copy qw/copy/;
8 use File::Path qw/mkpath/;
9 use File::Spec::Functions qw/catdir catfile/;
10
11 use IPC::Cmd qw/run can_run/;
12
13 use CPANPLUS::Error;
14
15 use base qw/CPANPLUS::Dist::Base/;
16
17 =head1 NAME
18
19 CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
20
21 =head1 VERSION
22
23 Version 0.04
24
25 =cut
26
27 our $VERSION = '0.04';
28
29 =head1 SYNOPSIS
30
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" \
38               Any::Module You::Like
39
40 =head1 DESCRPITON
41
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>.
43
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>.
45
46 =head1 INSTALLATION
47
48 After installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
49
50 =head1 METHODS
51
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.
53
54 =cut
55
56 use constant CATEGORY => 'perl-gcpanp';
57
58 my $overlays;
59
60 sub format_available {
61  for my $prog (qw/emerge ebuild/) {
62   unless (can_run($prog)) {
63    error "$prog is required to write ebuilds -- aborting";
64    return 0;
65   }
66  }
67
68  if (IPC::Cmd->can_capture_buffer) {
69   my ($success, $errmsg, $output) = run command => [ qw/emerge --info/ ],
70                                         verbose => 0;
71   if ($success) {
72    for (@{$output || []}) {
73     if (/^PORTDIR_OVERLAY=(.*)$/m) {
74      my $o = $1;
75      $o =~ s/^["']*//;
76      $o =~ s/["']*$//;
77      $overlays = [ map abs_path($_), grep length, split /:/, $o ];
78     }
79    }
80   } else {
81    error $errmsg;
82   }
83  }
84
85  return 1;
86 }
87
88 sub init {
89  my ($self) = @_;
90  my $stat = $self->status;
91  my $conf = $self->parent->parent->configure_object;
92
93  $stat->mk_accessors(qw/name version author distribution desc uri src license
94                         deps eb_name eb_version eb_dir eb_file fetched_arch
95                         portdir_overlay
96                         overlay distdir keywords do_manifest header footer
97                         force verbose/);
98
99  $stat->force($conf->get_conf('force'));
100  $stat->verbose($conf->get_conf('verbose'));
101
102  return 1;
103 }
104
105 my %gentooism = (
106  'ANSIColor'               => 'Term-ANSIColor',
107  'Audio-CD'                => 'Audio-CD-disc-cover',
108  'CGI-Simple'              => 'Cgi-Simple',
109  'Cache-Mmap'              => 'cache-mmap',
110  'Class-Loader'            => 'class-loader',
111  'Class-ReturnValue'       => 'class-returnvalue',
112  'Config-General'          => 'config-general',
113  'Convert-ASCII-Armour'    => 'convert-ascii-armour',
114  'Convert-PEM'             => 'convert-pem',
115  'Crypt-CBC'               => 'crypt-cbc',
116  'Crypt-DES_EDE3'          => 'crypt-des-ede3',
117  'Crypt-DH'                => 'crypt-dh',
118  'Crypt-DSA'               => 'crypt-dsa',
119  'Crypt-IDEA'              => 'crypt-idea',
120  'Crypt-Primes'            => 'crypt-primes',
121  'Crypt-RSA'               => 'crypt-rsa',
122  'Crypt-Random'            => 'crypt-random',
123  'DBIx-SearchBuilder'      => 'dbix-searchbuilder',
124  'Data-Buffer'             => 'data-buffer',
125  'Digest'                  => 'digest-base',
126  'Digest-BubbleBabble'     => 'digest-bubblebabble',
127  'Digest-MD2'              => 'digest-md2',
128  'ExtUtils-Depends'        => 'extutils-depends',
129  'ExtUtils-PkgConfig'      => 'extutils-pkgconfig',
130  'Frontier-RPC'            => 'frontier-rpc',
131  'Gimp'                    => 'gimp-perl',
132  'Glib'                    => 'glib-perl',
133  'Gnome2-Canvas'           => 'gnome2-canvas',
134  'Gnome2-GConf'            => 'gnome2-gconf',
135  'Gnome2-Print'            => 'gnome2-print',
136  'Gnome2-VFS'              => 'gnome2-vfs-perl',
137  'Gnome2-Wnck'             => 'gnome2-wnck',
138  'Gtk2'                    => 'gtk2-perl',
139  'Gtk2-Ex-FormFactory'     => 'gtk2-ex-formfactory',
140  'Gtk2-GladeXML'           => 'gtk2-gladexml',
141  'Gtk2-Spell'              => 'gtk2-spell',
142  'Gtk2-TrayIcon'           => 'gtk2-trayicon',
143  'Gtk2-TrayManager'        => 'gtk2-traymanager',
144  'Gtk2Fu'                  => 'gtk2-fu',
145  'I18N-LangTags'           => 'i18n-langtags',
146  'Image-Info'              => 'ImageInfo',
147  'Image-Size'              => 'ImageSize',
148  'Inline-Files'            => 'inline-files',
149  'Locale-Maketext'         => 'locale-maketext',
150  'Locale-Maketext-Fuzzy'   => 'locale-maketext-fuzzy',
151  'Locale-Maketext-Lexicon' => 'locale-maketext-lexicon',
152  'Log-Dispatch'            => 'log-dispatch',
153  'Math-Pari'               => 'math-pari',
154  'Module-Info'             => 'module-info',
155  'Net-Ping'                => 'net-ping',
156  'Net-SFTP'                => 'net-sftp',
157  'Net-SSH-Perl'            => 'net-ssh-perl',
158  'Net-Server'              => 'net-server',
159  'OLE-Storage_Lite'        => 'OLE-StorageLite',
160  'Ogg-Vorbis-Header'       => 'ogg-vorbis-header',
161  'PathTools'               => 'File-Spec',
162  'Pod-Parser'              => 'PodParser',
163  'Regexp-Common'           => 'regexp-common',
164  'SDL_Perl'                => 'sdl-perl',
165  'Set-Scalar'              => 'set-scalar',
166  'String-CRC32'            => 'string-crc32',
167  'Text-Autoformat'         => 'text-autoformat',
168  'Text-Reform'             => 'text-reform',
169  'Text-Template'           => 'text-template',
170  'Text-Wrapper'            => 'text-wrapper',
171  'Tie-EncryptedHash'       => 'tie-encryptedhash',
172  'Tk'                      => 'perl-tk',
173  'Wx'                      => 'wxperl',
174  'YAML'                    => 'yaml',
175  'gettext'                 => 'Locale-gettext',
176  'txt2html'                => 'TextToHTML',
177 );
178
179 sub prepare {
180  my $self = shift;
181  my $mod  = $self->parent;
182  my $stat = $self->status;
183  my $int  = $mod->parent;
184  my $conf = $int->configure_object;
185
186  my %opts = @_;
187
188  $stat->prepared(0);
189
190  my $keywords = delete $opts{'keywords'};
191  $keywords = 'x86' unless defined $keywords;
192  $keywords = [ split ' ', $keywords ];
193  $stat->keywords($keywords);
194
195  my $manifest = delete $opts{'manifest'};
196  $manifest = 1 unless defined $manifest;
197  $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
198  $stat->do_manifest($manifest);
199
200  my $header = delete $opts{'header'};
201  if (defined $header) {
202   1 while chomp $header;
203   $header .= "\n\n";
204  } else {
205   $header = '';
206  }
207  $stat->header($header);
208
209  my $footer = delete $opts{'footer'};
210  if (defined $footer) {
211   $footer = "\n" . $footer;
212  } else {
213   $footer = '';
214  }
215  $stat->footer($footer);
216
217  my $overlay = delete $opts{'overlay'};
218  $overlay = (defined $overlay) ? abs_path $overlay : '/usr/local/portage';
219  $stat->overlay($overlay);
220
221  my $distdir = delete $opts{'distdir'};
222  $distdir = (defined $distdir) ? abs_path $distdir : '/usr/portage/distfiles';
223  $stat->distdir($distdir);
224
225  if ($stat->do_manifest && !-w $stat->distdir) {
226   error 'distdir isn\'t writable -- aborting';
227   return 0;
228  }
229  $stat->fetched_arch($mod->status->fetch);
230
231  my $cur = File::Spec::Functions::curdir();
232  my $portdir_overlay;
233  for (@$overlays) {
234   if ($_ eq $overlay or File::Spec::Functions::abs2rel($overlay, $_) eq $cur) {
235    $portdir_overlay = join ':', @$overlays;
236    last;
237   }
238  }
239  $portdir_overlay = join ':', @$overlays, $overlay
240                                                 unless defined $portdir_overlay;
241  $stat->portdir_overlay($portdir_overlay);
242
243  my $name = $mod->package_name;
244  $stat->name($name);
245
246  my $version = $mod->package_version;
247  $stat->version($version);
248
249  my $author = $mod->author->cpanid;
250  $stat->author($author);
251
252  $stat->distribution($name . '-' . $version);
253
254  $version =~ s/[^\d._]+//g;
255  $version =~ s/^[._]*//;
256  $version =~ s/[._]*$//;
257  $version =~ s/[._]*_[._]*/_/g;
258  {
259   ($version, my $patch, my @rest) = split /_/, $version;
260   $version .= '_p' . $patch if defined $patch;
261   $version .= join('.', '', @rest) if @rest;
262  }
263  $stat->eb_version($version);
264
265  $stat->eb_name($gentooism{$name} || $name);
266
267  $stat->eb_dir(catdir($stat->overlay, CATEGORY, $stat->eb_name));
268
269  my $file = catfile($stat->eb_dir,
270                     $stat->eb_name . '-' . $stat->eb_version . '.ebuild');
271  $stat->eb_file($file);
272
273  if (-e $file) {
274   my $skip = 1;
275   if ($stat->force) {
276    if (-w $file) {
277     1 while unlink $file;
278     $skip = 0;
279    } else {
280     error "Can't force rewriting of $file -- skipping";
281    }
282   } else {
283    msg 'Ebuild already generated for ' . $stat->distribution . ' -- skipping';
284   }
285   if ($skip) {
286    $stat->prepared(1);
287    $stat->created(1);
288    $stat->dist($file);
289    return 1;
290   }
291  }
292
293  $self->SUPER::prepare(%opts);
294
295  $stat->prepared(0);
296
297  my $desc = $mod->description;
298  ($desc = $name) =~ s/-+/::/g unless $desc;
299  $stat->desc($desc);
300
301  $stat->uri('http://search.cpan.org/dist/' . $name);
302
303  unless ($author =~ /^(.)(.)/) {
304   error 'Wrong author name -- aborting';
305   return 0;
306  }
307  $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/"
308             . $mod->package);
309
310  $stat->license([ qw/Artistic GPL-2/ ]);
311
312  my $prereqs = $mod->status->prereqs;
313  my @depends;
314  for my $prereq (sort keys %$prereqs) {
315   next if $prereq =~ /^perl(?:-|\z)/;
316   my $obj = $int->module_tree($prereq);
317   unless ($obj) {
318    error 'Wrong module object -- aborting';
319    return 0;
320   }
321   next if $obj->package_is_perl_core;
322   {
323    my $version;
324    if ($prereqs->{$prereq}) {
325     if ($obj->installed_version && $obj->installed_version < $obj->version) {
326      $version = $obj->installed_version;
327     } else {
328      $version = $obj->package_version;
329     }
330    }
331    push @depends, [ $obj , $version ];
332   }
333  }
334  $stat->deps(\@depends);
335
336  $stat->prepared(1);
337  return 1;
338 }
339
340 sub create {
341  my $self = shift;
342  my $stat = $self->status;
343
344  unless ($stat->prepared) {
345   error 'Can\'t create ' . $stat->distribution . ' since it was never prepared -- aborting';
346   $stat->created(0);
347   $stat->dist(undef);
348   return 0;
349  }
350
351  if ($stat->created) {
352   msg $stat->distribution . ' was already created -- skipping';
353   $stat->dist($stat->eb_file);
354   return 1;
355  }
356
357  $stat->created(0);
358  $stat->dist(undef);
359
360  $self->SUPER::create(@_);
361
362  $stat->created(0);
363  $stat->dist(undef);
364
365  my $dir = $stat->eb_dir;
366  unless (-d $dir) {
367   eval { mkpath $dir };
368   if ($@) {
369    error "mkpath($dir): $@";
370    return 0;
371   }
372  }
373
374  my $d = $stat->header;
375  $d   .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
376  $d   .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
377  $d   .= 'S="${WORKDIR}/' . $stat->distribution . "\"\n";
378  $d   .= 'DESCRIPTION="' . $stat->desc . "\"\n";
379  $d   .= 'HOMEPAGE="' . $stat->uri . "\"\n";
380  $d   .= 'SRC_URI="' . $stat->src . "\"\n";
381  $d   .= "SLOT=\"0\"\n";
382  $d   .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
383  $d   .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
384  $d   .= 'DEPEND="' . join "\n",
385   'dev-lang/perl',
386   map {
387    my $a = $_->[0]->package_name;
388    $a = $gentooism{$a} || $a;
389    my $x = '';
390    if (defined $_->[1]) {
391     $x  = '>=';
392     $a .= '-' . $_->[1];
393    }
394    '|| ( ' . join(' ', map "$x$_/$a",
395                            qw/perl-core dev-perl perl-gcpan/, CATEGORY)
396            . ' )';
397   } @{$stat->deps};
398  $d   .= "\"\n";
399  $d   .= "SRC_TEST=\"do\"\n";
400  $d   .= $stat->footer;
401
402  my $file = $stat->eb_file;
403  open my $eb, '>', $file or do {
404   error "open($file): $! -- aborting";
405   return 0;
406  };
407  print $eb $d;
408  close $eb;
409
410  if ($stat->do_manifest) {
411   unless (copy $stat->fetched_arch, $stat->distdir) {
412    error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
413    1 while unlink $file;
414    return 0;
415   }
416
417   msg 'Adding Manifest entry for ' . $stat->distribution;
418   unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
419    1 while unlink $file;
420    return 0;
421   }
422  }
423
424  $stat->created(1);
425  $stat->dist($file);
426  return 1;
427 }
428
429 sub install {
430  my $self = shift;
431  my $stat = $self->status;
432  my $conf = $self->parent->parent->configure_object;
433
434  my $sudo = $conf->get_program('sudo');
435  my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
436  unshift @cmd, $sudo if $sudo;
437
438  my $success = $self->_run(\@cmd, 1);
439  $stat->installed($success);
440
441  return $success;
442 }
443
444 sub uninstall {
445  my $self = shift;
446  my $stat = $self->status;
447  my $conf = $self->parent->parent->configure_object;
448
449  my $sudo = $conf->get_program('sudo');
450  my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
451  unshift @cmd, $sudo if $sudo;
452
453  my $success = $self->_run(\@cmd, 1);
454  $stat->uninstalled($success);
455
456  return $success;
457 }
458
459 sub _run {
460  my ($self, $cmd, $verbose) = @_;
461  my $stat = $self->status;
462
463  my ($success, $errmsg, $output) = do {
464   local $ENV{PORTDIR_OVERLAY}     = $stat->portdir_overlay;
465   local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
466   run command => $cmd, verbose => $verbose;
467  };
468
469  unless ($success) {
470   error "$errmsg -- aborting";
471   if (not $verbose and defined $output and $self->status->verbose) {
472    my $msg = join '', @$output;
473    1 while chomp $msg;
474    error $msg;
475   }
476  }
477
478  return $success;
479 }
480
481 =head1 DEPENDENCIES
482
483 Gentoo (L<http://gentoo.org>).
484
485 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5).
486
487 L<Cwd> (since perl 5) L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
488
489 =head1 SEE ALSO
490
491 L<cpan2dist>.
492
493 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
494
495 =head1 AUTHOR
496
497 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
498
499 You can contact me by mail or on C<irc.perl.org> (vincent).
500
501 =head1 BUGS
502
503 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.
504
505 =head1 SUPPORT
506
507 You can find documentation for this module with the perldoc command.
508
509     perldoc CPANPLUS::Dist::Gentoo
510
511 =head1 ACKNOWLEDGEMENTS
512
513 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
514
515 Kent Fredric, for testing and suggesting improvements.
516
517 =head1 COPYRIGHT & LICENSE
518
519 Copyright 2008 Vincent Pit, all rights reserved.
520
521 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
522
523 =cut
524
525 1; # End of CPANPLUS::Dist::Gentoo