-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmerge.py
789 lines (711 loc) · 30.3 KB
/
merge.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
#!/usr/bin/python
#merge.py
#
#
# <<<COPYRIGHT>>>
#
#
#
#
import os
import sys
import re
import hashlib
import string
#------------------------------------------------------------------------------
D = None
#------------------------------------------------------------------------------
def compareCondition(found_condition, known_condition):
found_words = found_condition.split(' ')
known_words = known_condition.split(' ')
correct_words = []
for found in found_words:
opened = 0
while found.startswith('('):
found = found[1:]
opened += 1
closed = 0
while found.endswith('))'):
found = found[:-1]
closed += 1
for i in range(opened):
correct_words.append('(')
if found.count('()') and not found.count('.A('):
found = found.replace('()', '(arg)')
if found.strip():
correct_words.append(found)
for i in range(closed):
correct_words.append(')')
known_joined = ' '.join(known_words)
correct_joined = ' '.join(correct_words)
return known_joined == correct_joined
def processLink(name, condition, state_from, state_to):
global D
found_link_id = ''
for link_id, link_info in D[name]['links'].items():
if compareCondition(condition, link_info['condition']):
# print ' (%s) %s' % (link_id, link_info['condition'])
# print ' [%s %s]' % (state_from, state_to)
# print ' [%s %s]' % (link_info['from'], link_info['to'])
if link_info['from'] == state_from and link_info['to'] == state_to:
found_link_id = link_id
break
elif link_info['to'] == state_from and link_info['from'] == state_to:
# found_link_id = link_id
print(' WARNING it seems connection is turned contrary in Visio Document: [%s]<->[%s]' % (state_from, state_to))
break
# print ' <' + found_link_id + '> ' + condition
return found_link_id
def mergeStateMachine(old, new, name):
global D
if name not in D.keys():
return old, []
A = {}
merged = ''
errors = []
lines = old.splitlines()
start_line = -1
end_line = -1
found_class = ''
found_def_A = False
paragraph_def_A = -1
found_state = ''
first_state_line = -1
found_newstate = False
found_return_newstate = False
found_flag_post = ''
found_flag_fast = ''
states_found = []
found_condition = ''
flags = []
if D[name]['label'].endswith(']'):
for flag in D[name]['label'].split('\\n')[-1].strip('[]').split(','):
flags.append(flag.strip())
for line_index in range(1, len(lines)+1):
line = lines[line_index-1]
if line.strip().startswith('#'):
continue
paragraph = len(line) - len(line.lstrip(' '))
if line.count('class ') and ( line.count('Automat') or line.count('automat.Automat')):
found_class = re.search('class (\w+?)\(.*Automat.*\):', line)
if found_class is None:
found_class = re.search('class (\w+?)\(.*automat\.Automat.*\):', line)
if found_class is None:
errors.append('ERROR class name not recognized in line %d' % line_index)
found_class = ''
return old, errors
else:
found_class = found_class.group(1)
elif line.count('fast') and line.count('=') and (line.count('True') or line.count('False')):
if found_class and found_flag_fast == '' and not line.strip().startswith('#'):
found_flag_fast = 'True' if line.count('True') else 'False'
if found_flag_fast and 'fast' not in flags:
flags.append('fast')
elif line.count('post') and line.count('=') and (line.count('True') or line.count('False')):
if found_class and found_flag_post == '' and not line.strip().startswith('#'):
found_flag_post = 'True' if line.count('True') else 'False'
if found_flag_post and 'post' not in flags:
flags.append('post')
# elif line.count('fast') and line.count('=') and (line.count('True') or line.count('False')) and found_class:
# found_flag_fast = 'True' if line.count('True') else 'False'
elif (line.count('def A(self, event, arg):') or line.count('def A(self, event, *args, **kwargs):')) and found_class:
found_def_A = True
paragraph_def_A = paragraph
start_line = line_index
elif line.count('newstate = self.state') and found_def_A:
found_newstate = True
elif line.count('return newstate') and found_def_A:
found_return_newstate = True
elif ( line.count('if self.state is') or line.count('if self.state ==') ) and found_class and found_def_A:
if found_condition and found_state:
link_id = processLink(name, found_condition, found_state, found_state)
found_condition = ''
if link_id:
A[found_state].append({'from': found_state,
'to': found_state,
'condition': D[name]['links'][link_id]['condition'],
'link_id': link_id,})
found_state = re.search('if self\.state == \'(.+?)\'.*?\:', line)
if found_state is None:
found_state = re.search('if self\.state is \'(.+?)\'.*?\:', line)
if found_state is None:
errors.append('ERROR state not recognized in line %d' % line_index)
found_state = ''
return old, errors
else:
found_state = found_state.group(1)
if found_state in D[name]['states']:
A[found_state] = []
states_found.append(found_state)
else:
found_state = ''
found_condition = ''
if first_state_line == -1:
first_state_line = line_index - 1
if lines[first_state_line-1].strip().startswith('#---'):
if lines[first_state_line-1].strip().endswith('---'):
first_state_line -= 1
elif ( line.count(' if ') or line.count(' elif ') ) and line.count(':') and found_state:
if found_condition and found_state:
link_id = processLink(name, found_condition, found_state, found_state)
found_condition = ''
if link_id:
A[found_state].append({'from': found_state,
'to': found_state,
'condition': D[name]['links'][link_id]['condition'],
'link_id': link_id,})
found_condition = re.search('if(.+?)\:', line, )
if found_condition is None:
errors.append('ERROR condition not recognized in line %d' % line_index)
found_condition = ''
return old, errors
else:
found_condition = found_condition.group(1).strip()
# print ' [%s]'%found_condition
elif line.count('self.state =') and found_state:
state_to = re.search('self\.state = \'(.+?)\'', line)
if state_to is None:
errors.append('ERROR transition state not recognized in line %d' % line_index)
found_condition = ''
return old, errors
else:
state_to = state_to.group(1)
if found_condition:
link_id = processLink(name, found_condition, found_state, state_to)
found_condition = ''
if link_id:
A[found_state].append({'from': found_state,
'to': state_to,
'condition': D[name]['links'][link_id]['condition'],
'link_id': link_id,})
else:
if paragraph == paragraph_def_A or line_index == len(lines):
if found_condition and found_state:
link_id = processLink(name, found_condition, found_state, found_state)
found_condition = ''
if link_id:
A[found_state].append({'from': found_state,
'to': found_state,
'condition': D[name]['links'][link_id]['condition'],
'link_id': link_id,})
found_def_A = False
found_state = ''
found_condition = ''
end_line = line_index
break
merged = ''
state_index = 0
first_state = ''
for state_index in range(len(states_found)):
state = states_found[state_index]
if state not in D[name]['states']:
continue
merged += ' #---%s---\n' % state
if not first_state:
merged += " if self.state == '%s':\n" % state
first_state = state
else:
merged += " elif self.state == '%s':\n" % state
has_actions = False
for link_id, link_info in D[name]['links'].items():
if link_info['from'] != state:
continue
found_link = False
for link_index in range(len(A[state])):
if link_id == A[state][link_index]['link_id']:
found_link = True
break
if not found_link:
A[state].append({ 'from': link_info['from'],
'to': link_info['to'],
'condition': link_info['condition'],
'link_id': link_id,})
# print ' append '+link_info['condition']
for link_index in range(len(A[state])):
link_id = A[state][link_index]['link_id']
condition = A[state][link_index]['condition']
actions = D[name]['links'][link_id]['actions']
# print link_id, actions
if link_index == 0:
merged += " if %s:\n" % condition
else:
merged += " elif %s:\n" % condition
has_actions = has_actions or len(actions) > 0
if len(actions) == 0:
merged += " pass\n"
else:
for action in actions:
merged += " %s\n" % action
if not has_actions:
merged += " pass\n"
for state in D[name]['states']:
if state in states_found:
continue
merged += ' #---%s---\n' % state
if not first_state:
merged += " if self.state == '%s':\n" % state
first_state = state
else:
merged += " elif self.state == '%s':\n" % state
has_actions = False
first_link = ''
for link_id, link_info in D[name]['links'].items():
if link_info['from'] != state:
continue
condition = D[name]['links'][link_id]['condition']
actions = D[name]['links'][link_id]['actions']
if not first_link:
merged += " if %s:\n" % condition
first_link = link_id
else:
merged += " elif %s:\n" % condition
has_actions = has_actions or len(actions) > 0
if len(actions) == 0:
merged += " pass\n"
else:
for action in actions:
merged += " %s\n" % action
if not has_actions:
merged += " pass\n"
if 'post' in flags and not found_newstate:
# if not found_newstate:
merged = ' newstate = self.state\n' + merged
if 'post' in flags:
merged += ' return newstate\n'
else:
merged += ' return None\n'
if first_state_line >= 0 and end_line >= 0:
src = ''
src += '\n'.join(lines[:first_state_line])
src += '\n' + merged + '\n'
src += '\n'.join(lines[end_line-1:])
src += '\n'
return src, errors
else:
errors.append(' ERROR not found State Machine code in existing file, start=%d, end=%d' % (start_line, end_line))
return old, errors
def mergeMethods(old, new, name):
global D
if name not in D.keys():
return old, []
merged = ''
errors = []
lines = old.splitlines()
start_line = -1
end_line = -1
last_not_empty_line_index = -1
found_class = ''
found_def_A = False
paragraph_class = -1
paragraph_def_A = -1
known_conditions = list(D[name]['conditions'])
known_actions = list(D[name]['actions'])
automat_method_line_index = -1
non_automat_method_line_index = -1
class_finished_line_index = -1
for line_index in range(1, len(lines)+1):
line = lines[line_index-1]
if line.strip().startswith('#'):
continue
paragraph = len(line) - len(line.lstrip(' '))
if line.strip():
last_not_empty_line_index = line_index
if found_class and paragraph <= paragraph_class and class_finished_line_index == -1:
class_finished_line_index = line_index - 1
if non_automat_method_line_index == -1:
non_automat_method_line_index = class_finished_line_index
if line.count('class ') and ( line.count('Automat') or line.count('automat.Automat')):
found_class = re.search('class (\w+?)\(.*Automat.*\):', line)
if found_class is None:
found_class = re.search('class (\w+?)\(.*automat\.Automat.*\):', line)
if found_class is None:
errors.append('ERROR class name not recognized in line %d' % line_index)
found_class = ''
return old, errors
else:
found_class = found_class.group(1)
paragraph_class = paragraph
elif (line.count('def A(self, event, arg):') or line.count('def A(self, event, *args, **kwargs):')) and found_class:
found_def_A = True
paragraph_def_A = paragraph
start_line = line_index
else:
if paragraph <= paragraph_def_A and found_def_A:
found_def_A = False
end_line = line_index - 1
# print 'end_line', end_line
# class_finished_line_index = line_index - 1
if paragraph <= paragraph_class and line.strip() != '' and end_line > 0 and class_finished_line_index == -1:
class_finished_line_index = line_index - 1
# print 'class_finished_line_index', class_finished_line_index
if class_finished_line_index == -1 and end_line > 0:
class_finished_line_index = last_not_empty_line_index + 1
if end_line < 0 or class_finished_line_index < 0:
errors.append(' ERROR not found State Machine code in existing file, end_class=%d, end_def=%d' % (class_finished_line_index, end_line))
return old, errors
merged = '\n'.join(lines[:end_line])
merged += '\n'
for line_index in range(end_line+1, len(lines)+1):
line = lines[line_index-1]
comment = line.strip().startswith('#')
paragraph = len(line) - len(line.lstrip(' '))
ln = line
line = line.strip()
if line.startswith('class '):
merged += '\n'.join(lines[line_index-1:])
non_automat_method_line_index = line_index - 1
break
# continue
if not ( line.startswith('def ') and line.endswith(':') ):
merged += ln + '\n'
continue
if paragraph != paragraph_def_A:
merged += ln + '\n'
continue
method = line[line.find('def') + 4 : line.rfind(':')].strip(' ')
if method[:2] not in ['is', 'do']:
if non_automat_method_line_index < automat_method_line_index:
non_automat_method_line_index = line_index - 1
merged += ln + '\n'
continue
automat_method_line_index = line_index - 1
params = method[method.find('(')+1 : method.rfind(')')]
params = list(map(lambda p: p.strip(), params.split(',')))
ok = False
if len(params) == 0:
params = ['self', '*args', '**kwargs']
ok = True
elif len(params) == 1:
if params[0] == 'self':
params.append('*args')
params.append('**kwargs')
ok = True
else:
if params[0] == 'self':
ok = True
if not ok:
print(' WARNING non canonical function name in line %d: %s' % (line_index, method))
params = ['self', '*args', '**kwargs']
method_name = method[:method.find('(')]
# print method_name
found = False
# print('method_name: %r' % method_name)
# print('known_conditions before1: %r' % known_conditions)
if method_name.startswith('is'):
for condition in D[name]['conditions']:
if condition.startswith('self.'+method_name+'('):
found = True
known_conditions.remove(condition)
# print ' ' + condition
# break
else:
for action in D[name]['actions']:
if action.startswith('self.'+method_name+'('):
found = True
known_actions.remove(action)
# print ' ' + action
# break
# print('known_conditions before2: %r' % known_conditions)
if not found:
print(' WARNING method %s in line %s is unused' % (method_name, line_index))
merged += ln + '\n'
continue
merged += ' ' * paragraph + 'def ' + method_name + '(' + ', '.join(params) + '):\n'
# print 'non_automat_method_line_index', non_automat_method_line_index
if non_automat_method_line_index == -1:
non_automat_method_line_index = class_finished_line_index
merged_lines = merged.splitlines()
src = '\n'.join(merged_lines[:non_automat_method_line_index])
src += '\n'
condition_names = set()
# print('known_conditions after: %r' % known_conditions)
for condition in known_conditions:
func_name, _, func_args = condition.replace('self.', '').partition('(')
if func_name in condition_names:
continue
condition_names.add(func_name)
method = 'def %s(self, *args, **kwargs):' % func_name
src += ' ' * paragraph_def_A + method + '\n'
src += ' ' * (paragraph_def_A + 4) + '"""\n'
src += ' ' * (paragraph_def_A + 4) + 'Condition method.\n'
src += ' ' * (paragraph_def_A + 4) + '"""\n\n'
print(' made empty method %s' % method)
for action in known_actions:
method = action.replace('self.', 'def ').replace('(', '(self, ')+':'
src += ' ' * paragraph_def_A + method + '\n'
src += ' ' * (paragraph_def_A + 4) + '"""\n'
src += ' ' * (paragraph_def_A + 4) + 'Action method.\n'
src += ' ' * (paragraph_def_A + 4) + '"""\n\n'
print(' made empty method %s' % action)
# src += '\n'
src += '\n'.join(merged_lines[non_automat_method_line_index:])
src += '\n'
return src, errors
def mergeTimers(old, new, name):
global D
if name not in D.keys():
return old, []
if len(D[name]['timers']) == 0:
return old, []
errors = []
lines = old.splitlines()
start_line = -1
end_line = -1
found_class = ''
found_timers = False
for line_index in range(1, len(lines)+1):
line = lines[line_index-1]
if line.strip().startswith('#'):
continue
paragraph = len(line) - len(line.lstrip(' '))
if line.count('class ') and ( line.count('Automat') or line.count('automat.Automat')):
found_class = re.search('class (\w+?)\(.*Automat.*\):', line)
if found_class is None:
found_class = re.search('class (\w+?)\(.*automat\.Automat.*\):', line)
if found_class is None:
errors.append('ERROR class name not recognized in line %d' % line_index)
found_class = ''
return old, errors
else:
found_class = found_class.group(1)
elif line.count('timers = {') and found_class:
found_timers = True
start_line = line_index
if line.count('}'):
end_line = line_index
break
elif line.count('}') and found_timers:
end_line = line_index
break
if not found_class:
errors.append(' ERROR not found State Machine code in existing file')
return old, errors
if not found_timers:
errors.append(' ERROR not found timers in existing file')
return old, errors
stimers = ''
for timer, states in D[name]['timers'].items():
try:
delay = timer[6:].strip()
if delay.endswith('sec'):
delay = delay[:-3]
if delay.startswith('0') and '0.'+delay[1:] != delay:
delay = '0.'+delay[1:]
delay = float(delay)
elif delay.endswith('min'):
delay = int(delay[:-3]) * 60
elif delay.endswith('hour'):
delay = int(delay[:-4]) * 60 * 60
else:
delay = 0
except:
delay = 0
if delay == 0:
errors.append(' WARNING: can not understand timer event: %s' % timer)
continue
stimers += " '%s': (%s, [%s]),\n" % (timer, str(delay), ', '.join(map(lambda x: "'%s'" % x, states)))
# print start_line, end_line
# print stimers
# print '----'
# print '\n'.join(lines[start_line-1:start_line])
# print '----'
# print '\n'.join(lines[end_line-1:end_line])
# print '----'
merged = '\n'.join(lines[:start_line])
merged += '\n'
merged += stimers
merged += '\n'.join(lines[end_line-1:])
merged += '\n'
return merged, errors
def mergeEvents(old, new, name):
global D
if name not in D.keys():
return old, []
if len(D[name]['events']) == 0:
return old, []
errors = []
lines = old.splitlines()
start_line = -1
end_line = -1
head_start_line = -1
head_end_line = -1
found_class = ''
found_heading = False
found_events = False
for line_index in range(1, len(lines)+1):
line = lines[line_index-1]
if line.strip().startswith('#'):
continue
paragraph = len(line) - len(line.lstrip(' '))
if line.count('.. module::'):
found_heading = True
head_start_line = line_index
elif line.count('"""') and found_heading and head_end_line == -1:
head_end_line = line_index
elif line.count('class ') and ( line.count('Automat') or line.count('automat.Automat')):
found_class = re.search('class (\w+?)\(.*Automat.*\):', line)
if found_class is None:
found_class = re.search('class (\w+?)\(.*automat\.Automat.*\):', line)
if found_class is None:
errors.append('ERROR class name not recognized in line %d' % line_index)
found_class = ''
return old, errors
else:
found_class = found_class.group(1)
elif line.count('EVENTS:'):
found_events = True
start_line = line_index
end_line = line_index
elif line.count('* :red:`') and found_events:
end_line = line_index
if not found_class:
errors.append(' ERROR not found State Machine code in existing file')
return old, errors
if not found_events:
# if not found_heading or head_end_line == -1:
errors.append(' ERROR not found "EVENTS:" marker in existing file')
return old, errors
# start_line = head_end_line
# end_line = head_end_line - 1
events = '\n'
events += 'EVENTS:\n'
for event in sorted(D[name]['events']):
events += ' * :red:`%s`\n' % event
# events += '\n'
merged = '\n'.join(lines[:start_line-1])
merged += events
merged += '\n'.join(lines[end_line:])
merged += '\n'
return merged, errors
def mergeHeading(old, new, name):
global D
if name not in D.keys():
return old, []
merged = ''
errors = []
lines = old.splitlines()
head_start_line = -1
head_end_line = -1
found_heading = False
for line_index in range(1, len(lines)+1):
line = lines[line_index-1]
if line.strip().startswith('#'):
continue
paragraph = len(line) - len(line.lstrip(' '))
if line.count('.. module::'):
found_heading = True
head_start_line = line_index
elif line.count('"""') and found_heading and head_end_line == -1:
head_end_line = line_index
if not found_heading:
head = '\n\n"""\n'
head += '.. module:: %s\n' % name.replace('(', '').replace(')', '')
head += '.. role:: red\n\n'
if D[name]['label']:
head += ' '.join(D[name]['label'].split('\\n'))
head += '\n\n'
head += '.. raw:: html\n\n'
head += ' <a href="%s.png" target="_blank">\n' % name.replace('(', '').replace(')', '')
head += ' <img src="%s.png" style="max-width:100%%;">\n' % name.replace('(', '').replace(')', '')
head += ' </a>\n\n'
head += '"""\n\n'
merged += head
merged += old
else:
merged = old
return merged, errors
def main():
global D
D = eval(open(sys.argv[1]).read())
path_new = sys.argv[2]
path_old = sys.argv[3]
only_update = sys.argv[4] == '1'
remove_new = sys.argv[5] == '1'
dont_modify = sys.argv[6] == '1'
files_new = os.listdir(path_new)
files_old = os.listdir(path_old)
# print
for filename in files_new:
if not filename.endswith('.py'):
continue
filepath_new = os.path.abspath(os.path.join(path_new, filename))
if not os.path.isfile(filepath_new):
continue
if not os.access(filepath_new, os.R_OK):
print('can not read file %s, do not have read permissions' % filepath_new)
continue
filepath_old = os.path.abspath(os.path.join(path_old, filename))
if os.path.isfile(filepath_old) and not os.access(filepath_old, os.W_OK):
print('do not have write permissions for file %s' % filepath_old)
continue
automat_name = filename.replace('.py', '') + '()'
filepath = os.path.join(path_old, filename)
fp = filepath.ljust(50)
src_new = open(filepath_new).read()
if filename not in files_old:
if not only_update:
open(filepath_old, 'w').write(src_new)
print('%s new file created %d bytes long' % (fp, len(src_new), ))
continue
continue
fin = open(filepath_old)
src_old = fin.read()
fin.close()
src_merged = src_old
print('process %s' % fp)
src_merged, errors = mergeStateMachine(src_merged, src_new, automat_name)
if errors:
print('%s : mergeStateMachine()\n%s' % (fp, ( '\n '.join(errors))))
continue
src_merged, errors = mergeMethods(src_merged, src_new, automat_name)
if errors:
print('%s : mergeMethods()\n%s' % (fp,( '\n '.join(errors))))
continue
src_merged, errors = mergeTimers(src_merged, src_new, automat_name)
if errors:
print('%s : mergeTimers()\n%s' % (fp, ( '\n '.join(errors))))
continue
src_merged, errors = mergeHeading(src_merged, src_new, automat_name)
if errors:
print('%s : mergeHeading()\n%s' % (fp, ( '\n '.join(errors))))
continue
src_merged, errors = mergeEvents(src_merged, src_new, automat_name)
if errors:
print('%s : mergeEvents()\n%s' % (fp, ( '\n '.join(errors))))
continue
md5_old = hashlib.md5(src_old.encode('utf-8')).hexdigest()
md5_merged = hashlib.md5(src_merged.encode('utf-8')).hexdigest()
if md5_old != md5_merged:
if dont_modify:
print(' changed'.upper())
else:
print(' no changes detected')
if dont_modify:
import difflib
for line in difflib.unified_diff(src_old.splitlines(1), src_merged.splitlines(1)):
sys.stdout.write(line)
# d = difflib.Differ()
# result = list(d.compare(src_old.splitlines(1), src_merged.splitlines(1)))
# pprint.pprint(result)
# sys.stdout.writelines(result)
else:
# fout = open(filepath_old+'.new', 'w')
if md5_old != md5_merged:
fout = open(filepath_old, 'w')
fout.write(src_merged.replace('\r\n', '\n'))
fout.close()
print(' updated'.upper())
# os.system('diff %s %s' % (filepath_old, filepath_old+'.new'))
if remove_new:
try:
os.remove(filepath_new)
except:
print(' can not delete the file %s' % filepath_new)
# if md5_old == md5_merged:
# print fp, ' not changed'.upper()
# if automat_name.count('fire_hire'):
# break
print('DONE')
# raw_input("\nPress ENTER to close the window")
if __name__ == '__main__':
main()