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