tools/android/dependency_analysis/count_cycles.py
def main():
    """Enumerates the cycles within a certain length in a graph."""

    arg_parser = argparse.ArgumentParser(
        description='Given a JSON dependency graph, count the number of cycles '
        'in the package graph.')
    required_arg_group = arg_parser.add_argument_group('required arguments')
    required_arg_group.add_argument(
        '-f',
        '--file',
        required=True,
        help='Path to the JSON file containing the dependency graph. '
        'See the README on how to generate this file.')
    required_arg_group.add_argument(
        '-l',
        '--cycle-length',
        type=int,
        required=True,
        help='The maximum length of cycles to find, at most 5 or 6 to keep the '
        'script runtime low.')
    arg_parser.add_argument(
        '-o',
        '--output',
        type=argparse.FileType('w'),
        help='Path to the file to write the list of cycles to.')
    args = arg_parser.parse_args()

    _, package_graph, _ = serialization.load_class_and_package_graphs_from_file(
        args.file)

    all_cycles = find_cycles(package_graph, args.cycle_length)
    # There are no cycles of length 0 or 1 (since self-loops are disallowed).
    nonzero_cycles = all_cycles[2:]

    print(f'Found {sum(len(cycles) for cycles in nonzero_cycles)} cycles.')

    for cycle_length, cycles in enumerate(nonzero_cycles, 2):
        print(f'Found {len(cycles)} cycles of length {cycle_length}.')

    if args.output is not None:
        print(f'Dumping cycles to {args.output.name}.')
        with args.output as output_file:
            for cycle_length, cycles in enumerate(nonzero_cycles, 2):
                output_file.write(f'Cycles of length {cycle_length}:\n')
                cycle_texts = []
                for cycle in cycles:
                    cycle_texts.append(' > '.join(cycle_node.name
                                                  for cycle_node in cycle))
                output_file.write('\n'.join(sorted(cycle_texts)))
                output_file.write('\n')
Enumerates the cycles within a certain length in a graph.
Enumerates the cycles within a certain length in a graph.
Enumerates the cycles within a certain length in a graph .
def main():
    arg_parser = argparse.ArgumentParser(
        description='Given a JSON dependency graph, count the number of cycles '
        'in the package graph.')
    required_arg_group = arg_parser.add_argument_group('required arguments')
    required_arg_group.add_argument(
        '-f',
        '--file',
        required=True,
        help='Path to the JSON file containing the dependency graph. '
        'See the README on how to generate this file.')
    required_arg_group.add_argument(
        '-l',
        '--cycle-length',
        type=int,
        required=True,
        help='The maximum length of cycles to find, at most 5 or 6 to keep the '
        'script runtime low.')
    arg_parser.add_argument(
        '-o',
        '--output',
        type=argparse.FileType('w'),
        help='Path to the file to write the list of cycles to.')
    args = arg_parser.parse_args()
    _, package_graph, _ = serialization.load_class_and_package_graphs_from_file(
        args.file)
    all_cycles = find_cycles(package_graph, args.cycle_length)
    nonzero_cycles = all_cycles[2:]
    print(f'Found {sum(len(cycles) for cycles in nonzero_cycles)} cycles.')
    for cycle_length, cycles in enumerate(nonzero_cycles, 2):
        print(f'Found {len(cycles)} cycles of length {cycle_length}.')
    if args.output is not None:
        print(f'Dumping cycles to {args.output.name}.')
        with args.output as output_file:
            for cycle_length, cycles in enumerate(nonzero_cycles, 2):
                output_file.write(f'Cycles of length {cycle_length}:\n')
                cycle_texts = []
                for cycle in cycles:
                    cycle_texts.append(' > '.join(cycle_node.name
                                                  for cycle_node in cycle))
                output_file.write('\n'.join(sorted(cycle_texts)))
                output_file.write('\n')
def main ( ) : arg_parser = argparse . ArgumentParser ( description = 'Given a JSON dependency graph, count the number of cycles ' 'in the package graph.' ) required_arg_group = arg_parser . add_argument_group ( 'required arguments' ) required_arg_group . add_argument ( '-f' , '--file' , required = True , help = 'Path to the JSON file containing the dependency graph. ' 'See the README on how to generate this file.' ) required_arg_group . add_argument ( '-l' , '--cycle-length' , type = int , required = True , help = 'The maximum length of cycles to find, at most 5 or 6 to keep the ' 'script runtime low.' ) arg_parser . add_argument ( '-o' , '--output' , type = argparse . FileType ( 'w' ) , help = 'Path to the file to write the list of cycles to.' ) args = arg_parser . parse_args ( ) _ , package_graph , _ = serialization . load_class_and_package_graphs_from_file ( args . file ) all_cycles = find_cycles ( package_graph , args . cycle_length ) nonzero_cycles = all_cycles [ 2 : ] print ( f'Found {sum(len(cycles) for cycles in nonzero_cycles)} cycles.' ) for cycle_length , cycles in enumerate ( nonzero_cycles , 2 ) : print ( f'Found {len(cycles)} cycles of length {cycle_length}.' ) if args . output is not None : print ( f'Dumping cycles to {args.output.name}.' ) with args . output as output_file : for cycle_length , cycles in enumerate ( nonzero_cycles , 2 ) : output_file . write ( f'Cycles of length {cycle_length}:\n' ) cycle_texts = [ ] for cycle in cycles : cycle_texts . append ( ' > ' . join ( cycle_node . name for cycle_node in cycle ) ) output_file . write ( '\n' . join ( sorted ( cycle_texts ) ) ) output_file . write ( '\n' )
Enumerates the cycles within a certain length in a graph.
Enumerates the cycles within a certain length in a graph .
"""Enumerates the cycles within a certain length in a graph.""" # There are no cycles of length 0 or 1 (since self-loops are disallowed).