Skip to content

Commit fd518f5

Browse files
authored
Merge pull request #886 from rexdivakar/main
Pylinter
2 parents 2b7e3fc + d996102 commit fd518f5

File tree

8 files changed

+131
-0
lines changed

8 files changed

+131
-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/output.png

56.9 KB
Loading

pylinter/readme.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
## Setup instructions
10+
11+
- Kindly install the system dependencies using the below command,
12+
13+
```shell
14+
pip install -r requirements.txt
15+
```
16+
17+
## Running pylinter
18+
19+
```shell
20+
# before
21+
$ python -m mccabe foo/bar.py -m 10
22+
4:0: 'complex_fun' 12
23+
24+
# after
25+
$ ./ci/init.sh
26+
$ bad.py:4:0 complex_fun 12
27+
```
28+
29+
For simplified execution, kindly update the init.sh file with the python script name and start the ./init.sh
30+
31+
```shell
32+
#!/bin/bash -xe
33+
34+
pycodestyle .
35+
python ./run-pyflakes.py
36+
python ./run-mccabe.py 10
37+
pylint ./test.py
38+
```
39+
40+
### To run,
41+
42+
```shell
43+
sh ./init.sh
44+
```
45+
46+
## Output
47+
48+
![image](https://user-images.githubusercontent.com/15235122/194733562-68167398-238d-49eb-a310-29812427c01d.png)
49+
50+
51+
## Author(s)
52+
53+
Divakar R

pylinter/requirements.txt

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

pylinter/run-mccabe.py

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

pylinter/run-pyflakes.py

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