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