From: Artem Bityutskiy Date: Wed, 18 Sep 2013 07:21:37 +0000 (+0300) Subject: BmapCopy: improve XML parsing error message X-Git-Tag: v3.0~22 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4be13434129cd7444811203d3f657debbd86ef1f;p=tools%2Fbmap-tools.git BmapCopy: improve XML parsing error message 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 --- diff --git a/bmaptools/BmapCopy.py b/bmaptools/BmapCopy.py index e209f60..e22aa77 100644 --- a/bmaptools/BmapCopy.py +++ b/bmaptools/BmapCopy.py @@ -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'))