Advertisement
vimdiesel

Untitled

May 22nd, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. import re
  2. import os
  3.  
  4. path = 'D:\\Development\\CustomDevelopmentUtilities\\CustomDevelopmentUtility\\CAPP_Core'
  5. report_name = 'shipping'
  6. replace_method = False
  7. method_to_replace = 'void removefromspool'
  8. code_to_paste = """     protected override void RemoveRecordKeyFromSpool(RecordKey recordKey)
  9.         {
  10.             PackingListSearch.CreateInstance(userInfo).ShippingLabel_RemoveFromSpool(recordKey);
  11.         }\n"""
  12.  
  13. files = []
  14. count = 0
  15. already_done_count = 0
  16. func_not_found_count = 0
  17. function_start = 0
  18. function_end = 0
  19.  
  20. # r=root, d=directories, f = files
  21. for r, d, f in os.walk(path):
  22.     for file in f:
  23.         if 'controller' in file.lower() and report_name in file.lower():
  24.             files.append(os.path.join(r, file))
  25.             count = count + 1
  26.  
  27. for file in files:
  28.     fin_file = []
  29.     data_found = False
  30.     function_found = False
  31.     with open(file) as f:
  32.         content = f.readlines()
  33.         already_done = False
  34.  
  35.         line_number = 0
  36.         for line in content:
  37.             # make sure our line isnt already in
  38.             if 'RemoveRecordKeyFromSpool' in line:
  39.                 print('RemoveRecordKeyFromSpool found in file: ' + file)
  40.                 already_done = True
  41.                 break
  42.             # make sure using Data is here
  43.             if 'E2SSPro.Data' in line:
  44.                 data_found = True
  45.  
  46.             if method_to_replace in line.lower():
  47.                 function_found = True
  48.                 function_start = line_number
  49.                 # find the method to replace by backet counting
  50.                 bracket_count = 0
  51.                 line_number = line_number + 1 # skip to next line
  52.                 while True:
  53.                     edit_line = content[line_number]
  54.                     if '{' in edit_line:
  55.                         bracket_count = bracket_count + 1
  56.                     if '}' in edit_line:
  57.                         bracket_count = bracket_count - 1
  58.                     if bracket_count == 0:
  59.                         function_end = line_number
  60.                         fin_file = content
  61.                         break
  62.                     line_number = line_number + 1
  63.                    
  64.                 break # we found our function, we are done here
  65.             line_number = line_number + 1
  66.     # break if we have already done this one
  67.     if not already_done and function_found:
  68.         # add E2SSPro.Data if needed
  69.         if not data_found:
  70.             fin_file.insert(0, 'using E2SSPro.Data;\n')
  71.         # remove the method_to_replace
  72.         if replace_method:
  73.             del fin_file[function_start:function_end + 1]
  74.         # add new function
  75.         fin_file.insert(function_start, code_to_paste)
  76.  
  77.         # write file
  78.         with open(file, 'w') as f:
  79.             for line in fin_file:
  80.                 line.strip('\n')
  81.                 f.write("%s" % line)
  82.         print(file)
  83.     if already_done:
  84.         already_done_count = already_done_count + 1
  85.     if not function_found:
  86.         func_not_found_count = func_not_found_count + 1
  87.        
  88. print('Total found: ' + str(count))
  89. print('Already done: ' + str(already_done_count))
  90. print('Func not found: ' + str(func_not_found_count))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement