Browse files

generalize %setup parsing to support more %setup options

  • Loading branch information...
1 parent 04bfb48 commit ea0ad868e86a6a83e560f016cab96ffb9f8bcfc4 @mapleoin committed Jan 22, 2013
Showing with 59 additions and 12 deletions.
  1. +20 −12 git_tarballs
  2. +39 −0 test_git_tarballs.py
View
32 git_tarballs
@@ -130,22 +130,30 @@ def package_version(upstream_version, upstream_commit):
upstream_commit[:COMMIT_HASH_SIZE])
-def update_spec_file(package_version, tarball_parent_dir, filename):
+def parse_update_spec_file(contents, package_version,
+ tarball_parent_dir, filename):
+ contents = re.sub(r'\n((Version:\s+).*)\n',
+ r'\n\g<2>%s\n' % package_version,
+ contents, count=1)
+ # only replace the "-n" option, leave everything else intact
+ contents = re.sub(r'\n(%setup.*?-n\s+)(\S+?)(|[ \t]+.*?)\n',
+ r'\n\g<1>%s\g<3>\n' % tarball_parent_dir,
+ contents, count=1)
+ contents = re.sub(r'\n((Source0?:\s+).*)\n',
+ r'\n\g<2>%s\n' % filename,
+ contents, count=1)
+ return contents
+
+
+def update_spec_files(package_version, tarball_parent_dir, filename):
for specfile in glob.glob('./*.spec'):
try:
f = open(specfile, 'r+')
contents = f.read()
f.seek(0)
f.truncate()
- contents = re.sub(r'\n((Version:\s+).*)\n',
- r'\n\g<2>%s\n' % package_version,
- contents, count=1)
- contents = re.sub(r'\n((%setup\s+).*)\n',
- r'\n\g<2>-q -n %s\n' % tarball_parent_dir,
- contents, count=1)
- contents = re.sub(r'\n((Source0?:\s+).*)\n',
- r'\n\g<2>%s\n' % filename,
- contents, count=1)
+ contents = parse_update_spec_file(contents, package_version,
+ tarball_parent_dir, filename)
f.write(contents)
finally:
f.close()
@@ -251,12 +259,12 @@ if __name__ == '__main__':
if not package_commit:
# on first run, we only set the fields in the spec file;
# there are no changes
- update_spec_file(package_version, tarball_parent_dir, args.filename)
+ update_spec_files(package_version, tarball_parent_dir, args.filename)
print "Initialized .spec file with git_tarball."
sys.exit()
changes = create_changes(changes_list, package_version,
package_commit, args.email)
if changes:
update_changes_file(args.package, changes)
- update_spec_file(package_version, tarball_parent_dir, args.filename)
+ update_spec_files(package_version, tarball_parent_dir, args.filename)
View
39 test_git_tarballs.py
@@ -36,6 +36,45 @@ def test_version_parse_comment(self):
ghb.get_commit_from_spec('example_pkg'))
+class TestGitParseUpdateSpec(unittest.TestCase):
+ def test_normal(self):
+ self.assertEqual(
+ "\nVersion: 14.0\n%setup -n bar\nSource: bar-14.0.tar.gz\n",
+ ghb.parse_update_spec_file(
+ "\nVersion: 13.14\n%setup -n foo\nSource: foo-13.14.tar.gz\n",
+ "14.0", "bar", "bar-14.0.tar.gz"))
+
+ def test_source_zero(self):
+ self.assertEqual(
+ "\nVersion: 14.0\n%setup -n bar\nSource: bar-14.0.tar.gz\n",
+ ghb.parse_update_spec_file(
+ "\nVersion: 13.14\n%setup -n foo\nSource: foo-13.14.tar.gz\n",
+ "14.0", "bar", "bar-14.0.tar.gz"))
+
+ def test_tabs(self):
+ self.assertEqual(
+ "\nVersion:\t14.0\n%setup\t-n bar\nSource:\tbar-14.0.tar.gz\n",
+ ghb.parse_update_spec_file(
+ "\nVersion:\t13.14\n%setup\t-n foo\nSource:\tfoo-13.14.tar.gz\n",
+ "14.0", "bar", "bar-14.0.tar.gz"))
+
+ def test_other_options_in_setup(self):
+ self.assertEqual(
+ "\nVersion:\t14.0\n"
+ "%setup -q -a0 -n bar -f 33\nSource:\tbar-14.0.tar.gz\n",
+ ghb.parse_update_spec_file(
+ "\nVersion:\t13.14\n"
+ "%setup -q -a0 -n foo -f 33\nSource:\tfoo-13.14.tar.gz\n",
+ "14.0", "bar", "bar-14.0.tar.gz"))
+
+ def test_no_n_option_in_setup(self):
+ self.assertEqual(
+ "\nVersion: 14.0\n%setup -q\nSource: bar-14.0.tar.gz\n",
+ ghb.parse_update_spec_file(
+ "\nVersion: 13.14\n%setup -q\nSource: foo-13.14.tar.gz\n",
+ "14.0", "bar", "bar-14.0.tar.gz"))
+
+
@contextmanager
def mock_open(contents):
with mock.patch("__builtin__.open", return_value=io.StringIO(contents)):

0 comments on commit ea0ad86

Please sign in to comment.