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