0: #!/usr/bin/perl -w
1: # Make an RPM from a perl module, straight from CPAN.
2:
3: use strict;
4:
5: use CPAN 1.54;
6: use Data::Dumper qw(Dumper); $Data::Dumper::Indent=1;
7: use Fatal qw(open close chdir opendir closedir);
8: use File::Copy qw(copy);
9: use Cwd qw(cwd);
10: use Getopt::Std;
11: use vars qw(*SPEC $opt_h $opt_n $opt_r $opt_i $opt_g $opt_c $VERSION);
12: require blib;
13:
14: $VERSION = '1.00';
15:
16: BEGIN {
17: unless ($< == $( && $< == 0) {
18: die "Must be running as root!";
19: }
20: }
21:
22: unless (caller) {
23: getopts(join('',
24: 'n:', # Name of rpm
25: 'r:', # Revision
26: 'i:', # Ignore list
27: 'g:', # Group
28: 'c:', # Copyright
29: 'h', # Help
30: )
31: );
32:
33: exit(main(@ARGV));
34: }
35:
36: sub main {
37: my @ARGS = @_;
38:
39: return usage() if $opt_h;
40: return usage() unless (@ARGS || -e 'Makefile.PL');
41:
42: if (!@ARGS) {
43: print "About to build local module, not a CPAN module. OK? [Y/n] ";
44: my $ret = <STDIN>;
45: if ($ret !~ /^y/i) {
46: print "OK, exiting\n";
47: return 1;
48: }
49: }
50:
51: my $perl_version = `rpm -q perl`;
52: $perl_version =~ s/^\D*([\d\.]+).*$/$1/s;
53: warn("got perl version: $perl_version\n");
54:
55: my $cpan_file = @ARGS ? get_cpan_file(shift(@ARGS)) : 'STAR/Module';
56:
57: # Get and set build dir
58: my $distro = $CPAN::META->instance('CPAN::Distribution', $cpan_file);
59: $distro->{writemakefile} = 1;
60: # warn("got distro:", Dumper($distro));
61: if (my $dir = $distro->dir) {
62: warn("Changing dir from ", cwd, " to $dir\n");
63: chdir($dir);
64: }
65: else {
66: $distro->{build_dir} = '.';
67: }
68:
69: xsystem("$^X Makefile.PL");
70:
71: # Build pre-requisites list
72: my $prereq = $distro->prereq_pm;
73: my $prereq_str = "perl = $perl_version";
74: foreach my $mod (keys %$prereq) {
75: next if $opt_i =~ /\Q$mod\E/;
76: $mod =~ s/::/-/g;
77: next if $opt_i =~ /\Q$mod\E/;
78: $prereq_str .= ", perl-$mod";
79: if ($prereq->{$mod}) {
80: $prereq_str .= " >= $prereq->{$mod}";
81: }
82: }
83: warn("Generated prereq string: $prereq_str\n");
84:
85: # Find distname and version from the Makefile
86: my $distname = `grep 'DISTNAME =' Makefile | cut -f3 -d' ' | head -1`;
87: chomp($distname);
88: my $version = `grep 'VERSION =' Makefile | cut -f3 -d' ' | head -1`;
89: chomp($version);
90: die "No version number!" unless $version;
91:
92: (my $modname = $distname) =~ s/-/::/g;
93:
94: warn("Building spec file for $modname $version\n");
95:
96: $opt_n ||= "perl-$distname";
97: my $copyright = '';
98: if ($opt_c) {
99: $copyright = "Copyright: $opt_c\n";
100: }
101: $opt_r ||= '1';
102: $opt_g ||= 'Perl';
103:
104: open(SPEC, ">${distname}.spec");
105: print SPEC <<"EOT";
106: Name: $opt_n
107: Version: $version
108: Release: $opt_r
109: $copyright
110: License: Artistic (probably)
111: Group: $opt_g
112: Source: $distname-\%{version}.tar.gz
113: BuildRoot: /tmp/\%{name}-buildroot
114: Summary: perl module $modname
115: Requires: $prereq_str
116:
117: %description
118: perl module $modname
119:
120: %prep
121: %setup -n ${distname}-${version}
122:
123: %build
124: $^X Makefile.PL
125: make
126: make test
127:
128: %install
129: rm -rf \$RPM_BUILD_ROOT
130: make PREFIX=\$RPM_BUILD_ROOT/usr INSTALLMAN3DIR=\$RPM_BUILD_ROOT/usr/man/man3 install
131: find \$RPM_BUILD_ROOT -name perllocal.pod | xargs rm -f
132:
133: %clean
134: rm -rf \$RPM_BUILD_ROOT
135:
136: %files
137: /usr/
138:
139: %changelog
140:
141: EOT
142:
143: close SPEC;
144:
145: xsystem("rm -f *.tar.gz");
146: xsystem("make dist");
147: copy("${distname}-${version}.tar.gz", "/usr/src/redhat/SOURCES/");
148: xsystem("rpm -bb ${distname}.spec");
149: return 0;
150: }
151:
152: sub usage {
153: print <<EOT;
154: This is mkperlrpm version $VERSION. This is free software.
155:
156: Usage: mkperlrpm [options] [Module-Name]
157: Options:
158:
159: -n name Name of rpm. Default: perl-[Module-Name]
160: -r revision Revision of the module (not version). Default: 1.
161: -g group Group to put module in. Default: Perl
162: -c copyright Copyright to use. Default: none
163: -i ignore list The ignore list is a list of perl modules to ignore
164: in the dependencies list. See the man page for details.
165:
166: EOT
167: return 1;
168: }
169:
170: sub get_cpan_file {
171: my $modulename = shift;
172: warn("Downloading $modulename from CPAN...\n");
173: my $obj = CPAN::Shell->expandany($modulename);
174: $obj->get();
175: my $cpan_file = $obj->cpan_file;
176: warn("Downloaded\n");
177: return $cpan_file;
178: }
179:
180: ##########################################################################################
181:
182: sub xsystem {
183: my $retval = system(@_);
184: my $exit_val = $? >> 8;
185: my $signal_num = $? & 127;
186: my $dumped_core = $? & 128;
187: if ($dumped_core) {
188: die "$_[0] failed: dumped core\n";
189: }
190: if ($signal_num) {
191: die "$_[0] failed: exited with signal $signal_num\n";
192: }
193: if ($exit_val) {
194: die "$_[0] failed: exited with code: $exit_val\n";
195: }
196: return;
197: }
198:
199: 1;
In reply to mkperlrpm
by Matts
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.