Now handles hyphenated Perl requirements as well as those in parentheses, and
[freeside.git] / rpm / rpm2Bundle
1 #!/usr/bin/perl -Tw
2 #
3 # Make a bundle file from an RPM
4 #
5 use strict;
6
7 $ENV{PATH} = '/bin:/usr/bin/';
8
9 my $verbose = 0;
10
11 # These are Perl dependencies that should be ignored/suppressed
12 my %suppress;
13
14 foreach (qw/strict subs vars FS/) {
15         $suppress{$_} = $_;
16 }
17
18 ## These are root packages that shouldn't be cited multiple times
19 ## Should figure this out with CPAN
20 #my %rootpkgs;
21 #
22 #foreach (qw/FS/) {
23 #       $rootpkgs{$_} = 1;
24 #}
25
26 foreach my $rawrpm (@ARGV) {
27         $rawrpm =~ /^([-\.a-z0-9\/]+)\s*$/i;
28         my $rpm = $1 or next;
29         my @parts = split '/', $rpm;
30         my $name = pop @parts;
31         my $version = 0.01;
32         if ($name =~ m<([^/]+?)[-._]?v?-?([-_.\d]+[a-z]*?\d*)\.\w+\.rpm$>) {
33                 $name = $1;
34                 $version = $2;
35         }
36         print STDERR "rpm: $rpm ($name, $version)\n";
37         my @deps = sort `rpm -qp --requires $rpm`;
38
39         my %mods;
40
41         foreach (@deps) {
42                 if (/^perl\((.*)\)\s*((>=|=|<=)\s+([\d\.]+))?$/
43                  || /^perl-(.*)\s*((>=|=|<=)\s+([\d\.]+))?$/) {
44                         next if exists($suppress{$1});
45                         my @parts = split /::/, $1;
46                         if (scalar @parts > 1) {
47                                 next if exists($suppress{$parts[0]});
48                         }
49                         if ($verbose) {
50                                 print STDERR "$1";
51                                 print STDERR " $3 $4" if $4;
52                                 print STDERR "\n";
53                         }
54                         $mods{$1} = $4 ? $4 : undef;
55                 }
56         }
57
58         my $hdr =<<END;
59 # -*- perl -*-
60
61 package Bundle::$name;
62
63 \$VERSION = '$version';
64
65 1;
66
67 __END__
68
69 =head1 NAME
70
71 Bundle::$name - A bundle to install prerequisites for the $name package
72
73 =head1 SYNOPSIS
74
75 C<perl -MCPAN -e 'install Bundle::$name'>
76
77 =head1 CONTENTS
78
79 END
80
81         my $ftr =<<END;
82 =head1 DESCRIPTION
83
84 This bundle includes all prerequisites needed by the $name package.
85
86 =cut
87 END
88
89         print $hdr;
90         foreach (sort keys %mods) {
91                 print "$_";
92                 print " $mods{$_}" if exists($mods{$_}) && $mods{$_};
93                 print " -\n\n";
94         }
95         print $ftr;
96 }
97
98 1;
99