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

40 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-06 10:20 +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 typing import Optional 

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.apio_pb2 import GraphOutputType, GraphParams, Verbosity 

24 

25 

26# ---------- apio graph 

27 

28svg_option = click.option( 

29 "svg", # Var name. 

30 "--svg", 

31 is_flag=True, 

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

33 cls=cmd_util.ApioOption, 

34) 

35 

36png_option = click.option( 

37 "png", # Var name. 

38 "--png", 

39 is_flag=True, 

40 help="Generate a png file.", 

41 cls=cmd_util.ApioOption, 

42) 

43 

44pdf_option = click.option( 

45 "pdf", # Var name. 

46 "--pdf", 

47 is_flag=True, 

48 help="Generate a pdf file.", 

49 cls=cmd_util.ApioOption, 

50) 

51 

52no_viewer_option = click.option( 

53 "no_viewer", # Var name. 

54 "-n", 

55 "--no-viewer", 

56 is_flag=True, 

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

58 cls=cmd_util.ApioOption, 

59) 

60 

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

62APIO_GRAPH_HELP = """ 

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

64and then opens it in a graphical viewer. 

65 

66Examples:[code] 

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

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

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

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

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

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

73 

74 

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

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

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

78""" 

79 

80 

81@click.command( 

82 name="graph", 

83 cls=cmd_util.ApioCommand, 

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

85 help=APIO_GRAPH_HELP, 

86) 

87@click.pass_context 

88@svg_option 

89@png_option 

90@pdf_option 

91@options.env_option_gen() 

92@options.project_dir_option 

93@options.top_module_option_gen( 

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

95) 

96@no_viewer_option 

97@options.verbose_option 

98def cli( 

99 cmd_ctx: click.Context, 

100 # Options 

101 svg: bool, 

102 png: bool, 

103 pdf: bool, 

104 env: Optional[str], 

105 project_dir: Optional[Path], 

106 top_module: str, 

107 no_viewer: bool, 

108 verbose: bool, 

109): 

110 """Implements the apio graph command.""" 

111 

112 # pylint: disable=too-many-arguments 

113 # pylint: disable=too-many-positional-arguments 

114 

115 # -- Make pylint happy. 

116 _ = (svg,) 

117 

118 # -- Sanity check the options. 

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

120 

121 # -- Create the apio context. 

122 apio_ctx = ApioContext( 

123 project_policy=ProjectPolicy.PROJECT_REQUIRED, 

124 remote_config_policy=RemoteConfigPolicy.CACHED_OK, 

125 packages_policy=PackagesPolicy.ENSURE_PACKAGES, 

126 project_dir_arg=project_dir, 

127 env_arg=env, 

128 ) 

129 

130 # -- Create the scons manager. 

131 scons = SConsManager(apio_ctx) 

132 

133 # -- Determine the output type. 

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

135 output_type = GraphOutputType.PDF 

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

137 output_type = GraphOutputType.PNG 

138 else: 

139 output_type = GraphOutputType.SVG 

140 

141 # -- Construct the command info. 

142 graph_params = GraphParams( 

143 output_type=output_type, 

144 open_viewer=not no_viewer, 

145 ) 

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

147 graph_params.top_module = top_module 

148 

149 # -- Construct the verbosity 

150 verbosity = Verbosity(all=verbose) 

151 

152 # -- Graph the project with the given parameters 

153 exit_code = scons.graph( 

154 graph_params, 

155 verbosity, 

156 ) 

157 

158 # -- Done! 

159 sys.exit(exit_code)