BmapCopy: improve XML parsing error message
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Wed, 18 Sep 2013 07:21:37 +0000 (10:21 +0300)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Thu, 19 Sep 2013 10:28:48 +0000 (13:28 +0300)
When BmapCopy fails to parse the XML file, it prints something like this:

bmaptool: ERROR: cannot parse the bmap file '/home/dedekind/tmp/Fedora-19-i386-CHECKSUM' which should be a proper XML file: not well-formed (invalid token): line 1, column 1

(yes, I deliberately fed bmaptool a bogus file)

The problem is that sometimes we actually modify the bmap file a bit before
parsing. For example, we do this when the bmap file is signed with a clearsign
OpenPGP signature.

This means that the line number does not match the file name we print. This
patch improves the situation by providing the bogus line number and a bit of
the context in the exception error message.

Change-Id: I62b8aefde4a3ea524f528b83c4b6e70fa469e387
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptools/BmapCopy.py

index e209f60a86780322797d6d47c5f44e54e59a9cdc..e22aa7789f0c9e725627f49ef627843a1d45db60 100644 (file)
@@ -282,8 +282,16 @@ class BmapCopy:
         try:
             self._xml = ElementTree.parse(self._f_bmap)
         except  ElementTree.ParseError as err:
+            # Extrace the erroneous line with some context
+            self._f_bmap.seek(0)
+            xml_extract = ""
+            for num, line in enumerate(self._f_bmap):
+                if num >= err.position[0] - 4 and num <= err.position[0] + 4:
+                    xml_extract += "Line %d: %s" % (num, line)
+
             raise Error("cannot parse the bmap file '%s' which should be a "
-                        "proper XML file: %s" % (self._bmap_path, err))
+                        "proper XML file: %s, the XML extract:\n%s" %
+                        (self._bmap_path, err, xml_extract))
 
         xml = self._xml
         self.bmap_version = str(xml.getroot().attrib.get('version'))