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>
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'))