From 33fcb6bc7a476708ed76d6457a3365d2f747c978 Mon Sep 17 00:00:00 2001 From: Ed Bartosh Date: Fri, 24 Jan 2014 14:34:00 +0200 Subject: [PATCH] common/mapping.py: Improve xml parsing error handling Added local exception MappingError and properly process it in policycheck job. Fixes: #1541 Change-Id: I3e4ab8a7ec150933876ecb4f02461406833b8e6d Signed-off-by: Ed Bartosh --- common/mapping.py | 18 ++++++++++++------ job_policycheck.py | 9 ++++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/common/mapping.py b/common/mapping.py index 1b5376e..efb17af 100644 --- a/common/mapping.py +++ b/common/mapping.py @@ -2,20 +2,26 @@ import os from common.utils import xml2obj +from xml.sax import SAXException + +class MappingError(Exception): + """Local exception class.""" + pass class Mapping(): """A class to handle mapping xml file """ def __init__(self, mapping_file): """ convert xml to git obj """ if not os.path.isfile(mapping_file): - raise Exception, "Fatal: %s is not a regular file" % mapping_file + raise MappingError("Fatal: %s is not a regular file" \ + % mapping_file) with open(mapping_file) as filei: - self.mapping_obj = xml2obj(''.join(filei.readlines())) - - if not self.mapping_obj: - raise Exception, "Fatal: parser xml file %s failed" - + try: + self.mapping_obj = xml2obj(''.join(filei.readlines())) + except SAXException, err: + raise MappingError("Fatal: parsing of xml file %s failed: %s" \ + % (mapping_file, str(err))) @staticmethod def __encode_to_ascii(source): """ encode unicode list element to ascii """ diff --git a/job_policycheck.py b/job_policycheck.py index 93a4a95..6b8bab7 100755 --- a/job_policycheck.py +++ b/job_policycheck.py @@ -15,7 +15,7 @@ from common import utils from common.gerrit import get_gerrit_event from common.git import Git, clone_gitproject from common import gerrit -from common import mapping +from common.mapping import Mapping, MappingError from common.tempbuildpkg import TempPackageBuild from common import runner @@ -144,8 +144,11 @@ def main(): except gerrit.GerritError, err: print >> sys.stderr, str(err) - mymapping = mapping.Mapping('%s/%s/git-obs-mapping.xml' % (git_cache_dir, \ - mapping_prj)) + try: + mymapping = Mapping('%s/%s/git-obs-mapping.xml' % \ + (git_cache_dir, mapping_prj)) + except MappingError, err: + print >> sys.stderr, str(err) obstargets = mymapping.get_submit_mapping(events['project'], \ events['branch']) -- 2.7.4