10
10
import gensim
11
11
import tempfile
12
12
import json
13
+ from enum import Enum
13
14
from nltk .tokenize import word_tokenize
14
15
from collections import OrderedDict
15
16
19
20
similarity_label_length = len (similarity_column_label )
20
21
21
22
23
+ class ReturnCode (Enum ):
24
+ SUCCESS = 0
25
+ BAD_INPUT = 1
26
+ THRESHOLD_EXCEEDED = 2
27
+
28
+
22
29
class CliColors :
23
30
HEADER = '\033 [95m'
24
31
OKBLUE = '\033 [94m'
@@ -89,7 +96,7 @@ def run(fail_threshold, directories, files, ignore_directories, ignore_files,
89
96
for directory in directories :
90
97
if not os .path .isdir (directory ):
91
98
print ("Path does not exist or is not a directory:" , directory )
92
- return (1 , {})
99
+ return (ReturnCode . BAD_INPUT , {})
93
100
source_code_files += get_all_source_code_from_directory (
94
101
directory , file_extensions )
95
102
for directory in ignore_directories :
@@ -98,25 +105,25 @@ def run(fail_threshold, directories, files, ignore_directories, ignore_files,
98
105
else :
99
106
if len (files ) < 2 :
100
107
print ("Too few files to compare, you need to supply at least 2" )
101
- return (1 , {})
108
+ return (ReturnCode . BAD_INPUT , {})
102
109
for supplied_file in files :
103
110
if not os .path .isfile (supplied_file ):
104
111
print ("Supplied file does not exist:" , supplied_file )
105
- return (1 , {})
112
+ return (ReturnCode . BAD_INPUT , {})
106
113
source_code_files = files
107
114
108
115
files_to_ignore += ignore_files if ignore_files else list ()
109
116
source_code_files = list (set (source_code_files ) - set (files_to_ignore ))
110
117
if len (source_code_files ) < 2 :
111
118
print ("Not enough source code files found" )
112
- return (1 , {})
119
+ return (ReturnCode . BAD_INPUT , {})
113
120
114
121
# Get the absolute project root directory path to remove when printing out the results
115
122
if project_root_dir :
116
123
if not os .path .isdir (project_root_dir ):
117
124
print (
118
125
"The project root directory does not exist or is not a directory:" , project_root_dir )
119
- return (1 , {})
126
+ return (ReturnCode . BAD_INPUT , {})
120
127
project_root_dir = os .path .abspath (project_root_dir )
121
128
project_root_dir = os .path .join (
122
129
project_root_dir , '' ) # Add the trailing slash
@@ -138,7 +145,7 @@ def run(fail_threshold, directories, files, ignore_directories, ignore_files,
138
145
num_features = len (dictionary ))
139
146
140
147
largest_string_length = len (max (source_code_files , key = len ))
141
- exit_code = 0
148
+ exit_code = ReturnCode . SUCCESS
142
149
code_similarity = dict ()
143
150
for source_file in source_code :
144
151
# Check for similarities
@@ -170,12 +177,12 @@ def run(fail_threshold, directories, files, ignore_directories, ignore_files,
170
177
code_similarity [short_source_file_path ][short_source_path ] = round (
171
178
similarity_percentage , 2 )
172
179
if similarity_percentage > fail_threshold :
173
- exit_code = 1
180
+ exit_code = ReturnCode . THRESHOLD_EXCEEDED
174
181
color = CliColors .OKGREEN if similarity_percentage < 10 else (
175
182
CliColors .WARNING if similarity_percentage < 20 else CliColors .FAIL )
176
183
conditional_print ("%s " % (short_source_path .ljust (largest_string_length )) +
177
184
color + "%.2f" % (similarity_percentage ) + CliColors .ENDC , json_output )
178
- if exit_code == 1 :
185
+ if exit_code == ReturnCode . THRESHOLD_EXCEEDED :
179
186
conditional_print (
180
187
"Code duplication threshold exceeded. Please consult logs." , json_output )
181
188
if json_output :
@@ -187,4 +194,4 @@ def run(fail_threshold, directories, files, ignore_directories, ignore_files,
187
194
188
195
if __name__ == "__main__" :
189
196
exit_code , _ = main ()
190
- sys .exit (exit_code )
197
+ sys .exit (exit_code . value )
0 commit comments