-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
187 lines (161 loc) · 5.73 KB
/
main.py
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
# importing python modules
import csv
import glob
import os
import shutil
from zipfile import ZipFile
import re
# path of the parent directory
current_dir = os.getcwd()
bucket_dir = os.path.join(current_dir, "Bucket")
if os.path.exists(bucket_dir):
os.chdir(bucket_dir)
else:
os.chdir(current_dir)
os.mkdir("Bucket")
os.chdir(bucket_dir)
# function to extract the EAR and WAR files
def extract():
zip_ext="*.zip"
war_ext="*.war"
ear_ext="*.ear"
if glob.glob(zip_ext):
zip_files=glob.glob(zip_ext)
print("The list of ear files detected:", zip_files)
for file in zip_files:
zip_folder = file + "_zip"
os.mkdir(zip_folder)
with ZipFile(file, 'r') as zipObj:
# Extract all the contents of zip file in current directory
zipObj.extractall(zip_folder)
print(file, "has been scanned ")
for files in glob.glob(os.path.join(zip_folder, ear_ext)):
shutil.copy(files, bucket_dir)
for files in glob.glob(os.path.join(zip_folder, war_ext)):
shutil.copy(files, bucket_dir)
if glob.glob(ear_ext):
ear_files=glob.glob(ear_ext)
print("\n\nThe list of ear files detected:", ear_files)
for file in ear_files:
ear_folder = file + "_ear"
os.mkdir(ear_folder)
with ZipFile(file, 'r') as zipObj:
# Extract all the contents of EAR file in current directory
zipObj.extractall(ear_folder)
print(file, "has been scanned ")
for files in glob.glob(os.path.join(ear_folder, war_ext)):
shutil.copy(files, bucket_dir)
if glob.glob(war_ext):
war_files=glob.glob(war_ext)
print("\n\nThe list of war files detected:", war_files)
for file in war_files:
name= file.split(".war")
war_folder = name[0]
os.mkdir(war_folder)
with ZipFile(file, 'r') as zipObj:
# Extract all the contents of WAR file in current directory
zipObj.extractall(war_folder)
print(file, "has been scanned ")
else:
print("There is no file to be extracted")
extract()
# Generating the list of all unique the jars
all_jars=[]
bucket_jars = os.path.join(bucket_dir + "\**\*.jar")
all_jars_files = glob.glob(bucket_jars,
recursive=True)
for file in all_jars_files:
all_jars.append(os.path.basename(file))
all_jars = list(set(all_jars))
# Importing the product created jar list from CSV to suppression list
requisite_path = os.path.join(current_dir, "Requisite")
if os.path.isfile(os.path.join(requisite_path, "suppression_jars.csv")):
os.chdir(requisite_path)
suppression = open('suppression_jars.csv', 'r')
# creating dictreader object
file = csv.DictReader(suppression)
# creating empty lists
suppression_jar = []
# iterating over each row and append
# values to empty list
for col in file:
suppression_jar.append(col['Jars'])
suppression_jar = list(set(suppression_jar))
# Reading the list of jars from the License CSV file
if os.path.isfile(os.path.join(requisite_path, "License.csv")):
os.chdir(requisite_path)
license = open('License.csv', 'r')
# creating dictreader object
file = csv.DictReader(license)
# creating empty lists
license_jar = []
# iterating over each row and append
# values to empty list
for col in file:
license_jar.append(col['jars'])
third_party = []
for element in all_jars:
if element not in suppression_jar:
third_party.append(element)
deprecated_jars = []
newly_added_jars = []
active_license = []
for element in license_jar:
if element not in third_party:
deprecated_jars.append(element)
elif element in third_party:
active_license.append(element)
for element in third_party:
if element not in license_jar:
newly_added_jars.append(element)
newly_added_jars= sorted(set(newly_added_jars))
########################################################
version = []
deprected_withoutext = []
jars_oldversion = []
jars_newversion = []
for element in deprecated_jars:
result = re.split(r"-\d.+", element)
deprected_withoutext.append(result[0])
final_new_jars= []
versionchanged_jars = []
for element in newly_added_jars:
name = re.split(r"-\d.+", element)
if name[0] not in deprected_withoutext:
final_new_jars.append(element)
else:
jars_newversion.append(element)
versionchanged_jars.append(name[0])
#######################################################
my_dict = {}
unutilized_jars = []
test = []
for element in deprecated_jars:
name = re.split(r"-\d.+", element)
nameOfJar = name[0];
ver = []
if nameOfJar in versionchanged_jars:
test.append(element)
if nameOfJar in my_dict.keys():
a = element.split(nameOfJar + "-")
b = a[1].split(".jar")
new_version = "approved version : " + b[0]
my_dict[nameOfJar].append(new_version)
else:
a = element.split(nameOfJar+"-")
b = a[1].split(".jar")
old_version = "approved version : "+b[0]
ver.append(old_version)
my_dict.setdefault(nameOfJar,ver)
else:
unutilized_jars.append(element)
for element in jars_newversion:
name = re.split(r"-\d.+", element)
nameOfJar = name[0]
if nameOfJar in my_dict.keys():
a = element.split(nameOfJar + "-")
b = a[1].split(".jar")
new_version = "new version : " + b[0]
my_dict[nameOfJar].append(new_version)
else:
print("check the flow if it enters this else condition")