-
Notifications
You must be signed in to change notification settings - Fork 1
/
split-replace.py
67 lines (56 loc) · 2.11 KB
/
split-replace.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
# -*- coding: utf-8 -*-
import os, sys,shutil, re
mydir= 'C:\Users\Fruch\GitHub\python-snippts\split-replace.py'
# find and replace in a dir by multiple pairs of regex
findreplace = [
(re.compile(ur'''if ([^{]+) {([^}]+)}''',re.U|re.M), ur'''if \1 then \2end'''),
(re.compile(ur'''!=''',re.U|re.M), ur'''~='''),
(re.compile(ur'''next;''',re.U|re.M), ur'''back()'''),
(re.compile(ur'''//''',re.U|re.M), ur'''--'''),
(re.compile(ur'''/\*''',re.U|re.M), ur'''--[['''),
(re.compile(ur'''\*/''',re.U|re.M), ur'''--]]'''),
(re.compile(ur'''stats \|= ([^;]+)''',re.U|re.M), ur'''stats = ((stat) or (\1))''')
# more regex pairs here
]
def splitIntoFiles(filePath):
"split the result into different files"
input = open(filePath,'rb')
s=unicode(input.read(),'utf-8')
input.close()
numRep = re.findall(r'''void ([^\(]+) \(\) {([^}]+)}''',s)
for couple in numRep:
print ' filename', couple[0]
print ' text',couple[1]
outtext = str(couple[1])
input = open(mydir+'/'+couple[0]+'.lua','w')
input.write(outtext)
input.truncate()
input.close()
def replaceStringInFile(filePath):
"""replaces all string by a regex substitution"""
backupName=filePath+'~re~'
print 'reading:', filePath
input = open(filePath,'rb')
s=unicode(input.read(),'utf-8')
input.close()
numRep=None
for couple in findreplace:
if numRep is not None:
numRep = re.search(couple[0],s)
outtext = re.sub(couple[0],couple[1], s)
s=outtext
if numRep:
print ' writing:', filePath
shutil.copy2(filePath,backupName)
outF = open(filePath,'r+b')
outF.read() # we do this way to preserve file creation date
outF.seek(0)
outF.write(s.encode('utf-8'))
outF.truncate()
outF.close()
def myfun(dummy, curdir, filess):
for child in filess:
if re.search(r'.+\.c$', child, re.U) and os.path.isfile(curdir + '/' + child):
replaceStringInFile(curdir+'/'+child)
splitIntoFiles(curdir+'/'+child)
os.path.walk(mydir, myfun, 3)