| | 160 | #======================================================================= |
|---|
| | 161 | # make_src_rpm( $c ) |
|---|
| | 162 | # makes a .src.rpm from the source tree |
|---|
| | 163 | # by setting up a rpmbuild environment and creating a special |
|---|
| | 164 | # rpmrc file for rpmbuild. |
|---|
| | 165 | # The version for rpm must look like '2.5.6', and we set |
|---|
| | 166 | # the release of the rpm to the svn revision number. |
|---|
| | 167 | # (This will need to be revisited for release .src.rpms) |
|---|
| | 168 | # |
|---|
| | 169 | # The .src.rpm is deposited into $builddir/rpmbuild/SRPMS |
|---|
| | 170 | # |
|---|
| | 171 | # Building .src.rpm can be tested with: |
|---|
| | 172 | # % mkdir -p /tmp/swish_daily_build /tmp/swish-daily; |
|---|
| | 173 | # % ./swish-daily.pl -topdir /tmp/swish_daily_build/ -tardir /tmp/swish-daily -v -nolog |
|---|
| | 174 | sub make_src_rpm { |
|---|
| | 175 | my $c = shift; |
|---|
| | 176 | |
|---|
| | 177 | return 1 unless $c->{srpm}; |
|---|
| | 178 | |
|---|
| | 179 | my ($rpmbuilddir, $rpmrcfile) = setup_rpmbuild_environment( $c ); |
|---|
| | 180 | my $tarball = "$c->{tardir}/swish-e-$c->{version}.tar.gz"; # this should be factored out |
|---|
| | 181 | my $srcdir = $c->{srcdir}; |
|---|
| | 182 | my $builddir = $c->{builddir}; |
|---|
| | 183 | my $specversion = $c->{version}; |
|---|
| | 184 | |
|---|
| | 185 | # if we're in --timestamp mode, then we need to make sure that the tarball |
|---|
| | 186 | # unpacks into EG: swish-e-2.5.6, and not swish-e-2.5.6-2007-12-12 |
|---|
| | 187 | if ($config{timestamp}) { |
|---|
| | 188 | |
|---|
| | 189 | my $origspecversion = $specversion; |
|---|
| | 190 | $specversion =~ s/-.*//; # remove all after and including the hyphen from 2.5.6-2007-12-12 |
|---|
| | 191 | # IE, from 2.5.6-2007-12-08 to 2.5.6 |
|---|
| | 192 | |
|---|
| | 193 | my $newtarball = $tarball; |
|---|
| | 194 | $newtarball =~ s/-\d+-\d+-\d+\.tar.gz$/.tar.gz/; # remove -2007-12-12 from tarball |
|---|
| | 195 | |
|---|
| | 196 | die "$0: failed to figure out name of new tarball for .src.rpm\n" |
|---|
| | 197 | unless ($newtarball ne $tarball); |
|---|
| | 198 | |
|---|
| | 199 | # create a new tarball that extacts to the right dirname, and set $tarball to it |
|---|
| | 200 | _rewrite_tarball( $c, $tarball, $newtarball, "swish-e-$origspecversion", "swish-e-$specversion" ); |
|---|
| | 201 | $tarball = $newtarball; |
|---|
| | 202 | } |
|---|
| | 203 | |
|---|
| | 204 | run_command( "cp $tarball $rpmbuilddir/SOURCES/swish-e-$specversion.tar.gz" ); |
|---|
| | 205 | run_command( "cp $srcdir/rpm/swish-e.xpm $rpmbuilddir/SOURCES" ); |
|---|
| | 206 | run_command( "cp $builddir/rpm/swish-e.spec $rpmbuilddir/SPECS" ); |
|---|
| | 207 | |
|---|
| | 208 | # fixup the version string in the .spec file, IE, from 2.5.6-2007-12-08 to 2.5.6 |
|---|
| | 209 | # also change the release string to our new specrelease if in timestamp mode |
|---|
| | 210 | _apply_regexes( "$rpmbuilddir/SPECS/swish-e.spec", |
|---|
| | 211 | qq{s/^%define[[:space:]]+version.*/%define version $specversion/ims} ); |
|---|
| | 212 | |
|---|
| | 213 | # also, if in timestamp mode, change the specrelease to be YYYYMMDD |
|---|
| | 214 | if ($config{timestamp}) { |
|---|
| | 215 | chomp(my $specrelease = `date '+%Y%m%d'`); # normally this is a 1-2 digits |
|---|
| | 216 | _apply_regexes( "$rpmbuilddir/SPECS/swish-e.spec", |
|---|
| | 217 | qq{s/^%define[[:space:]]+release.*/%define release $specrelease/ims} ); |
|---|
| | 218 | } |
|---|
| | 219 | |
|---|
| | 220 | # build the new .src.rpm using our rpmrcfile and swish-e.spec |
|---|
| | 221 | # (and all the stuff in $builddir/rpmbuild/) |
|---|
| | 222 | run_command( "rpmbuild --rcfile=$rpmrcfile -bs $rpmbuilddir/SPECS/swish-e.spec" ); |
|---|
| | 223 | |
|---|
| | 224 | log_message( "new .src.rpm build in $rpmbuilddir/SRPMS" ); |
|---|
| | 225 | } |
|---|
| | 226 | |
|---|
| | 227 | #======================================================================= |
|---|
| | 228 | # setup_rpmbuild_environment( $c ) |
|---|
| | 229 | # sets up an rpmbuild environment and returns the rpmbuilddir |
|---|
| | 230 | # creates the directories RPMS SOURCES SPECS SRPMS BUILD in ./rpmbuild, |
|---|
| | 231 | # and creates an rpmrc file that uses our .rpmmacros file |
|---|
| | 232 | # in order to use our rpmbuild environment. |
|---|
| | 233 | # returns (fullpath to ./rpmbuild, rpmrc file) |
|---|
| | 234 | sub setup_rpmbuild_environment { |
|---|
| | 235 | my $c = shift; |
|---|
| | 236 | # we'll assume we're in $c->{builddir} |
|---|
| | 237 | my $cwd = getcwd(); |
|---|
| | 238 | print "debug: cwd is $cwd\n"; |
|---|
| | 239 | |
|---|
| | 240 | # where the whole rpmbuild tree goes |
|---|
| | 241 | my $rpmbuilddir = "$cwd/rpmbuild/"; |
|---|
| | 242 | |
|---|
| | 243 | # make the subdirs needed by rpmbuild |
|---|
| | 244 | for my $dir (qw( RPMS SOURCES SPECS SRPMS BUILD ) ) { |
|---|
| | 245 | system( "mkdir -p $rpmbuilddir/$dir" ) unless -d $dir; |
|---|
| | 246 | } |
|---|
| | 247 | |
|---|
| | 248 | # write our rpmmacros file that informs rpmbuild of our rpmbuild tree |
|---|
| | 249 | my $rpmmacrosfile = "$cwd/.rpmmacros"; |
|---|
| | 250 | log_message( "Creating new $rpmmacrosfile" ); |
|---|
| | 251 | open(my $fh, ">", $rpmmacrosfile) || die "$0: Failed to open $rpmmacrosfile: $!"; |
|---|
| | 252 | print $fh "%_topdir $rpmbuilddir\n"; |
|---|
| | 253 | print $fh "%make make\n"; |
|---|
| | 254 | close($fh) || die "$0: Failed to close $rpmmacrosfile: $!"; |
|---|
| | 255 | |
|---|
| | 256 | # write our rpmrc file that gets rpmbuild to use our special $rpmmacros file |
|---|
| | 257 | my $rpmrcfile = "$rpmbuilddir/rpmrc"; |
|---|
| | 258 | log_message( "Creating new $rpmrcfile" ); |
|---|
| | 259 | run_command( "cp /usr/lib/rpm/rpmrc $rpmrcfile" ); |
|---|
| | 260 | _apply_regexes( $rpmrcfile, q{s!^(macrofiles:.*)!$1:./.rpmmacros!ims} ); # append ./.rpmmacros |
|---|
| | 261 | |
|---|
| | 262 | return ($rpmbuilddir, $rpmrcfile); |
|---|
| | 263 | } |
|---|
| | 554 | } |
|---|
| | 555 | |
|---|
| | 556 | #================================================================ |
|---|
| | 557 | # _apply_regexes( $file, @search_and_replace_regexes ) |
|---|
| | 558 | # backs up $file to $file.bak, and |
|---|
| | 559 | # applies supplied regexes to the lines of a file, |
|---|
| | 560 | sub _apply_regexes { |
|---|
| | 561 | my ($file, @regexes) = @_; |
|---|
| | 562 | # changes a file by applying the supplied regexes to each line |
|---|
| | 563 | my $tmpfile = "$file.tmp"; |
|---|
| | 564 | open(my $rfh, "<", $file) || die "$0: Can't open $file: $!"; |
|---|
| | 565 | open(my $wfh, ">", $tmpfile) || die "$0: Can't open $tmpfile: $!"; # clobber old $file.tmp |
|---|
| | 566 | print "Applying regexes:to file $file\n" . join("\n", @regexes) . "\n"; |
|---|
| | 567 | while(<$rfh>) { |
|---|
| | 568 | chomp(); |
|---|
| | 569 | for my $r (@regexes) { |
|---|
| | 570 | # $r should operate on $_ ! |
|---|
| | 571 | eval $r; |
|---|
| | 572 | die "$0: Error in regex: $r: $@" if $@; |
|---|
| | 573 | } |
|---|
| | 574 | print $wfh "$_\n"; |
|---|
| | 575 | } |
|---|
| | 576 | close($rfh) || die "$0: Can't open $file: $!"; |
|---|
| | 577 | close($wfh) || die "$0: Can't close $tmpfile: $!"; |
|---|
| | 578 | rename( $file, "$file.bak" ); |
|---|
| | 579 | rename( $tmpfile, $file ) || die "$0: Can't rename $tmpfile to $file: $!"; |
|---|
| | 580 | } |
|---|
| | 581 | |
|---|
| | 582 | #================================================================ |
|---|
| | 583 | # my $newtarball = _rewrite_tarball( $fromtarball, $totarball, $fromdir, $todir ) |
|---|
| | 584 | # creates a new tarball from $tarball that extracts into $todir instead of $fromdir |
|---|
| | 585 | sub _rewrite_tarball { |
|---|
| | 586 | my ($c, $fromtarball, $totarball, $fromdir, $todir) = @_; |
|---|
| | 587 | my $builddir = $c->{builddir}; |
|---|
| | 588 | my $cwd = getcwd(); |
|---|
| | 589 | |
|---|
| | 590 | #warn "$0: rewriting $fromtarball to $totarball,\n$0 to extract into $todir, not $fromdir\n$0: Currently in $cwd\n"; |
|---|
| | 591 | |
|---|
| | 592 | # create and cd into our tmp dir |
|---|
| | 593 | mkdir( "$builddir/tmp" ) || die "$0: Couldn't create $builddir/tmp"; |
|---|
| | 594 | chdir( "$builddir/tmp" ) || die "$0: Couldn't cd into $builddir/tmp"; |
|---|
| | 595 | |
|---|
| | 596 | # uncompress and recompress the tarball with a different dir name. |
|---|
| | 597 | run_command( "tar -zxf $fromtarball" ); |
|---|
| | 598 | die "$0: tarball didn't extract into $fromdir\n" unless -d $fromdir; |
|---|
| | 599 | run_command( "mv $fromdir $todir" ); |
|---|
| | 600 | run_command( "tar -zcf $totarball $todir" ); |
|---|
| | 601 | |
|---|
| | 602 | # go back to the right directory. |
|---|
| | 603 | chdir( $cwd ) || die "$0: Couldn't cd into $cwd"; |
|---|