Files
chromium_depot_tools/metrics_xml_format.py
Scott Lee 88bc812650 Reland "Support formatting metrics xml(s) in the subfolders."
This reverts commit bfe1a9282d.

Reason for revert: reland with a fix.
- Find the diff between ps#1 and ps#2.
- Tested at https://paste.googleplex.com/4670451708854272

Original change's description:
> Revert "Support formatting metrics xml(s) in the subfolders."
>
> This reverts commit 597ba08be5.
>
> Reason for revert: it broke git_cl.py. Need further patch
>
> Original change's description:
> > Support formatting metrics xml(s) in the subfolders.
> >
> > https://crrev.com/c/6072565 assumed that the XMLs are located under
> > tools/metrics/{actions,ukm,structured,histograms} directly, such as
> > tools/metrics/histograms/enums.xml.
> >
> > However, its subfolders may have XML files, and it should format
> > the files. This CL fixes it.
> >
> > Bug: 384940858
> > Change-Id: I56484144e6f72f41eb5bc37a5ad462a0de1ec0e3
> > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6111994
> > Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
> > Auto-Submit: Scott Lee <ddoman@chromium.org>
> > Commit-Queue: Scott Lee <ddoman@chromium.org>
>
> Bug: 384940858
> Change-Id: I322573ad6d2d758cd3d2de872efdbba4fd9330c2
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6111996
> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> Commit-Queue: Scott Lee <ddoman@chromium.org>

Bug: 384940858
Change-Id: Ibe20d5e46c519d7fdbd1114565ec3856e5bf928e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6111997
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Commit-Queue: Scott Lee <ddoman@chromium.org>
2024-12-19 14:14:40 -08:00

120 lines
3.5 KiB
Python
Executable File

#!/usr/bin/env vpython3
# Copyright (c) 2024 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Redirects to the version of the metrics XML formatter in the Chromium tree.
"""
import gclient_paths
import os
import shutil
import subprocess
import sys
def GetMetricsDir(top_dir, path):
metrics_xml_dirs = [
os.path.join(top_dir, 'tools', 'metrics', 'actions'),
os.path.join(top_dir, 'tools', 'metrics', 'histograms'),
os.path.join(top_dir, 'tools', 'metrics', 'structured'),
os.path.join(top_dir, 'tools', 'metrics', 'ukm'),
]
abs_dirname = os.path.dirname(os.path.realpath(path))
for xml_dir in metrics_xml_dirs:
if abs_dirname.startswith(xml_dir):
return xml_dir
return None
def IsSupportedHistogramsXML(path):
supported_xmls = set([
'histograms.xml',
'enums.xml',
'histogram_suffixes_list.xml',
])
return os.path.basename(path) in supported_xmls
def log(msg, verbose):
if verbose:
print(msg)
def FindMetricsXMLFormatterTool(path, verbose=False):
"""Returns a path to the metrics XML formatter executable."""
top_dir = gclient_paths.GetPrimarySolutionPath()
if not top_dir:
log('Not executed in a Chromium checkout; skip formatting', verbose)
return ''
xml_dir = GetMetricsDir(top_dir, path)
if not xml_dir:
log(f'{path} is not a metric XML; skip formatting', verbose)
return ''
# Just to ensure that the given file is located in the current checkout
# folder. If not, skip the formatting.
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)
return ''
histograms_base_dir = os.path.join(top_dir, 'tools', 'metrics',
'histograms')
if xml_dir.startswith(histograms_base_dir):
# Skips the formatting, if the XML file is not one of the known types.
if not IsSupportedHistogramsXML(path):
log(f'{path} is not a supported histogram XML; skip formatting',
verbose)
return ''
return os.path.join(xml_dir, 'pretty_print.py')
usage_text = """Usage: %s [option] filepath
Format a given metrics XML file with the metrics XML formatters in the Chromium
checkout. Noop, if executed out of a Chromium checkout.
Note that not all the options are understood by all the formatters.
Find the formatter binaries for all the options supported by each formatter.
positional arguments:
filepath if the path is not under tools/metrics,
no formatter will be run.
options:,
-h, --help show this help message and exit'
--presubmit
--diff"""
def _print_help():
print(usage_text % sys.argv[0])
def main(args):
path = next((arg for arg in args if not arg.startswith('-')), None)
if not path:
_print_help()
return 0
if not os.path.exists(path):
raise FileNotFoundError(f'{path} does not exist.')
tool = FindMetricsXMLFormatterTool(path, verbose=True)
if not tool:
# Fail (almost) silently.
return 0
subprocess.run([
shutil.which('vpython3'),
tool,
] + args)
return 0
if __name__ == '__main__':
try:
sys.exit(main(sys.argv[1:]))
except KeyboardInterrupt:
sys.stderr.write('interrupted\n')
sys.exit(1)