Coverage for apio/commands/apio_lint.py: 100%

32 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 lint' command""" 

9import sys 

10from typing import Optional 

11from pathlib import Path 

12import click 

13from apio.managers.scons_manager import SConsManager 

14from apio.utils import util 

15from apio.utils import cmd_util 

16from apio.commands import options 

17from apio.apio_context import ( 

18 ApioContext, 

19 PackagesPolicy, 

20 ProjectPolicy, 

21 RemoteConfigPolicy, 

22) 

23from apio.common.proto.apio_pb2 import LintParams 

24 

25 

26# ------- apio lint 

27 

28 

29nostyle_option = click.option( 

30 "nostyle", # Var name 

31 "--nostyle", 

32 is_flag=True, 

33 help="Disable all style warnings.", 

34 cls=cmd_util.ApioOption, 

35) 

36 

37 

38nowarn_option = click.option( 

39 "nowarn", # Var name 

40 "--nowarn", 

41 type=str, 

42 metavar="nowarn", 

43 help="Disable specific warning(s).", 

44 cls=cmd_util.ApioOption, 

45) 

46 

47warn_option = click.option( 

48 "warn", # Var name 

49 "--warn", 

50 type=str, 

51 metavar="warn", 

52 help="Enable specific warning(s).", 

53 cls=cmd_util.ApioOption, 

54) 

55 

56 

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

58APIO_LINT_HELP = """ 

59The command 'apio lint' scans the project's source files and reports errors, \ 

60inconsistencies, and style violations. The command uses the Verilator tool, \ 

61which is included with the standard Apio installation. 

62 

63Examples:[code] 

64 apio lint 

65 apio lint -t my_module 

66 apio lint --all[/code] 

67""" 

68 

69 

70@click.command( 

71 name="lint", 

72 cls=cmd_util.ApioCommand, 

73 short_help="Lint the source code.", 

74 help=APIO_LINT_HELP, 

75) 

76@click.pass_context 

77@nostyle_option 

78@nowarn_option 

79@warn_option 

80@options.all_option_gen( 

81 short_help="Enable all warnings, including code style warnings." 

82) 

83@options.top_module_option_gen( 

84 short_help="Restrict linting to this module and its dependencies." 

85) 

86@options.env_option_gen() 

87@options.project_dir_option 

88def cli( 

89 _: click.Context, 

90 # Options 

91 nostyle: bool, 

92 nowarn: str, 

93 warn: str, 

94 all_: bool, 

95 top_module: str, 

96 env: Optional[str], 

97 project_dir: Optional[Path], 

98): 

99 """Lint the source code.""" 

100 

101 # pylint: disable=too-many-arguments 

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

103 

104 # -- Create the apio context. 

105 apio_ctx = ApioContext( 

106 project_policy=ProjectPolicy.PROJECT_REQUIRED, 

107 remote_config_policy=RemoteConfigPolicy.CACHED_OK, 

108 packages_policy=PackagesPolicy.ENSURE_PACKAGES, 

109 project_dir_arg=project_dir, 

110 env_arg=env, 

111 ) 

112 

113 # -- Create the scons manager. 

114 scons = SConsManager(apio_ctx) 

115 

116 # -- Convert the comma separated args values to python lists 

117 no_warns_list = util.split(nowarn, ",", strip=True, keep_empty=False) 

118 warns_list = util.split(warn, ",", strip=True, keep_empty=False) 

119 

120 # -- Create the lint params 

121 lint_params = LintParams( 

122 top_module=top_module if top_module else None, 

123 verilator_all=all_, 

124 verilator_no_style=nostyle, 

125 verilator_no_warns=no_warns_list, 

126 verilator_warns=warns_list, 

127 ) 

128 

129 assert lint_params.IsInitialized(), lint_params 

130 

131 # -- Lint the project with the given parameters 

132 exit_code = scons.lint(lint_params) 

133 sys.exit(exit_code)