Replace old Perl script for munging RPM release number.

This commit is contained in:
Alex Wood 2015-06-10 18:31:36 -04:00
parent c8371dc368
commit d73ab2d661
6 changed files with 156 additions and 92 deletions

View file

@ -1,65 +0,0 @@
#!/usr/bin/perl
#
# Copyright (c) 2008-2009 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
use strict;
use warnings FATAL => 'all';
my ($IN, $SHA1, $CNT, $DIR, $TAR_GZ) = @ARGV;
open IN, $IN or die "Error reading [$IN]\n";
my @lines = <IN>;
close IN;
my ($have_release, $have_source, $have_setup) = (0, 0, 0);
my $i = 0;
for (@lines) {
no warnings 'uninitialized';
if (s/^(Release:\s*)(.+?)(%{\?dist})?\s*\n$/$1$2.git.$CNT.$SHA1$3\n/i) {
if ($have_release) {
die "Duplicate Release line found in [$IN] at line [$i]\n";
}
$have_release++;
}
if (defined $TAR_GZ and s/^(Source0?:\s*)(.+?)\n$/$1$TAR_GZ\n/i) {
if ($have_source) {
die "Duplicate Source (or Source0) line found in [$IN] at line [$i]\n";
}
$have_source++;
}
if (defined $DIR and /^%setup/) {
if (not s/\s+-n\s+\S+(\s*)/ -n $DIR$1/) {
s/\n/ -n $DIR\n/;
}
$have_setup++;
}
$i++;
}
if (not $have_release) {
die "The specfile [$IN] does not seem to have Release: line we could use\n";
}
if (defined $TAR_GZ and not $have_source) {
die "The specfile [$IN] does not seem to have Source: line we could use\n";
}
#if (defined $DIR and not $have_setup) {
# die "The specfile [$IN] does not seem to have %setup line we could use\n";
#}
my $OUT = "$IN.$SHA1";
open OUT, "> $OUT" or die "Error writing [$OUT]\n";
print OUT @lines;
close OUT;
rename $OUT, $IN;

View file

