]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/blob - lib/CPANPLUS/Dist/Gentoo.pm
Move the formatting of the ebuild source into a new ebuild_source() method
[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 ();
8 use File::Path ();
9 use File::Spec;
10
11 use IPC::Cmd qw/run can_run/;
12
13 use CPANPLUS::Error ();
14
15 use base qw/CPANPLUS::Dist::Base/;
16
17 use CPANPLUS::Dist::Gentoo::Maps;
18
19 =head1 NAME
20
21 CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
22
23 =head1 VERSION
24
25 Version 0.07
26
27 =cut
28
29 our $VERSION = '0.07';
30
31 =head1 SYNOPSIS
32
33     cpan2dist --format=CPANPLUS::Dist::Gentoo \
34               --dist-opts overlay=/usr/local/portage \
35               --dist-opts distdir=/usr/portage/distfiles \
36               --dist-opts manifest=yes \
37               --dist-opts keywords=x86 \
38               --dist-opts header="# Copyright 1999-2008 Gentoo Foundation" \
39               --dist-opts footer="# End" \
40               Any::Module You::Like
41
42 =head1 DESCRPITON
43
44 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.
45
46 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>.
47
48 =head1 INSTALLATION
49
50 After installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
51
52 =head1 METHODS
53
54 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.
55
56 =cut
57
58 use constant CATEGORY => 'perl-gcpanp';
59
60 my $overlays;
61 my $default_keywords;
62 my $default_distdir;
63 my $main_portdir;
64
65 my %forced;
66
67 my $unquote = sub {
68  my $s = shift;
69  $s =~ s/^["']*//;
70  $s =~ s/["']*$//;
71  return $s;
72 };
73
74 my $format_available;
75
76 sub format_available {
77  return $format_available if defined $format_available;
78
79  for my $prog (qw/emerge ebuild/) {
80   unless (can_run($prog)) {
81    __PACKAGE__->_abort("$prog is required to write ebuilds");
82    return $format_available = 0;
83   }
84  }
85
86  if (IPC::Cmd->can_capture_buffer) {
87   my $buffers;
88   my ($success, $errmsg) = run command => [ qw/emerge --info/ ],
89                                verbose => 0,
90                                buffer  => \$buffers;
91   if ($success) {
92    if ($buffers =~ /^PORTDIR_OVERLAY=(.*)$/m) {
93     $overlays = [ map abs_path($_), split ' ', $unquote->($1) ];
94    }
95    if ($buffers =~ /^ACCEPT_KEYWORDS=(.*)$/m) {
96     $default_keywords = [ split ' ', $unquote->($1) ];
97    }
98    if ($buffers =~ /^DISTDIR=(.*)$/m) {
99     $default_distdir = abs_path($unquote->($1));
100    }
101    if ($buffers =~ /^PORTDIR=(.*)$/m) {
102     $main_portdir = abs_path($unquote->($1));
103    }
104   } else {
105    __PACKAGE__->_abort($errmsg);
106   }
107  }
108
109  $default_keywords = [ 'x86' ] unless defined $default_keywords;
110  $default_distdir  = '/usr/portage/distfiles' unless defined $default_distdir;
111
112  return $format_available = 1;
113 }
114
115 sub init {
116  my ($self) = @_;
117  my $stat = $self->status;
118  my $conf = $self->parent->parent->configure_object;
119
120  $stat->mk_accessors(qw/name version author distribution desc uri src license
121                         deps eb_name eb_version eb_dir eb_file fetched_arch
122                         portdir_overlay
123                         overlay distdir keywords do_manifest header footer
124                         force verbose/);
125
126  $stat->force($conf->get_conf('force'));
127  $stat->verbose($conf->get_conf('verbose'));
128
129  return 1;
130 }
131
132 sub prepare {
133  my $self = shift;
134  my $mod  = $self->parent;
135  my $stat = $self->status;
136  my $int  = $mod->parent;
137  my $conf = $int->configure_object;
138
139  my %opts = @_;
140
141  my $OK   = sub { $stat->prepared(1); 1 };
142  my $FAIL = sub { $stat->prepared(0); $self->_abort(@_) if @_; 0 };
143
144  my $keywords = delete $opts{'keywords'};
145  if (defined $keywords) {
146   $keywords = [ split ' ', $keywords ];
147  } else {
148   $keywords = $default_keywords;
149  }
150  $stat->keywords($keywords);
151
152  my $manifest = delete $opts{'manifest'};
153  $manifest = 1 unless defined $manifest;
154  $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
155  $stat->do_manifest($manifest);
156
157  my $header = delete $opts{'header'};
158  if (defined $header) {
159   1 while chomp $header;
160   $header .= "\n\n";
161  } else {
162   $header = '';
163  }
164  $stat->header($header);
165
166  my $footer = delete $opts{'footer'};
167  if (defined $footer) {
168   $footer = "\n" . $footer;
169  } else {
170   $footer = '';
171  }
172  $stat->footer($footer);
173
174  my $overlay = delete $opts{'overlay'};
175  $overlay = (defined $overlay) ? abs_path $overlay : '/usr/local/portage';
176  $stat->overlay($overlay);
177
178  my $distdir = delete $opts{'distdir'};
179  $distdir = (defined $distdir) ? abs_path $distdir : $default_distdir;
180  $stat->distdir($distdir);
181
182  if ($stat->do_manifest && !-w $stat->distdir) {
183   return $FAIL->('distdir isn\'t writable');
184  }
185  $stat->fetched_arch($mod->status->fetch);
186
187  my $cur = File::Spec->curdir();
188  my $portdir_overlay;
189  for (@$overlays) {
190   if ($_ eq $overlay or File::Spec->abs2rel($overlay, $_) eq $cur) {
191    $portdir_overlay = [ @$overlays ];
192    last;
193   }
194  }
195  $portdir_overlay = [ @$overlays, $overlay ] unless $portdir_overlay;
196  $stat->portdir_overlay($portdir_overlay);
197
198  my $name = $mod->package_name;
199  $stat->name($name);
200
201  my $version = $mod->package_version;
202  $stat->version($version);
203
204  my $author = $mod->author->cpanid;
205  $stat->author($author);
206
207  $stat->distribution($name . '-' . $version);
208
209  $stat->eb_version(CPANPLUS::Dist::Gentoo::Maps::version_c2g($version));
210
211  $stat->eb_name(CPANPLUS::Dist::Gentoo::Maps::name_c2g($name));
212
213  $stat->eb_dir(File::Spec->catdir(
214   $stat->overlay,
215   CATEGORY,
216   $stat->eb_name,
217  ));
218
219  my $file = File::Spec->catfile(
220   $stat->eb_dir,
221   $stat->eb_name . '-' . $stat->eb_version . '.ebuild',
222  );
223  $stat->eb_file($file);
224
225  if (-e $file) {
226   my $skip = 1;
227   if ($stat->force and not $forced{$file}) {
228    if (-w $file) {
229     1 while unlink $file;
230     $forced{$file} = 1;
231     $skip = 0;
232    } else {
233     $self->_skip("Can't force rewriting of $file");
234    }
235   } else {
236    $self->_skip('Ebuild already generated for', $stat->distribution);
237   }
238   if ($skip) {
239    $stat->prepared(1);
240    $stat->created(1);
241    $stat->dist($file);
242    return 1;
243   }
244  }
245
246  $self->SUPER::prepare(%opts);
247
248  $stat->prepared(0);
249
250  my $desc = $mod->description;
251  ($desc = $name) =~ s/-+/::/g unless $desc;
252  $stat->desc($desc);
253
254  $stat->uri('http://search.cpan.org/dist/' . $name);
255
256  unless ($author =~ /^(.)(.)/) {
257   return $FAIL->('Wrong author name');
258  }
259  $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/"
260             . $mod->package);
261
262  $stat->license([ qw/Artistic GPL-2/ ]);
263
264  my $prereqs = $mod->status->prereqs;
265  my @depends;
266  for my $prereq (sort keys %$prereqs) {
267   next if $prereq =~ /^perl(?:-|\z)/;
268   my $obj = $int->module_tree($prereq);
269   return $FAIL->('Wrong module object') unless $obj;
270   next if $obj->package_is_perl_core;
271   {
272    my $version;
273    if ($prereqs->{$prereq}) {
274     if ($obj->installed_version && $obj->installed_version < $obj->version) {
275      $version = $obj->installed_version;
276     } else {
277      $version = $obj->package_version;
278     }
279    }
280    push @depends, [ $obj->package_name, $version ];
281   }
282  }
283  $stat->deps(\@depends);
284
285  return $OK->();
286 }
287
288 sub create {
289  my $self = shift;
290  my $stat = $self->status;
291
292  my $OK   = sub { $stat->created(1); $stat->dist($stat->eb_file); 1 };
293  my $FAIL = sub { $stat->created(0); $stat->dist(undef); $self->_abort(@_) if @_; 0 };
294
295  unless ($stat->prepared) {
296   return $FAIL->(
297    'Can\'t create', $stat->distribution, 'since it was never prepared'
298   );
299  }
300
301  if ($stat->created) {
302   $self->_skip($stat->distribution, 'was already created');
303   return $OK->();
304  }
305
306  my $dir = $stat->eb_dir;
307  unless (-d $dir) {
308   eval { File::Path::mkpath($dir) };
309   return $FAIL->("mkpath($dir): $@") if $@;
310  }
311
312  my $file = $stat->eb_file;
313  open my $eb, '>', $file or return $FAIL->("open($file): $!");
314  print $eb $self->ebuild_source;
315  close $eb;
316
317  $stat->created(0);
318  $stat->dist(undef);
319
320  $self->SUPER::create(@_);
321
322  if ($stat->do_manifest and not $self->update_manifest) {
323   1 while unlink $file;
324   return $FAIL->();
325  }
326
327  return $OK->();
328 }
329
330 =head2 C<update_manifest>
331
332 Update the F<Manifest> file for the ebuild associated to the current dist object.
333
334 =cut
335
336 sub update_manifest {
337  my $self = shift;
338  my $stat = $self->status;
339
340  my $file = $stat->eb_file;
341  unless ($file and -e $file) {
342   return $self->_abort('The ebuild file is invalid or does not exist');
343  }
344
345  unless (File::Copy::copy($stat->fetched_arch => $stat->distdir)) {
346   return $self->_abort("Couldn\'t copy the distribution file to distdir ($!)");
347  }
348
349  $self->_notify('Adding Manifest entry for', $stat->distribution);
350
351  return $self->_run([ 'ebuild', $stat->eb_file, 'manifest' ], 0);
352 }
353
354 =head2 C<ebuild_source>
355
356 Returns the source of the ebuild for the current dist object.
357
358 =cut
359
360 sub ebuild_source {
361  my $self = shift;
362  my $stat = $self->status;
363
364  # We must resolve the deps now and not inside prepare because _cpan2portage
365  # has to see the ebuilds already generated for the dependencies of the current
366  # dist.
367  my @deps = do {
368   my %seen;
369   sort grep !$seen{$_}++, 'dev-lang/perl',
370                           map $self->_cpan2portage(@$_), @{$stat->deps}
371  };
372
373  my $d = $stat->header;
374  $d   .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
375  $d   .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
376  $d   .= 'S="${WORKDIR}/' . $stat->distribution . "\"\n";
377  $d   .= 'DESCRIPTION="' . $stat->desc . "\"\n";
378  $d   .= 'HOMEPAGE="' . $stat->uri . "\"\n";
379  $d   .= 'SRC_URI="' . $stat->src . "\"\n";
380  $d   .= "SLOT=\"0\"\n";
381  $d   .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
382  $d   .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
383  $d   .= 'DEPEND="' . join("\n", @deps) . "\"\n";
384  $d   .= "SRC_TEST=\"do\"\n";
385  $d   .= $stat->footer;
386
387  return $d;
388 }
389
390 sub _cpan2portage {
391  my ($self, $name, $version) = @_;
392
393  $name = CPANPLUS::Dist::Gentoo::Maps::name_c2g($name);
394  my $ver;
395  $ver = CPANPLUS::Dist::Gentoo::Maps::version_c2g($version) if defined $version;
396
397  my @portdirs = ($main_portdir, @{$self->status->portdir_overlay});
398
399  for my $category (qw/virtual perl-core dev-perl perl-gcpan/, CATEGORY) {
400   my $atom = ($category eq 'virtual' ? 'perl-' : '') . $name;
401
402   for my $portdir (@portdirs) {
403    my @ebuilds = glob File::Spec->catfile(
404     $portdir,
405     $category,
406     $atom,
407     "$atom-*.ebuild",
408    ) or next;
409
410    if (defined $ver) { # implies that $version is defined
411     for (@ebuilds) {
412      my ($eb_ver) = /\Q$atom\E-v?([\d._pr-]+).*?\.ebuild$/;
413      return ">=$category/$atom-$ver"
414             if  defined $eb_ver
415             and CPANPLUS::Dist::Gentoo::Maps::version_gcmp($eb_ver, $ver) > 0;
416     }
417    } else {
418     return "$category/$atom";
419    }
420
421   }
422
423  }
424
425  $self->_skip(
426   "Couldn't find an appropriate ebuild for $name in the portage tree"
427  );
428
429  return '';
430 }
431
432 sub install {
433  my $self = shift;
434  my $stat = $self->status;
435  my $conf = $self->parent->parent->configure_object;
436
437  my $sudo = $conf->get_program('sudo');
438  my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
439  unshift @cmd, $sudo if $sudo;
440
441  my $success = $self->_run(\@cmd, 1);
442  $stat->installed($success);
443
444  return $success;
445 }
446
447 sub uninstall {
448  my $self = shift;
449  my $stat = $self->status;
450  my $conf = $self->parent->parent->configure_object;
451
452  my $sudo = $conf->get_program('sudo');
453  my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
454  unshift @cmd, $sudo if $sudo;
455
456  my $success = $self->_run(\@cmd, 1);
457  $stat->uninstalled($success);
458
459  return $success;
460 }
461
462 sub _run {
463  my ($self, $cmd, $verbose) = @_;
464  my $stat = $self->status;
465
466  my ($success, $errmsg, $output) = do {
467   local $ENV{PORTDIR_OVERLAY}     = join ' ', @{$stat->portdir_overlay};
468   local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
469   run command => $cmd, verbose => $verbose;
470  };
471
472  unless ($success) {
473   $self->_abort($errmsg);
474   if (not $verbose and defined $output and $stat->verbose) {
475    my $msg = join '', @$output;
476    1 while chomp $msg;
477    CPANPLUS::Error::error($msg);
478   }
479  }
480
481  return $success;
482 }
483
484 sub _abort {
485  my $self = shift;
486
487  CPANPLUS::Error::error("@_ -- aborting");
488
489  return 0;
490 }
491
492 sub _notify {
493  my $self = shift;
494
495  CPANPLUS::Error::msg("@_");
496
497  return 1;
498 }
499
500 sub _skip { shift->_notify(@_, '-- skipping') }
501
502 =head1 DEPENDENCIES
503
504 Gentoo (L<http://gentoo.org>).
505
506 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5).
507
508 L<Cwd>, L<Carp> (since perl 5), L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec> (5.00405).
509
510 =head1 SEE ALSO
511
512 L<cpan2dist>.
513
514 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
515
516 =head1 AUTHOR
517
518 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
519
520 You can contact me by mail or on C<irc.perl.org> (vincent).
521
522 =head1 BUGS
523
524 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.
525
526 =head1 SUPPORT
527
528 You can find documentation for this module with the perldoc command.
529
530     perldoc CPANPLUS::Dist::Gentoo
531
532 =head1 ACKNOWLEDGEMENTS
533
534 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
535
536 Kent Fredric, for testing and suggesting improvements.
537
538 =head1 COPYRIGHT & LICENSE
539
540 Copyright 2008-2009 Vincent Pit, all rights reserved.
541
542 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
543
544 =cut
545
546 1; # End of CPANPLUS::Dist::Gentoo