-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_compare.py
396 lines (337 loc) · 12.3 KB
/
project_compare.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
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
"""
A tool to compare two project in source and compare path.
It also calculate the size of files in two paths, and find
files with biggest different in size.
"""
import csv
import filecmp
import optparse
import os
import project_config
import re
import stat
import sys
def _create_option_parser():
"""
create option parser
"""
usage = 'usage: %prog [options]'
version = '%prog 1.0'
parser = optparse.OptionParser(usage=usage, version=version)
parser.add_option('-c', '--config',
dest='config_file', metavar='FILE',
default='project_compare_config.xml', help='config xml file')
parser.add_option('--source',
dest='source', help='source project path')
parser.add_option('--compare',
dest='compare', help='compare project path')
parser.add_option('--number',
dest='number', type='int', default=10, help='number of results')
parser.add_option('-r', '--result',
dest='result_file', metavar='FILE',
default='project_compare_result.csv', help='result csv format file')
return parser
def _check_options_and_args(options, args):
"""
check options and args
"""
if options.source is None:
print 'source path is empty!'
sys.exit()
if options.compare is None:
print 'compare path is empty!'
sys.exit()
def analyze_dir_diff(src_path, com_path, project_dict):
"""
analyze file path size change information
test how big the change is from src_path to com_path
"""
dcmp = filecmp.dircmp(src_path, com_path)
src_only_array = []
com_only_array = []
common_array = []
append_size_to_array(
dcmp.left_only, src_path, src_only_array, project_dict)
append_size_to_array(
dcmp.right_only, com_path, com_only_array, project_dict)
for name in dcmp.common_files:
tsrc_path = src_path + os.sep + name
tcom_path = com_path + os.sep + name
if check_path_with_filter(tsrc_path, project_dict):
continue
if check_path_with_filter(tcom_path, project_dict):
continue
src_size = os.stat(tsrc_path)[stat.ST_SIZE]
com_size = os.stat(tcom_path)[stat.ST_SIZE]
diff_size = src_size - com_size
obj = {
'src_path': tsrc_path,
'com_path': tcom_path,
'src_size': src_size,
'com_size': com_size,
'diff_size': diff_size
}
common_array.append(obj)
for name in dcmp.common_dirs:
if check_path_with_filter(src_path + os.sep + name, project_dict):
continue
if check_path_with_filter(com_path + os.sep + name, project_dict):
continue
sub_src_only_array, sub_com_only_array, sub_common_array = analyze_dir_diff(
src_path + os.sep + name, com_path + os.sep + name, project_dict)
append_list_from_list(sub_src_only_array, src_only_array)
append_list_from_list(sub_com_only_array, com_only_array)
append_list_from_list(sub_common_array, common_array)
return src_only_array, com_only_array, common_array
def analyze_dir_diff_with_only(src_path, com_path, project_dict):
"""
first do the same thing as analyze_dir_diff
and then put src_only_array and com_only_array to common_array
"""
src_only_array, com_only_array, common_array = analyze_dir_diff(
src_path, com_path, project_dict)
append_src_only_to_common_array(src_only_array, common_array)
append_com_only_to_common_array(com_only_array, common_array)
return src_only_array, com_only_array, common_array
def append_src_only_to_common_array(only_array, common_array):
"""
append content of src only array to common array
"""
for only in only_array:
obj = {
'src_path': only['path'],
'src_size': only['size'],
'com_path': '',
'com_size': 0,
'diff_size': only['size']
}
common_array.append(obj)
def append_com_only_to_common_array(only_array, common_array):
"""
append content of com only array to common array
"""
for only in only_array:
obj = {
'com_path': only['path'],
'com_size': only['size'],
'src_path': '',
'src_size': 0,
'diff_size': 0 - only['size']
}
common_array.append(obj)
def append_list_from_list(src_list, des_list):
"""
append contents in one list to another
"""
for obj in src_list:
des_list.append(obj)
def append_size_to_array(plist, root, array, project_dict):
"""
append file size to array for storage
"""
for name in plist:
path = root + os.sep + name
if os.path.isdir(path):
for r, ds, fs in os.walk(path):
for name in fs:
append_file_size_to_array(
r + os.sep + name, array, project_dict)
else:
append_file_size_to_array(path, array, project_dict)
def append_file_size_to_array(path, array, project_dict):
"""
append file to array
"""
if check_path_with_filter(path, project_dict) == False:
obj = {
'path': path,
'size': os.stat(path)[stat.ST_SIZE]
}
array.append(obj)
def check_path_with_filter(path, project_dict):
"""
check path with filter regex
[param] project_dict: a project dict get from config xml
file contains filter regex used
for path string check
[return] True if match anly one of filter regex,
False if not
"""
result = False
if 'task_array' in project_dict:
task_array = project_dict['task_array']
for task_dict in task_array:
if 'id' in task_dict and 'list_array' in task_dict:
if task_dict['id'] == 'file':
list_array = task_dict['list_array']
for list_dict in list_array:
if _check_path_with_filter_dict(path, list_dict):
result = True
break
return result
def _check_path_with_filter_dict(path, list_dict):
"""
check path with filter regex
[param] list_dict: a list dict get from config xml
file contains filter regex used
for path string check
[return] True if match anly one of filter regex,
False if not
"""
result = False
if 'id' in list_dict and 'string_array' in list_dict:
if list_dict['id'] == 'filter_regex':
string_array = list_dict['string_array']
for pattern in string_array:
if re.match(pattern, path) is not None:
result = True
break
return result
def check_path_with_count(path, project_dict):
"""
check path with count regex
[param] project_dict: a project dict get from config xml
file contains count regex used
for path string check
[return] result, strings
result: True if match anly one of count regex,
False if not
strings: pattern strings that matches path
"""
result = False
strings = []
if 'task_array' in project_dict:
task_array = project_dict['task_array']
for task_dict in task_array:
if 'id' in task_dict and 'list_array' in task_dict:
if task_dict['id'] == 'file':
list_array = task_dict['list_array']
for list_dict in list_array:
res, strs = _check_path_with_count_dict(path, list_dict)
if res:
result = True
for astr in strs:
strings.append(astr)
break
return result, strings
def _check_path_with_count_dict(path, list_dict):
"""
check path with count regex
[param] list_dict: a list dict get from config xml
file contains count regex used
for path string check
[return] result, strings
result: True if match anly one of count regex,
False if not
strings: pattern strings that matches path
"""
result = False
strings = []
if 'id' in list_dict and 'string_array' in list_dict:
if list_dict['id'] == 'count_regex':
string_array = list_dict['string_array']
for pattern in string_array:
if re.match(pattern, path) is not None:
result = True
strings.append(pattern)
break
return result, strings
def calc_total_size(src_array, com_array, common_array):
"""
to calculate dir total size
"""
src_size = 0
com_size = 0
for obj in src_array:
src_size += obj['size']
for obj in com_array:
com_size += obj['size']
for obj in common_array:
src_size += obj['src_size']
com_size += obj['com_size']
return src_size, com_size, src_size - com_size
def append_count_strings_to_dict(size, strings, result_dict):
"""
append results of count regex analysis to dict result
"""
for string in strings:
if string in result_dict:
pass
else:
result_dict[string] = 0
result_dict[string] += size
def calc_regex_files_size(src_array, com_array, common_array, project_dict):
"""
to calculate files' total size with regular expression
"""
src_dict = {}
com_dict = {}
for src_obj in src_array:
res, strings = check_path_with_count(src_obj['path'], project_dict)
if res:
append_count_strings_to_dict(src_obj['size'], strings, src_dict)
for com_obj in com_array:
res, strings = check_path_with_count(com_obj['path'], project_dict)
if res:
append_count_strings_to_dict(com_obj['size'], strings, com_dict)
for common_obj in common_array:
res, strings = check_path_with_count(
common_obj['src_path'], project_dict)
if res:
append_count_strings_to_dict(
common_obj['src_size'], strings, src_dict)
res, strings = check_path_with_count(
common_obj['com_path'], project_dict)
if res:
append_count_strings_to_dict(
common_obj['com_size'], strings, com_dict)
return src_dict, com_dict
def get_pos_increase_change_files(array, number):
"""
get positive increase changed files from common array
"""
sorted_array = sorted(array, key=lambda x:x['diff_size'], reverse=True)
return get_increase_change_files(sorted_array, number)
def get_neg_increase_change_files(array, number):
"""
get negative increase changed files from common array
"""
sorted_array = sorted(array, key=lambda x:x['diff_size'])
return get_increase_change_files(sorted_array, number)
def get_increase_change_files(sorted_array, number):
"""
get changed files with sorted array and number
"""
array = []
i = 0
for fobj in sorted_array:
if i < number:
array.append(fobj)
else:
break
i = i + 1
return array
# main function
if __name__ == '__main__':
parser = _create_option_parser()
(options, args) = parser.parse_args()
_check_options_and_args(options, args)
project_dict = project_config.parse_config_file(options.config_file)
src_array, com_array, common_array = analyze_dir_diff_with_only(
options.source, options.compare, project_dict)
positive_array = get_pos_increase_change_files(common_array, options.number)
negative_array = get_neg_increase_change_files(common_array, options.number)
total_size = calc_total_size(src_array, com_array, common_array)
print 'total:'
print total_size
src_dict, com_dict = calc_regex_files_size(src_array, com_array, common_array, project_dict)
print 'source:'
for string in src_dict:
print string, src_dict[string]
print 'compare:'
for string in com_dict:
print string, com_dict[string]
tag_array = ['src_path', 'com_path', 'src_size', 'com_size', 'diff_size']
for n in negative_array:
positive_array.append(n)
csv.write_array_dict_to_file(options.result_file, positive_array, tag_array)