Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import os
- path = 'D:\\Development\\CustomDevelopmentUtilities\\CustomDevelopmentUtility\\CAPP_Core'
- report_name = 'shipping'
- replace_method = False
- method_to_replace = 'void removefromspool'
- code_to_paste = """ protected override void RemoveRecordKeyFromSpool(RecordKey recordKey)
- {
- PackingListSearch.CreateInstance(userInfo).ShippingLabel_RemoveFromSpool(recordKey);
- }\n"""
- files = []
- count = 0
- already_done_count = 0
- func_not_found_count = 0
- function_start = 0
- function_end = 0
- # r=root, d=directories, f = files
- for r, d, f in os.walk(path):
- for file in f:
- if 'controller' in file.lower() and report_name in file.lower():
- files.append(os.path.join(r, file))
- count = count + 1
- for file in files:
- fin_file = []
- data_found = False
- function_found = False
- with open(file) as f:
- content = f.readlines()
- already_done = False
- line_number = 0
- for line in content:
- # make sure our line isnt already in
- if 'RemoveRecordKeyFromSpool' in line:
- print('RemoveRecordKeyFromSpool found in file: ' + file)
- already_done = True
- break
- # make sure using Data is here
- if 'E2SSPro.Data' in line:
- data_found = True
- if method_to_replace in line.lower():
- function_found = True
- function_start = line_number
- # find the method to replace by backet counting
- bracket_count = 0
- line_number = line_number + 1 # skip to next line
- while True:
- edit_line = content[line_number]
- if '{' in edit_line:
- bracket_count = bracket_count + 1
- if '}' in edit_line:
- bracket_count = bracket_count - 1
- if bracket_count == 0:
- function_end = line_number
- fin_file = content
- break
- line_number = line_number + 1
- break # we found our function, we are done here
- line_number = line_number + 1
- # break if we have already done this one
- if not already_done and function_found:
- # add E2SSPro.Data if needed
- if not data_found:
- fin_file.insert(0, 'using E2SSPro.Data;\n')
- # remove the method_to_replace
- if replace_method:
- del fin_file[function_start:function_end + 1]
- # add new function
- fin_file.insert(function_start, code_to_paste)
- # write file
- with open(file, 'w') as f:
- for line in fin_file:
- line.strip('\n')
- f.write("%s" % line)
- print(file)
- if already_done:
- already_done_count = already_done_count + 1
- if not function_found:
- func_not_found_count = func_not_found_count + 1
- print('Total found: ' + str(count))
- print('Already done: ' + str(already_done_count))
- print('Func not found: ' + str(func_not_found_count))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement