forked from meekhumor/ProjectX-Task-3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.py
More file actions
121 lines (105 loc) · 3.91 KB
/
analyze.py
File metadata and controls
121 lines (105 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
def remove_whitespaces_from_end(clone):
if clone[len(clone) - 1] == ' ' and len(clone) >= 80:
return remove_whitespaces_from_end(clone[:len(clone) - 1])
return clone[:len(clone)]
def line_length_violation(clone):
remove_whitespaces_from_end(clone)
if len(clone) <= 80:
return False
return True
def remove_comment(clone):
if clone.find('#') >= 0:
return clone[:clone.find('#')]
else:
return clone
def check_unclosed_string(clone):
no_of_double = 0
no_of_single = 0
for char in clone:
if char == '\'':
no_of_single += 1
elif char == '\"':
no_of_double += 1
return no_of_double % 2 != 0 or no_of_single % 2 != 0
def check_print(word):
if "print" in word:
return True
return False
def check_eval(word):
if "eval" in word:
return True
return False
def check_exec(word):
if "exec" in word:
return True
return False
def check_violations(line):
no_of_violations_for_this_line = 0
clone = line
if line_length_violation(clone):
no_of_violations_for_this_line += 1
clone = remove_comment(clone)
if check_unclosed_string(clone):
no_of_violations_for_this_line += 1
wordlist = clone.split()
forbidden_keyword_was_used_in_this_line = False
for word in wordlist:
if check_eval(word):
no_of_violations_for_this_line += 1
forbidden_keyword_was_used_in_this_line = True
if check_exec(word):
no_of_violations_for_this_line += 1
forbidden_keyword_was_used_in_this_line = True
if check_print(word):
no_of_violations_for_this_line += 1
forbidden_keyword_was_used_in_this_line = True
return no_of_violations_for_this_line, forbidden_keyword_was_used_in_this_line
src_folder = 'src'
file_path_list = []
no_of_violations_list_filewise = []
forbidden_keyword_used_list_filewise = []
for root, dirs, files in os.walk(src_folder):
for file in files:
if file.endswith('.py'):
no_of_violations_in_this_file = 0
forbidden_keyword_was_used_in_this_file = False
file_path = os.path.join(root, file)
with open(file_path, 'r') as f:
for line in f:
no_of_violations_in_this_line, forbidden_keyword_was_used_in_this_line = check_violations(line)
no_of_violations_in_this_file += no_of_violations_in_this_line
forbidden_keyword_was_used_in_this_file = forbidden_keyword_was_used_in_this_file or forbidden_keyword_was_used_in_this_line
file_path_list.append(file_path)
no_of_violations_list_filewise.append(no_of_violations_in_this_file)
forbidden_keyword_used_list_filewise.append(forbidden_keyword_was_used_in_this_file)
max_path_length = 9
for i in range(len(file_path_list)):
if len(file_path_list[i]) > max_path_length:
max_path_length = len(file_path_list[i])
s = '| Sr. No. | File Path '
for _ in range(9, max_path_length):
s += ' '
s += '| Status | No of violations |'
print(s)
for i in range(len(file_path_list)):
s = '| ' + str(i + 1) + ' | ' + file_path_list[i]
for _ in range(max_path_length - len(file_path_list[i])):
s += " "
s += " | "
if no_of_violations_list_filewise[i] == 0:
s += "CLEAN | 0 |"
elif no_of_violations_list_filewise[i] > 5 or forbidden_keyword_used_list_filewise[i]:
s += "HIGH RISK | "
s += str(no_of_violations_list_filewise[i])
if no_of_violations_list_filewise[i] <= 9:
s += " |"
elif no_of_violations_list_filewise[i] <= 99:
s += " |"
else:
s += " |"
else:
s += "LOW RISK | "
s += str(no_of_violations_list_filewise[i])
s += " |"
print(s)