Paste Code
Paste Blends
Paste Images
# paths = ['/b/release/ui', '/b/release/io']
paths = ['/b/release', '/b/source/blender/makesrna/intern']

#paths = ['/root/blender-svn/blender/']
tmp = '/tmp/batch_edit.tmp'

import sys, os

def convertSpace2Tabs(lines):
for i, l in enumerate(lines):

l_strip = l.lstrip()
if l_strip:
diff = len(l)-len(l_strip)
if diff:
white = l[:diff]
white = white.replace('\t', ' ')
white = white.replace(' ', '\t')
white = white.replace(' ', '')

'''
print "---"
print l
print white+l[diff:]
print ">>>"
'''
lines[i] = white+l[diff:]

def convertTabs2Spaces(lines):
for i, l in enumerate(lines):
l = lines[i] = lines[i].rstrip() + '\n'
l_strip = l.lstrip()
if l_strip:
diff = len(l)-len(l_strip)
if diff:
white = l[:diff]
white = white.replace('\t', ' ')
lines[i] = white+l[diff:]



def Fix_b25_enums(lines):

for i, l in enumerate(lines):

replacements = []
if '"' in l:
l_split = l.split('"')
if l_split:
for j, w in enumerate(l_split):

if ' ' not in w and \
'.' not in w and \
',' not in w and \
'(' not in w and \
')' not in w and \
'[' not in w and \
']' not in w and \
'\\' not in w and \
'/' not in w and \
'+' not in w and \
'-' not in w and \
"'" not in w and \
"!" not in w and \
":" not in w and \
'#' not in w:

if w.isupper():
replacements.append(w)

replace_ok = False
for w in replacements:
rep_from = '"'+w+'"'
rep_to = "'"+w+"'"

# Check this isnt a text arg
index = l.find(rep_from)
if index != -1:
try:
arg = l[:index].replace('=', ' ').replace(',', ' ').replace('"', ' ').replace('(', ' ').replace(')', ' ').split()[-1]
except:
arg = ''

if 'text' not in arg:
replace_ok= True
l = l.replace(rep_from, rep_to)

if replace_ok:
lines[i] = l

def replace_mtx(lines):

for i, l in enumerate(lines):

l = l.replace('#include "MTC_vectorops.h"', '')
l = l.replace('#include "MTC_matrixops.h"', '')
l = l.replace('MTC_Mat4Invert(', 'Mat4Invert(')
l = l.replace('MTC_Mat3MulVecfl(', 'Mat3MulVecfl(')
l = l.replace('MTC_Mat4MulVec4fl(', 'Mat4MulVec4fl(')
l = l.replace('MTC_Mat4Ortho(', 'Mat4Ortho(')
l = l.replace('MTC_dot3Float(', 'Inpf(')
l = l.replace('MTC_Mat4MulVecfl(', 'Mat4MulVecfl(')
l = l.replace('MTC_Mat3MulVecd(', 'Mat3MulVecd(')
l = l.replace('MTC_Mat4Mul3Vecfl(', 'Mat4Mul3Vecfl(')
l = l.replace('MTC_Mat4One(', 'Mat4One(')
l = l.replace('MTC_Mat4CpyMat4(', 'Mat4CpyMat4(')
l = l.replace('MTC_Mat3CpyMat4(', 'Mat3CpyMat4(')
l = l.replace('MTC_Mat4MulMat4(', 'Mat4MulMat4(')
l = l.replace('MTC_Mat4SwapMat4(', 'Mat4SwapMat4(')
l = l.replace('MTC_cross3Float(', 'Crossf(')
l = l.replace('MTC_diff3Float(', 'VecSubf(')
l = l.replace('MTC_Mat4MulSerie(', 'Mat4MulSerie(')

lines[i] = l

def replace_25_ui(lines):

def foo(l, from_, to_):
l = l.replace(from_, to_)
l = l.replace('"'+from_[:-1]+'"', '"'+to_[:-1]+'"')

