|
1 | | -import collections |
2 | | -import logging |
3 | | -import json |
4 | | -import os |
5 | | -import pathlib |
6 | | -import re |
7 | | -import subprocess |
8 | | -import sys |
9 | | - |
10 | | -from flask import Flask |
11 | | -from distutils.sysconfig import get_python_lib |
12 | 1 |
|
13 | 2 | __version__ = "4.1.0" |
14 | | - |
15 | | -app = Flask(__name__, template_folder="server/templates") |
16 | | -app.config.setdefault("SQLALCHEMY_DATABASE_URI", "sqlite:///:memory:") |
17 | | -app.config.setdefault("SQLALCHEMY_BINDS", None) |
18 | | -app.config.setdefault("SQLALCHEMY_TRACK_MODIFICATIONS", False) |
19 | | - |
20 | | -# Keep track of microSALT installation |
21 | | -wd = os.path.dirname(os.path.realpath(__file__)) |
22 | | - |
23 | | -# Load configuration |
24 | | -preset_config = "" |
25 | | -logger = "" |
26 | | -default = os.path.join(os.environ["HOME"], ".microSALT/config.json") |
27 | | - |
28 | | -if "MICROSALT_CONFIG" in os.environ: |
29 | | - try: |
30 | | - envvar = os.environ["MICROSALT_CONFIG"] |
31 | | - with open(envvar, "r") as conf: |
32 | | - preset_config = json.load(conf) |
33 | | - except Exception as e: |
34 | | - print("Config error: {}".format(str(e))) |
35 | | - pass |
36 | | -elif os.path.exists(default): |
37 | | - try: |
38 | | - with open(os.path.abspath(default), "r") as conf: |
39 | | - preset_config = json.load(conf) |
40 | | - except Exception as e: |
41 | | - print("Config error: {}".format(str(e))) |
42 | | - pass |
43 | | - |
44 | | -# Config dependent section: |
45 | | -if preset_config != "": |
46 | | - try: |
47 | | - # Load flask info |
48 | | - app.config.update(preset_config["database"]) |
49 | | - |
50 | | - # Add `folders` configuration |
51 | | - app.config["folders"] = preset_config.get("folders", {}) |
52 | | - |
53 | | - # Ensure PubMLST configuration is included |
54 | | - |
55 | | - app.config["pubmlst"] = preset_config.get("pubmlst", { |
56 | | - "client_id": "", |
57 | | - "client_secret": "" |
58 | | - }) |
59 | | - |
60 | | - app.config["pubmlst"] = preset_config.get("pubmlst", {"client_id": "", "client_secret": ""}) |
61 | | - |
62 | | - |
63 | | - # Add extrapaths to config |
64 | | - preset_config["folders"]["expec"] = os.path.abspath( |
65 | | - os.path.join(pathlib.Path(__file__).parent.parent, "unique_references/ExPEC.fsa") |
66 | | - ) |
67 | | - # Check if release install exists |
68 | | - for entry in os.listdir(get_python_lib()): |
69 | | - if "microSALT-" in entry: |
70 | | - preset_config["folders"]["expec"] = os.path.abspath( |
71 | | - os.path.join(os.path.expandvars("$CONDA_PREFIX"), "expec/ExPEC.fsa") |
72 | | - ) |
73 | | - break |
74 | | - preset_config["folders"]["adapters"] = os.path.abspath( |
75 | | - os.path.join( |
76 | | - os.path.expandvars("$CONDA_PREFIX"), |
77 | | - "share/trimmomatic/adapters/", |
78 | | - ) |
79 | | - ) |
80 | | - |
81 | | - # Initialize logger |
82 | | - logger = logging.getLogger("main_logger") |
83 | | - logger.setLevel(logging.INFO) |
84 | | - ch = logging.StreamHandler() |
85 | | - ch.setLevel(logging.INFO) |
86 | | - ch.setFormatter(logging.Formatter("%(levelname)s - %(message)s")) |
87 | | - logger.addHandler(ch) |
88 | | - |
89 | | - # Create paths mentioned in config |
90 | | - db_file = re.search( |
91 | | - "sqlite:///(.+)", |
92 | | - preset_config["database"]["SQLALCHEMY_DATABASE_URI"], |
93 | | - ).group(1) |
94 | | - for entry in preset_config.keys(): |
95 | | - if entry != "_comment": |
96 | | - if ( |
97 | | - isinstance(preset_config[entry], str) |
98 | | - and "/" in preset_config[entry] |
99 | | - and entry not in ["genologics"] |
100 | | - ): |
101 | | - if not preset_config[entry].startswith("/"): |
102 | | - sys.exit(-1) |
103 | | - unmade_fldr = os.path.abspath(preset_config[entry]) |
104 | | - if not pathlib.Path(unmade_fldr).exists(): |
105 | | - os.makedirs(unmade_fldr) |
106 | | - logger.info("Created path {}".format(unmade_fldr)) |
107 | | - |
108 | | - # level two |
109 | | - elif isinstance(preset_config[entry], collections.Mapping): |
110 | | - for thing in preset_config[entry].keys(): |
111 | | - if ( |
112 | | - isinstance(preset_config[entry][thing], str) |
113 | | - and "/" in preset_config[entry][thing] |
114 | | - and entry not in ["genologics"] |
115 | | - ): |
116 | | - # Special string, mangling |
117 | | - if thing == "log_file": |
118 | | - unmade_fldr = os.path.dirname(preset_config[entry][thing]) |
119 | | - bash_cmd = "touch {}".format(preset_config[entry][thing]) |
120 | | - proc = subprocess.Popen(bash_cmd.split(), stdout=subprocess.PIPE) |
121 | | - output, error = proc.communicate() |
122 | | - elif thing == "SQLALCHEMY_DATABASE_URI": |
123 | | - unmade_fldr = os.path.dirname(db_file) |
124 | | - bash_cmd = "touch {}".format(db_file) |
125 | | - proc = subprocess.Popen(bash_cmd.split(), stdout=subprocess.PIPE) |
126 | | - output, error = proc.communicate() |
127 | | - if proc.returncode != 0: |
128 | | - logger.error( |
129 | | - "Database writing failed! Invalid user access detected!" |
130 | | - ) |
131 | | - sys.exit(-1) |
132 | | - else: |
133 | | - unmade_fldr = preset_config[entry][thing] |
134 | | - if not pathlib.Path(unmade_fldr).exists(): |
135 | | - os.makedirs(unmade_fldr) |
136 | | - logger.info("Created path {}".format(unmade_fldr)) |
137 | | - |
138 | | - fh = logging.FileHandler(os.path.expanduser(preset_config["folders"]["log_file"])) |
139 | | - fh.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")) |
140 | | - logger.addHandler(fh) |
141 | | - |
142 | | - # Integrity check database |
143 | | - cmd = "sqlite3 {0}".format(db_file) |
144 | | - cmd = cmd.split() |
145 | | - cmd.append("pragma integrity_check;") |
146 | | - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
147 | | - output, error = proc.communicate() |
148 | | - if not "ok" in str(output): |
149 | | - logger.error("Database integrity failed! Lock-state detected!") |
150 | | - sys.exit(-1) |
151 | | - |
152 | | - except Exception as e: |
153 | | - print("Config error: {}".format(str(e))) |
154 | | - pass |
0 commit comments