-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeParser.py
More file actions
458 lines (436 loc) · 17.7 KB
/
codeParser.py
File metadata and controls
458 lines (436 loc) · 17.7 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#====================================================================================
#FileName : codeParser.py
#Description : Python script to analyse code
#Author : Hassene ELHABIBI <elhabibi.hassene@gmail.com>
#date : 28 February 2021
#====================================================================================
import os
import sys
import csv
def ask_user():
"""
Description: this function will ask the user if he want to tray to enter a valid choise or not (Y/N).
To conitune, please tape "q".
"""
check = str(input("Do You Want To Try Again ? (Y/N): ")).lower().strip()
try:
if check[0] == 'y':
return True
elif check[0] == 'n':
return False
else:
print('Invalid Input')
return ask_user()
except Exception as error:
print("Please enter valid inputs")
print(error)
return ask_user()
def maketempfile(dir_name,base_filename,filename_suffix):
"""
inputParm: dir_name : directory where the file will be added
base_filename : the name of the file
filename_suffix: file extension
Description: this function will create a temporary file.
To conitune, please tape "q".
"""
file_path = os.path.join(dir_name, '.'.join((base_filename, filename_suffix)))
if not os.path.exists(file_path):
os.mknod(file_path)
return file_path
else:
return None
def maketempDir(current_path):
"""
inputParm: Current_path => path that will parsed
Description: this function will create a temporary directory.
Return: dirNam => None: if the creation the directory failed
Path: the directory path if the successfully created
To conitune, please tape "q".
"""
FolderName = 'tempDir'
dirName = os.path.join(current_path,FolderName)
try:
os.makedirs(dirName)
except OSError:
print(("Creation of the directory %s failed" % dirName))
dirName = None
else:
print ("Successfully created the directory %s " % dirName)
return(dirName)
def SearchKeyWordInLines(file_name,keyWord):
"""
inputParm: file_name : file to be parsed
keyWord : Key word to search for
Description: this function will check if any line in the file contains given string.
Return: research result ( found (True/False))
To conitune, please tape "q".
"""
keyWordFound = False
with open(file_name, encoding="utf8", errors='ignore') as read_obj:
count = 0
# Read all lines in the file one by one
for line in read_obj:
count +=1
# For each line, check if line contains the string
if keyWord in line:
#print(f'line {count}: {line}')
keyWordFound = True
else:
continue
return keyWordFound
def SearchKeyWordInWords(file_name,keyWord):
"""
inputParm: file_name : file to be parsed
keyWord : Key word to search for
Description: this function will check if any line in the file contains given string.
Return: research result ( found (True/False))
"""
keyWordFound = False
# Open the file in read only mode
with open(file_name, encoding="utf8", errors='ignore') as read_obj:
# Read all lines in the file one by one
for line in read_obj:
for word in line:
# For each line, check if line contains the string
if keyWord in word:
#print(f'Word: {word}')
keyWordFound = True
else:
continue
return keyWordFound
def parseDirectory(Current_path,keyWord):
"""
inputParm: Current_path => path that will parsed
keyWord => key word to search
Description: this function will print all folders, subfolders and filenames found in the current path
excluding the folders, subfolders and filename which start with a dot.
To conitune, please tape "q".
"""
print(f"the directory which it will be parsed is {Current_path}")
for folderName, subfolders, filenames in os.walk(Current_path):
if str(os.path.basename(folderName)).startswith('.'):
continue
else:
print("the current folder is "+ folderName)
for subfolder in subfolders:
if str(subfolder).startswith('.'):
subfolders.remove(subfolder)
else:
print("SUBFOLDER of "+ folderName + ":" + subfolder)
for filename in filenames:
if str(filename).startswith('.'):
filenames.remove('filename')
else:
print("File Inside" + folderName + ":" + filename)
print("Search a keyword in a line")
SearchKeyWordInLines(filename,keyWord)
print("Search a keyword in a word")
SearchKeyWordInWords(filename,keyWord)
#FileObj=open(filename,'r')
#lines = FileObj.readlines()
#count = 0
#for line in lines:
#count += 1
#print(f'line {count}: {line}')
def getStaticArch(working_dir):
"""
inputParm: Current_path => path that will parsed
Description: this function will analyse the static architecture by:
- Parsing the project structure
- get all folders, subfolders and filenames found in the current path
excluding the folders, subfolders and filename which start with a dot.
- write them to a cvs file
To conitune, please tape "q".
"""
current_path = os.getcwd()
dir_name = maketempDir(current_path)
if dir_name != None:
csvFile = maketempfile(dir_name,"AnalysisResult","csv")
if csvFile != None:
csvFileObj = open(csvFile, 'w', newline='')
else:
sys.exit("Failed to create a csv file!")
csvWriter = csv.writer(csvFileObj)
csvWriter.writerow(["Directory", "Folder", "fileName", "File_Sufix"])
print(f"the directory which it will be parsed is {working_dir}")
for folderName, subfolders, filenames in os.walk(working_dir):
if str(os.path.basename(folderName)).startswith('.'):
continue
else:
#csvWriter.writerow([folderName])
for subfolder in subfolders:
if str(subfolder).startswith('.'):
subfolders.remove(subfolder)
for filename in filenames:
if str(filename).startswith('.'):
filenames.remove(filename)
csvWriter.writerow([folderName,subfolders,filenames])
csvFileObj.close()
def searchForData(working_dir,keyWord):
"""
inputParm: working_dir : path that will parsed
keyWord : Key word to searched for
Description: this function will search for datas by:
- Parsing the code in the current path
excluding the folders, subfolders and filename which start with a dot.
- get all datas which contain a keyword
- write them to a cvs file
To conitune, please tape "q".
"""
print("Implementation of this function is ongoing !")
current_path = os.getcwd()
dir_name = maketempDir(current_path)
if dir_name != None:
csvFile = maketempfile(dir_name,"AnalysisResult","csv")
ReadeMeFile = maketempfile(dir_name,"ReadMe","txt")
if csvFile != None:
if ReadeMeFile == None:
sys.exit("Failed to create a ReadMe files")
else:
sys.exit("Failed to create csv file!")
else:
sys.exit("Failed to create temp directory!")
ReadeMeFileObj = open(ReadeMeFile,"a")
ReadeMeFileObj.write("Search for data which contain the keyword " + keyWord + " in files under the path " + working_dir)
ReadeMeFileObj.write("For more details, please check the file" + csvFile + "\n")
csvFileObj = open(csvFile, 'w', newline='')
csvWriter = csv.writer(csvFileObj)
csvFileObj.close()
ReadeMeFileObj.close()
def searchForAPI(working_dir,keyWord):
"""
inputParm: working_dir : path that will parsed
keyWord : Key word to searched for
Description: this function will search for APIs by:
- Parsing the code in the current path
excluding the folders, subfolders and filename which start with a dot.
- get all APIs which contain a keyword
- write them to a cvs file
To conitune, please tape "q".
"""
current_path = os.getcwd()
dir_name = maketempDir(current_path)
if dir_name != None:
csvFile = maketempfile(dir_name,"AnalysisResult","csv")
ReadeMeFile = maketempfile(dir_name,"ReadMe","txt")
if csvFile != None:
if ReadeMeFile == None:
sys.exit("Failed to create a ReadMe files")
else:
sys.exit("Failed to create csv file!")
else:
sys.exit("Failed to create temp directory!")
ReadeMeFileObj = open(ReadeMeFile,"a")
ReadeMeFileObj.write("Search for APIs which contain the keyword " + keyWord + " in files under the path " + working_dir)
ReadeMeFileObj.close()
def searchForTypeDef(working_dir,keyWord):
"""
inputParm: working_dir : path that will parsed
keyWord : Key word to searched for
Description: this function will search for type definition by:
- Parsing the code in the current path
excluding the folders, subfolders and filename which start with a dot.
- get all types defintion which contain a keyword
- write them to a cvs file
To conitune, please tape "q".
"""
current_path = os.getcwd()
dir_name = maketempDir(current_path)
if dir_name != None:
csvFile = maketempfile(dir_name,"AnalysisResult","csv")
ReadeMeFile = maketempfile(dir_name,"ReadMe","txt")
if csvFile != None:
if ReadeMeFile == None:
sys.exit("Failed to create a ReadMe files")
else:
sys.exit("Failed to create csv file!")
else:
sys.exit("Failed to create temp directory!")
ReadeMeFileObj = open(ReadeMeFile,"a")
ReadeMeFileObj.write("Search for type definition which contain the keyword " + keyWord + " in files under the path " + working_dir)
ReadeMeFileObj.write("For more details, please check the file" + csvFile + "\n")
csvFileObj = open(csvFile, 'w', newline='')
csvWriter = csv.writer(csvFileObj)
csvFileObj.close()
ReadeMeFileObj.close()
def SearchForFile(working_dir,keyWord):
"""
inputParm: working_dir : path that will parsed
keyWord : Key word to searched for
Description: this function will search for Files by:
- Parsing the code in the current path
excluding the folders, subfolders and filename which start with a dot.
- get all files name which contain a keyword
- write them to a cvs file
To conitune, please tape "q".
"""
current_path = os.getcwd()
dir_name = maketempDir(current_path)
if dir_name != None:
csvFile = maketempfile(dir_name,"AnalysisResult","csv")
ReadeMeFile = maketempfile(dir_name,"ReadMe","txt")
if csvFile != None:
if ReadeMeFile == None:
sys.exit("Failed to create a ReadMe files")
else:
sys.exit("Failed to create csv file!")
else:
sys.exit("Failed to create temp directory!")
ReadeMeFileObj = open(ReadeMeFile,"a")
ReadeMeFileObj.write("Search for files which contain the keyword " + keyWord + " under the path " + working_dir + "\n")
csvFileObj = open(csvFile, 'w', newline='')
csvWriter = csv.writer(csvFileObj)
FileFoundNb = 0
#Parsing the code
for folderName, subfolders, filenames in os.walk(working_dir):
if str(os.path.basename(folderName)).startswith('.'):
continue
else:
for filename in filenames:
if str(filename).startswith('.'):
filenames.remove(filename)
else:
if keyWord.lower() in filename.lower():
FileFoundNb +=1
csvWriter.writerow([folderName,filename])
if FileFoundNb !=0:
ReadeMeFileObj.write(str(FileFoundNb) + " File is found, for more details please check the file" + csvFile + "\n")
csvFileObj.close()
ReadeMeFileObj.close()
def SearchForEntryPoint(working_dir):
"""
inputParm: working_dir : path that will parsed
keyWord : Key word to searched for
Description: this function will search for the program entry point by:
- Parsing the code in the current path
excluding the folders, subfolders and filename which start with a dot.
- get all possible entry point in the code (search the keyword ENTRY)
- write them to a cvs file
To conitune, please tape "q".
"""
print("Implementation of this function is ongoing !")
current_path = os.getcwd()
dir_name = maketempDir(current_path)
if dir_name != None:
csvFile = maketempfile(dir_name,"AnalysisResult","csv")
ReadeMeFile = maketempfile(dir_name,"ReadMe","txt")
if csvFile != None:
if ReadeMeFile == None:
sys.exit("Failed to create a ReadMe files")
else:
sys.exit("Failed to create csv file!")
else:
sys.exit("Failed to create temp directory!")
ReadeMeFileObj = open(ReadeMeFile,"a")
ReadeMeFileObj.write("Search for the program entry point in files under the path " + working_dir + "\n")
csvFileObj = open(csvFile, 'w', newline='')
csvWriter = csv.writer(csvFileObj)
EntryPointFoundNb = 0
#Parsing the code
for folderName, subfolders, filenames in os.walk(working_dir):
if str(os.path.basename(folderName)).startswith('.'):
continue
else:
for filename in filenames:
if str(filename).startswith('.'):
filenames.remove(filename)
else:
file_path = os.path.join(folderName, filename)
with open(file_path, encoding="utf8", errors='ignore') as read_obj:
for line in read_obj:
if line.startswith('ENTRY('):
EntryPointFoundNb +=1
csvWriter.writerow([folderName,filename])
else:
continue
if EntryPointFoundNb !=0:
ReadeMeFileObj.write(str(EntryPointFoundNb) + " entry point is found, for more details please check the file" + csvFile + "\n")
csvFileObj.close()
ReadeMeFileObj.close()
def GetAnalysisType(working_dir):
"""
Description: this function will get the type of analysis to be done.
To conitune, please tape "q".
"""
ListOfAnalysis = [1,2,3,4,5]
while True:
AnalysisType = int(input("""Please choose the analyse to be done
in the following list:
#1.Search for a data
#2.Search for an API
#3.Search for a type definition
#4.Search for fileName with keyword in the title
#5.Search for the program entry point
Analysis Type: """))
if AnalysisType in ListOfAnalysis:
if AnalysisType == 1:
print("You choosen to search for a data")
keyWord = str(input("Please Enter the key word to search:"))
searchForData(working_dir,keyWord)
break
elif AnalysisType ==2:
print("Your choosen to search for an API")
keyWord = str(input("Please Enter the key word to search:"))
searchForAPI(working_dir,keyWord)
break
elif AnalysisType ==3:
print("Your choosen to search for a type definition")
keyWord = str(input("Please Enter the key word to search:"))
searchForTypeDef(working_dir,keyWord)
break
elif AnalysisType ==4:
print("Your choosen to search for a file with a keyword in the filename")
keyWord = str(input("Please Enter the key word to search:"))
SearchForFile(working_dir,keyWord)
break
else:
print("Your choosen to search for the progrm entry point ")
SearchForEntryPoint(working_dir)
break
else:
print("Wrong Choise!")
if ask_user() == False:
break
else:
continue
def GetActionToDo():
"""
Description: this function will get the action to be done.
To conitune, please tape "q".
"""
ListOfAction = [1,2]
while True:
ActionType = int(input("""Please choose what you want to do
from the following list:
#1.Analyse the code structue
#2.Analyse the code content
Analysis Type: """))
if ActionType in ListOfAction:
if ActionType == 1:
print("You need to analyse the code struture")
workingDir = getWorkingDir()
getStaticArch(workingDir)
break
else:
print("Your choosen to analyse the code")
workingDir = getWorkingDir()
GetAnalysisType(workingDir)
break
else:
print("Wrong Choise!")
if ask_user() == False:
break
else:
continue
def getWorkingDir():
"""
Description: this function will get the working directory.
Return: the working dircctory
To conitune, please tape "q".
"""
workingDir = str(input("Please Enter the working directory:"))
return workingDir
def main():
GetActionToDo()
if __name__ == "__main__":
main()