return l

for i, l in enumerate(lines):

l = foo(l, 'itemR(', 'prop(')
l = foo(l, 'items_enumR(', 'props_enum(')
l = foo(l, 'item_menu_enumR(', 'prop_menu_enum(')
l = foo(l, 'item_pointerR(', 'prop_pointer(')
l = foo(l, 'itemO(', 'operator(')
l = foo(l, 'item_enumO(', 'operator_enum(')
l = foo(l, 'items_enumO(', 'operator_enums(')
l = foo(l, 'item_menu_enumO(', 'operator_menu_enum(')
l = foo(l, 'item_booleanO(', 'operator_boolean(')
l = foo(l, 'item_intO(', 'operator_int(')
l = foo(l, 'item_floatO(', 'operator_float(')
l = foo(l, 'item_stringO(', 'operator_string(')
l = foo(l, 'itemL(', 'label(')
l = foo(l, 'itemM(', 'menu(')
l = foo(l, 'itemS(', 'separator(')



lines[i] = l

def replace_7_curve(lines):
for i, l in enumerate(lines):
ll = l
if "->type" in l and "7" in l and '&' in l and ('pNurb' in l or "nu" in l):
#l = l.replace('&7', '& 7')
#l = l.replace('& 7', '& 7')
l = l.replace('if((nu->type & 7)==CU_BEZIER)', 'if(nu->type == CU_BEZIER)')
l = l.replace('if((nu1->type & 7)==CU_BEZIER)', 'if(nu1->type == CU_BEZIER)')
l = l.replace('if( (nu->type & 7)==CU_BEZIER )', 'if(nu->type == CU_BEZIER)')
l = l.replace('if( (nu->type & 7)==CU_BEZIER)', 'if(nu->type == CU_BEZIER)')
l = l.replace('((nu->type & 7)==CU_NURBS)', '(nu->type == CU_NURBS)')
l = l.replace('((nu->type & 7)!=CU_BEZIER)', '(nu->type != CU_BEZIER)')
l = l.replace('((nu->type & 7)==CU_BEZIER)', '(nu->type == CU_BEZIER)')
l = l.replace('((nu->type & 7)!=CU_NURBS)', '(nu->type != CU_NURBS)')
l = l.replace('((nu1->type & 7)==CU_NURBS)', '(nu1->type == CU_NURBS)')
l = l.replace('((nu->type & 7)==CU_POLY)', '(nu->type == CU_POLY)')
l = l.replace('( (nu->type & 7)==CU_POLY )', '(nu->type == CU_POLY)')
l = l.replace('((nu->type & 7)==1)', '(nu->type == CU_BEZIER)')
l = l.replace('switch(nu->type & 7)', 'switch(nu->type)')
l = l.replace('(nu->type & 7)==CU_NURBS', 'nu->type == CU_NURBS')
l = l.replace('((nu->type & 7) == CU_BEZIER)', '(nu->type == CU_BEZIER)')

if l != ll:
# print l[:-1]
pass
else:
print l[:-1]
pass

lines[i] = l

static = '''
weightSetData
winqreadnodespace
wm_search_menu_poll
write_voxeldata_header
xsortvert_flag
zero_lfvector
'''.split()
def find_funcs(lines, fname, funcs):
ignore = ['for', 'if', 'float', 'while']

for i, l in enumerate(lines):
if not l.startswith('\t') and not l.startswith(' '):
if '(' in l and ')' in l:
l_strip = l.strip()
if not l_strip.startswith('//'):
if not l_strip.startswith('/*'):
if not l_strip.startswith('#'):
#if l_strip.endswith('{') or (i+1 != len(lines) and '{' in lines[i+1]):
if 1:

if 'typedef' not in l:
if '#define' not in l:
if 'enum' not in l:


# were looking for non-static funcs

