Commit 9fd2ca9e authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

[Lorenz] Print build target of class dependencies

When auditing dependencies, a fundamental information is in which build
target the outbound or inbound dependency is.

Change-Id: I55513f18b76e6bad01d7a2f68aad5e56b256d49c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2391651
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Reviewed-by: default avatarMohamed Heikal <mheikal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804387}
parent 7678ba8a
......@@ -5,7 +5,7 @@
"""Implementation of the graph module for a [Java class] dependency graph."""
import re
from typing import List, Tuple
from typing import Set, Tuple
import graph
import class_json_consts
......@@ -72,14 +72,18 @@ class JavaClass(graph.Node):
"""A set of nested classes contained within this class."""
return self._nested_classes
@nested_classes.setter
def nested_classes(self, other):
self._nested_classes = other
@property
def build_targets(self) -> List[str]:
def build_targets(self) -> Set[str]:
"""Which build target(s) contain the class."""
return self._build_targets
@nested_classes.setter
def nested_classes(self, other):
self._nested_classes = other
@build_targets.setter
def build_targets(self, other):
self._build_targets = other
def add_nested_class(self, nested: str):
self._nested_classes.add(nested)
......
......@@ -6,6 +6,7 @@
import argparse
from dataclasses import dataclass
from typing import List
import class_dependency
import graph
......@@ -18,6 +19,16 @@ class PrintMode:
"""Options of how and which dependencies to output."""
inbound: bool
outbound: bool
build_targets: bool
def print_class_nodes(class_nodes: List[class_dependency.JavaClass],
print_mode: PrintMode):
for class_node in class_nodes:
output = f'\t{class_node.name}'
if print_mode.build_targets:
output += f' [{", ".join(class_node.build_targets)}]'
print(output)
def print_class_dependencies_for_key(
......@@ -28,14 +39,13 @@ def print_class_dependencies_for_key(
if print_mode.inbound:
print(f'{len(node.inbound)} inbound dependency(ies) for {node.name}:')
for inbound_dep in graph.sorted_nodes_by_name(node.inbound):
print(f'\t{inbound_dep.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) for {node.name}:')
for outbound_dep in graph.sorted_nodes_by_name(node.outbound):
print(f'\t{outbound_dep.name}')
print_class_nodes(graph.sorted_nodes_by_name(node.outbound),
print_mode)
def main():
......@@ -70,10 +80,14 @@ def main():
dest='outbound_only',
action='store_true',
help='Print outbound dependencies only.')
arg_parser.add_argument('--no-build-target',
action='store_true',
help='Do not print build target (cleaner output).')
arguments = arg_parser.parse_args()
print_mode = PrintMode(inbound=not arguments.outbound_only,
outbound=not arguments.inbound_only)
outbound=not arguments.inbound_only,
build_targets=not arguments.no_build_target)
class_graph = serialization.load_class_graph_from_file(arguments.file)
class_graph_keys = [node.name for node in class_graph.nodes]
......
......@@ -78,8 +78,11 @@ def create_class_graph_from_json_obj(
name = node_json_obj[json_consts.NAME]
nested = node_json_obj[json_consts.META][
class_json_consts.NESTED_CLASSES]
build_targets = node_json_obj[json_consts.META][
class_json_consts.BUILD_TARGETS]
added_node = class_graph.add_node_if_new(name)
added_node.nested_classes = set(nested)
added_node.build_targets = set(build_targets)
for edge_json_obj in json_obj[json_consts.EDGES]:
begin_key = edge_json_obj[json_consts.BEGIN]
......
......@@ -180,6 +180,11 @@ class TestSerialization(unittest.TestCase):
self.assertEqual(node_1.nested_classes,
{self.CLASS_1_NESTED_1, self.CLASS_1_NESTED_2})
self.assertEqual(node_2.nested_classes, {self.CLASS_2_NESTED_1})
self.assertEqual(node_3.nested_classes, set())
self.assertEqual(node_1.build_targets, {self.BUILD_TARGET_1})
self.assertEqual(node_2.build_targets, set())
self.assertEqual(node_3.build_targets,
{self.BUILD_TARGET_1, self.BUILD_TARGET_2})
self.assertEqual(
graph.sorted_edges_by_name(test_graph.edges),
graph.sorted_edges_by_name([(node_1, node_2), (node_1, node_3),
......
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