-
Notifications
You must be signed in to change notification settings - Fork 12
/
clean-base.py
executable file
·130 lines (106 loc) · 4.1 KB
/
clean-base.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
#!/usr/bin/env python3
import argparse
import datetime
import getpass
import os
import subprocess
import sys
try:
whoami = getpass.getuser()
except Exception:
whoami = "viur"
ap = argparse.ArgumentParser(
description="Setting up a clean ViUR project base.",
epilog="The script runs interactively if not command-line arguments are passed."
)
ap.add_argument(
"-A",
"--app_id",
type=str,
help="The application-id that should be replaced in the arbitrary places."
)
ap.add_argument(
"-a",
"--author",
type=str,
default=whoami,
help="The author's name that is placed in arbitrary places."
)
ap.add_argument(
"-c",
"--clean-history",
action="store_true",
default=True,
help="Clean the git history."
)
args = ap.parse_args()
app_id = args.app_id
whoami = args.author
clean_history = args.clean_history
update = False # this might be changed by command-line flag later on
if args.app_id is None:
prompt = input(f"Enter author name (leave empty to default to {whoami}): ")
if prompt:
whoami = prompt
app_id = os.path.split(os.getcwd())[-1]
prompt = input(f"Enter application-id (leave empty to default to {app_id}): ")
if prompt:
app_id = prompt
while (prompt := input("Do you want to clean the git history? [Y/n] ").lower().strip()) not in {"", "y", "n"}:
print(f'Invalid option "{prompt}". "y", "n" or empty input expected.')
clean_history = prompt != "n"
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
workdir = os.getcwd() + "/deploy"
file_list = ["viur-project.md"]
replacements = {"{{app_id}}": app_id, "{{whoami}}": whoami, "{{timestamp}}": timestamp}
# Build file list in which to search for placeholders to replace
for subdir, dirs, files in os.walk("."):
for file in files:
filepath = subdir + os.sep + file
if any([filepath.endswith(ext) for ext in [".py", ".yaml", ".html", ".md", ".sh", ".json", ".js", ".less"]]):
file_list.append(filepath)
# Replace placeholders with values entered by user or defaults
for file_obj in file_list:
lines = []
with open(file_obj) as infile:
for line in infile:
for src, target in replacements.items():
line = line.replace(src, target)
lines.append(line)
with open(file_obj, "w") as outfile:
for line in lines:
outfile.write(line)
# Update submodules
if clean_history and os.path.exists(".git"):
print("Cleaning git history")
subprocess.check_output("git checkout --orphan main_tmp", shell=True)
print(subprocess.check_output("git branch -D main", shell=True).decode().rstrip("\n"))
subprocess.check_output("git branch -m main", shell=True)
print(
"Current branch is:",
subprocess.check_output("git branch --show-current", shell=True)
.decode().rstrip("\n")
)
print("---")
# Generate files
sys.stdout.write("Generating project documentation...")
sys.stdout.flush()
# Create a README.md
os.remove("README.md") # this is needed because on windows os.rename will fail caused by existing dest!!!
os.rename("viur-project.md", "README.md")
# Remove yourself!
os.remove(sys.argv[0])
print("Done")
print("##############################################################")
print("# Well done! Project repository has been set-up now. #")
print("# #")
print("# Next run #")
print("# #")
print("# pipenv install --dev #")
print("# pipenv run viur build release #")
print("# #")
print("# Afterwards, run #")
print("# #")
print("# ./viur-gcloud-setup.sh #")
print("# #")
print("##############################################################")