-
Notifications
You must be signed in to change notification settings - Fork 1
/
rename.py
executable file
·65 lines (54 loc) · 2.14 KB
/
rename.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
#!/usr/bin/env python
from os import rename, walk
from os.path import *
from sys import argv
ignoreFiles = ["rename.py", "turtle.jpg", "__pycache__"]
def change_content(dirname, names):
# ignore some dirs
if basename(dirname) in ignoreFiles:
return
for name in names:
# ignore some files
if name in ignoreFiles:
continue
full_path = join(dirname, name)
if isfile(full_path):
print("Adjusting content in %s" % full_path)
content = open(full_path, "r").read()
content = content.replace(change_from.capitalize(), change_to.capitalize())
content = content.replace(change_from.upper(), change_to.upper())
content = content.replace(change_from, change_to)
open(full_path, "w").write(content)
elif isdir(full_path):
for baseDir, dirName, fileNames in walk(full_path):
change_content(baseDir, fileNames)
def rename_files(dirname, names):
# ignore some dirs
if basename(dirname) in ignoreFiles:
return
# rename dir
if basename(dirname).__eq__(change_from):
newdirname = dirname.replace(change_from, change_to)
rename(dirname, newdirname)
for baseDir, dirName, fileNames in walk(newdirname):
rename_files(baseDir, fileNames)
return
for name in names:
# ignore some files
if name in ignoreFiles:
continue
full_path = join(dirname, name)
if isdir(full_path):
for baseDir, dirName, fileNames in walk(full_path):
rename_files(baseDir, fileNames)
if name.find(change_from) != -1:
new_name = name.replace(change_from, change_to)
print("Renaming %s to %s" % (full_path, join(dirname, new_name)))
rename(full_path, join(dirname, new_name))
change_from = 'skeleton'
change_to = argv[-1].lower()
print("Changing name of toolkit project to '%s'." % change_to)
for directory, dirnames, filenames in walk("."):
change_content(directory, filenames)
for directory, dirnames, filenames in walk("."):
rename_files(directory, filenames)