From ab3bfa4f96bea5d0339921d8a2b1e5bf8db643aa Mon Sep 17 00:00:00 2001 From: Scott Lee Date: Tue, 17 Dec 2024 10:09:03 -0800 Subject: [PATCH] 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 Commit-Queue: Scott Lee --- metrics_xml_format.py | 4 ++-- tests/metrics_xml_format_test.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/metrics_xml_format.py b/metrics_xml_format.py index f511ee6cf..a415291b5 100755 --- a/metrics_xml_format.py +++ b/metrics_xml_format.py @@ -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) diff --git a/tests/metrics_xml_format_test.py b/tests/metrics_xml_format_test.py index 0fbd8c926..4d83b6bab 100755 --- a/tests/metrics_xml_format_test.py +++ b/tests/metrics_xml_format_test.py @@ -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