-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.py
135 lines (114 loc) · 4.35 KB
/
run.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
import argparse
import os
import re
import shutil
import subprocess
import uuid
from pathlib import Path
import uvicorn
from cryptography.fernet import Fernet
is_cloud = os.getenv("CODESPACES") or os.getenv("GITPOD_WORKSPACE_ID")
def replace_uuids():
file_path = "app/config.py"
with open(file_path, "r") as file:
lines = file.readlines()
uuids = [
"0a856eb1-91ab-4f38-b757-23fbe1f73130",
"9cda34b1-af68-4dc6-b97c-e63ef6284671",
"4f342d85-3a49-41cb-90a5-37b1f2219040",
"34041f4f-9cb4-4830-afb5-db44b2a70e0e",
"70428e53-8113-421c-8fe2-9b74fcb94ee5",
]
with open(file_path, "w") as file:
for line in lines:
if "manifest_id" in line and any(uuid in line for uuid in uuids):
line = re.sub(
r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
str(uuid.uuid4()),
line,
)
file.write(line)
else:
file.write(line)
def create_dotenv():
if not Path(".env").exists():
shutil.copy(".env.template", ".env")
insert_secret_key()
else:
print("Didn't create an '.env' file as one already exists.")
def insert_secret_key():
secret_key = Fernet.generate_key().decode()
with open(".env", "r") as file:
lines = file.readlines()
with open(".env", "w") as file:
for line in lines:
if line.startswith("XLWINGS_SECRET_KEY="):
file.write(f'XLWINGS_SECRET_KEY="{secret_key}"\n')
else:
file.write(line)
def init():
replace_uuids()
create_dotenv()
print("Success! Now open the .env file and add a license key")
def deps_compile(upgrade=False):
# The order of how these files matters because they are all interdependent
file_names = ["requirements-core", "requirements", "requirements-dev"]
for file_name in file_names:
cmd_linux = f"uv pip compile {file_name}.in --universal -o {file_name}.txt --unsafe-package pywin32 --unsafe-package appscript --unsafe-package psutil {'--upgrade' if upgrade else ''}"
subprocess.run(cmd_linux, shell=True, check=True)
print(
f"Success! Requirements files {'upgraded' if upgrade else 'compiled'} successfully. Now commit the requirements.txt files!"
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="subcommand")
init_parser = subparsers.add_parser("init", help="Initialize the application.")
# Deps command
deps_parser = subparsers.add_parser("deps", help="Manage dependencies.")
deps_subparsers = deps_parser.add_subparsers(
dest="deps_command",
required=True,
help="Use either 'deps sync' or 'deps upgrade'",
)
# Sync subcommand under deps
compile_parser = deps_subparsers.add_parser(
"compile", help="Synchronize dependencies."
)
upgrade_parser = deps_subparsers.add_parser("upgrade", help="Upgrade dependencies.")
update_parser = deps_subparsers.add_parser("update", help="Upgrade dependencies.")
args = parser.parse_args()
if args.subcommand == "init":
init()
elif args.subcommand == "deps":
if args.deps_command == "compile":
deps_compile()
elif args.deps_command in ("upgrade", "update"):
deps_compile(upgrade=True)
else:
ssl_keyfile_path = Path("certs/localhost+2-key.pem")
ssl_certfile_path = Path("certs/localhost+2.pem")
ssl_keyfile = (
str(ssl_keyfile_path)
if ssl_keyfile_path.exists() and not is_cloud
else None
)
ssl_certfile = (
str(ssl_certfile_path)
if ssl_certfile_path.exists() and not is_cloud
else None
)
if (ssl_keyfile is None or ssl_certfile is None) and not is_cloud:
print(
"NO SSL KEYFILE OR CERTFILE FOUND. RUNNING ON HTTP, NOT HTTPS!.\n"
"THIS WILL ONLY WORK WITH VBA AND OFFICE SCRIPTS, BUT NOT WITH "
"OFFICE.JS ADD-INS!"
)
uvicorn.run(
"app.main:main_app",
host="127.0.0.1",
port=8000,
reload=True,
reload_includes=[".py", ".env"],
ssl_keyfile=ssl_keyfile,
ssl_certfile=ssl_certfile,
)