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