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