summaryrefslogtreecommitdiff
path: root/rpm/build/enrpm
blob: 3e674bc3adb7a9ca4be7d9a9eca108c16e8ad947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/perl -w
#
# Attempt to build RPMs for Freeside from a tarball or module
#
# Contains portions of "cpanspec" http://cpanspec.sourceforge.net/

=head1 NAME

enrpm - Attempt to build RPMs for Freeside from a tarball or a module

=head1 SYNOPSIS

enrpm [options] [tarball|module-name [...]]

 Options:
   --help       -h      Help message

=head1 DESCRIPTION

B<enrpm> will generate a spec file to build a rpm from a CPAN-style
Perl module distribution, then try to build RPMs from that spec file
under mock, and update the repositories if successful.

B<enrpm> uses the file extension of the tarball or module-name specified
on the command line to determine if the argument is a tarball or a module name.

Modules are downloaded from the CPAN.  You must have initialized CPAN on the machine
for B<enrpm> to be able to do this.

For tarballs B<enrpm> attempts to build a usable .spec file using cpanflute2, and then builds
an SRPM from the .spec file and the tarball.

Once an SRPM has been built, B<enrpm> uses an external script to build binary RPMs under "mock".
It then uses a second script to update the repositories.

=head1 OPTIONS

=over 4

=item B<--help>

Print a brief help message and exit.

=item B<--packager>

Specify the name and e-mail address of the packager.  This winds up in auto-generated .spec files.

=item B<--server>

Specify the host name or IP address of the server on which the SRPM will be built.  This server must be
set up for SRPM building, which means it must have both B<ovid> and B<cpanflute2> installed on it.

=item B<--dry-run>

Just print out commands, don't actually run them.

=back

=head1 BUGS

Currently has little in the way of error detection and diagnostics.

The back-end should be replaced with an existing build system such as koji.

=head1 AUTHOR

Richard Siddall <richard.siddall@elirion.net>

=head1 SEE ALSO

L<perl(1)>, L<cpan2rpm(1)>, L<cpanflute2(1)>

=cut

use strict;
use Getopt::Long;
use Cwd qw/getcwd abs_path/;
use File::Basename;
use Sys::Hostname; # False laziness to get around a real config file...

my %opts;

GetOptions(\%opts, 'packager=s', 'release=s', 'server=s', 'dryrun');

$ENV{PATH} = "/bin:/usr/bin";

sub usage {
	print STDERR "usage: enrpm --packager <Your Name <you\@example.com>> [--release=<rpm-release-string>] --server <server-name> [--dryrun] tarball|module-name\n";
	print STDERR "where:\n\t--packager is the name and e-mail address of the packager for the .spec file\n\t--release is the rpm release number for the .spec file\n\t--server is the server to build the SRPM on\n\t--dryrun means do not actually run the commands\n\n";
	print STDERR "\ttarball is the name of a tarball containing the Perl module to be packaged\n";
	print STDERR "\tmodule-name is the name of a CPAN Perl module to be packaged\n";
	exit;
}

# Feeble excuse for not having a real configuration file
my $user = 'ivan';
my $buildsys = "/home/$user/buildsys";
#my $rembuild = '/home/rsiddall/buildsys/ref';
my $remdeps = '/home/ivan';
my $rpmtopdir = "/home/$user/redhat";
my $packager = 'Ivan Kohler <ivan@freeside.biz>';
my $server = '10.5.4.5';

$server = $opts{server} if defined($opts{server});
$user = $1 if $server =~ /(\w+)\@[\w\d\.]+/;

$packager = $opts{packager} if defined($opts{packager});

# Go ahead and build an SRPM...

my $ovidignore = '--ignore ' . join ('--ignore ', qw/libwww/);


for my $file (@ARGV) {
	my ($name,$version,$type);

	if ($file =~ /^(?:.*\/)?(.*)-(?:v\.?)?([^-]+)\.(tar)\.(?:gz|bz2)$/) {
		$name = $1;
		$version = $2;
		$type = $3;
	} elsif ($file =~ /^(?:.*\/)?(.*)-(?:v\.?)?([^-]+)\.tgz$/) {
		$name = $1;
		$version = $2;
		$type = 'tar';
	} elsif ($file =~ /^(?:.*\/)?(.*)-(?:v\.?)?([^-]+)\.(zip)$/) {
		$name = $1;
		$version = $2;
		$type = $3;
	} else {
		# keep things happy if we get "Foo-Bar" instead of "Foo::Bar"
		$file =~ s/-/::/g;
		$name = $file;
		$version = undef;
		$type = 'module';
	}

	if ($type eq 'module') {
		my $fluteopts = '';
		$fluteopts .= "--packager='$packager' " if defined($packager);
		$fluteopts .= "--release='$opts{release}' " if defined($opts{release});
		do_cmd("ssh $user\@$server \"ovid --deps $ovidignore $name | tail -1 | $remdeps/ovid2flute $fluteopts | /bin/sh\"");
	} else {
		my $absfile = abs_path($file);
		my $fname = basename($file);
		die "Packager name and e-mail required" if !defined($packager);
		die "RPM release string required" if !defined($opts{release});
		do_cmd("scp $absfile $user\@$server:/home/$user/$fname");
#		do_cmd("ssh $user\@$server 'cpanflute2 --just-spec --noperlreqs --email=\\\"$packager\\\" --release=$opts{release}  /home/$user/$fname > /home/$user/work/redhat/SPECS/$name.spec;'");
#		do_cmd("ssh $user\@$server 'perl -pi -e \\\"s/perl\(perl\)/perl/g\\\" /home/$user/work/redhat/SPECS/$name.spec;'");
#		do_cmd("ssh $user\@$server 'rpmbuild -bs --nodeps --define \\\"_sourcedir /home/$user/\\\" --define \\\"_srcrpmdir /home/$user/work/redhat/SRPMS\\\" /home/$user/work/redhat/SPECS/$name.spec'");
		open SCRIPT, ">/home/$user/makesrpm" or die "Can't create SRPM construction script: ", $!;
		print SCRIPT "#!/bin/sh\n\n";
		print SCRIPT "cpanflute2 --just-spec --noperlreqs --email='$packager' --release=$opts{release}  /home/$user/$fname > $rpmtopdir/SPECS/$name.spec;\n";
		print SCRIPT "perl -pi -e 's/perl(perl)/perl/g' $rpmtopdir/SPECS/$name.spec\n";
		print SCRIPT "rpmbuild -bs --nodeps --define '_sourcedir /home/$user/' --define '_srcrpmdir $rpmtopdir/SRPMS' $rpmtopdir/SPECS/$name.spec\n";
		close SCRIPT or die "Can't close SRPM construction script: ", $!;
		chmod(0711, "/home/$user/makesrpm");
		do_cmd("scp /home/$user/makesrpm $user\@$server:/home/$user/makesrpm");
		do_cmd("ssh $user\@$server /home/$user/makesrpm");
	}
	my $olddir = getcwd();
	do_cmd("$buildsys/build-freeside");
#	if (-x "$buildsys/refresh-repo") {
		do_cmd("$buildsys/refresh-repo");
#	} else {
#		do_cmd("ssh $user\@$server $rembuild/refresh-repo");
#	}
}

sub do_cmd {
	my $cmd = shift;

	print "$cmd\n";
	`$cmd` if !exists($opts{dryrun});
}

1;