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