-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport
380 lines (294 loc) · 12.1 KB
/
report
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
#! python
# Author: Fab-T
# Rev: 5/17/18 - v1.0
# Rev: 8/17/18 - v1.1 (configfilename as argument + regexp update to consider new use cases)
# Rev: 9/12/18 - v1.2 (enhanced regexp to consider new use cases)
import sys
import re
import datetime
from collections import Counter
""" compare properties value exercise
Context : In our JEE applications, some environment properties are contained in property files (most likely with Dev
environment values) While same properties value for Production environment are stored in Database.
I intend to extract those properties and format the content to easily compare the values...
identify matches, redundancy, differences and discrepancies.
Define a function that extract properties from file Properties-ComponentName.rpt in a new format
File : [componentName]-prod.properties
[componentName];propertyName;propertyValue
Define a function that extract properties from Dev file in a new format
File : [componentName]-dev.properties
[componentName];propertyName;propertyValue
Define a function that compare 2 files and propose differences between files
Here's what properties extracted from Database looks like in the Properties-ComponentName.rpt file:
...
ArchiveLogs;ftp.remotedir;/Access_Error_Logs/${env.COMPUTERNAME}
...
CRM&crm_log_level&WARN
CRM&wls_node_server_ip&@frontend.host@
...
Here's what properties from Dev looks like:
######################################################################################
# logging settings
log_trace_file_directory=trace
log_record_file_directory=trace/record
log_proxy_file_enabled=true
...
Suggested milestones for incremental development:
-Extract the list of Dev properties in tuple named Dev
-Extract the list of Prod properties in tuple named Prd
-Generate a result of comparing each dev property value with Prd value
-Generate a file containing the exact same list of Dev properties with proposed changed values
-Fix main() to use multiple arguments
"""
def fileread(file_read):
#check is filename is a string then open file to read/put lines in a single string, then close filename.
if isinstance(file_read,str):
with open(file_read,'r') as f:
file_string=f.read()
f.close()
else:
print ("filename: "+str(fileread)+" can\'t be read")
sys.exit(1)
return file_string
def getfilename(filename):
#check if subfodler provided in filename
try:
foundsubfolder=filename.rfind("/")
extracted_filename=filename[foundsubfolder+1:]
except ValueError:
extracted_filename=filename
return extracted_filename
def getshortfilename(filename):
#Extract filename from folder (if exists)
f=getfilename(filename)
file,fileExtension = str(f).split(".",1)
return file
def checkduplicates(filename):
#readfilename into list and warn if duplicate entries
text=fileread(filename)
texttoline=text.splitlines()
duplicates = []
counter = Counter(texttoline)
for line in texttoline:
try:
if not line.lstrip().startswith('#'):
name,value=line.split("=",1)
#check if exact same line exists
occurrence=counter[line]
if occurrence > 1:
if not line in duplicates: duplicates.append(line)
#check if property name is defined twice with different values
occurrenceName=counter[name]
if occurrenceName > 1:
#if not line in duplicates: duplicates.append(line)
duplicates.append(line)
except ValueError:
#that's ok, not a property, need to do something anyway: true statement
line in texttoline
if duplicates:
sys.exit("####\n Filename \'"+filename+"\' contains duplicate entries : \n"+str(duplicates)+ \
"\n\n !!! Please remove the duplicates !!!")
def update_file(filename,input):
"""
Create a new file with new values from an input list
:param filename: name of the file to update
:param input: list of new elements to insert
This function replace each pair property=value with new values if needed.
We keep the exact same format of the file to ease the compare with BeyondCompare
"""
#make sure to extract the filename and not folders
justthefile=getfilename(filename)
newfilename="newproperties/new_"+justthefile
f=open(newfilename,'w')
text=fileread(filename)
texttoline=text.splitlines()
#start with first item in input : index=0
index=0
for line in texttoline:
#we parse each line of filename for a property to set
try:
if not line.lstrip().startswith('#'):
name,value=line.split("=",1)
# if property = value found : we need to set the new value
newline=input[index]
#we increment to next item in input
index+=1
else:
newline=line
except Exception:
#there was a problem keep same line
newline=line
continue
finally:
f.write(newline+'\n')
f.close()
def extract_from_properties(filename):
"""
create a list of propertyname:propertyvalues from properties files
in order to match them with the one extracted from the Properties DB
"""
#Having duplicates within properties files is not correct
#This function checks for you and exit if duplicates found !
checkduplicates(filename)
text=fileread(filename)
#Get the filename without extension
shortfilename=getshortfilename(filename)
#defining the regexp
#Need to define text as multiline to use ^ and ignore commented prop in files
myRegexp=re.compile('^[-_. \w ]*=[ <!-?$@_.> \\:\/{}*\w]*', re.MULTILINE)
#properties line is a tuple:
propertylines = re.findall(myRegexp, text)
#debug
#print("Debug propertyLines"+str(propertylines))
#create a list for Dev properties
Listresult = []
for propertyline in propertylines:
propertyname,propertyvalue = propertyline.split("=",1)
Listresult.append([shortfilename,propertyname,propertyvalue])
#create a tuple (immutable) from list
result=tuple(Listresult)
return result
def extract_from_DB(DBExtractfile):
"""
:param DBExtractfile: name of the file containing properties values from DB
:param name: property to find
:return: list of property with values from Prod (DBExtractFile)
"""
text=fileread(DBExtractfile)
#properties line is a tuple:
propertylines = re.findall(r'[-_.\w]*&[-_.\w]*&[<!-?$@\/_.=&> :\/{}*\\\w]*', text)
#debug
#print("Debug DBPropertyLines"+str(propertylines))
#create a list of DB properties
Listresult = []
for propertyline in propertylines:
shortfilename,propertyname,propertyvalue = propertyline.split("&",2)
Listresult.append([shortfilename,propertyname,propertyvalue])
#create a tuple (immutable) from list
result=tuple(Listresult)
#Debug
#print(Listresult)
return result
def generate_result(DevTuple,PrdTuple,filename):
#now = datetime.datetime.now()
resulttosumlist = []
#resulttofilelist = ['### created on ='+now.strftime("%Y-%m-%d %H:%M")+'###']
resulttofilelist = []
#Inner functions
def buildresulttofile(thisindex,thistuple):
thisitem = thistuple[thisindex][1]+"="+thistuple[thisindex][2]
#Check if item already exists in List before inserting
if not thisitem in resulttofilelist:
#Item (prop+value) does not exist: Use case 2 & 3
#Check if lastitem is same prop but with different value
#Prevent issue with pop on empty list
if resulttofilelist:
lastitem=resulttofilelist.pop()
#debug
#print("Debug here is the lastitem :" +lastitem)
lastitemname,lastitemvalue=lastitem.split("=",1)
#check prop is the same
if lastitemname == thistuple[thisindex][1]:
#lastitem property is identical : replace with new value
resulttofilelist.append(thisitem)
else:
#lastitem property is different : insert a new item
#Because of pop() - recreating last element -
resulttofilelist.append(lastitem)
#insert new element
resulttofilelist.append(thisitem)
else:
#insert new element on empty list
resulttofilelist.append(thisitem)
else:
#Item is already present: Use case 1
print("item "+thisitem+" is already present in list")
return resulttofilelist
def buildresulttosum(thisindex,thistuple,thisflag):
if thisflag:
resulttosumlist.append(thisflag+" : "+thistuple[thisindex][1]+"="+thistuple[thisindex][2])
else:
resulttosumlist.append(thistuple[thisindex][0]+" : "+thistuple[thisindex][1]+"=" \
+thistuple[thisindex][2])
return resulttosumlist
def compare_item(DevItem,PrdItem):
if DevTuple[DevItem][2] == PrdTuple[PrdItem][2]:
#Use case 2 Prd value is different from Dev
isdevitem = True
else:
#Use case 3 Prd value is different from Dev
isdevitem = False
return isdevitem
#Get all PropertyName from DevTuple (Dev properties)
index=0
while index < len(DevTuple):
tofind=DevTuple[index][1].replace(" ","")
#debug
#if tofind == "product_url":
# print ("debug string to find:"+tofind)
newindex=0
propnotfound = True
while newindex < len(PrdTuple):
if tofind == PrdTuple[newindex][1]:
propnotfound = False
resulttofilelist=buildresulttofile(newindex,PrdTuple)
#debug
#if PrdTuple[newindex][1] == "product_url":
# print ("debug PrdTuple searched:"+PrdTuple[newindex][2])
newindex+=1
#Use case 4 : Dev property not found in Prod properties
if propnotfound:
#Set value to Dev
resulttofilelist=buildresulttofile(index,DevTuple)
#Warn within Summary
resulttosumlist=buildresulttosum(index,DevTuple,"WARNING")
index+=1
print("this is resulttofilelist:")
print (resulttofilelist)
print("this is resulttosumlist:")
print(resulttosumlist)
#Generate new property file
update_file(filename,resulttofilelist)
def main():
# This command-line parsing code is provided.
# Make a list of command line arguments, omitting the [0] element
# which is the script itself.
args = sys.argv[1:]
if not args:
print('usage: [--job filename] [--summary] file [file ...]')
sys.exit(1)
# Notice the job flag and remove it from args if it is present.
configfilename = ''
if args[0] == '--job':
configfilename = args[1]
del args[:2]
# Notice the summary flag and remove it from args if it is present.
summary = False
if args[0] == '--summary':
summary = True
del args[0]
index=0
#debug
print('configfilename '+configfilename)
print('propertyfilename '+args[0] )
while index < len(args):
if summary:
filename="summary_"+args[index]
f=open(filename,'w')
myList=extract_from_properties(args[index])
for item in myList:
if item:
f.write(str(item)+'\n')
f.close()
else:
mydevPropTuple=extract_from_properties(args[index])
#print(mydevPropTuple)
myprodPropTuple=extract_from_DB(configfilename)
#print(myprodPropTuple)
generate_result(mydevPropTuple,myprodPropTuple,args[index])
#print(myResult)
index+=1
# For each filename, get the names, then either print the text output
# or write it to a summary file
if __name__ == '__main__':
main()