]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/blob - lib/CPANPLUS/Dist/Gentoo.pm
Properly asset the situation at the end of prepare()/create()
[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    error "$prog is required to write ebuilds -- aborting";
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    error $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); 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   error 'distdir isn\'t writable -- aborting';
184   return $FAIL->();
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->eb_version(CPANPLUS::Dist::Gentoo::Maps::version_c2g($version));
211
212  $stat->eb_name(CPANPLUS::Dist::Gentoo::Maps::name_c2g($name));
213
214  $stat->eb_dir(File::Spec->catdir(
215   $stat->overlay,
216   CATEGORY,
217   $stat->eb_name,
218  ));
219
220  my $file = File::Spec->catfile(
221   $stat->eb_dir,
222   $stat->eb_name . '-' . $stat->eb_version . '.ebuild',
223  );
224  $stat->eb_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     error "Can't force rewriting of $file -- skipping";
235    }
236   } else {
237    msg 'Ebuild already generated for ' . $stat->distribution . ' -- skipping';
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   error 'Wrong author name -- aborting';
259   return $FAIL->();
260  }
261  $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/"
262             . $mod->package);
263
264  $stat->license([ qw/Artistic GPL-2/ ]);
265
266  my $prereqs = $mod->status->prereqs;
267  my @depends;
268  for my $prereq (sort keys %$prereqs) {
269   next if $prereq =~ /^perl(?:-|\z)/;
270   my $obj = $int->module_tree($prereq);
271   unless ($obj) {
272    error 'Wrong module object -- aborting';
273    return $FAIL->();
274   }
275   next if $obj->package_is_perl_core;
276   {
277    my $version;
278    if ($prereqs->{$prereq}) {
279     if ($obj->installed_version && $obj->installed_version < $obj->version) {
280      $version = $obj->installed_version;
281     } else {
282      $version = $obj->package_version;
283     }
284    }
285    push @depends, [ $obj->package_name, $version ];
286   }
287  }
288  $stat->deps(\@depends);
289
290  return $OK->();
291 }
292
293 sub create {
294  my $self = shift;
295  my $stat = $self->status;
296
297  my $OK   = sub { $stat->created(1); $stat->dist($stat->eb_file); 1 };
298  my $FAIL = sub { $stat->created(0); $stat->dist(undef);          0 };
299
300  unless ($stat->prepared) {
301   error 'Can\'t create ' . $stat->distribution . ' since it was never prepared -- aborting';
302   return $FAIL->();
303  }
304
305  if ($stat->created) {
306   msg $stat->distribution . ' was already created -- skipping';
307   return $OK->();
308  }
309
310  my $dir = $stat->eb_dir;
311  unless (-d $dir) {
312   eval { File::Path::mkpath($dir) };
313   if ($@) {
314    error "mkpath($dir): $@";
315    return $FAIL->();
316   }
317  }
318
319  my %seen;
320
321  my $d = $stat->header;
322  $d   .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
323  $d   .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
324  $d   .= 'S="${WORKDIR}/' . $stat->distribution . "\"\n";
325  $d   .= 'DESCRIPTION="' . $stat->desc . "\"\n";
326  $d   .= 'HOMEPAGE="' . $stat->uri . "\"\n";
327  $d   .= 'SRC_URI="' . $stat->src . "\"\n";
328  $d   .= "SLOT=\"0\"\n";
329  $d   .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
330  $d   .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
331  $d   .= 'DEPEND="' . join("\n",
332   sort grep !$seen{$_}++, 'dev-lang/perl',
333                           map $self->_cpan2portage(@$_), @{$stat->deps}
334  ) . "\"\n";
335  $d   .= "SRC_TEST=\"do\"\n";
336  $d   .= $stat->footer;
337
338  my $file = $stat->eb_file;
339  open my $eb, '>', $file or do {
340   error "open($file): $! -- aborting";
341   return $FAIL->();
342  };
343  print $eb $d;
344  close $eb;
345
346  $stat->created(0);
347  $stat->dist(undef);
348
349  $self->SUPER::create(@_);
350
351  if ($stat->do_manifest) {
352   unless (File::Copy::copy($stat->fetched_arch => $stat->distdir)) {
353    error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
354    1 while unlink $file;
355    return $FAIL->();
356   }
357
358   msg 'Adding Manifest entry for ' . $stat->distribution;
359   unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
360    1 while unlink $file;
361    return $FAIL->();
362   }
363  }
364
365  return $OK->();
366 }
367
368 sub _cpan2portage {
369  my ($self, $name, $version) = @_;
370
371  $name = CPANPLUS::Dist::Gentoo::Maps::name_c2g($name);
372  my $ver;
373  $ver = CPANPLUS::Dist::Gentoo::Maps::version_c2g($version) if defined $version;
374
375  my @portdirs = ($main_portdir, @{$self->status->portdir_overlay});
376
377  for my $category (qw/virtual perl-core dev-perl perl-gcpan/, CATEGORY) {
378   my $atom = ($category eq 'virtual' ? 'perl-' : '') . $name;
379
380   for my $portdir (@portdirs) {
381    my @ebuilds = glob File::Spec->catfile(
382     $portdir,
383     $category,
384     $atom,
385     "$atom-*.ebuild",
386    ) or next;
387
388    if (defined $ver) { # implies that $version is defined
389     for (@ebuilds) {
390      my ($eb_ver) = /\Q$atom\E-v?([\d._pr-]+).*?\.ebuild$/;
391      return ">=$category/$atom-$ver"
392             if  defined $eb_ver
393             and CPANPLUS::Dist::Gentoo::Maps::version_gcmp($eb_ver, $ver) > 0;
394     }
395    } else {
396     return "$category/$atom";
397    }
398
399   }
400
401  }
402
403  error "Couldn't find an appropriate ebuild for $name in the portage tree -- skipping";
404  return '';
405 }
406
407 sub install {
408  my $self = shift;
409  my $stat = $self->status;
410  my $conf = $self->parent->parent->configure_object;
411
412  my $sudo = $conf->get_program('sudo');
413  my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
414  unshift @cmd, $sudo if $sudo;
415
416  my $success = $self->_run(\@cmd, 1);
417  $stat->installed($success);
418
419  return $success;
420 }
421
422 sub uninstall {
423  my $self = shift;
424  my $stat = $self->status;
425  my $conf = $self->parent->parent->configure_object;
426
427  my $sudo = $conf->get_program('sudo');
428  my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
429  unshift @cmd, $sudo if $sudo;
430
431  my $success = $self->_run(\@cmd, 1);
432  $stat->uninstalled($success);
433
434  return $success;
435 }
436
437 sub _run {
438  my ($self, $cmd, $verbose) = @_;
439  my $stat = $self->status;
440
441  my ($success, $errmsg, $output) = do {
442   local $ENV{PORTDIR_OVERLAY}     = join ' ', @{$stat->portdir_overlay};
443   local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
444   run command => $cmd, verbose => $verbose;
445  };
446
447  unless ($success) {
448   error "$errmsg -- aborting";
449   if (not $verbose and defined $output and $stat->verbose) {
450    my $msg = join '', @$output;
451    1 while chomp $msg;
452    error $msg;
453   }
454  }
455
456  return $success;
457 }
458
459 =head1 DEPENDENCIES
460
461 Gentoo (L<http://gentoo.org>).
462
463 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5).
464
465 L<Cwd>, L<Carp> (since perl 5), L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec> (5.00405).
466
467 =head1 SEE ALSO
468
469 L<cpan2dist>.
470
471 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
472
473 =head1 AUTHOR
474
475 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
476
477 You can contact me by mail or on C<irc.perl.org> (vincent).
478
479 =head1 BUGS
480
481 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.
482
483 =head1 SUPPORT
484
485 You can find documentation for this module with the perldoc command.
486
487     perldoc CPANPLUS::Dist::Gentoo
488
489 =head1 ACKNOWLEDGEMENTS
490
491 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
492
493 Kent Fredric, for testing and suggesting improvements.
494
495 =head1 COPYRIGHT & LICENSE
496
497 Copyright 2008-2009 Vincent Pit, all rights reserved.
498
499 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
500
501 =cut
502
503 1; # End of CPANPLUS::Dist::Gentoo