Commit fcd9c476 authored by newt's avatar newt Committed by Commit bot

Clearer error message when parsing Android XML resource fails.

Previously, when generate_v14_compatible_resources.py encountered an
invalid XML file, it showed an error message that didn't tell which
file it was processing. E.g.:

  Traceback (most recent call last):
    ...
    File "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 207, in parseFile
      parser.Parse(buffer, 0)
  ExpatError: unbound prefix: line 6, column 0

Now it also prints the name of the file that couldn't be parsed. E.g.

  Traceback (most recent call last):
    ...
    File "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 207, in parseFile
      parser.Parse(buffer, 0)
  ExpatError: unbound prefix: line 6, column 0
  Failed to parse XML file: chrome/android/res/layout/my_layout.xml

Review URL: https://codereview.chromium.org/661433005

Cr-Commit-Position: refs/heads/master@{#302138}
parent 0794ea8d
...@@ -65,6 +65,16 @@ def IterateXmlElements(node): ...@@ -65,6 +65,16 @@ def IterateXmlElements(node):
yield child_node_element yield child_node_element
def ParseAndReportErrors(filename):
try:
return minidom.parse(filename)
except Exception:
import traceback
traceback.print_exc()
sys.stderr.write('Failed to parse XML file: %s\n' % filename)
sys.exit(1)
def AssertNotDeprecatedAttribute(name, value, filename): def AssertNotDeprecatedAttribute(name, value, filename):
"""Raises an exception if the given attribute is deprecated.""" """Raises an exception if the given attribute is deprecated."""
msg = None msg = None
...@@ -100,7 +110,7 @@ def HasStyleResource(dom): ...@@ -100,7 +110,7 @@ def HasStyleResource(dom):
def ErrorIfStyleResourceExistsInDir(input_dir): def ErrorIfStyleResourceExistsInDir(input_dir):
"""If a style resource is in input_dir, raises an exception.""" """If a style resource is in input_dir, raises an exception."""
for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'):
dom = minidom.parse(input_filename) dom = ParseAndReportErrors(input_filename)
if HasStyleResource(dom): if HasStyleResource(dom):
raise Exception('error: style file ' + input_filename + raise Exception('error: style file ' + input_filename +
' should be under ' + input_dir + ' should be under ' + input_dir +
...@@ -177,7 +187,7 @@ def GenerateV14LayoutResource(input_filename, output_v14_filename, ...@@ -177,7 +187,7 @@ def GenerateV14LayoutResource(input_filename, output_v14_filename,
don't do anything. If not, write the generated resource to don't do anything. If not, write the generated resource to
output_v14_filename, and copy the original resource to output_v17_filename. output_v14_filename, and copy the original resource to output_v17_filename.
""" """
dom = minidom.parse(input_filename) dom = ParseAndReportErrors(input_filename)
is_modified = GenerateV14LayoutResourceDom(dom, input_filename) is_modified = GenerateV14LayoutResourceDom(dom, input_filename)
if is_modified: if is_modified:
...@@ -196,7 +206,7 @@ def GenerateV14StyleResource(input_filename, output_v14_filename): ...@@ -196,7 +206,7 @@ def GenerateV14StyleResource(input_filename, output_v14_filename):
It's mostly a simple replacement, s/Start/Left s/End/Right, It's mostly a simple replacement, s/Start/Left s/End/Right,
on the attribute names. on the attribute names.
""" """
dom = minidom.parse(input_filename) dom = ParseAndReportErrors(input_filename)
GenerateV14StyleResourceDom(dom, input_filename) GenerateV14StyleResourceDom(dom, input_filename)
# Write the generated resource. # Write the generated resource.
...@@ -231,7 +241,7 @@ def VerifyV14ResourcesInDir(input_dir, resource_type): ...@@ -231,7 +241,7 @@ def VerifyV14ResourcesInDir(input_dir, resource_type):
' Pre-v17 resources should not include it because it ' ' Pre-v17 resources should not include it because it '
'can cause crashes on certain devices. Please refer to ' 'can cause crashes on certain devices. Please refer to '
'http://crbug.com/243952 for the details.') 'http://crbug.com/243952 for the details.')
dom = minidom.parse(input_filename) dom = ParseAndReportErrors(input_filename)
if resource_type in ('layout', 'xml'): if resource_type in ('layout', 'xml'):
if GenerateV14LayoutResourceDom(dom, input_filename, False): if GenerateV14LayoutResourceDom(dom, input_filename, False):
raise Exception(exception_message) raise Exception(exception_message)
...@@ -244,7 +254,7 @@ def AssertNoDeprecatedAttributesInDir(input_dir, resource_type): ...@@ -244,7 +254,7 @@ def AssertNoDeprecatedAttributesInDir(input_dir, resource_type):
"""Raises an exception if resources in input_dir have deprecated attributes, """Raises an exception if resources in input_dir have deprecated attributes,
e.g., paddingLeft, paddingRight""" e.g., paddingLeft, paddingRight"""
for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'):
dom = minidom.parse(input_filename) dom = ParseAndReportErrors(input_filename)
if resource_type in ('layout', 'xml'): if resource_type in ('layout', 'xml'):
GenerateV14LayoutResourceDom(dom, input_filename) GenerateV14LayoutResourceDom(dom, input_filename)
elif resource_type == 'values': elif resource_type == 'values':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment