From f1065b8150bdb7cc5e9c273a28ecf0f51da9c729 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Thu, 4 Jan 2024 18:59:51 +0000 Subject: [PATCH] Updated docker and script files to make them more robust --- dockerfile | 3 ++- scripts/create_database.py | 19 +++++++++++++++---- scripts/run.sh | 2 +- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/dockerfile b/dockerfile index d0ff9e5..df68b1d 100644 --- a/dockerfile +++ b/dockerfile @@ -3,4 +3,5 @@ COPY ./requirements.txt /app/requirements.txt WORKDIR /app RUN pip install -r requirements.txt COPY . /app -CMD ["scripts/run.sh"] \ No newline at end of file +RUN chmod +x scripts/run.sh +CMD ["sh", "scripts/run.sh"] \ No newline at end of file diff --git a/scripts/create_database.py b/scripts/create_database.py index eb3a44e..adcc2b9 100644 --- a/scripts/create_database.py +++ b/scripts/create_database.py @@ -1,13 +1,18 @@ import sqlite3 +import os -def create_connection(db_file): +def create_connection(path: str, filename: str): """ create a database connection to a SQLite database """ conn = None try: - print("Opening Database file and ensuring table integrity") - conn = sqlite3.connect(db_file) + # Make the directory for the file to go into + create_directory(path) + print("Opening Database file and ensuring table integrity") + conn = sqlite3.connect(path + filename) + + print("Database file open") # Execute creation scripts sql = open("scripts/create_tables.sql", "r"); conn.executescript(sql.read()) @@ -20,6 +25,12 @@ def create_connection(db_file): if conn: conn.close() +# Ensure a directory is created given a path to it +def create_directory(dir: str): + try: + os.makedirs(dir) + except FileExistsError: + pass if __name__ == '__main__': - create_connection(r"./data/wmgzon.db") + create_connection(r"./data/", r"wmgzon.db") diff --git a/scripts/run.sh b/scripts/run.sh index 5a50f62..e9019d0 100644 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -1,4 +1,4 @@ #! /bin/sh -python ./scripts/create_database.py +python ./scripts/create_database.py python ./app.py \ No newline at end of file