Coverage for apio/commands/apio_graph.py: 87%

39 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-23 03:53 +0000

1# -*- coding: utf-8 -*- 

2# -- This file is part of the Apio project 

3# -- (C) 2016-2024 FPGAwars 

4# -- Authors 

5# -- * Jesús Arroyo (2016-2019) 

6# -- * Juan Gonzalez (obijuan) (2019-2024) 

7# -- License GPLv2 

8"""Implementation of 'apio graph' command""" 

9 

10import sys 

11from pathlib import Path 

12import click 

13from apio.managers.scons_manager import SConsManager 

14from apio.commands import options 

15from apio.apio_context import ( 

16 ApioContext, 

17 PackagesPolicy, 

18 ProjectPolicy, 

19 RemoteConfigPolicy, 

20) 

21from apio.utils import cmd_util 

22from apio.common.proto.apio_scons_pb2 import ( 

23 GraphOutputType, 

24 GraphParams, 

25 Verbosity, 

26) 

27 

28 

29# ---------- apio graph 

30 

31svg_option = click.option( 

32 "svg", # Var name. 

33 "--svg", 

34 is_flag=True, 

35 help="Generate a svg file (default).", 

36 cls=cmd_util.ApioOption, 

37) 

38 

39png_option = click.option( 

40 "png", # Var name. 

41 "--png", 

42 is_flag=True, 

43 help="Generate a png file.", 

44 cls=cmd_util.ApioOption, 

45) 

46 

47pdf_option = click.option( 

48 "pdf", # Var name. 

49 "--pdf", 

50 is_flag=True, 

51 help="Generate a pdf file.", 

52 cls=cmd_util.ApioOption, 

53) 

54 

55no_viewer_option = click.option( 

56 "no_viewer", # Var name. 

57 "-n", 

58 "--no-viewer", 

59 is_flag=True, 

60 help="Do not open graph viewer.", 

61 cls=cmd_util.ApioOption, 

62) 

63 

64# -- Text in the rich-text format of the python rich library. 

65APIO_GRAPH_HELP = """ 

66The command 'apio graph' generates a graphical representation of the design \ 

67and then opens it in a graphical viewer. 

68 

69Examples:[code] 

70 apio graph # Generate a svg file (default). 

71 apio graph --no-viewer # Suppress the graphical viewer. 

72 apio graph --svg # Generate a svg file. 

73 apio graph --pdf # Generate a pdf file. 

74 apio graph --png # Generate a png file. 

75 apio graph -t my_module # Graph my_module module.[/code] 

76 

77 

78[b][Hint][/b] On Windows, type 'explorer _build/default/graph.svg' to view \ 

79the graph, and on Mac OS type 'open _build/default/graph.svg' (if your \ 

80env is different than 'default' change the commands accordingly). 

81""" 

82 

83 

84@click.command( 

85 name="graph", 

86 cls=cmd_util.ApioCommand, 

87 short_help="Generate a visual graph of the code.", 

88 help=APIO_GRAPH_HELP, 

89) 

90@click.pass_context 

91@svg_option 

92@png_option 

93@pdf_option 

94@options.env_option_gen() 

95@options.project_dir_option 

96@options.top_module_option_gen( 

97 short_help="Set the name of the top module to graph." 

98) 

99@no_viewer_option 

100@options.verbose_option 

101def cli( 

102 cmd_ctx: click.Context, 

103 *, 

104 # Options 

105 svg: bool, 

106 png: bool, 

107 pdf: bool, 

108 env: str | None, 

109 project_dir: Path | None, 

110 top_module: str, 

111 no_viewer: bool, 

112 verbose: bool, 

113): 

114 """Implements the apio graph command.""" 

115 

116 # pylint: disable=too-many-arguments 

117 

118 # -- Make pylint happy. 

119 _ = (svg,) 

120 

121 # -- Sanity check the options. 

122 cmd_util.check_at_most_one_param(cmd_ctx, ["svg", "png", "pdf"]) 

123 

124 # -- Create the apio context. 

125 apio_ctx = ApioContext( 

126 project_policy=ProjectPolicy.PROJECT_REQUIRED, 

127 remote_config_policy=RemoteConfigPolicy.CACHED_OK, 

128 packages_policy=PackagesPolicy.ENSURE_PACKAGES, 

129 project_dir_arg=project_dir, 

130 env_arg=env, 

131 ) 

132 

133 # -- Create the scons manager. 

134 scons = SConsManager(apio_ctx) 

135 

136 # -- Determine the output type. 

137 if pdf: 137 ↛ 138line 137 didn't jump to line 138 because the condition on line 137 was never true

138 output_type = GraphOutputType.PDF 

139 elif png: 139 ↛ 140line 139 didn't jump to line 140 because the condition on line 139 was never true

140 output_type = GraphOutputType.PNG 

141 else: 

142 output_type = GraphOutputType.SVG 

143 

144 # -- Construct the command info. 

145 graph_params = GraphParams( 

146 output_type=output_type, 

147 open_viewer=not no_viewer, 

148 ) 

149 if top_module: 149 ↛ 150line 149 didn't jump to line 150 because the condition on line 149 was never true

150 graph_params.top_module = top_module 

151 

152 # -- Construct the verbosity 

153 verbosity = Verbosity(all=verbose) 

154 

155 # -- Graph the project with the given parameters 

156 exit_code = scons.graph( 

157 graph_params, 

158 verbosity, 

159 ) 

160 

161 # -- Done! 

162 sys.exit(exit_code)