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