rpm.SpecFile: support parsing spec as raw text data
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Fri, 6 Sep 2013 14:15:52 +0000 (17:15 +0300)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 5 Jun 2014 11:20:07 +0000 (14:20 +0300)
Instead of reading from a file.

Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
gbp/rpm/__init__.py
tests/test_rpm.py

index 6815b0c..8bbab70 100644 (file)
@@ -118,18 +118,31 @@ class SpecFile(object):
             'files', 'changelog', 'triggerin', 'triggerpostin', 'triggerun',
             'triggerpostun')
 
-    def __init__(self, specfile):
+    def __init__(self, filename=None, filedata=None):
 
-        # Load spec file into our special data structure
-        self.specfile = os.path.basename(specfile)
-        self.specdir = os.path.dirname(os.path.abspath(specfile))
         self._content = LinkedList()
-        try:
-            with open(specfile) as spec_file:
-                for line in spec_file.readlines():
-                    self._content.append(line)
-        except IOError as err:
-            raise NoSpecError("Unable to read spec file: %s" % err)
+
+        # Check args: only filename or filedata can be given, not both
+        if filename is None and filedata is None:
+            raise NoSpecError("No filename or raw data given for parsing!")
+        elif filename and filedata:
+            raise NoSpecError("Both filename and raw data given, don't know "
+                              "which one to parse!")
+        elif filename:
+            # Load spec file into our special data structure
+            self.specfile = os.path.basename(filename)
+            self.specdir = os.path.dirname(os.path.abspath(filename))
+            try:
+                with open(filename) as spec_file:
+                    for line in spec_file.readlines():
+                        self._content.append(line)
+            except IOError as err:
+                raise NoSpecError("Unable to read spec file: %s" % err)
+        else:
+            self.specfile = None
+            self.specdir = None
+            for line in filedata.splitlines():
+                self._content.append(line + '\n')
 
         # Use rpm-python to parse the spec file content
         self._filtertags = ("excludearch", "excludeos", "exclusivearch",
index b37007b..47aaf7f 100644 (file)
@@ -150,6 +150,23 @@ class TestSpecFile(object):
         assert spec.name == 'gbp-test-native2'
         assert spec.orig_src is None
 
+    def test_parse_raw(self):
+        """Test parsing of a valid spec file"""
+        with assert_raises(NoSpecError):
+            SpecFile(None, None)
+        with assert_raises(NoSpecError):
+            SpecFile('filename', 'filedata')
+
+        spec_filepath = os.path.join(SPEC_DIR, 'gbp-test.spec')
+        with open(spec_filepath, 'r') as spec_fd:
+            spec_data = spec_fd.read()
+        spec = SpecFile(filedata=spec_data)
+
+        # Test basic properties
+        assert spec.specfile == None
+        assert spec.specdir == None
+        assert spec.name == 'gbp-test'
+
     def test_update_spec(self):
         """Test spec autoupdate functionality"""
         # Create temporary spec file