Commit 8c2a7d5f authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

[Lorenz] Shorten names and add bullet points in class deps audit script

Output now looks like this (removed some lines for brevity):

Printing class dependencies for .c.b.app.flags.ChromeCachedFlags:
2 inbound dependency(ies) into .c.b.app.flags.ChromeCachedFlags:
    [chrome_java]
        <- .c.b.app.ChromeActivity
        <- .c.b.init.ChromeBrowserInitializer
6 outbound dependency(ies) from .c.b.app.flags.ChromeCachedFlags:
    [chrome_java]
        -> .c.b.app.appmenu.AppMenuPropertiesDelegateImpl
        -> .c.b.tasks.ReturnToChromeExperimentsUtil
        -> .c.b.tasks.tab_management.TabUiFeatureUtilities
        -> .chrome.features.start_surface.StartSurfaceConfiguration
    [//c/b/flags:java]
        -> .c.b.flags.BooleanCachedFieldTrialParameter
        -> .c.b.flags.CachedFeatureFlags


was before:

Printing class dependencies for org.chromium.chrome.browser.app.flags.ChromeCachedFlags:
2 inbound dependency(ies) into org.chromium.chrome.browser.app.flags.ChromeCachedFlags:
    [//chrome/android:chrome_java]
        org.chromium.chrome.browser.app.ChromeActivity
        org.chromium.chrome.browser.init.ChromeBrowserInitializer
6 outbound dependency(ies) from org.chromium.chrome.browser.app.flags.ChromeCachedFlags:
    [//chrome/android:chrome_java]
        org.chromium.chrome.browser.app.appmenu.AppMenuPropertiesDelegateImpl
        org.chromium.chrome.browser.tasks.ReturnToChromeExperimentsUtil
        org.chromium.chrome.browser.tasks.tab_management.TabUiFeatureUtilities
        org.chromium.chrome.features.start_surface.StartSurfaceConfiguration
    [//chrome/browser/flags:java]
        org.chromium.chrome.browser.flags.BooleanCachedFieldTrialParameter
        org.chromium.chrome.browser.flags.CachedFeatureFlags

Change-Id: If9535c19ab5121a6a057e5c6d1dedea657e453ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2392942Reviewed-by: default avatarMohamed Heikal <mheikal@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808894}
parent ef3e67b7
# Lint as: python3
# Copyright 2020 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.
"""Functions dealing with Chrome-specific naming conventions."""
def shorten_class(class_name: str) -> str:
"""Returns a shortened version of the fully qualilied class name."""
return class_name.replace('org.chromium.',
'.').replace('chrome.browser.', 'c.b.')
def shorten_build_target(build_target: str) -> str:
"""Returns a shortened version of the build target."""
if build_target == '//chrome/android:chrome_java':
return 'chrome_java'
return build_target.replace('//chrome/browser/', '//c/b/')
# Lint as: python3
# Copyright 2020 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.
"""Unit tests for dependency_analysis.print_dependencies_helper."""
import unittest
import chrome_names
class TestChromeNames_ShortenClass(unittest.TestCase):
"""Unit tests for shorten_class."""
def test_shorten_chrome_browser_class(self):
self.assertEqual(
'.c.b.flags.ChromeFeatureList',
chrome_names.shorten_class(
'org.chromium.chrome.browser.flags.ChromeFeatureList'))
def test_shorten_base_class(self):
self.assertEqual(
'.base.Callback',
chrome_names.shorten_class('org.chromium.base.Callback'))
def test_shorten_components_class(self):
self.assertEqual(
'.components.prefs.PrefService',
chrome_names.shorten_class(
'org.chromium.components.prefs.PrefService'))
def test_does_not_shorten_third_party_class(self):
self.assertEqual('org.other_project.Class',
chrome_names.shorten_class('org.other_project.Class'))
class TestChromeNames_ShortenBuildTarget(unittest.TestCase):
"""Unit tests for shorten_build_target."""
def test_shorten_chrome_java(self):
self.assertEqual(
'chrome_java',
chrome_names.shorten_build_target('//chrome/android:chrome_java'))
def test_shorten_chrome_browser(self):
self.assertEqual(
'//c/b/flags:java',
chrome_names.shorten_build_target('//chrome/browser/flags:java'))
def test_does_not_shorten_other_directories(self):
self.assertEqual('//base:base_java',
chrome_names.shorten_build_target('//base:base_java'))
......@@ -8,6 +8,7 @@ import argparse
from dataclasses import dataclass
from typing import List
import chrome_names
import class_dependency
import graph
import print_dependencies_helper
......@@ -20,14 +21,35 @@ class PrintMode:
inbound: bool
outbound: bool
build_targets: bool
fully_qualified: bool
def print_with_indent(indent, message):
print(' ' * indent + message)
def get_class_name_to_display(fully_qualified_name: str,
print_mode: PrintMode) -> str:
if print_mode.fully_qualified:
return fully_qualified_name
else:
return chrome_names.shorten_class(fully_qualified_name)
def get_build_target_name_to_display(build_target: str,
print_mode: PrintMode) -> str:
if print_mode.fully_qualified:
return build_target
else:
return chrome_names.shorten_build_target(build_target)
def print_with_indent(indent: int, message: str,
bullet_point: str = '') -> None:
indents = ' ' * indent
bullet_point_text = f'{bullet_point} ' if bullet_point else ''
print(f'{indents}{bullet_point_text}{message}')
def print_class_nodes_grouped_by_target(
class_nodes: List[class_dependency.JavaClass], print_mode: PrintMode):
class_nodes: List[class_dependency.JavaClass], print_mode: PrintMode,
bullet_point: None):
# TODO(crbug.com/1124836): This is not quite correct because
# sets considered equal can be converted to different strings. Fix this by
# making JavaClass.build_targets return a List instead of a Set.
......@@ -36,18 +58,27 @@ def print_class_nodes_grouped_by_target(
for class_node in class_nodes:
build_target = str(class_node.build_targets)
if last_build_target != build_target:
print_with_indent(4, f'[{", ".join(class_node.build_targets)}]')
build_target_names = [
get_build_target_name_to_display(target, print_mode)
for target in class_node.build_targets
]
print_with_indent(4, f'[{", ".join(build_target_names)}]')
last_build_target = build_target
print_with_indent(8, f'{class_node.name}')
print_with_indent(
8, get_class_name_to_display(class_node.name, print_mode),
bullet_point)
def print_class_nodes(class_nodes: List[class_dependency.JavaClass],
print_mode: PrintMode):
print_mode: PrintMode, bullet_point: None):
if print_mode.build_targets:
print_class_nodes_grouped_by_target(class_nodes, print_mode)
print_class_nodes_grouped_by_target(class_nodes, print_mode,
bullet_point)
else:
for class_node in class_nodes:
print_with_indent(8, f'{class_node.name}')
print_with_indent(
8, get_class_name_to_display(class_node.name, print_mode),
bullet_point)
def print_class_dependencies_for_key(
......@@ -55,16 +86,20 @@ def print_class_dependencies_for_key(
print_mode: PrintMode):
"""Prints dependencies for a valid key into the class graph."""
node: class_dependency.JavaClass = class_graph.get_node_by_key(key)
class_name = get_class_name_to_display(node.name, print_mode)
if print_mode.inbound:
print(f'{len(node.inbound)} inbound dependency(ies) into {node.name}:')
print_class_nodes(graph.sorted_nodes_by_name(node.inbound), print_mode)
print(
f'{len(node.inbound)} inbound dependency(ies) into {class_name}:')
print_class_nodes(graph.sorted_nodes_by_name(node.inbound), print_mode,
'<-')
if print_mode.outbound:
print(
f'{len(node.outbound)} outbound dependency(ies) from {node.name}:')
f'{len(node.outbound)} outbound dependency(ies) from {class_name}:'
)
print_class_nodes(graph.sorted_nodes_by_name(node.outbound),
print_mode)
print_mode, '->')
def main():
......@@ -102,11 +137,16 @@ def main():
arg_parser.add_argument('--no-build-target',
action='store_true',
help='Do not print build target (cleaner output).')
arg_parser.add_argument('--fully-qualified',
action='store_true',
help='Use fully qualified class names instead of '
'shortened ones.')
arguments = arg_parser.parse_args()
print_mode = PrintMode(inbound=not arguments.outbound_only,
outbound=not arguments.inbound_only,
build_targets=not arguments.no_build_target)
build_targets=not arguments.no_build_target,
fully_qualified=arguments.fully_qualified)
class_graph = serialization.load_class_graph_from_file(arguments.file)
class_graph_keys = [node.name for node in class_graph.nodes]
......@@ -127,9 +167,13 @@ def main():
'please disambiguate between one of the following options:')
for valid_key in valid_keys:
print(f'\t{valid_key}')
else:
print(f'Printing class dependencies for {valid_keys[0]}:')
print_class_dependencies_for_key(class_graph, valid_keys[0],
else: # len(valid_keys) == 1
fully_qualified_class_name = valid_keys[0]
class_name = get_class_name_to_display(fully_qualified_class_name,
print_mode)
print(f'Printing class dependencies for {class_name}:')
print_class_dependencies_for_key(class_graph,
fully_qualified_class_name,
print_mode)
......
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