rpm: change guess_spec() to return SpecFile object
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Fri, 6 Sep 2013 13:43:23 +0000 (16:43 +0300)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 5 Jun 2014 11:20:07 +0000 (14:20 +0300)
Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
gbp/rpm/__init__.py
gbp/scripts/buildpackage_rpm.py
gbp/scripts/import_orig_rpm.py
gbp/scripts/import_srpm.py
gbp/scripts/pq_rpm.py
tests/test_rpm.py

index e62ae2d..6815b0c 100644 (file)
@@ -755,13 +755,6 @@ def parse_srpm(srpmfile):
     return srcrpm
 
 
-def parse_spec(specfile):
-    try:
-        return SpecFile(specfile)
-    except IOError, err:
-        raise GbpError, "Error reading spec file: %s" % err
-
-
 def guess_spec(topdir, recursive=True, preferred_name=None):
     """Guess a spec file"""
     specs = []
@@ -790,7 +783,8 @@ def guess_spec(topdir, recursive=True, preferred_name=None):
         filenames = [os.path.relpath(spec, abstop) for spec in specs]
         raise NoSpecError("Multiple spec files found (%s), don't know which "
                           "to use." % ', '.join(filenames))
-    return specs[0]
+    return SpecFile(specs[0])
+
 
 def guess_spec_repo(repo, branch, packaging_dir):
     """
index 56c6b89..b57c528 100755 (executable)
@@ -514,12 +514,12 @@ def main(argv):
             options.packaging_dir = os.path.dirname(specfile)
             if not os.path.exists(specfile):
                 raise rpm.NoSpecError("Failed to export specfile: %s" % options.spec_file)
+            spec = rpm.SpecFile(specfile)
         else:
-            specfile = rpm.guess_spec(os.path.join(dump_dir, options.packaging_dir),
-                                      True,
-                                      os.path.basename(repo.path) + '.spec')
-        spec = rpm.SpecFile(specfile)
-        gbp.log.debug("Using spec file '%s'" % specfile)
+            spec = rpm.guess_spec(os.path.join(dump_dir, options.packaging_dir),
+                                  True,
+                                  os.path.basename(repo.path) + '.spec')
+        gbp.log.debug("Using spec file '%s'" % spec.specfile)
 
         if not options.tag_only:
             # Setup builder opts
index 62edd14..b208ad1 100755 (executable)
@@ -25,8 +25,8 @@ import gbp.tmpfile as tempfile
 import gbp.command_wrappers as gbpc
 import string
 from gbp.pkg import parse_archive_filename
-from gbp.rpm import (RpmUpstreamSource, SpecFile, NoSpecError, parse_spec,
-                     guess_spec, guess_spec_repo)
+from gbp.rpm import (RpmUpstreamSource, SpecFile, NoSpecError, guess_spec,
+                     guess_spec_repo)
 from gbp.rpm.policy import RpmPkgPolicy
 from gbp.rpm.git import (GitRepositoryError, RpmGitRepository)
 from gbp.config import GbpOptionParserRpm, GbpOptionGroup, no_upstream_branch_msg
@@ -48,17 +48,17 @@ def detect_name_and_version(repo, source, options):
 
     # Try to find the source package name
     try:
-        spec = parse_spec(guess_spec(os.path.join(repo.path, options.packaging_dir),
-                                     True,
-                                     os.path.basename(repo.path) + '.spec'))
+        preferred_fn = os.path.basename(repo.path) + '.spec'
+        spec = guess_spec(os.path.join(repo.path, options.packaging_dir), True,
+                          preferred_fn)
         sourcepackage = spec.name
     except NoSpecError:
         try:
             # Check the spec file from the repository, in case
             # we're not on the packaging-branch (but upstream, for
             # example).
-            specfile = guess_spec_repo(repo, options.packaging_branch, options.packaging_dir)
-            spec = SpecFile(specfile)
+            spec = guess_spec_repo(repo, options.packaging_branch,
+                                   options.packaging_dir)
             sourcepackage = spec.name
         except NoSpecError:
             if options.interactive:
index bf66102..01c225c 100755 (executable)
@@ -29,8 +29,8 @@ import urllib2
 
 import gbp.tmpfile as tempfile
 import gbp.command_wrappers as gbpc
-from gbp.rpm import (parse_srpm, guess_spec, NoSpecError, parse_spec,
-                     RpmUpstreamSource)
+from gbp.rpm import (parse_srpm, guess_spec, SpecFile, NoSpecError,
+                    RpmUpstreamSource)
 from gbp.rpm.policy import RpmPkgPolicy
 from gbp.rpm.git import (RpmGitRepository, GitRepositoryError)
 from gbp.git.modifier import GitModifier
@@ -290,12 +290,12 @@ def main(argv):
         if os.path.isdir(srpm):
             gbp.log.debug("Trying to import an unpacked srpm from '%s'" % srpm)
             dirs['src'] = os.path.abspath(srpm)
-            spec = parse_spec(guess_spec(srpm, True, preferred_spec))
+            spec = guess_spec(srpm, True, preferred_spec)
         else:
             gbp.log.debug("Trying to import an srpm from '%s' with spec "\
                           "file '%s'" % (os.path.dirname(srpm), srpm))
             dirs['src'] = os.path.abspath(os.path.dirname(srpm))
-            spec = parse_spec(srpm)
+            spec = SpecFile(srpm)
 
         # Check the repository state
         try:
@@ -489,9 +489,8 @@ def main(argv):
                 # (only for non-native packages with non-orphan packaging)
                 force_to_branch_head(repo, options.packaging_branch)
                 if options.patch_import:
-                    spec = parse_spec(os.path.join(repo.path,
-                                            options.packaging_dir,
-                                            spec.specfile))
+                    spec = SpecFile(os.path.join(repo.path,
+                                        options.packaging_dir, spec.specfile))
                     import_spec_patches(repo, spec, dirs)
                     commit = options.packaging_branch
 
index cf7b29a..aba6353 100755 (executable)
@@ -185,13 +185,11 @@ def export_patches(repo, branch, options):
     # Find and parse .spec file
     try:
         if options.spec_file != 'auto':
-            specfilename = options.spec_file
             options.packaging_dir = os.path.dirname(specfilename)
+            spec = SpecFile(options.packaging_dir)
         else:
-            specfilename = guess_spec(options.packaging_dir,
-                                      True,
-                                      os.path.basename(repo.path) + '.spec')
-        spec = SpecFile(specfilename)
+            spec = guess_spec(options.packaging_dir, True,
+                              os.path.basename(repo.path) + '.spec')
     except KeyError:
         raise GbpError, "Can't parse spec"
 
@@ -293,13 +291,11 @@ def import_spec_patches(repo, branch, options):
     # Find and parse .spec file
     try:
         if options.spec_file != 'auto':
-            specfilename = options.spec_file
-            options.packaging_dir = os.path.dirname(specfilename)
+            options.packaging_dir = os.path.dirname(options.spec_file)
+            spec = SpecFile(options.spec_file)
         else:
-            specfilename = guess_spec(options.packaging_dir,
-                                      True,
-                                      os.path.basename(repo.path) + '.spec')
-        spec = SpecFile(specfilename)
+            spec = guess_spec(options.packaging_dir, True,
+                              os.path.basename(repo.path) + '.spec')
     except KeyError:
         raise GbpError, "Can't parse spec"
 
@@ -352,13 +348,11 @@ def rebase_pq(repo, branch, options):
     # Find and parse .spec file
     try:
         if options.spec_file != 'auto':
-            specfilename = options.spec_file
-            options.packaging_dir = os.path.dirname(specfilename)
+            options.packaging_dir = os.path.dirname(options.spec_file)
+            spec = SpecFile(options.spec_file)
         else:
-            specfilename = guess_spec(options.packaging_dir,
-                                      True,
-                                      os.path.basename(repo.path) + '.spec')
-        spec = SpecFile(specfilename)
+            spec = guess_spec(options.packaging_dir, True,
+                              os.path.basename(repo.path) + '.spec')
     except KeyError:
         raise GbpError, "Can't parse spec"
 
index ca1cc6a..b37007b 100644 (file)
@@ -23,8 +23,7 @@ import tempfile
 from nose.tools import assert_raises
 
 from gbp.errors import GbpError
-from gbp.rpm import (SrcRpmFile, SpecFile, parse_srpm, parse_spec, guess_spec,
-                    NoSpecError)
+from gbp.rpm import SrcRpmFile, SpecFile, parse_srpm, guess_spec, NoSpecError
 
 DATA_DIR = os.path.abspath(os.path.splitext(__file__)[0] + '_data')
 SRPM_DIR = os.path.join(DATA_DIR, 'srpms')
@@ -306,14 +305,6 @@ class TestSpecFile(object):
 class TestUtilityFunctions(object):
     """Test utility functions of L{gbp.rpm}"""
 
-    def test_parse_spec(self):
-        """Test parse_spec() function"""
-        parse_spec(os.path.join(SPEC_DIR, 'gbp-test.spec'))
-        with assert_raises(NoSpecError):
-            parse_spec(os.path.join(DATA_DIR, 'notexists.spec'))
-        with assert_raises(GbpError):
-            parse_spec(os.path.join(SRPM_DIR, 'gbp-test-1.0-1.src.rpm'))
-
     def test_parse_srpm(self):
         """Test parse_srpm() function"""
         parse_srpm(os.path.join(SRPM_DIR, 'gbp-test-1.0-1.src.rpm'))
@@ -333,8 +324,9 @@ class TestUtilityFunctions(object):
         with assert_raises(NoSpecError):
             guess_spec(SPEC_DIR, recursive=False)
         # Spec found
-        spec_fn = guess_spec(SPEC_DIR, recursive=False,
+        spec = guess_spec(SPEC_DIR, recursive=False,
                              preferred_name = 'gbp-test2.spec')
-        assert spec_fn == os.path.join(SPEC_DIR, 'gbp-test2.spec')
+        assert spec.specfile == 'gbp-test2.spec'
+        assert spec.specdir == SPEC_DIR
 
 # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: