~aleteoryx/muditaos

ref: 7fc6627759b0de8b576bb15ddc6b31c70f3617b7 muditaos/tools/init_databases.py -rwxr-xr-x 2.8 KiB
7fc66277 — Lefucjusz [MOS-783] Fix crash on entering SAR menu 3 years ago
                                                                                
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
#!/usr/bin/python3
# Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

# import required module
import os
import sqlite3
import argparse
import logging
import sys

log = logging.getLogger(__name__)
logging.basicConfig(format='%(asctime)s [%(levelname)s]: %(message)s', level=logging.INFO)


# this helper script creates DBs from SQL schema files

def initialize_database(script_path: os.path, dst_directory: os.path) -> int:
    file_name = os.path.basename(script_path)
    connection = None

    db_name = file_name.split("_0")[0]
    db_name = f"{db_name}.db"
    dst_db_path = os.path.join(dst_directory, db_name)
    log.debug(f"Executing {script_path} script into {dst_db_path} database")

    ret = 0
    try:
        connection = sqlite3.connect(dst_db_path)
        with open(script_path) as fp:
            connection.executescript(fp.read())
        connection.commit()
    except OSError as e:
        log.error(f"System error: {e}")
        ret = 1
    except sqlite3.Error as e:
        log.error(f"[SQLite] {db_name} database error: {e}")
        ret = 1
    finally:
        if connection:
            connection.close()

    return ret


def main() -> int:
    parser = argparse.ArgumentParser(description='Create databases from schema scripts')
    parser.add_argument('--input_path',
                        metavar='schema_path',
                        type=str,
                        help='path to schema scripts',
                        required=True)

    parser.add_argument('--output_path',
                        metavar='db_path',
                        type=str,
                        help='destination path for databases',
                        required=True)

    parser.add_argument('--development',
                        metavar='devel',
                        type=bool,
                        help='with development schema scripts',
                        default=False)

    args = parser.parse_args()

    db_script_files = [
        os.path.join(args.input_path, file)
        for file in os.listdir(args.input_path)
        if os.path.isfile(os.path.join(args.input_path, file)) and ".sql" in file
    ]
    db_script_devel = [file for file in db_script_files if "devel" in file]
    db_script_no_devel = list(set(db_script_files) - set(db_script_devel))

    db_script_devel.sort()
    db_script_no_devel.sort()

    if not os.path.exists(args.output_path):
        os.makedirs(args.output_path, exist_ok=True)

    ret = 0

    for script in db_script_no_devel:
        ret |= initialize_database(script, args.output_path)

    if args.development:
        for script in db_script_devel:
            ret |= initialize_database(script, args.output_path)

    return ret


if __name__ == "__main__":
    sys.exit(main())