if 'static' not in l and "_OT_" not in l:
f = l.split('(')[0]
if f:
f = f.split()[-1]
f = f.split('*')[-1]
f = f.strip()
if f and f not in ignore and len(f) > 3:
# print f


# funcs[f] = {}



if f in static:
# MAKE STATIC
lines[i] = 'static ' + l




def get_lines(f):
file = open(f, 'r')
lines = file.readlines()
file.close()
return lines

def write_lines(f, lines):
file = open(f, 'w')
file.writelines(lines)
file.close()



def do_file_cmake_ws(f):
if not f.endswith('.txt'):
return
lines = get_lines(f)

convertSpace2Tabs(lines)
write_lines(f, lines)


def do_pep8_ws(f):
if not f.endswith('.py'):
return
lines = get_lines(f)

convertTabs2Spaces(lines)
write_lines(f, lines)

def do_ui_funcs(f):
if not f.endswith('.py') and not f.endswith('.c'):
return
lines = get_lines(f)

replace_25_ui(lines)
write_lines(f, lines)

def do_file_25_enum(f):
if not f.endswith('.py'):
return
lines = get_lines(f)

Fix_b25_enums(lines)
write_lines(f, lines)

def do_file_MTC_replace(f):
if not f.endswith('.c'):
return
lines = get_lines(f)
replace_mtx(lines)
write_lines(f, lines)

def do_file_7_curve(f):
if not f.endswith('.c'):
return
lines = get_lines(f)
replace_7_curve(lines)
write_lines(f, lines)

def do_file_extract_funcs(f):
if not f.endswith('.c'):
return
lines = get_lines(f)
find_funcs(lines, f, do_file_extract_funcs.funcs)
write_lines(f, lines)
do_file_extract_funcs.funcs = {}


files = []
for path in paths:
cmd = 'find "' + path + '" -name "*.*" -print | grep -v "\.svn" > ' + tmp
os.system( cmd )
print cmd
file = open(tmp, 'rU')
files.extend([l.strip() for l in file.readlines()])
file.close()


for f in files:
# print(f)
# do_pep8_ws(f) # <- change for different utility functions.
do_ui_funcs(f)

os.system('rm /tmp/batch_edit.tmp')

