-
Notifications
You must be signed in to change notification settings - Fork 3
/
personalize.py
executable file
·135 lines (97 loc) · 3.09 KB
/
personalize.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
"""
Creates a personally named version of plomobile.
1. Copy to a new folder (assume we are in buildout/src)
1. Do in-place template string replacement of template strings
2. Walk through all files/folders and rename them
sed/awk would be nice but let's stick in Python because all other code is Python,
and we might need to take care of poor Windoze users :(
"""
import sys
import shutil
import os
import fnmatch
TEMPLATE_NAME = "plomobile"
IGNORE_MASKS = ["*.pyc", "*.pyo", "*.git*", "*.egg*", "*.EGG*"]
FILES_TO_DELETE = [
".git",
"README.rst",
"plomobile.egg-info",
"plomobile/templates/helloworld.pt",
"plomobile/templates/myfooter.pt",
"plomobile/templates/plone.app.layout.viewlets.logo.pt",
"plomobile/static/pony.png"
]
def process(fname, newname):
""" """
# See if we don't want to touch this file
for mask in IGNORE_MASKS:
if fnmatch.fnmatch(fname, mask):
return
# Do in-place replacement of template strings,
# all one of them.
# Because we are workin on a copy, don't be
# that pick about atomicity
if not os.path.isdir(fname):
# Read the source file
f = open(fname, "rt")
data = f.read()
f.close()
# Replace template variables
data = data.replace(TEMPLATE_NAME, newname)
new_lines = []
filtering = False
for line in data.split("\n"):
filtering = False
# Write the output
data = "\n".join(new_lines)
f = open(fname, "wt")
f.write(data)
f.close()
# pylint: disable=
path, filepart = os.path.split(fname)
if filepart == TEMPLATE_NAME:
# Rename plomobile folders to something else
newname = os.path.join(path, newname)
shutil.move(fname, newname)
# pylint: disable=W0613
def post_cleanup(target, newname):
"""
Remove unneeded files.
"""
for f in FILES_TO_DELETE:
fname = os.path.join(target, f)
if os.path.exists(fname):
if os.path.isdir(fname):
shutil.rmtree(fname)
else:
os.remove(fname)
def fancy_replace(newname):
""" """
source = os.getcwd()
target = os.path.join(os.getcwd(), "..", newname)
if os.path.exists(target):
print "Already exists:" + target
print "Plese remove first"
sys.exit(1)
# Create a copy of the skeleton
shutil.copytree(source, target)
post_cleanup(target, newname)
# Replace strings and filenames
for root, dirs, files in os.walk(target, topdown=False):
for name in files:
fname = os.path.join(root, name)
process(fname, newname)
for name in dirs:
fname = os.path.join(root, name)
process(fname, newname)
print "Created ../" + newname
def main():
""" """
if len(sys.argv) < 2:
print "Usage: ./personalize.py yourfancyname"
print "The name must contain only lowercase a-z"
sys.exit(1)
fancy_replace(sys.argv[1])
if __name__ == "__main__":
main()