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