Skip to content

Commit 491b908

Browse files
committed
added pylinters
1 parent e0f9b7f commit 491b908

File tree

7 files changed

+117
-0
lines changed

7 files changed

+117
-0
lines changed

pylinter/init.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash -xe
2+
3+
pycodestyle .
4+
python ./run-pyflakes.py
5+
python ./run-mccabe.py 10
6+
pylint ./test.py

pylinter/readme.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Pylinter
2+
3+
Pylint is a static code analysis tool for the Python programming language
4+
5+
## Prerequisites
6+
7+
- Make sure Python 3 is installed in your system
8+
9+
- Kindly install the system dependencies using the below command,
10+
11+
```shell
12+
pip install -r requirements.txt
13+
```
14+
15+
## Running pylinter
16+
17+
```
18+
# before
19+
$ python -m mccabe foo/bar.py -m 10
20+
4:0: 'complex_fun' 12
21+
22+
# after
23+
$ ./ci/init.sh
24+
$ bad.py:4:0 complex_fun 12
25+
```
26+
27+
For simplified execution, kindly update the init.sh file with the python script name and start the ./init.sh
28+
29+
```shell
30+
#!/bin/bash -xe
31+
32+
pycodestyle .
33+
python ./run-pyflakes.py
34+
python ./run-mccabe.py 10
35+
pylint ./test.py
36+
```
37+
38+
### To run,
39+
40+
```shell
41+
sh ./init.sh
42+
```

pylinter/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mccabe==0.7.0
2+
path.py==12.5.0
3+
pylint==2.15.3

pylinter/run-mccabe.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import ast, sys
2+
import mccabe
3+
from .utils import collect_sources
4+
5+
6+
def process(py_source, max_complexity):
7+
code = py_source.text()
8+
tree = compile(code, py_source, "exec", ast.PyCF_ONLY_AST)
9+
visitor = mccabe.PathGraphingAstVisitor()
10+
visitor.preorder(tree, visitor)
11+
for graph in visitor.graphs.values():
12+
if graph.complexity() > max_complexity:
13+
text = "{}:{}:{} {} {}"
14+
return text.format(py_source, graph.lineno, graph.column, graph.entity,
15+
graph.complexity())
16+
17+
18+
def main():
19+
max_complexity = int(sys.argv[1])
20+
ok = True
21+
for py_source in collect_sources():
22+
error = process(py_source, max_complexity)
23+
if error:
24+
ok = False
25+
print(error)
26+
if not ok:
27+
sys.exit(1)
28+
29+
30+
if __name__ == "__main__":
31+
main()

pylinter/run-pyflakes.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import subprocess
2+
3+
from .utils import collect_sources
4+
5+
6+
def ignore(p):
7+
""" Ignore hidden and test files """
8+
parts = p.splitall()
9+
if any(x.startswith(".") for x in parts):
10+
return True
11+
if 'test' in parts:
12+
return True
13+
return False
14+
15+
16+
def run_pyflakes():
17+
cmd = ["pyflakes"]
18+
cmd.extend(collect_sources(ignore_func = ignore))
19+
return subprocess.call(cmd)
20+
21+
22+
if __name__ == "__main__":
23+
rc = run_pyflakes()
24+
sys.exit(rc)

pylinter/tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pycodestyle]
2+
exclude=/

pylinter/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import path
2+
3+
4+
def collect_sources(ignore_func):
5+
top_path = path.Path(".")
6+
for py_path in top_path.walkfiles("*.py"):
7+
py_path = py_path.normpath() # get rid of the leading '.'
8+
if not ignore_func(py_path):
9+
yield py_path

0 commit comments

Comments
 (0)