generated from potassco/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
executable file
·67 lines (54 loc) · 1.58 KB
/
init.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
#!/usr/bin/env python3
"""
Init script to rename project.
"""
import os
import re
def read(prompt, regex):
"""
Read a string from command line.
The string has to match the given regular expression.
"""
while True:
ret = input(prompt)
match = re.match(regex, ret)
if match is not None:
return ret
print(f"the project name has to match the regular expression: {regex}")
def main():
"""
Rename the project.
"""
project = read("project name: ", r"^[a-z][a-z0-9_]*$")
author = read("author: ", r".+")
email = read("email: ", r".+")
replacements = {
"fillname": project,
"<author-email>": email,
"<author>": author,
}
def replace(filepath):
with open(filepath, "r", encoding="utf-8") as hnd:
content = hnd.read()
for key, val in replacements.items():
content = content.replace(key, val)
with open(filepath, "w", encoding="utf-8") as hnd:
hnd.write(content)
for rootpath in ["fillname", "tests"]:
for dirpath, _, filenames in os.walk(rootpath):
for filename in filenames:
if not filename.endswith(".py"):
continue
filepath = os.path.join(dirpath, filename)
replace(filepath)
for filepath in [
"setup.cfg",
"noxfile.py",
"README.md",
"doc/index.rst",
".pre-commit-config.yaml",
]:
replace(filepath)
os.rename("fillname", project)
if __name__ == "__main__":
main()