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