print "DONE!"
  1. # paths = ['/b/release/ui', '/b/release/io']
  2. paths = ['/b/release', '/b/source/blender/makesrna/intern']
  3.  
  4. #paths = ['/root/blender-svn/blender/']
  5. tmp = '/tmp/batch_edit.tmp'
  6.  
  7. import sys, os
  8.  
  9. def convertSpace2Tabs(lines):
  10.     for i, l in enumerate(lines):
  11.        
  12.         l_strip = l.lstrip()
  13.         if l_strip:
  14.             diff = len(l)-len(l_strip)
  15.             if diff:
  16.                 white = l[:diff]
  17.                 white = white.replace('\t', '    ')
  18.                 white = white.replace('  ', '\t')
  19.                 white = white.replace(' ', '')
  20.                
  21.                 '''
  22.                 print "---"
  23.                 print l
  24.                 print white+l[diff:]
  25.                 print ">>>"
  26.                 '''
  27.                 lines[i] = white+l[diff:]
  28.  
  29. def convertTabs2Spaces(lines):
  30.     for i, l in enumerate(lines):
  31.         l = lines[i] = lines[i].rstrip() + '\n'
  32.         l_strip = l.lstrip()
  33.         if l_strip:
  34.             diff = len(l)-len(l_strip)
  35.             if diff:
  36.                 white = l[:diff]
  37.                 white = white.replace('\t', '    ')
  38.                 lines[i] = white+l[diff:]
  39.        
  40.  
  41.  
  42. def Fix_b25_enums(lines):
  43.    
  44.     for i, l in enumerate(lines):
  45.        
  46.         replacements = []
  47.         if '"' in l:
  48.             l_split = l.split('"')
  49.             if l_split:
  50.                 for j, w in enumerate(l_split):
  51.                    
  52.                     if     ' ' not in w and \
  53.                         '.' not in w and \
  54.                         ',' not in w and \
  55.                         '(' not in w and \
  56.                         ')' not in w and \
  57.                         '[' not in w and \
  58.                         ']' not in w and \
  59.                         '\\' not in w and \
  60.                         '/' not in w and \
  61.                         '+' not in w and \
  62.                         '-' not in w and \
  63.                         "'" not in w and \
  64.                         "!" not in w and \
  65.                         ":" not in w and \
  66.                         '#' not in w:
  67.                        
  68.                             if w.isupper():
  69.                                 replacements.append(w)
  70.        
  71.         replace_ok = False
  72.         for w in replacements:
  73.             rep_from =    '"'+w+'"'
  74.             rep_to =    "'"+w+"'"
  75.            
  76.             # Check this isnt a text arg
  77.             index = l.find(rep_from)
  78.             if index != -1:
  79.                 try:
  80.                     arg = l[:index].replace('=', ' ').replace(',', ' ').replace('"', ' ').replace('(', ' ').replace(')', ' ').split()[-1]
  81.                 except:
  82.                     arg = ''
  83.                
  84.                 if 'text' not in arg:
  85.                     replace_ok= True
  86.                     l = l.replace(rep_from, rep_to)
  87.        
  88.         if replace_ok:
  89.             lines[i] = l
  90.    
  91. def replace_mtx(lines):
  92.    
  93.     for i, l in enumerate(lines):
  94.        
  95.         l = l.replace('#include "MTC_vectorops.h"', '')
  96.         l = l.replace('#include "MTC_matrixops.h"', '')
  97.         l = l.replace('MTC_Mat4Invert(', 'Mat4Invert(')
  98.         l = l.replace('MTC_Mat3MulVecfl(', 'Mat3MulVecfl(')
  99.         l = l.replace('MTC_Mat4MulVec4fl(', 'Mat4MulVec4fl(')
  100.         l = l.replace('MTC_Mat4Ortho(', 'Mat4Ortho(')
  101.         l = l.replace('MTC_dot3Float(', 'Inpf(')
  102.         l = l.replace('MTC_Mat4MulVecfl(', 'Mat4MulVecfl(')
  103.         l = l.replace('MTC_Mat3MulVecd(', 'Mat3MulVecd(')
  104.         l = l.replace('MTC_Mat4Mul3Vecfl(', 'Mat4Mul3Vecfl(')
  105.         l = l.replace('MTC_Mat4One(', 'Mat4One(')
  106.         l = l.replace('MTC_Mat4CpyMat4(', 'Mat4CpyMat4(')
  107.         l = l.replace('MTC_Mat3CpyMat4(', 'Mat3CpyMat4(')
  108.         l = l.replace('MTC_Mat4MulMat4(', 'Mat4MulMat4(')
  109.         l = l.replace('MTC_Mat4SwapMat4(', 'Mat4SwapMat4(')
  110.         l = l.replace('MTC_cross3Float(', 'Crossf(')
  111.         l = l.replace('MTC_diff3Float(', 'VecSubf(')
  112.         l = l.replace('MTC_Mat4MulSerie(', 'Mat4MulSerie(')
  113.        
  114.         lines[i] = l
  115.  
  116. def replace_25_ui(lines):
  117.    
  118.     def foo(l, from_, to_):
  119.         l = l.replace(from_, to_)
  120.         l = l.replace('"'+from_[:-1]+'"', '"'+to_[:-1]+'"')
  121.        
  122.         return l
  123.    
  124.     for i, l in enumerate(lines):
  125.        
  126.         l = foo(l, 'itemR(', 'prop(')
  127.         l = foo(l, 'items_enumR(', 'props_enum(')
  128.         l = foo(l, 'item_menu_enumR(', 'prop_menu_enum(')
  129.         l = foo(l, 'item_pointerR(', 'prop_pointer(')
  130.         l = foo(l, 'itemO(', 'operator(')
  131.         l = foo(l, 'item_enumO(', 'operator_enum(')
  132.         l = foo(l, 'items_enumO(', 'operator_enums(')
  133.         l = foo(l, 'item_menu_enumO(', 'operator_menu_enum(')
  134.         l = foo(l, 'item_booleanO(', 'operator_boolean(')
  135.         l = foo(l, 'item_intO(', 'operator_int(')
  136.         l = foo(l, 'item_floatO(', 'operator_float(')
  137.         l = foo(l, 'item_stringO(', 'operator_string(')
  138.         l = foo(l, 'itemL(', 'label(')
  139.         l = foo(l, 'itemM(', 'menu(')
  140.         l = foo(l, 'itemS(', 'separator(')
  141.        
  142.  
  143.        
  144.         lines[i] = l
  145.  
  146. def replace_7_curve(lines):
  147.     for i, l in enumerate(lines):
  148.         ll = l
  149.         if "->type" in l and "7" in l and '&' in l and ('pNurb' in l or "nu" in l):
  150.             #l = l.replace('&7', '&   7')
  151.             #l = l.replace('& 7', '&   7')
  152.             l = l.replace('if((nu->type & 7)==CU_BEZIER)', 'if(nu->type == CU_BEZIER)')
  153.             l = l.replace('if((nu1->type & 7)==CU_BEZIER)', 'if(nu1->type == CU_BEZIER)')
  154.             l = l.replace('if( (nu->type & 7)==CU_BEZIER )', 'if(nu->type == CU_BEZIER)')
  155.             l = l.replace('if( (nu->type & 7)==CU_BEZIER)', 'if(nu->type == CU_BEZIER)')
  156.             l = l.replace('((nu->type & 7)==CU_NURBS)', '(nu->type == CU_NURBS)')
  157.             l = l.replace('((nu->type & 7)!=CU_BEZIER)', '(nu->type != CU_BEZIER)')
  158.             l = l.replace('((nu->type & 7)==CU_BEZIER)', '(nu->type == CU_BEZIER)')
  159.             l = l.replace('((nu->type & 7)!=CU_NURBS)', '(nu->type != CU_NURBS)')
  160.             l = l.replace('((nu1->type & 7)==CU_NURBS)', '(nu1->type == CU_NURBS)')
  161.             l = l.replace('((nu->type & 7)==CU_POLY)', '(nu->type == CU_POLY)')
  162.             l = l.replace('( (nu->type & 7)==CU_POLY )', '(nu->type == CU_POLY)')
  163.             l = l.replace('((nu->type & 7)==1)', '(nu->type == CU_BEZIER)')
  164.             l = l.replace('switch(nu->type & 7)', 'switch(nu->type)')
  165.             l = l.replace('(nu->type & 7)==CU_NURBS', 'nu->type == CU_NURBS')
  166.             l = l.replace('((nu->type & 7) == CU_BEZIER)', '(nu->type == CU_BEZIER)')
  167.            
  168.             if l != ll:
  169.                 # print l[:-1]
  170.                 pass
  171.             else:
  172.                 print l[:-1]
  173.                 pass
  174.        
  175.         lines[i] = l
  176.    
  177. static = '''
  178. weightSetData
  179. winqreadnodespace
  180. wm_search_menu_poll
  181. write_voxeldata_header
  182. xsortvert_flag
  183. zero_lfvector
  184. '''.split()
  185. def find_funcs(lines, fname, funcs):
  186.     ignore = ['for', 'if', 'float', 'while']
  187.    
  188.     for i, l in enumerate(lines):
  189.         if not l.startswith('\t') and not l.startswith(' '):
  190.             if '(' in l and ')' in l:
  191.                 l_strip = l.strip()
  192.                 if not l_strip.startswith('//'):
  193.                     if not l_strip.startswith('/*'):
  194.                         if not l_strip.startswith('#'):
  195.                             #if l_strip.endswith('{') or (i+1 != len(lines) and '{' in lines[i+1]):
  196.                             if 1:
  197.                                
  198.                                 if 'typedef' not in l:
  199.                                     if '#define' not in l:
  200.                                         if 'enum' not in l:
  201.                                            
  202.                                            
  203.                                             # were looking for non-static funcs
  204.                                            
  205.                                             if 'static' not in l and "_OT_" not in l:
  206.                                                 f = l.split('(')[0]
  207.                                                 if f:
  208.                                                     f = f.split()[-1]
  209.                                                     f = f.split('*')[-1]
  210.                                                     f = f.strip()
  211.                                                     if f and f not in ignore and len(f) > 3:
  212.                                                         # print f
  213.                                                        
  214.                                                        
  215.                                                         # funcs[f] = {}
  216.                                                        
  217.                                                        
  218.                                                        
  219.                                                         if f in static:
  220.                                                             # MAKE STATIC
  221.                                                             lines[i] = 'static ' + l
  222.    
  223.    
  224.    
  225.  
  226. def get_lines(f):
  227.     file = open(f, 'r')
  228.     lines = file.readlines()
  229.     file.close()
  230.     return lines
  231.  
  232. def write_lines(f, lines):
  233.     file = open(f, 'w')
  234.     file.writelines(lines)
  235.     file.close()
  236.  
  237.  
  238.  
  239. def do_file_cmake_ws(f):
  240.     if not f.endswith('.txt'):
  241.         return
  242.     lines = get_lines(f)
  243.    
  244.     convertSpace2Tabs(lines)
  245.     write_lines(f, lines)
  246.  
  247.  
  248. def do_pep8_ws(f):
  249.     if not f.endswith('.py'):
  250.         return
  251.     lines = get_lines(f)
  252.    
  253.     convertTabs2Spaces(lines)
  254.     write_lines(f, lines)
  255.    
  256. def do_ui_funcs(f):
  257.     if not f.endswith('.py') and not f.endswith('.c'):
  258.         return
  259.     lines = get_lines(f)
  260.    
  261.     replace_25_ui(lines)
  262.     write_lines(f, lines)
  263.  
  264. def do_file_25_enum(f):
  265.     if not f.endswith('.py'):
  266.         return
  267.     lines = get_lines(f)
  268.    
  269.     Fix_b25_enums(lines)
  270.     write_lines(f, lines)
  271.        
  272. def do_file_MTC_replace(f):
  273.     if not f.endswith('.c'):
  274.         return
  275.     lines = get_lines(f)
  276.     replace_mtx(lines)
  277.     write_lines(f, lines)
  278.  
  279. def do_file_7_curve(f):
  280.     if not f.endswith('.c'):
  281.         return
  282.     lines = get_lines(f)
  283.     replace_7_curve(lines)
  284.     write_lines(f, lines)
  285.  
  286. def do_file_extract_funcs(f):    
  287.     if not f.endswith('.c'):
  288.         return
  289.     lines = get_lines(f)
  290.     find_funcs(lines, f, do_file_extract_funcs.funcs)
  291.     write_lines(f, lines)
  292. do_file_extract_funcs.funcs = {}
  293.  
  294.  
  295. files = []
  296. for path in paths:
  297.     cmd = 'find "' + path + '" -name "*.*" -print | grep -v "\.svn" > ' + tmp
  298.     os.system( cmd )
  299.     print cmd
  300.     file = open(tmp, 'rU')
  301.     files.extend([l.strip() for l in file.readlines()])
  302.     file.close()
  303.  
  304.  
  305. for f in files:
  306.     # print(f)
  307.     # do_pep8_ws(f) # <- change for different utility functions.
  308.     do_ui_funcs(f)
  309.  
  310. os.system('rm /tmp/batch_edit.tmp')
  311.  
  312. print "DONE!"
go to heaven