Debian Codegraph @

tasks/check.py

raw symbols SCIP
1 # -*- coding: ascii -*-
2 #
3 # Copyright 2018 - 2025
4 # Andr\xe9 Malo or his licensors, as applicable
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 """
18 Run code checkers
19 ~~~~~~~~~~~~~~~~~
20
21 """
22
23 import os as _os
24
25 import invoke as _invoke
26
27 from . import _features
28 from ._inv import tasks as _tasks
29
30 _CHECKERS = []
31 _CHECKERS_TF = []
32 _CHECKERS_SAM = []
33
34
35 @_tasks.optional(_CHECKERS, _features.pylint)
36 @_invoke.task("clean.py")
37 def lint(ctx):
38 """Run pylint"""
39 if ctx.get("package"):
40 cmd = (
41 [ctx.which("pylint")] + ctx.s("--rcfile pylintrc") + [ctx.package]
42 )
43 with ctx.shell.root_dir():
44 ctx.run(ctx.c(cmd), echo=True)
45
46 cmd = [ctx.which("pylint")] + ctx.s("--rcfile tasks/pylintrc") + ["tasks"]
47 with ctx.shell.root_dir():
48 ctx.run(ctx.c(cmd), echo=True)
49
50
51 @_tasks.optional(_CHECKERS, _features.mypy)
52 @_invoke.task("clean.py")
53 def mypy(ctx):
54 """Run mypy"""
55 cmd = [
56 ctx.which("mypy"),
57 "--config-file",
58 "pyproject.toml",
59 "-p",
60 ctx.package,
61 ]
62 with ctx.shell.root_dir():
63 ctx.run(ctx.c(cmd), echo=True)
64
65
66 @_tasks.optional(_CHECKERS, _features.flake8)
67 @_invoke.task("clean.py")
68 def flake8(ctx):
69 """Run flake8"""
70 if ctx.get("package"):
71 if _os.path.exists(ctx.shell.native(ctx.package + ".py")):
72 candidate = ctx.package + ".py"
73 else:
74 candidate = ctx.package
75 cmd = [ctx.which("flake8"), candidate]
76 with ctx.shell.root_dir():
77 ctx.run(ctx.c(cmd), echo=True)
78
79 cmd = [ctx.which("flake8"), "tasks"]
80 with ctx.shell.root_dir():
81 ctx.run(ctx.c(cmd), echo=True)
82
83
84 @_tasks.optional(_CHECKERS, _features.black)
85 @_invoke.task("clean.py")
86 def black(ctx):
87 """Run black"""
88 cmd = [ctx.which("black")] + ctx.s(
89 "-q --check --diff --color --config pyproject.toml ."
90 )
91
92 with ctx.shell.root_dir():
93 ctx.run(ctx.c(cmd), echo=True)
94
95
96 @_tasks.optional(_CHECKERS, _features.isort)
97 @_invoke.task("clean.py")
98 def isort(ctx):
99 """Run isort"""
100 cmd = [ctx.which("isort")]
101 cmd += ctx.s("--check --diff --color --settings-path pyproject.toml")
102 cmd += ["tasks"]
103 if ctx.get("package"):
104 cmd += [ctx.package]
105 if _os.path.exists(ctx.shell.native("tests")):
106 cmd += ["tests"]
107
108 with ctx.shell.root_dir():
109 ctx.run(ctx.c(cmd), echo=True)
110
111
112 @_tasks.optional(_CHECKERS_TF, _features.terraform)
113 @_invoke.task()
114 def tf_fmt(ctx):
115 """Run terraform fmt -check"""
116 base_cmd = [ctx.which("terraform"), "-chdir=" + ctx.paths.terraform]
117 with ctx.shell.root_dir():
118 ctx.run(ctx.c(base_cmd + ctx.s("fmt -recursive -check")), echo=True)
119
120
121 @_tasks.optional(_CHECKERS_TF, _features.terraform)
122 @_invoke.task()
123 def tf_validate(ctx):
124 """Run terraform validate"""
125 base_cmd = [ctx.which("terraform"), "-chdir=" + ctx.paths.terraform]
126 with ctx.shell.root_dir():
127 ctx.run(
128 ctx.c(base_cmd + ctx.s("init -backend=false")),
129 hide=True,
130 echo=True,
131 )
132 ctx.run(ctx.c(base_cmd + ctx.s("validate")), echo=True)
133
134
135 # @_tasks.optional(_CHECKERS_SAM, _features.sam)
136 # @_invoke.task()
137 # def sam_validate(ctx):
138 # """Run sam validate"""
139 # cmd = (
140 # [ctx.which("sam")]
141 # + ctx.s("validate --lint -t")
142 # + [ctx.cloudformation.template]
143 # )
144 # with ctx.shell.root_dir():
145 # ctx.run(ctx.c(cmd), echo=True)
146
147
148 # Collections of checks
149 # =====================
150
151
152 @_tasks.optional(_CHECKERS_TF)
153 @_invoke.task(name="tf")
154 def terraform(ctx):
155 """Run terraform checks"""
156 _tasks.execute(ctx, *["check." + checker for checker in _CHECKERS_TF])
157
158
159 @_tasks.optional(_CHECKERS_SAM)
160 @_invoke.task()
161 def sam(ctx):
162 """Run sam checks"""
163 _tasks.execute(ctx, *["check." + checker for checker in _CHECKERS_SAM])
164
165
166 _CHECKERS += _CHECKERS_TF
167 _CHECKERS += _CHECKERS_SAM
168
169
170 @_tasks.optional(_CHECKERS)
171 @_invoke.task(name="all", default=True)
172 def all_(ctx):
173 """Run all checkers"""
174 _tasks.execute(ctx, *["check." + checker for checker in _CHECKERS])