]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/blob - lib/CPANPLUS/Dist/Gentoo.pm
2efbffa4608cae3c2be0273e73d916811eae51f1
[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 =head1 NAME
18
19 CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
20
21 =head1 VERSION
22
23 Version 0.04
24
25 =cut
26
27 our $VERSION = '0.04';
28
29 =head1 SYNOPSIS
30
31     cpan2dist --format=CPANPLUS::Dist::Gentoo \
32               --dist-opts overlay=/usr/local/portage \
33               --dist-opts distdir=/usr/portage/distfiles \
34               --dist-opts manifest=yes \
35               --dist-opts keywords=x86 \
36               --dist-opts header="# Copyright 1999-2008 Gentoo Foundation" \
37               --dist-opts footer="# End" \
38               Any::Module You::Like
39
40 =head1 DESCRPITON
41
42 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>). You also need to specify the correct keyword for your architecture if it differs from the default C<x86>.
43
44 The generated ebuilds are placed into the C<perl-gcpanp> category. They favour depending on C<perl-core>, C<dev-perl> or C<perl-gcpan> (in that order) rather than C<perl-gcpanp>.
45
46 =head1 INSTALLATION
47
48 After installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
49
50 =head1 METHODS
51
52 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.
53
54 =cut
55
56 use constant CATEGORY => 'perl-gcpanp';
57
58 sub format_available {
59  for my $prog (qw/emerge ebuild/) {
60   unless (can_run($prog)) {
61    error "$prog is required to write ebuilds -- aborting";
62    return 0;
63   }
64  }
65  return 1;
66 }
67
68 sub init {
69  my ($self) = @_;
70  my $stat = $self->status;
71  my $conf = $self->parent->parent->configure_object;
72
73  $stat->mk_accessors(qw/name version author distribution desc uri src license
74                         deps eb_name eb_version eb_dir eb_file fetched_arch
75                         overlay distdir keywords do_manifest header footer
76                         force verbose/);
77
78  $stat->force($conf->get_conf('force'));
79  $stat->verbose($conf->get_conf('verbose'));
80
81  return 1;
82 }
83
84 my %gentooism = (
85  'Crypt-RSA'         => 'crypt-rsa',
86  'Digest'            => 'digest-base',
87  'Locale-Maketext'   => 'locale-maketext',
88  'Math-Pari'         => 'math-pari',
89  'Net-Ping'          => 'net-ping',
90  'PathTools'         => 'File-Spec',
91  'PodParser'         => 'Pod-Parser',
92  'Set-Scalar'        => 'set-scalar',
93  'Tie-EncryptedHash' => 'tie-encryptedhash',
94  'YAML'              => 'yaml',
95 );
96
97 sub prepare {
98  my $self = shift;
99  my $mod  = $self->parent;
100  my $stat = $self->status;
101  my $int  = $mod->parent;
102  my $conf = $int->configure_object;
103
104  my %opts = @_;
105
106  $stat->prepared(0);
107
108  my $keywords = delete $opts{'keywords'};
109  $keywords = 'x86' unless defined $keywords;
110  $keywords = [ split ' ', $keywords ];
111  $stat->keywords($keywords);
112
113  my $manifest = delete $opts{'manifest'};
114  $manifest = 1 unless defined $manifest;
115  $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
116  $stat->do_manifest($manifest);
117
118  my $header = delete $opts{'header'};
119  if (defined $header) {
120   1 while chomp $header;
121   $header .= "\n\n";
122  } else {
123   $header = '';
124  }
125  $stat->header($header);
126
127  my $footer = delete $opts{'footer'};
128  if (defined $footer) {
129   $footer = "\n" . $footer;
130  } else {
131   $footer = '';
132  }
133  $stat->footer($footer);
134
135  my $overlay = delete $opts{'overlay'};
136  $overlay = (defined $overlay) ? abs_path $overlay : '/usr/local/portage';
137  $stat->overlay($overlay);
138
139  my $distdir = delete $opts{'distdir'};
140  $distdir = (defined $distdir) ? abs_path $distdir : '/usr/portage/distfiles';
141  $stat->distdir($distdir);
142
143  if ($stat->do_manifest && !-w $stat->distdir) {
144   error 'distdir isn\'t writable -- aborting';
145   return 0;
146  }
147  $stat->fetched_arch($mod->status->fetch);
148
149  my $name = $mod->package_name;
150  $stat->name($name);
151
152  my $version = $mod->package_version;
153  $stat->version($version);
154
155  my $author = $mod->author->cpanid;
156  $stat->author($author);
157
158  $stat->distribution($name . '-' . $version);
159
160  $version =~ s/[^\d._]+//g;
161  $version =~ s/^[._]*//;
162  $version =~ s/[._]*$//;
163  $version =~ s/[._]*_[._]*/_/g;
164  {
165   ($version, my $patch, my @rest) = split /_/, $version;
166   $version .= '_p' . $patch if defined $patch;
167   $version .= join('.', '', @rest) if @rest;
168  }
169  $stat->eb_version($version);
170
171  $stat->eb_name($gentooism{$name} || $name);
172
173  $stat->eb_dir(catdir($stat->overlay, CATEGORY, $stat->eb_name));
174
175  my $file = catfile($stat->eb_dir,
176                     $stat->eb_name . '-' . $stat->eb_version . '.ebuild');
177  if (-e $file) {
178   my $skip = 1;
179   if ($stat->force) {
180    if (-w $file) {
181     1 while unlink $file;
182     $skip = 0;
183    } else {
184     error "Can't force rewriting of $file -- skipping";
185    }
186   } else {
187    msg 'Ebuild already generated for ' . $stat->distribution . ' -- skipping';
188   }
189   if ($skip) {
190    $stat->prepared(1);
191    $stat->created(1);
192    $stat->dist($file);
193    return 1;
194   }
195  }
196  $stat->eb_file($file);
197
198  $self->SUPER::prepare(%opts);
199
200  my $desc = $mod->description;
201  ($desc = $name) =~ s/-+/::/g unless $desc;
202  $stat->desc($desc);
203
204  $stat->uri('http://search.cpan.org/dist/' . $name);
205
206  unless ($author =~ /^(.)(.)/) {
207   error 'Wrong author name -- aborting';
208   return 0;
209  }
210  $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/"
211             . $mod->package);
212
213  $stat->license([ qw/Artistic GPL-2/ ]);
214
215  my $prereqs = $mod->status->prereqs;
216  my @depends;
217  for my $prereq (sort keys %$prereqs) {
218   next if $prereq =~ /^perl(?:-|\z)/;
219   my $obj = $int->module_tree($prereq);
220   unless ($obj) {
221    error 'Wrong module object -- aborting';
222    return 0;
223   }
224   next if $obj->package_is_perl_core;
225   {
226    my $version;
227    if ($prereqs->{$prereq}) {
228     if ($obj->installed_version && $obj->installed_version < $obj->version) {
229      $version = $obj->installed_version;
230     } else {
231      $version = $obj->package_version;
232     }
233    }
234    push @depends, [ $obj , $version ];
235   }
236  }
237  $stat->deps(\@depends);
238
239  $stat->prepared(1);
240  return 1;
241 }
242
243 sub create {
244  my $self = shift;
245  my $stat = $self->status;
246
247  unless ($stat->prepared) {
248   error 'Can\'t create ' . $stat->distribution . ' since it was never prepared -- aborting';
249   $stat->created(0);
250   $stat->dist(undef);
251   return 0;
252  }
253
254  if ($stat->created) {
255   msg $stat->distribution . ' was already created -- skipping';
256   $stat->dist($stat->eb_file);
257   return 1;
258  }
259
260  $stat->created(0);
261  $stat->dist(undef);
262
263  $self->SUPER::create(@_);
264
265  my $dir = $stat->eb_dir;
266  unless (-d $dir) {
267   eval { mkpath $dir };
268   if ($@) {
269    error "mkpath($dir): $@";
270    return 0;
271   }
272  }
273
274  my $d = $stat->header;
275  $d   .= "# Generated by CPANPLUS::Dist::Gentoo version $VERSION\n\n";
276  $d   .= 'MODULE_AUTHOR="' . $stat->author . "\"\ninherit perl-module\n\n";
277  $d   .= 'S="${WORKDIR}/' . $stat->distribution . "\"\n";
278  $d   .= 'DESCRIPTION="' . $stat->desc . "\"\n";
279  $d   .= 'HOMEPAGE="' . $stat->uri . "\"\n";
280  $d   .= 'SRC_URI="' . $stat->src . "\"\n";
281  $d   .= "SLOT=\"0\"\n";
282  $d   .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
283  $d   .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
284  $d   .= 'DEPEND="' . join "\n",
285   'dev-lang/perl',
286   map {
287    my $a = $_->[0]->package_name;
288    $a = $gentooism{$a} || $a;
289    my $x = '';
290    if (defined $_->[1]) {
291     $x  = '>=';
292     $a .= '-' . $_->[1];
293    }
294    '|| ( ' . join(' ', map "$x$_/$a",
295                            qw/perl-core dev-perl perl-gcpan/, CATEGORY)
296            . ' )';
297   } @{$stat->deps};
298  $d   .= "\"\n";
299  $d   .= "SRC_TEST=\"do\"\n";
300  $d   .= $stat->footer;
301
302  my $file = $stat->eb_file;
303  open my $eb, '>', $file or do {
304   error "open($file): $! -- aborting";
305   return 0;
306  };
307  print $eb $d;
308  close $eb;
309
310  if ($stat->do_manifest) {
311   unless (copy $stat->fetched_arch, $stat->distdir) {
312    error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
313    1 while unlink $file;
314    return 0;
315   }
316
317   msg 'Adding Manifest entry for ' . $stat->distribution;
318   unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
319    1 while unlink $file;
320    return 0;
321   }
322  }
323
324  $stat->created(1);
325  $stat->dist($file);
326  return 1;
327 }
328
329 sub install {
330  my $self = shift;
331  my $stat = $self->status;
332  my $conf = $self->parent->parent->configure_object;
333
334  my $sudo = $conf->get_program('sudo');
335  my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
336  unshift @cmd, $sudo if $sudo;
337
338  my $success = $self->_run(\@cmd, 1);
339  $stat->installed($success);
340
341  return $success;
342 }
343
344 sub uninstall {
345  my $self = shift;
346  my $stat = $self->status;
347  my $conf = $self->parent->parent->configure_object;
348
349  my $sudo = $conf->get_program('sudo');
350  my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
351  unshift @cmd, $sudo if $sudo;
352
353  my $success = $self->_run(\@cmd, 1);
354  $stat->uninstalled($success);
355
356  return $success;
357 }
358
359 sub _run {
360  my ($self, $cmd, $verbose) = @_;
361  my $stat = $self->status;
362
363  my ($success, $errmsg, $output) = do {
364   local $ENV{PORTDIR_OVERLAY}     = $stat->overlay;
365   local $ENV{PORTAGE_RO_DISTDIRS} = $stat->distdir;
366   run command => $cmd, verbose => $verbose;
367  };
368
369  unless ($success) {
370   error "$errmsg -- aborting";
371   if (not $verbose and defined $output and $self->status->verbose) {
372    my $msg = join '', @$output;
373    1 while chomp $msg;
374    error $msg;
375   }
376  }
377
378  return $success;
379 }
380
381 =head1 DEPENDENCIES
382
383 Gentoo (L<http://gentoo.org>).
384
385 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5).
386
387 L<Cwd> (since perl 5) L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
388
389 =head1 SEE ALSO
390
391 L<cpan2dist>.
392
393 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
394
395 =head1 AUTHOR
396
397 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
398
399 You can contact me by mail or on C<irc.perl.org> (vincent).
400
401 =head1 BUGS
402
403 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.
404
405 =head1 SUPPORT
406
407 You can find documentation for this module with the perldoc command.
408
409     perldoc CPANPLUS::Dist::Gentoo
410
411 =head1 ACKNOWLEDGEMENTS
412
413 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
414
415 Kent Fredric, for testing and suggesting improvements.
416
417 =head1 COPYRIGHT & LICENSE
418
419 Copyright 2008 Vincent Pit, all rights reserved.
420
421 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
422
423 =cut
424
425 1; # End of CPANPLUS::Dist::Gentoo