forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefixer.py
More file actions
57 lines (43 loc) · 1.67 KB
/
prefixer.py
File metadata and controls
57 lines (43 loc) · 1.67 KB
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
#!/usr/bin/env python
#---------
# Script to change the prefix of a framework
#---------
import os
import re
import string
#------------------
# Config
#------------------
oldPrefix = "CP" # Set to empty string if there is no old prefix
newPrefix = "CPT"
# Root directories
origRootDir = "/Users/cormack/Desktop/CorePlotReprefix"
newRootDir = "/Users/cormack/Desktop/CorePlotReprefixNew"
#------------------
# Algorithm
#------------------
# Prefix strings in files in a single directory
def treatDirectory(arg, dirName, names):
dirRelativeToRoot = dirName[len(origRootDir):]
destDir = newRootDir + dirRelativeToRoot
print "Converting dir %s" % (dirName)
if not os.path.exists(destDir): os.mkdir(destDir)
filePrefixRegexString = "^(?P<type>_?)" + oldPrefix + "(?P<name>.+)$"
filePrefixRegex = re.compile(filePrefixRegexString)
contentPrefixRegex = re.compile("(?P<type>(k|_)?)" + oldPrefix + "(?P<name>\w+)")
fileSubString = "\g<type>" + newPrefix + "\g<name>"
contentSubString = "\g<type>" + newPrefix + "\g<name>"
for fileName in names:
fromPath = dirName + "/" + fileName
toPath = destDir + "/" + filePrefixRegex.sub(fileSubString, fileName)
if os.path.isfile(fromPath):
fromFile = open(fromPath, 'r')
toFile = open(toPath, 'w')
newFileContent = contentPrefixRegex.sub( contentSubString, fromFile.read() )
toFile.write(newFileContent)
fromFile.close()
toFile.close()
# Create new dir if necessary
if not os.path.exists(newRootDir): os.mkdir(newRootDir)
# Walk over all files in original directory
os.path.walk(origRootDir, treatDirectory, None)