-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
116 lines (98 loc) · 3.01 KB
/
main.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
# -*- coding: utf-8 -*-
from pathlib import Path
from sys import argv
from os import environ, system
from time import ctime
from subprocess import getoutput as output
from multiprocessing import Pool
processes = ("serve", "app")
installers = ("serve", "app")
root = Path(__file__).parent
app_home = root.joinpath("app")
envfile = root.joinpath(".env")
url = f'http://{output("hostname -I").split()[-1]}'
global dotenv, load_dotenv
try:
from dotenv import load_dotenv
except ModuleNotFoundError:
system("pip install python-dotenv")
from dotenv import load_dotenv
def getdate():
dt = ctime()
date = list(filter(lambda e: e != "", dt.split()))
t = date[3].split(":")
today = dict(
day=int(date[2]), month=date[1], year=int(date[4]),
hours=int(t[0]), minutes=int(t[1]), seconds=int(t[2])
)
for i in ("hours", "minutes", "seconds"):
s = str(today[i])
if len(s) == 1:
today[i] = f"0{s}"
return "{day}-{month}-{year}, {hours}:{minutes}:{seconds}".format(**today)
def getenv(key):
load_dotenv(envfile)
value = environ.get(key)
return value
def setenv(key, value, **data):
for line in envfile.open().readlines():
if line.endswith("\n"):
line = line[:-1]
x = line.split("=")
data[x[0]] = x[1]
data["last-update"] = getdate()
data[key] = value
envfile.open("w").write("\n".join([
f"{k}={v}" for k, v in data.items()
]))
load_dotenv(envfile)
def run(option):
system(f"bash make run-{option}")
def run_option():
cfg = root.joinpath("_data/config-dev.yml").open().read()
cfg = cfg.replace("<URL>", url)
root.joinpath("_config.yml").open("w").write(cfg)
system(f'cd {str(root.joinpath("assets/portfolio/resume"))} && python3 build.py')
system("clear")
with Pool(len(processes)) as p:
try:
p.map(run, processes)
except KeyboardInterrupt:
print("Shutdown ...")
def install_option():
system("clear")
system(f"bash install app")
system(f"bash install serve")
def read_input():
opt, args = argv[1] if argv[1:] else "", argv[2:]
if not opt:
opt = input("Input option not given. Write option: ")
if opt == "get":
if args:
for arg in args:
getenv(arg)
else:
print("key argument not given")
elif opt == "set":
if len(args) % 2 == 0:
for i in range(len(args) - 1):
setenv(args[i], args[i + 1])
else:
print("key-value data invalid")
elif opt == "run":
run_option()
elif opt == "update":
setenv("allow-push", "true")
system("bash push")
elif opt == "install":
install_option()
else:
print("Invalid option")
if __name__ == "__main__":
if getenv("allow-push"):
root.joinpath("_config.yml").open("w").write(
root.joinpath(
"_data/config-production.yml"
).open().read()
)
read_input()