Coverage for apio/commands/apio_graph.py: 87%
39 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
1# This file is part of the Apio project
2# (C) 2016-present FPGAwars
3# Project authors:
4# * Jesús Arroyo (Jesus89) (2016-2019)
5# * Juan Gonzalez (obijuan) (2019-2024)
6# * The Apio Team (2024-present)
7# License: GPLv2
9"""Implementation of 'apio graph' command"""
11import sys
12from pathlib import Path
13import click
14from apio.managers.scons_manager import SConsManager
15from apio.commands import options
16from apio.apio_context import (
17 ApioContext,
18 PackagesPolicy,
19 ProjectPolicy,
20 RemoteConfigPolicy,
21)
22from apio.utils import cmd_util
23from apio.common.proto.stubs.apio_scons_pb2 import (
24 GraphOutputType,
25 GraphParams,
26 Verbosity,
27)
30# ---------- apio graph
32svg_option = click.option(
33 "svg", # Var name.
34 "--svg",
35 is_flag=True,
36 help="Generate a svg file (default).",
37 cls=cmd_util.ApioOption,
38)
40png_option = click.option(
41 "png", # Var name.
42 "--png",
43 is_flag=True,
44 help="Generate a png file.",
45 cls=cmd_util.ApioOption,
46)
48pdf_option = click.option(
49 "pdf", # Var name.
50 "--pdf",
51 is_flag=True,
52 help="Generate a pdf file.",
53 cls=cmd_util.ApioOption,
54)
56no_viewer_option = click.option(
57 "no_viewer", # Var name.
58 "-n",
59 "--no-viewer",
60 is_flag=True,
61 help="Do not open graph viewer.",
62 cls=cmd_util.ApioOption,
63)
65# -- Text in the rich-text format of the python rich library.
66APIO_GRAPH_HELP = """
67The command 'apio graph' generates a graphical representation of the design \
68and then opens it in a graphical viewer.
70Examples:[code]
71 apio graph # Generate a svg file (default).
72 apio graph --no-viewer # Suppress the graphical viewer.
73 apio graph --svg # Generate a svg file.
74 apio graph --pdf # Generate a pdf file.
75 apio graph --png # Generate a png file.
76 apio graph -t my_module # Graph my_module module.[/code]
79[b][Hint][/b] On Windows, type 'explorer _build/default/graph.svg' to view \
80the graph, and on Mac OS type 'open _build/default/graph.svg' (if your \
81env is different than 'default' change the commands accordingly).
82"""
85@click.command(
86 name="graph",
87 cls=cmd_util.ApioCommand,
88 short_help="Generate a visual graph of the code.",
89 help=APIO_GRAPH_HELP,
90)
91@click.pass_context
92@svg_option
93@png_option
94@pdf_option
95@options.env_option_gen()
96@options.project_dir_option
97@options.top_module_option_gen(
98 short_help="Set the name of the top module to graph."
99)
100@no_viewer_option
101@options.verbose_option
102def cli(
103 cmd_ctx: click.Context,
104 *,
105 # Options
106 svg: bool,
107 png: bool,
108 pdf: bool,
109 env: str | None,
110 project_dir: Path | None,
111 top_module: str,
112 no_viewer: bool,
113 verbose: bool,
114):
115 """Implements the apio graph command."""
117 # pylint: disable=too-many-arguments
119 # -- Make pylint happy.
120 _ = (svg,)
122 # -- Sanity check the options.
123 cmd_util.check_at_most_one_param(cmd_ctx, ["svg", "png", "pdf"])
125 # -- Create the apio context.
126 apio_ctx = ApioContext(
127 project_policy=ProjectPolicy.PROJECT_REQUIRED,
128 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
129 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
130 project_dir_arg=project_dir,
131 env_arg=env,
132 )
134 # -- Create the scons manager.
135 scons = SConsManager(apio_ctx)
137 # -- Determine the output type.
138 if pdf: 138 ↛ 139line 138 didn't jump to line 139 because the condition on line 138 was never true
139 output_type = GraphOutputType.PDF
140 elif png: 140 ↛ 141line 140 didn't jump to line 141 because the condition on line 140 was never true
141 output_type = GraphOutputType.PNG
142 else:
143 output_type = GraphOutputType.SVG
145 # -- Construct the command info.
146 graph_params = GraphParams(
147 output_type=output_type,
148 open_viewer=not no_viewer,
149 )
150 if top_module: 150 ↛ 151line 150 didn't jump to line 151 because the condition on line 150 was never true
151 graph_params.top_module = top_module
153 # -- Construct the verbosity
154 verbosity = Verbosity(all=verbose)
156 # -- Graph the project with the given parameters
157 exit_code = scons.graph(
158 graph_params,
159 verbosity,
160 )
162 # -- Done!
163 sys.exit(exit_code)