@ -37,7 +37,6 @@ setup(
# non-python scripts go here
scripts=[
'bin/tito',
'bin/test-setup-specfile.pl',
'bin/generate-patches.pl'
],

View file

@ -32,7 +32,7 @@ from tito.common import scl_to_rpm_option, get_latest_tagged_version, \
get_commit_count, find_gemspec_file, create_builder, compare_version,\
find_cheetah_template_file, render_cheetah, replace_spec_release, \
find_spec_like_file, warn_out, get_commit_timestamp, chdir, mkdir_p, \
find_git_root, info_out
find_git_root, info_out, munge_specfile
from tito.compat import getstatusoutput
from tito.exception import RunCommandException
from tito.exception import TitoException
@ -496,19 +496,17 @@ class Builder(ConfigObject, BuilderBase):
# file we're building off. (note that this is a temp copy of the
# spec) Swap out the actual release for one that includes the git
# SHA1 we're building for our test package:
setup_specfile_script = get_script_path("test-setup-specfile.pl")
cmd = "%s %s %s %s %s-%s %s" % \
(
setup_specfile_script,
self.spec_file,
self.git_commit_id[:7],
self.commit_count,
self.project_name,
self.display_version,
self.tgz_filename,
)
run_command(cmd)
self.build_version += ".git." + str(self.commit_count) + "." + str(self.git_commit_id[:7])
sha = self.git_commit_id[:7]
fullname = "%s-%s" % (self.project_name, self.display_version)
munge_specfile(
self.spec_file,
sha,
self.commit_count,
fullname,
self.tgz_filename,
)
self.build_version += ".git." + str(self.commit_count) + "." + str(sha)
self.ran_setup_test_specfile = True
def _get_rpmbuild_dir_options(self):
@ -585,15 +583,11 @@ class NoTgzBuilder(Builder):
# spec) Swap out the actual release for one that includes the git
# SHA1 we're building for our test package:
debug("setup_test_specfile:commit_count = %s" % str(self.commit_count))
script = "test-setup-specfile.pl"
cmd = "%s %s %s %s" % \
(
script,
self.spec_file,
self.git_commit_id[:7],
self.commit_count,
)
run_command(cmd)
munge_specfile(
self.spec_file,
self.git_commit_id[:7],
self.commit_count
)
class GemBuilder(NoTgzBuilder):

View file

@ -628,6 +628,51 @@ def replace_spec_release(file_name, release):
print(line.rstrip('\n'))
def munge_specfile(spec_file, commit_id, commit_count, fullname=None, tgz_filename=None):
# If making a test rpm we need to get a little crazy with the spec
# file we're building off. (Note we are modifying a temp copy of the
# spec) Swap out the actual release for one that includes the git
# SHA1 we're building for our test package.
sha = commit_id[:7]
for line in fileinput.input(spec_file, inplace=True):
m = re.match(r'^(\s*Release:\s*)(.+?)(%{\?dist})?\s*$', line)
if m:
print('%s%s.git.%s.%s%s' % (
m.group(1),
m.group(2),
commit_count,
sha,
m.group(3),
))
continue
m = re.match(r'^(\s*Source0?):\s*(.+?)$', line)
if tgz_filename and m:
print('%s: %s' % (m.group(1), tgz_filename))
continue
m = re.match(r'^(\s*%setup)(.*?)$', line)
if fullname and m:
macro = m.group(1)
setup_arg = " -n %s" % fullname
args = m.group(2)
args_match = re.search(r'(.+?)\s+-n\s+\S+(.*)', args)
if args_match:
macro += args_match.group(1)
macro += args_match.group(2)
macro += setup_arg
else:
macro += args
macro += setup_arg
print(macro)
continue
print(line.rstrip('\n'))
def scrape_version_and_release(template_file_name):
"""Ideally, we'd let RPM report the version and release of a spec file as
in get_spec_version_and_release. However, when we are dealing with Cheetah

View file

@ -16,13 +16,14 @@
from tito.common import (replace_version, find_spec_like_file, increase_version,
search_for, compare_version, run_command_print, find_wrote_in_rpmbuild_output,
render_cheetah, increase_zstream, reset_release, find_file_with_extension,
normalize_class_name, extract_sha1, BugzillaExtractor, DEFAULT_BUILD_DIR
)
normalize_class_name, extract_sha1, BugzillaExtractor, DEFAULT_BUILD_DIR, munge_specfile)
import os
import unittest
from mock import Mock, patch, call
from tempfile import NamedTemporaryFile
from textwrap import dedent
from unit import open_mock, Capture
@ -240,6 +241,97 @@ class CheetahRenderTest(unittest.TestCase):
self.assertEquals(call("temp_pickle"), mock_unlink.mock_calls[0])
class SpecTransformTest(unittest.TestCase):
def setUp(self):
self.spec_file = NamedTemporaryFile(delete=False).name
def tearDown(self):
os.unlink(self.spec_file)
def test_simple_transform(self):
simple_spec = dedent("""
Name: Hello
Version: 1.0.0
Release: 1%{?dist}
Source: hello-1.0.0.tar.gz
%prep
%setup -q
""")
with open(self.spec_file, 'w') as f:
f.write(simple_spec)
sha = "acecafe"
commit_count = 5
display_version = "git-%s.%s" % (commit_count, sha)
fullname = "hello-%s" % display_version
munge_specfile(self.spec_file, sha, commit_count, fullname, "%s.tar.gz" % fullname)
output = open(self.spec_file, 'r').readlines()
self.assertEquals(8, len(output))
self.assertEquals("Release: 1.git.%s.%s%%{?dist}\n" % (commit_count, sha), output[3])
self.assertEquals("Source: %s.tar.gz\n" % fullname, output[4])
self.assertEquals("%%setup -q -n %s\n" % fullname, output[7])
# Spot check some things that should not change
self.assertEquals("Name: Hello\n", output[1])
self.assertEqual("%prep\n", output[6])
def test_transform_release_only(self):
simple_spec = dedent("""
Release: 1%{?dist}
Source: hello-1.0.0.tar.gz
%setup -q
""")
with open(self.spec_file, 'w') as f:
f.write(simple_spec)
sha = "acecafe"
commit_count = 5
display_version = "git-%s.%s" % (commit_count, sha)
munge_specfile(self.spec_file, sha, commit_count)
output = open(self.spec_file, 'r').readlines()
self.assertEquals(4, len(output))
self.assertEquals("Release: 1.git.%s.%s%%{?dist}\n" % (commit_count, sha), output[1])
self.assertEquals("Source: hello-1.0.0.tar.gz\n", output[2])
self.assertEquals("%setup -q\n", output[3])
def test_transform_no_whitespace_modifications(self):
simple_spec = dedent("""
Release: 1%{?dist}
Source: hello-1.0.0.tar.gz
""")
with open(self.spec_file, 'w') as f:
f.write(simple_spec)
sha = "acecafe"
commit_count = 5
display_version = "git-%s.%s" % (commit_count, sha)
munge_specfile(self.spec_file, sha, commit_count)
output = open(self.spec_file, 'r').readlines()
self.assertEquals(3, len(output))
self.assertEquals("Release: 1.git.%s.%s%%{?dist}\n" % (commit_count, sha), output[1])
self.assertEquals("Source: hello-1.0.0.tar.gz\n", output[2])
def test_complex_setup_transform(self):
simple_spec = dedent("""
%setup -q -n hello-1
""")
with open(self.spec_file, 'w') as f:
f.write(simple_spec)
sha = "acecafe"
commit_count = 5
display_version = "git-%s.%s" % (commit_count, sha)
fullname = "hello-%s" % display_version
munge_specfile(self.spec_file, sha, commit_count, fullname, "%s.tar.gz" % fullname)
output = open(self.spec_file, 'r').readlines()
self.assertEquals("%%setup -q -n %s\n" % fullname, output[1])
class VersionMathTest(unittest.TestCase):
def test_increase_version_minor(self):
line = "1.0.0"

View file

@ -118,7 +118,6 @@ rm -rf $RPM_BUILD_ROOT
%doc %{_mandir}/man5/releasers.conf.5*
%doc %{_mandir}/man8/tito.8*
%{_bindir}/tito
%{_bindir}/test-setup-specfile.pl
%{_bindir}/generate-patches.pl
%{_datadir}/bash-completion/completions/tito
%dir %{python_sitelib}/tito