Commit d983bff4 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Support use_base_dir in <structure>.

This is basically copy-and-pasted from the <include> support.

Bug: 564112
Change-Id: I402ae9c56f48de0d87726a0961687e1d6bac0e11
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1670354
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Commit-Queue: Robert Flack <flackr@chromium.org>
Reviewed-by: default avatarRobert Flack <flackr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#672491}
parent 3047d3f3
...@@ -73,14 +73,13 @@ class IncludeNode(base.Node): ...@@ -73,14 +73,13 @@ class IncludeNode(base.Node):
if os.path.isabs(os.path.expandvars(self.attrs['file'])): if os.path.isabs(os.path.expandvars(self.attrs['file'])):
return self.attrs['file'] return self.attrs['file']
# We have no control over code that calles ToRealPath later, so convert # We have no control over code that calls ToRealPath later, so convert
# the path to be relative against our basedir. # the path to be relative against our basedir.
if self.attrs.get('use_base_dir', 'true') != 'true': if self.attrs.get('use_base_dir', 'true') != 'true':
# Normalize the directory path to use the appropriate OS separator. # Normalize the directory path to use the appropriate OS separator.
# GetBaseDir() may return paths\like\this or paths/like/this, since it is # GetBaseDir() may return paths\like\this or paths/like/this, since it is
# read from the base_dir attribute in the grd file. # read from the base_dir attribute in the grd file.
norm_base_dir = os.path.normpath( norm_base_dir = util.normpath(self.GetRoot().GetBaseDir())
self.GetRoot().GetBaseDir().replace('\\', '/'))
return os.path.relpath(self.attrs['file'], norm_base_dir) return os.path.relpath(self.attrs['file'], norm_base_dir)
return self.attrs['file'] return self.attrs['file']
......
...@@ -145,6 +145,7 @@ class StructureNode(base.Node): ...@@ -145,6 +145,7 @@ class StructureNode(base.Node):
'sconsdep' : 'false', 'sconsdep' : 'false',
'variables': '', 'variables': '',
'compress': 'false', 'compress': 'false',
'use_base_dir': 'true',
} }
def IsExcludedFromRc(self): def IsExcludedFromRc(self):
...@@ -185,7 +186,8 @@ class StructureNode(base.Node): ...@@ -185,7 +186,8 @@ class StructureNode(base.Node):
return '\r' return '\r'
else: else:
raise exception.UnexpectedAttribute( raise exception.UnexpectedAttribute(
"Attribute 'line_end' must be one of 'unix' (default), 'windows' or 'mac'") "Attribute 'line_end' must be one of 'unix' (default), 'windows' or "
"'mac'")
def GetCliques(self): def GetCliques(self):
return self.gatherer.GetCliques() return self.gatherer.GetCliques()
...@@ -204,7 +206,24 @@ class StructureNode(base.Node): ...@@ -204,7 +206,24 @@ class StructureNode(base.Node):
return self.gatherer.GetHtmlResourceFilenames() return self.gatherer.GetHtmlResourceFilenames()
def GetInputPath(self): def GetInputPath(self):
return self.gatherer.GetInputPath() path = self.gatherer.GetInputPath()
if path is None:
return path
# Do not mess with absolute paths, that would make them invalid.
if os.path.isabs(os.path.expandvars(path)):
return path
# We have no control over code that calls ToRealPath later, so convert
# the path to be relative against our basedir.
if self.attrs.get('use_base_dir', 'true') != 'true':
# Normalize the directory path to use the appropriate OS separator.
# GetBaseDir() may return paths\like\this or paths/like/this, since it is
# read from the base_dir attribute in the grd file.
norm_base_dir = util.normpath(self.GetRoot().GetBaseDir())
return os.path.relpath(path, norm_base_dir)
return path
def GetTextualIds(self): def GetTextualIds(self):
if not hasattr(self, 'gatherer'): if not hasattr(self, 'gatherer'):
......
...@@ -19,6 +19,8 @@ import unittest ...@@ -19,6 +19,8 @@ import unittest
import StringIO import StringIO
from grit import util from grit import util
from grit.node import empty
from grit.node import misc
from grit.node import structure from grit.node import structure
from grit.format import rc from grit.format import rc
...@@ -67,6 +69,31 @@ class StructureUnittest(unittest.TestCase): ...@@ -67,6 +69,31 @@ class StructureUnittest(unittest.TestCase):
'</p>\n'), result) '</p>\n'), result)
os.remove(filepath) os.remove(filepath)
def testGetPath(self):
base_dir = util.PathFromRoot('grit/testdata')
grd = util.ParseGrdForUnittest('''
<structures>
<structure type="chrome_html" name="hello_tmpl" file="structure_variables.html" expand_variables="true" variables="GREETING=Hello,THINGS=foo,, bar,, baz,EQUATION=2+2==4,filename=simple" flattenhtml="true" use_base_dir="true"></structure>
</structures>''', base_dir)
grd.SetOutputLanguage('en')
grd.RunGatherers()
node, = grd.GetChildrenOfType(structure.StructureNode)
self.assertEqual(grd.ToRealPath(node.GetInputPath()),
os.path.abspath(os.path.join(
base_dir, ur'structure_variables.html')))
def testGetPathNoBasedir(self):
grd = util.ParseGrdForUnittest('''
<structures>
<structure type="chrome_html" name="hello_tmpl" file="structure_variables.html" expand_variables="true" variables="GREETING=Hello,THINGS=foo,, bar,, baz,EQUATION=2+2==4,filename=simple" flattenhtml="true" use_base_dir="false"></structure>
</structures>''', util.PathFromRoot('grit/testdata'))
grd.SetOutputLanguage('en')
grd.RunGatherers()
node, = grd.GetChildrenOfType(structure.StructureNode)
self.assertEqual(grd.ToRealPath(node.GetInputPath()),
os.path.abspath(os.path.join(
os.getcwd(), ur'structure_variables.html')))
def testCompressGzip(self): def testCompressGzip(self):
test_data_root = util.PathFromRoot('grit/testdata') test_data_root = util.PathFromRoot('grit/testdata')
root = util.ParseGrdForUnittest(''' root = util.ParseGrdForUnittest('''
......
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