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