]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/blob - lib/CPANPLUS/Dist/Gentoo.pm
72450d70784f488015170341ff0f948df60a4d87
[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 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  $stat->license([ qw/Artistic GPL-2/ ]);
326
327  my $prereqs = $mod->status->prereqs;
328  my @depends;
329  for my $prereq (sort keys %$prereqs) {
330   next if $prereq =~ /^perl(?:-|\z)/;
331   my $obj = $int->module_tree($prereq);
332   unless ($obj) {
333    error 'Wrong module object -- aborting';
334    return 0;
335   }
336   next if $obj->package_is_perl_core;
337   {
338    my $version;
339    if ($prereqs->{$prereq}) {
340     if ($obj->installed_version && $obj->installed_version < $obj->version) {
341      $version = $obj->installed_version;
342     } else {
343      $version = $obj->package_version;
344     }
345    }
346    push @depends, [ $obj->package_name, $version ];
347   }
348  }
349  $stat->deps(\@depends);
350
351  $stat->prepared(1);
352  return 1;
353 }
354
355 sub create {
356  my $self = shift;
357  my $stat = $self->status;
358
359  unless ($stat->prepared) {
360   error 'Can\'t create ' . $stat->distribution . ' since it was never prepared -- aborting';
361   $stat->created(0);
362   $stat->dist(undef);
363   return 0;
364  }
365
366  if ($stat->created) {
367   msg $stat->distribution . ' was already created -- skipping';
368   $stat->dist($stat->eb_file);
369   return 1;
370  }
371
372  $stat->created(0);
373  $stat->dist(undef);
374
375  $self->SUPER::create(@_);
376
377  $stat->created(0);
378  $stat->dist(undef);
379
380  my $dir = $stat->eb_dir;
381  unless (-d $dir) {
382   eval { mkpath $dir };
383   if ($@) {
384    error "mkpath($dir): $@";
385    return 0;
386   }
387  }
388
389  my $d = $stat->header;
390  $d   .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
391  $d   .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
392  $d   .= 'DESCRIPTION="' . $stat->desc . "\"\n";
393  $d   .= 'HOMEPAGE="' . $stat->uri . "\"\n\n";
394  $d   .= "SLOT=\"0\"\n";
395  $d   .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
396  $d   .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
397  $d   .= 'DEPEND="' . join("\n",
398   'dev-lang/perl',
399   map $self->_cpan2portage(@$_), @{$stat->deps}
400  ) . "\"\n";
401  $d   .= "SRC_TEST=\"do\"\n";
402  $d   .= $stat->footer;
403
404  my $file = $stat->eb_file;
405  open my $eb, '>', $file or do {
406   error "open($file): $! -- aborting";
407   return 0;
408  };
409  print $eb $d;
410  close $eb;
411
412  if ($stat->do_manifest) {
413   unless (copy $stat->fetched_arch, $stat->distdir) {
414    error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
415    1 while unlink $file;
416    return 0;
417   }
418
419   msg 'Adding Manifest entry for ' . $stat->distribution;
420   unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
421    1 while unlink $file;
422    return 0;
423   }
424  }
425
426  $stat->created(1);
427  $stat->dist($file);
428  return 1;
429 }
430
431 sub _cpan2portage {
432  my ($self, $name, $version) = @_;
433
434  $name = $gentooism{$name} || $name;
435  my $ver;
436  $ver = eval { version->new($version) } if defined $version;
437
438  my @portdirs = ($main_portdir, @{$self->status->portdir_overlay});
439
440  for my $category (qw/virtual perl-core dev-perl perl-gcpan/, CATEGORY) {
441   my $atom = ($category eq 'virtual' ? 'perl-' : '') . $name;
442
443   for my $portdir (@portdirs) {
444    my @ebuilds = glob catfile($portdir, $category, $atom,"$atom-*.ebuild");
445    next unless @ebuilds;
446
447    if (defined $version) {
448     for (@ebuilds) {
449      next unless /\Q$atom\E-v?([\d._]+).*?\.ebuild$/;
450      my $eb_ver = eval { version->new($1) };
451      next unless defined $eb_ver and $eb_ver >= $ver;
452      return ">=$category/$atom-$version";
453     }
454    } else {
455     return "$category/$atom";
456    }
457
458   }
459
460  }
461
462  error "Couldn't find an appropriate ebuild for $name in the portage tree -- skipping";
463  return '';
464 }
465
466 sub install {
467  my $self = shift;
468  my $stat = $self->status;
469  my $conf = $self->parent->parent->configure_object;
470
471  my $sudo = $conf->get_program('sudo');
472  my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
473  unshift @cmd, $sudo if $sudo;
474
475  my $success = $self->_run(\@cmd, 1);
476  $stat->installed($success);
477
478  return $success;
479 }
480
481 sub uninstall {
482  my $self = shift;
483  my $stat = $self->status;
484  my $conf = $self->parent->parent->configure_object;
485
486  my $sudo = $conf->get_program('sudo');
487  my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
488  unshift @cmd, $sudo if $sudo;
489
490  my $success = $self->_run(\@cmd, 1);
491  $stat->uninstalled($success);
492
493  return $success;
494 }
495
496 sub _run {
497  my ($self, $cmd, $verbose) = @_;
498  my $stat = $self->status;
499
500  my ($success, $errmsg, $output) = do {
501   local $ENV{PORTDIR_OVERLAY}     = join ' ', @{$stat->portdir_overlay};
502   local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
503   run command => $cmd, verbose => $verbose;
504  };
505
506  unless ($success) {
507   error "$errmsg -- aborting";
508   if (not $verbose and defined $output and $self->status->verbose) {
509    my $msg = join '', @$output;
510    1 while chomp $msg;
511    error $msg;
512   }
513  }
514
515  return $success;
516 }
517
518 =head1 DEPENDENCIES
519
520 Gentoo (L<http://gentoo.org>).
521
522 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5), L<version> (since 5.009).
523
524 L<Cwd> (since perl 5) L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
525
526 =head1 SEE ALSO
527
528 L<cpan2dist>.
529
530 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
531
532 =head1 AUTHOR
533
534 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
535
536 You can contact me by mail or on C<irc.perl.org> (vincent).
537
538 =head1 BUGS
539
540 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.
541
542 =head1 SUPPORT
543
544 You can find documentation for this module with the perldoc command.
545
546     perldoc CPANPLUS::Dist::Gentoo
547
548 =head1 ACKNOWLEDGEMENTS
549
550 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
551
552 Kent Fredric, for testing and suggesting improvements.
553
554 =head1 COPYRIGHT & LICENSE
555
556 Copyright 2008 Vincent Pit, all rights reserved.
557
558 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
559
560 =cut
561
562 1; # End of CPANPLUS::Dist::Gentoo