]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/blob - lib/CPANPLUS/Dist/Gentoo.pm
Move all the "command running" logic into a _run private method
[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 distdir fetched_arch
72                         keywords do_manifest
73                         verbose/);
74
75  $stat->verbose($conf->get_conf('verbose'));
76
77  return 1;
78 }
79
80 my %gentooism = (
81  'Digest'            => 'digest-base',
82  'Locale-Maketext'   => 'locale-maketext',
83  'Net-Ping'          => 'net-ping',
84  'PathTools'         => 'File-Spec',
85  'PodParser'         => 'Pod-Parser',
86  'Set-Scalar'        => 'set-scalar',
87  'Tie-EncryptedHash' => 'tie-encryptedhash',
88 );
89
90 sub prepare {
91  my $self = shift;
92  my $mod  = $self->parent;
93  my $stat = $self->status;
94  my $int  = $mod->parent;
95  my $conf = $int->configure_object;
96
97  my %opts = @_;
98
99  my $keywords = delete $opts{'keywords'};
100  $keywords = 'x86' unless defined $keywords;
101  $keywords = [ split ' ', $keywords ];
102  $stat->keywords($keywords);
103
104  my $manifest = delete $opts{'manifest'};
105  $manifest = 1 unless defined $manifest;
106  $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
107  $stat->do_manifest($manifest);
108
109  my $overlay = catdir(delete($opts{'overlay'}) || '/usr/local/portage',
110                       CATEGORY);
111
112  $stat->distdir(delete($opts{'distdir'}) || '/usr/portage/distfiles');
113  if ($stat->do_manifest && !-w $stat->distdir) {
114   error 'distdir isn\'t writable -- aborting';
115   return 0;
116  }
117  $stat->fetched_arch($mod->status->fetch);
118
119  my $name = $mod->package_name;
120  $stat->name($name);
121
122  my $version = $mod->package_version;
123  $stat->version($version);
124  $stat->dist($name . '-' . $version);
125  my $f = 1;
126  $version =~ s/_+/$f ? do { $f = 0; '_p' } : ''/ge;
127  1 while $version =~ s/(_p[^.]*)\.+/$1/;
128  $stat->eb_version($version);
129
130  $stat->eb_name($gentooism{$stat->name} || $stat->name);
131  $stat->eb_dir(catdir($overlay, $stat->eb_name));
132  $stat->eb_file(catfile($stat->eb_dir,
133                         $stat->eb_name . '-' . $stat->eb_version . '.ebuild'));
134  if (-r $stat->eb_file) {
135   msg 'Ebuild already generated for ' . $stat->dist . ' -- skipping';
136   $stat->prepared(1);
137   $stat->created(1);
138   return 1;
139  }
140
141  $self->SUPER::prepare(%opts);
142
143  my $desc = $mod->description;
144  ($desc = $name) =~ s/-+/::/g unless $desc;
145  $stat->desc($desc);
146  $stat->uri('http://search.cpan.org/dist/' . $name);
147  unless ($name =~ /^([^-]+)/) {
148   error 'Wrong distribution name -- aborting';
149   return 0;
150  }
151  $stat->src('mirror://cpan/modules/by-module/' . $1 . '/' . $mod->package);
152  $stat->license([ qw/Artistic GPL-2/ ]);
153
154  my $prereqs = $mod->status->prereqs;
155  $prereqs = { map { ($gentooism{$_} || $_) => $prereqs->{$_} } keys %$prereqs };
156  my @depends;
157  for my $prereq (sort keys %$prereqs) {
158   next if $prereq =~ /^perl(?:-|\z)/;
159   my $obj = $int->module_tree($prereq);
160   unless ($obj) {
161    error 'Wrong module object -- aborting';
162    return 0;
163   }
164   next if $obj->package_is_perl_core;
165   {
166    my $version;
167    if ($prereqs->{$prereq}) {
168     if ($obj->installed_version && $obj->installed_version < $obj->version) {
169      $version = $obj->installed_version;
170     } else {
171      $version = $obj->package_version;
172     }
173    }
174    push @depends, [ $obj , $version ];
175   }
176  }
177  $stat->deps(\@depends);
178
179  return 1;
180 }
181
182 sub create {
183  my $self = shift;
184  my $stat = $self->status;
185
186  unless ($stat->prepared) {
187   error 'Can\'t create ' . $stat->dist . ' since it was never prepared -- aborting';
188   return 0;
189  }
190
191  if ($stat->created) {
192   msg $stat->dist . ' was already created -- skipping';
193   return 1;
194  }
195
196  $self->SUPER::create(@_);
197
198  my $dir = $stat->eb_dir;
199  unless (-d $dir) {
200   eval { mkpath $dir };
201   if ($@) {
202    error "mkpath($dir): $@";
203    return 0;
204   }
205  }
206
207  my $d = "# Generated by CPANPLUS::Dist::Gentoo\n\ninherit perl-module\n\n";
208  $d   .= 'S="${WORKDIR}/' . $stat->dist . "\"\n";
209  $d   .= 'DESCRIPTION="' . $stat->desc . "\"\n";
210  $d   .= 'HOMEPAGE="' . $stat->uri . "\"\n";
211  $d   .= 'SRC_URI="' . $stat->src . "\"\n";
212  $d   .= "SLOT=\"0\"\n";
213  $d   .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
214  $d   .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
215  $d   .= 'DEPEND="' . join "\n",
216   'dev-lang/perl',
217   map {
218    my $a = $_->[0]->package_name;
219    my $x = '';
220    if (defined $_->[1]) {
221     $x  = '>=';
222     $a .= '-' . $_->[1];
223    }
224    '|| ( ' . join(' ', map "$x$_/$a",
225                            qw/perl-core dev-perl perl-gcpan/, CATEGORY)
226            . ' )';
227   } @{$stat->deps};
228  $d   .= "\"\n";
229
230  my $file = $stat->eb_file;
231  open my $eb, '>', $file or do {
232   error "open($file): $! -- aborting";
233   return 0;
234  };
235  print $eb $d;
236  close $eb;
237
238  if ($stat->do_manifest) {
239   unless (copy $stat->fetched_arch, $stat->distdir) {
240    error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
241    1 while unlink $file;
242    return 0;
243   }
244
245   msg 'Adding Manifest entry for ' . $stat->dist;
246   unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
247    1 while unlink $file;
248    return 0;
249   }
250  }
251
252  return 1;
253 }
254
255 sub install {
256  my $self = shift;
257  my $stat = $self->status;
258  my $conf = $self->parent->parent->configure_object;
259
260  my $sudo = $conf->get_program('sudo');
261  my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
262  unshift @cmd, $sudo if $sudo;
263
264  return $self->_run(\@cmd, 1);
265 }
266
267 sub uninstall {
268  my $self = shift;
269  my $stat = $self->status;
270  my $conf = $self->parent->parent->configure_object;
271
272  my $sudo = $conf->get_program('sudo');
273  my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
274  unshift @cmd, $sudo if $sudo;
275
276  return $self->_run(\@cmd, 1);
277 }
278
279 sub _run {
280  my ($self, $cmd, $verbose) = @_;
281
282  my ($success, $errmsg, $output) = run command => $cmd, verbose => $verbose;
283  unless ($success) {
284   error "$errmsg -- aborting";
285   if (not $verbose and defined $output and $self->status->verbose) {
286    my $msg = join '', @$output;
287    1 while chomp $msg;
288    error $msg;
289   }
290  }
291
292  return $success;
293 }
294
295 =head1 DEPENDENCIES
296
297 Gentoo (L<http://gentoo.org>).
298
299 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5).
300
301 L<File::Path> (since 5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
302
303 =head1 SEE ALSO
304
305 L<cpan2dist>.
306
307 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
308
309 =head1 AUTHOR
310
311 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
312
313 You can contact me by mail or on C<irc.perl.org> (vincent).
314
315 =head1 BUGS
316
317 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.
318
319 =head1 SUPPORT
320
321 You can find documentation for this module with the perldoc command.
322
323     perldoc CPANPLUS::Dist::Gentoo
324
325 =head1 ACKNOWLEDGEMENTS
326
327 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
328
329 =head1 COPYRIGHT & LICENSE
330
331 Copyright 2008 Vincent Pit, all rights reserved.
332
333 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
334
335 =cut
336
337 1; # End of CPANPLUS::Dist::Gentoo