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