forked from viur-framework/viur-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean-base.py
executable file
·146 lines (121 loc) · 4.47 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
import argparse
import datetime
import getpass
import io
import os
import subprocess
import sys
import time
import zipfile
from urllib.request import urlopen
VI_VERSION = "3.0.31"
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 = f"Enter Author Name (leave empty to default to {whoami}): "
name = input(prompt)
if name:
whoami = name
app_id = os.path.split(os.getcwd())[-1]
prompt = f"Enter application-id (leave empty to default to {app_id}): "
name = input(prompt)
if name:
app_id = name
prompt = "Do you want to clean the git history? [Y/n] "
while (option := input(prompt).lower().strip()) not in {"", "y", "n"}:
print(f'Invalid option "{option}". "y", "n" or empty input expected.')
clean_history = option != "n"
time = time.time()
timestamp = datetime.datetime.fromtimestamp(time).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 os.path.exists(".git") and clean_history:
print("Clean 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("---")
# Install prebuilt Vi
sys.stdout.write("Downloading latest build of viur-vi...")
zip = urlopen(f"https://github.com/viur-framework/viur-vi/releases/download/v{VI_VERSION}/viur-vi.zip").read()
zip = zipfile.ZipFile(io.BytesIO(zip))
zip.extractall("deploy/vi")
zip.close()
print("Done")
# 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("# #")
print("# Then, run #")
print("# #")
print("# ./viur-gcloud-setup.sh #")
print("# #")
print("##############################################################")