-
Notifications
You must be signed in to change notification settings - Fork 0
/
aozora.py
executable file
·65 lines (51 loc) · 1.85 KB
/
aozora.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 python3
import os
import re
import argparse
import logging
from distutils.dir_util import copy_tree
DEFAULT_PROJECT_CONTENT = "app/"
def create_project_content(name: str, location: str):
location = os.path.join(location, name)
os.mkdir(location)
copy_tree(DEFAULT_PROJECT_CONTENT, location)
def edit_file(name: str, location: str, _from: str, _to: str):
file_name = os.path.join(location, name)
fake_file = file_name + "_"
with open(file_name, "r") as fr:
lines = fr.readlines()
with open(fake_file, "w") as fw:
for line in lines:
if _from in line:
line = line.replace(_from, _to)
fw.write(line)
os.remove(file_name)
os.rename(fake_file, file_name)
def customize_project_content(name: str, location: str):
edit_file(f"{name}/app/settings.py", location, r"{{SERVICE_NAME}}", name)
edit_file(f"{name}/serverless.yml", location, r"{{SERVICE_NAME}}", name)
edit_file(f"{name}/README.md", location, r"{{SERVICE_NAME}}", name)
def project_name(name):
if re.search(r"[^\w\-_\. ]", name) or " " in name:
logging.error(
"Invalid project name. Use only letters, numbers and dashes."
)
raise argparse.ArgumentTypeError
return name
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Aozora - Serverless Framework API Skeleton"
)
parser.add_argument(
"--name", required=True, type=project_name, help="API project name"
)
parser.add_argument(
"--location",
required=False,
type=str,
default=".",
help="Path where you want your project to be created. Default is '.'",
)
args = parser.parse_args()
create_project_content(args.name, args.location)
customize_project_content(args.name, args.location)