-
Notifications
You must be signed in to change notification settings - Fork 1
/
preflight_checklist.py
executable file
·88 lines (68 loc) · 2.95 KB
/
preflight_checklist.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
import sys
import subprocess
import os
import time
import webbrowser
from dotenv import load_dotenv, find_dotenv
from datagator.config import get_DataGator_DataBaseURI
from pathlib import Path
# NOTE! This is the setup script to ensure all environment are properly configured and ready to og.
# Run this before anything.
path_module = Path(os.path.dirname(os.path.realpath(__file__)))
# Some preliminary work to automaticly source the binaries. Not working yet. @todo: testing in Win and Linux, for subprocesses.
sys.path.append(f"{path_module}/BinDependency/dcm2nii")
sys.path.append(f"{path_module}/BinDependency/dcm2niix")
sys.path.append(f"{path_module}/BinDependency/dcmtoolkit")
sys.path.append(f"{path_module}/datagator")
sys.path.append(f"{path_module}/PythonUtils")
sys.path.append(f"{path_module}/DICOMTransit/")
load_dotenv(find_dotenv())
# Path of the database URL is obtained from the environment or using the default string.
SQLALCHEMY_DATABASE_URI = get_DataGator_DataBaseURI()
os.chdir("datagator")
# Check datagator .env exist, if not exist, creates it.
path_datagator_env = path_module / "datagator" / ".env"
if not path_datagator_env.exists():
print("DataGator .env does not appear to exist. Create empty place holder.")
path_datagator_env.touch()
# Check dictomtransit .env exist.
path_dicomtransit_env = path_module / ".env"
if not path_dicomtransit_env.exists():
print("DICOMTransit .env does not appear to exist. Create empty place holder.")
path_dicomtransit_env.touch()
with open(path_dicomtransit_env, "w") as file_env:
file_env.write(
f"datagator_database={path_module / 'LocalDB' / 'dtconfigure.sqlite'}\nconfig_table=configuration"
)
print(
"DICOMTransit .env updated with the default relative 'LocalDB' folder path, and configuration table."
)
os.environ["FLASK_APP"] = "index.py"
os.environ["FLASK_ENV"] = "development"
# Check production and development
# import redcap.production
# import redcap.development
# Creat the local configuration database if it hasn't already exist.
if not os.path.exists(SQLALCHEMY_DATABASE_URI):
print(f"Flask database path: {SQLALCHEMY_DATABASE_URI}")
try:
subprocess.check_output(["flask", "db", "upgrade"])
except Exception as e:
raise ValueError("Could not initialize and update the local database!")
# fixme: gotta be platform independent. Need CentOS validation and Ubuntu.
DETACHED_PROCESS = 0x00000008
try:
if sys.platform == "win32":
pid = subprocess.Popen(
["flask", "run"], creationflags=DETACHED_PROCESS
).pid # @todo: wrap this into a starter function to run at will.
else:
# linux might need
# sudo lsof -t -i tcp:5000 | xargs kill -9
# to kill
pid = subprocess.Popen(["flask", "run"]).pid
except Exception as e:
raise ValueError
# Wait 5s before opening webbrower.
time.sleep(5)
webbrowser.open("http://127.0.0.1:5000/") # Open the webpage