-
Notifications
You must be signed in to change notification settings - Fork 5
/
refactor_model.py
449 lines (358 loc) · 18.6 KB
/
refactor_model.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
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
import re
from conventional_commits import build_cc_refactor_regex
from language_utils import file_scheme, term_seperator, build_separated_terms, negation_terms, modals\
, regex_to_big_query, generate_bq_function, match, SCHEMA_NAME, documentation_entities, prefective_entities\
, software_terms, build_non_positive_linguistic, software_goals_modification, software_goals, static_analyzers
# TODO - add Technical Debt, fixme
"""
Directions to consider
Spaghetti code - indication of mess, not always a refactor
Useless (as dead code)
Revert "Refactor start"
"""
# https://arxiv.org/pdf/2002.11049.pdf
refactor_entities = software_terms + ['(helper|utility|auxiliary) function(?:s)?']
# Well, we need them...
unnedded_terms = ['unnecessary', 'unneeded', 'unused', '(?:not|never|no longer) used'
#, 'old'
, 'no longer needed', 'redundant', 'useless', 'duplicate(?:d)?', 'deprecated', 'obsolete(?:d)?', 'commented']
core_refactor_terms = [
'clean(?:ing)?(?:-| )?up(?:s)?',
'clean(?:ing|s|ed)?',
'combin(?:e|es|ed|ing)',
'compos(?:e|es|ed|ing)',
'de(?:-| )?compos(?:e|es|ed|ing)',
'deprecat(?:e|es|ed|ing)',
'encapsulat(?:e|es|ed|ing)',
'polish(?:ed|es|ing)?',
're(?:-| )?factor(?:ed|s|ing|ings)?', # TODO - should be here - check why slightly decreases performance
're(?:-|)?organiz(?:e|es|ed|ing)',
're(?:-|)?structur(?:e|es|ed|ing)',
'rebuil(?:d|ds|ding|t)',
'tid(?:y|ying|ied)'
]
modification_activity = [
'(?:get|got|getting) rid',
'(?:make|makes|made|making)',
'convert(?:ed|s|ing)?',
'dead',
'drop(?:ed|s|ing)?',
'duplicat(?:e|es|ed|ing)',
'extract(?:ed|s|ing)?',
'hide(?:e|es|ed|ing)',
'improv(?:e|es|ed|ing)', # Goals modification only?
'increas(?:e|es|ed|ing)',
'mov(?:e|es|ed|ing)',
'parameteriz(?:e|es|ed|ing)',
'redundant',
'replac(?:e|es|ed|ing)',
'separat(?:e|e s|ed|ing)',
'short(:?en|er|ing|s)?',
'split(?:s|ing)?',
'subsitut(?:e|es|ed|ing)',
'substitut(?:e|es|ed|ing)',
'un(?:-| )?hid(?:e|es|ed|ing)'
#'chang(?:e|esed|ing)'
#, 'creat(?:e|es|ed|ing)'
#, 'delet(?:e|es|ed|ing)'
#, 'instead'
#, 'kill(?:ed|s|ing)?'
# , 'provid(?:e|es|ed|ing)'
#, 'introduc(?:e|es|ed|ing)'
] + core_refactor_terms + unnedded_terms
feedbak_terms = [ 'py(?:-| )?lint', 'lint', 'review comments(?:s)?', 'code review', 'cr', 'pep8'
]
feedback_action = ['fix(?:ed|s|es|ing)?', 'fix(?:-| )?up(?:s)?', 'resolv(?:e|ed|es|ing)', 'correct(?:ed|s|es|ing)?']
perfective_header_action = [
#'polish(?:ed|es|ing)?'
#, 'clean(?:ing|s|ed)?(?:-| )?up(?:s)?'
'clean(?:ing|s|ed)?(?:-| )?up(?:s)?'
, 'cleaner'
, 'deprecat(?:e|es|ed|ing)'
, 'extract(?:ed|s|ing)?',
're(?:-|)?organiz(?:e|es|ed|ing)', 're(?:-|)?structur(?:e|es|ed|ing)', 'tid(?:y|ying|ied) up'
, 'improv(?:e|ed|es|ing|ement|ements)' , 're(?:-|)?organiz(?:e|es|ed|ing)', 're(?:-|)?structur(?:e|es|ed|ing)'
, '(helper|utility|auxiliary) function(?:s)?'
, '(?:move|moved|moves|moving) to'
, 'separat(?:e|es|ed|ing)'
, 'split(?:s|ing)?', '->'
, build_separated_terms(static_analyzers) + 'fix(es|ed)?'
, 'fix(es|ed)?' + build_separated_terms(static_analyzers)
#, '(private|public|protected|static)'
]
# TODO - rewrited, move into/out???, deduplicate, remove legacy, redo, PR, feedback
# TODO - clean , style, prettier, "->", refine, "Removed commented code", "More startup improvements.", recode
# ""Remove another old function", "improved redis error message", utility functions, never used
# Checkstyle
# TODO - perfective, not refactor - ident, spacing, tabs, "tabs -> spaces", cosmetic, ""*** empty log message ***"
# examples ""DOC: remove mention of TimeSeries in docs"
# TODO - add "resolving review comments"
# TODO - lint, pylint
refactor_context = [ 'clean(ing)?(-| )?up(s)?'
,'call(?:s|ed|ing)?[\s\S]{1,50}instead'
, 'collaps(?:e|es|ed|ing)', 'consolidat(e|es|ed|ing)'
, 'decompos(?:e|es|ed|ing)'
, 'drop(?:ed|s|ing)?( back)', 'encapsulat(e|es|ed|ing)'
, 'gereneliz(?:e|es|ed|ing)'
# , 'inline'
# , 'no longer needed', 'not used', 'obsolete(d)?'
, 'optimiz(?:e|es|ed|ing|ation|ations)'
, 'pull(?:ed|s|ing)? (up|down)', 're(?:-)?(?:write|wrote)', 're(?:-| )?factor(?:ed|s|ing|ings)?'
, 're(-)?implement(ed|s|ing)?'
, 'renam(?:e|es|ed|ing|ings)', 'better nam(?:e|es|ing)','re(?:-)?organiz(e|es|ed|ing)', 're(?:-)?organization'
, 're(?:-)?work(ed|s|ing|ings)?'
, 'reorg' , 'simplif(y|es|ied|ying|ication)', 'suppress(es|ed|ing)? warning(?:s)?'
, 'unif(?:y|ies|ied|ing)', 'uninline'
, 'beef(?:ed|s|ing)? up', 'refactor(?:ing)?(?:s)?', 'code improvement(?:s)?'
#, '(?:^|^[\s\S]{0,25}%s)(?:%s)%s[\s\S]{0,25}$' % (term_seperator, "|".join(perfective_header_action), term_seperator)
, 'revis(?:e|es|ed|ing)'
, 're(?:-)?construct(?:s|ed|ing)?'
, 're(?:-)?(?:write|write|wrote|writing)'
, 're(?:-)?cod(?:e|ed|es|ing)'
, 'factor(?:ed|s|ing)? out'
, 're(?:-| )?packag(?:e|es|ed|ing)'
#, 'code review'
#, 'collapse'
#, "(?:(?:%s)(?:%s|%s[\s\S]{0,50}%s)(?:%s)%s)" % (build_separated_terms(feedback_action
# , just_before=True)
# , term_seperator
# , term_seperator
# , term_seperator
# , "|".join(feedbak_terms)
# , term_seperator)
# ,'us(e|es|ed|ing)[\s\S]{1,50}(instead)'
# , '(instead)[\s\S]{1,50}us(e|es|ed|ing)'
]
# https://refactoring.guru/refactoring/techniques
# TODO - change [\s\S] with . ?
removal = [ 'add(?:s|ed|ing)?[\s\S]{1,50}helper(?:s)?'
, 'us(?:e|es|ed|ing)[\s\S]{1,50}instead'
#, 'us(?:e|es|ed|ing)[\s\S]{1,25}\(\)[\s\S]{1,25}instead'
, 'split(?:s|ing)?[\s\S]{1,50}into'
, 'break(?:s|ing)?[\s\S]{1,50}into'
, 'separat(?:e|e s|ed|ing)[\s\S]{1,50}into'
#, 'replac(?:e|es|ed|ing)?[\s\S]{1,50}with'
, 'replac(?:e|es|ed|ing)?[\s\S]{1,50}(?:%s)' % "|".join(unnedded_terms)
, 'remov(?:e|es|ed|ing)[\s\S]{1,50}(?:%s)' % "|".join(unnedded_terms)
#, '(?:this|that|is)[\s\S]{1,50}(?:%s)' % "|".join(unnedded_terms)
, 'kill(?:s|ed|ing)?[\s\S]{1,50}(?:%s)' % "|".join(unnedded_terms)
, 'drop(?:s|ed|ing)?[\s\S]{1,50}(?:%s)' % "|".join(unnedded_terms)
, 'mov(?:e|es|ed|ing)?[\s\S]{1,50}(?:%s)' % "|".join(unnedded_terms)
]
adaptive_context = [
'(?:un)?hid(?:e|es|den)', 'add(?:s|ed|ing)?', 'allow(?:s|ed|ing)?'
, 'buil(?:t|ds|ing)', 'calibirat(?:e|es|ed|ing)'
, 'configure'
, 'creat(?:e|es|ing)' # O created
, 'deferr(?:ed|s|ing)?'
, 'disabl(?:e|es|ed|ing)'
, 'enhanc(?:e|es|ed|ing)', 'extend(?:s|ed|ing)?', 'form(?:ed|s|ing)?'
, 'implement(?:ed|s|ing)?', 'import(?:s|ed|ing)?', 'introduc(?:e|es|ed|ing)'
, 'port(?:s|ed|ing)?'
, 'provid(?:e|es|ed|ing)'
, 'report(?:s|ed|ing)?'
, 'support(s|ed|ing)?'
, 'updat(?:e|es|ed|ing)'
, 'upgrad(?:e|es|ed|ing)'
# , 'mov(e|es|ed|ing)'
# , 'print(s|ed|ing)?'
]
def build_core_refactor_regex():
return '(%s)' % build_separated_terms(core_refactor_terms)
def is_core_refactor(text):
return match(text, build_core_refactor_regex())
def build_refactor_regex(use_conventional_commits=True):
header_regex = '(?:^|^[\s\S]{0,25}%s)(?:%s)%s' % (term_seperator
, "|".join(perfective_header_action)
, term_seperator)
activity_regerx = "(?:(?:%s)(?:%s|%s[\s\S]{0,50}%s)(?:%s)%s)" % (build_separated_terms(modification_activity
, just_before=True)
, term_seperator
, term_seperator
, term_seperator
, "|".join(refactor_entities)
, term_seperator)
if use_conventional_commits:
agg_re = "(%s)|(%s)|(%s)|(%s)" % (build_separated_terms(refactor_context)
, activity_regerx
, header_regex
, build_cc_refactor_regex())
else:
agg_re = "(%s)|(%s)|(%s)" % (build_separated_terms(refactor_context)
, activity_regerx
, header_regex)
return agg_re
def build_refactor_goals_regex():
goals_regerx = "(?:(?:%s)(?:%s|%s[\s\S]{0,50}%s)(?:%s)%s)" % (build_separated_terms(software_goals_modification
, just_before=True)
, term_seperator
, term_seperator
, term_seperator
, "|".join(software_goals)
, term_seperator)
return goals_regerx
def build_non_code_perfective_regex():
non_perfective_entities = ['warning(?:s)?'
, 'format(?:ing)?'
, 'indentation(?:s)?'
]
# TODO - applied to perfective entities too here, which is a bug.
modification_action = ['clean(?:-| )?up(?:s)?']
non_perfective_context = [
'fix(?:es|ed|ing)?'
,'(?:get|got|getting) rid'
, 'support(?:s|ed|ing)?'
]
modifiers = modification_activity + non_perfective_context
activity_regerx = "((?:%s)(?:\s|%s[\s\S]{0,50}%s)(?:%s))" % (build_separated_terms(modifiers, just_before=True)
, term_seperator
, term_seperator
, "|".join(prefective_entities
+ non_perfective_entities))
doc_header_regex = '(?:^|^[\s\S]{0,25}%s)(?:%s)[\s\S]{0,25}(?:%s)' % (term_seperator
, "|".join(perfective_header_action)
, build_separated_terms(documentation_entities))
no_prefective_action = "|".join([
'convert(?:ed|s|ing)?(?:%s|%s[\s\S]{0,50}%s)support(?:s|ed|ing)?' % (
term_seperator,term_seperator, term_seperator)
, '(?:make|made|making|makes)(?:%s|%s[\s\S]{0,50}%s)work' % (term_seperator, term_seperator, term_seperator)
, '(?:make|made|making|makes)(?:%s|%s[\s\S]{0,50}%s)sense' % (term_seperator, term_seperator, term_seperator)
, 'improv(?:e|es|ed|ing) handling'
, 'need(?:s|ing)?\srefactor(?:ing)?'
, '(?:%s)(?:%s|%s[\s\S]{0,50}%s)(?:%s)' %(build_separated_terms(non_perfective_entities,just_before=True)
,term_seperator
, term_seperator
, term_seperator
, "|".join(modification_action)
)
, doc_header_regex
])
non_perfective_context = '(?:%s|%s)' % (no_prefective_action
, activity_regerx)
return non_perfective_context
def build_documentation_entities_context(positive_re):
return '(?:%s)' % "|".join([
# TODO - take care of documentation entities spereatly
'(?:%s)[\s\S]{0,10}(?:%s)' % (build_separated_terms(documentation_entities, just_before=True)
,positive_re)
])
# instead of
# make sure
# missed to create
# can be replaced, e9dee4fc76caaca231bf10728d4f82bc46581bc5
# no seperation for create eb1027b5e8c047059f68e7547188d08c7fde0b6f
# inline c9d4924dcf129512dadd22dcd6fe0046cbcded43
# can be optimized 197fc962fa8a3153dc058abfa2ae8c816d67ea04
# corrective trouble (use wordnet) b580f0706dc1dcded6d1a584c37a83dd1cb2ea2a
# not used 72894b26c24b1ea31c6dda4634cfde67e7dc3050
def core_refactor_to_bq():
print("# Core Refactor Term")
print( regex_to_big_query(build_core_refactor_regex()))
print("#Core Refactor Term - end")
def positive_refactor_to_bq():
print( "# Refactor :build_refactor_regex()")
#print( ",")
print( regex_to_big_query(build_refactor_regex()))
print( "# Refactor :build_separated_terms(removal)")
print( "+")
print( regex_to_big_query(build_separated_terms(removal)))
print( "# Refactor :build_refactor_goals_regex()")
print( "+")
print( regex_to_big_query(build_refactor_goals_regex()))
def non_code_refactor_to_bq():
print( "# Refactor :build_non_code_perfective_regex()")
print( regex_to_big_query(build_non_code_perfective_regex()))
def non_positive_linguistic_refactor_to_bq():
print( "# Refactor :build_non_positive_linguistic(build_refactor_regex())")
print( "-")
print( regex_to_big_query(build_non_positive_linguistic(build_refactor_regex(use_conventional_commits=False))))
def non_positive_linguistic_refactor_goals_to_bq():
print( "# Refactor :build_non_positive_linguistic(build_refactor_goals_regex())")
print( regex_to_big_query(build_non_positive_linguistic(build_refactor_goals_regex())))
def non_positive_linguistic_removal_to_bq():
print("# Refactor :build_non_positive_linguistic(build_separated_terms(removal))")
print(regex_to_big_query(build_non_positive_linguistic(build_separated_terms(removal))))
def documentation_entities_context_refactor_to_bq():
print("# Refactor :build_documentation_entities_context(build_refactor_regex())")
print(regex_to_big_query(
build_documentation_entities_context(build_refactor_regex(use_conventional_commits=False))))
def refactor_to_bq():
print( "# Refactor")
print('{schema}.bq_positive_refactor(message)'.format(schema=SCHEMA_NAME))
print(' - {schema}.bq_non_code_refactor(message)'.format(schema=SCHEMA_NAME))
print(' - {schema}.bq_non_positive_linguistic_refactor(message)'.format(schema=SCHEMA_NAME))
print(' - {schema}.bq_non_positive_linguistic_refactor_goals(message)'.format(schema=SCHEMA_NAME))
print(' - {schema}.bq_non_positive_linguistic_refactor_removal(message)'.format(schema=SCHEMA_NAME))
print(' - {schema}.bq_documentation_entities_context_refactor(message)'.format(schema=SCHEMA_NAME))
print( "# Refactor - end ")
def built_is_refactor(commit_text):
removal_re = build_separated_terms(removal)
return (match(commit_text, build_refactor_regex())
+ match(commit_text, removal_re)
+ match(commit_text, build_refactor_goals_regex())
- match(commit_text, build_non_code_perfective_regex())
- match(commit_text
, build_documentation_entities_context(build_refactor_regex(use_conventional_commits=False)))
- match(commit_text
, build_non_positive_linguistic(build_refactor_regex(use_conventional_commits=False)))
- match(commit_text, build_non_positive_linguistic(build_separated_terms(removal)))
- match(commit_text, build_non_positive_linguistic(build_refactor_goals_regex()))
) > 0
def build_perfective_regex():
non_code = build_separated_terms (prefective_entities)
perfective = "(%s)" % non_code
return perfective
def just_perfective_to_bq():
print("# Just Perfective")
print( "# Perfective :build_perfective_regex()")
#print( ",")
print( regex_to_big_query(build_perfective_regex()))
def perfective_to_bq():
print( "# Perfective")
print('{schema}.bq_just_perfective(message)'.format(schema=SCHEMA_NAME))
print(' + {schema}.bq_refactor(message)'.format(schema=SCHEMA_NAME))
print( "# Perfective - end ")
def print_refactor_functions(commit: str = 'XXX'):
generate_bq_function('{schema}.bq_core_refactor'.format(schema=SCHEMA_NAME)
, core_refactor_to_bq
, commit=commit)
print()
generate_bq_function('{schema}.bq_positive_refactor'.format(schema=SCHEMA_NAME)
, positive_refactor_to_bq
, commit=commit)
print()
generate_bq_function('{schema}.bq_non_code_refactor'.format(schema=SCHEMA_NAME)
, non_code_refactor_to_bq
, commit=commit)
print()
generate_bq_function('{schema}.bq_non_positive_linguistic_refactor'.format(schema=SCHEMA_NAME)
, non_positive_linguistic_refactor_to_bq
, commit=commit)
print()
generate_bq_function('{schema}.bq_non_positive_linguistic_refactor_goals'.format(schema=SCHEMA_NAME)
, non_positive_linguistic_refactor_goals_to_bq
, commit=commit)
print()
generate_bq_function('{schema}.bq_non_positive_linguistic_refactor_removal'.format(schema=SCHEMA_NAME)
, non_positive_linguistic_removal_to_bq
, commit=commit)
print()
generate_bq_function('{schema}.bq_documentation_entities_context_refactor'.format(schema=SCHEMA_NAME)
, documentation_entities_context_refactor_to_bq
, commit=commit)
print()
generate_bq_function('{schema}.bq_refactor'.format(schema=SCHEMA_NAME)
, refactor_to_bq
, commit=commit)
print()
print()
generate_bq_function('{schema}.bq_just_perfective'.format(schema=SCHEMA_NAME)
, just_perfective_to_bq
, commit=commit)
print()
generate_bq_function('{schema}.bq_perfective'.format(schema=SCHEMA_NAME)
, perfective_to_bq
, commit=commit)
print()
if __name__ == '__main__':
print_refactor_functions(commit='4b76d8e76af938824f91f4b99247731c21e37ff9')