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