use realpath instead of abspath

metrics_xml_format.py has a check to ensure that the given xml
is located in the current checkout using GetPrimarySolutionPath().

The problem is that GetPrimarySolutionPath() adds "src" if the path
doesn't end with "src".
https://source.chromium.org/chromium/chromium/tools/depot_tools/+/main:gclient_paths.py;l=78-79

The workspace in Cog doesn't have "src" in the path. Instead, it creates
a workspace at /..../$workspace_name/$repo_name, which doesn't match
GetPrimarySolutionPath() because the workspace root doesn't end with
'src'.

This must be a common problem in other modules, and Cog gets around
this issue by creating a symlink, 'src', to the workspace root.

This CL replaces os.path.abspath() with os.path.realpath() so that
the check is done properly.

Bug: b/369827156
Change-Id: Iaf56de0a9ccbd168004c4c80672c9dd18211bf5e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6094779
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Scott Lee <ddoman@chromium.org>
changes/79/6094779/2
Scott Lee 8 months ago committed by LUCI CQ
parent f548b21cd3
commit ab3bfa4f96

@ -18,7 +18,7 @@ def GetMetricsDir(path):
'tools/metrics/structured',
'tools/metrics/ukm',
]
normalized_abs_dirname = os.path.dirname(os.path.abspath(path)).replace(
normalized_abs_dirname = os.path.dirname(os.path.realpath(path)).replace(
os.sep, '/')
for xml_dir in metrics_xml_dirs:
if normalized_abs_dirname.endswith(xml_dir):
@ -52,7 +52,7 @@ def FindMetricsXMLFormatterTool(path, verbose=False):
return ''
# Just to ensure that the given file is located in the current checkout
# folder. If not, skip the formatting.
if not os.path.abspath(path).startswith(os.path.abspath(top_dir)):
if not os.path.realpath(path).startswith(os.path.realpath(top_dir)):
log(
f'{path} is not located in the current Chromium checkout; '
'skip formatting', verbose)

@ -23,16 +23,16 @@ class TestBase(gclient_paths_test.TestBase):
def setUp(self):
super().setUp()
# os.path.abspath() doesn't seem to use os.path.getcwd() to compute
# the abspath of a given path.
# os.path.realpath() doesn't seem to use os.path.getcwd() to compute
# the realpath of a given path.
#
# This mock os.path.abspath such that it uses the mocked getcwd().
mock.patch('os.path.abspath', self.abspath).start()
# This mock os.path.realpath such that it uses the mocked getcwd().
mock.patch('os.path.realpath', self.realpath).start()
# gclient_paths.GetPrimarysolutionPath() defaults to src.
self.make_file_tree({'.gclient': ''})
self.cwd = join(self.cwd, 'src')
def abspath(self, path):
def realpath(self, path):
if os.path.isabs(path):
return path

Loading…
Cancel
Save