From 33fab04c31ad5c287897aeb260a2db455b0be78b Mon Sep 17 00:00:00 2001 From: Stijn Peeters Date: Wed, 29 Sep 2021 15:48:43 +0200 Subject: [PATCH] Script that does first-time setup --- 4cat-daemon.py | 15 +++++++++++++ docker/docker_setup.py | 9 -------- helper-scripts/first-run.py | 42 +++++++++++++++++++++++++++++++++++++ webtool/__init__.py | 15 +++++++++++++ 4 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 helper-scripts/first-run.py diff --git a/4cat-daemon.py b/4cat-daemon.py index 44ea10c4f..4464f77a3 100644 --- a/4cat-daemon.py +++ b/4cat-daemon.py @@ -1,3 +1,4 @@ +import subprocess import argparse import time import sys @@ -18,6 +19,20 @@ cli.add_argument("command") args = cli.parse_args() +# --------------------------------------------- +# first-run.py ensures everything is set up +# right when running 4CAT for the first time +# --------------------------------------------- +first_run = Path(__file__).parent.joinpath("helper-scripts", "first-run.py") +result = subprocess.run([sys.executable, str(first_run)], stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + +if result.returncode != 0: + print("Unexpected error while preparing 4CAT. You may need to re-install 4CAT.") + print("stdout:\n".join([" " + line for line in result.stdout.decode("utf-8").split("\n")])) + print("stderr:\n".join([" " + line for line in result.stderr.decode("utf-8").split("\n")])) + exit(1) + # --------------------------------------------- # Do not start if migration is required # --------------------------------------------- diff --git a/docker/docker_setup.py b/docker/docker_setup.py index 91f8a0235..392cd4266 100644 --- a/docker/docker_setup.py +++ b/docker/docker_setup.py @@ -30,15 +30,6 @@ # Install specific packages if docker_config['DOCKER'].getboolean('new_installation'): - # NLTK resources - import nltk - nltk.download("wordnet") - nltk.download("punkt") - - # Spacy lexicon - import spacy - spacy.cli.download('en_core_web_sm') - # Update config to skip this on future runs docker_config['DOCKER']['new_installation'] = 'False' diff --git a/helper-scripts/first-run.py b/helper-scripts/first-run.py new file mode 100644 index 000000000..ccf3d3dda --- /dev/null +++ b/helper-scripts/first-run.py @@ -0,0 +1,42 @@ +""" +Actions to execute the first time a 4CAT instance is run + +There are some things that should be done once, and only once, in the life of +a 4CAT instance. These are set-up actions that cannot be handled by e.g. +running pip or setup.py because they rely on things not available to those +processes or that could interfere with other processes run at that time. + +This file is only fully executed if the file .current-version does NOT exist +in the 4CAT root folder. The file is created if it does not exist yet, ensuring +the set-up actions in this file are only ever run once. + +It should not be necessary to run this file directly; it is run automatically +by 4CAT while starting up. +""" +import shutil +import sys +from pathlib import Path + +# make sure version files are in order +version_file = Path(__file__).resolve().parent.parent.joinpath("VERSION") +if not version_file.exists(): + # this file should ALWAYS exist, because it is part of the repository, and + # required by other parts of 4CAT. If it's absent, something has gone + # wrong, and the preferred course of action is restarting from scratch + print("VERSION file not found. You should re-install 4CAT before continuing.", file=sys.stderr) + exit(1) + +current_version_file = version_file.with_name(".current-version") +if current_version_file.exists(): + # this file does not exist by default, so if it does, that means we don't + # need to do further on-boarding since 4CAT has already been run + exit(0) + +shutil.copy(version_file, current_version_file) + +# Now check for presence of required NLTK packages +import nltk +nltk_downloads = ("wordnet", "punkt") +for package in nltk_downloads: + # if it already exists, .download() will just NOP + nltk.download(package) \ No newline at end of file diff --git a/webtool/__init__.py b/webtool/__init__.py index b6b4ebdbb..deaa4da54 100644 --- a/webtool/__init__.py +++ b/webtool/__init__.py @@ -1,3 +1,18 @@ +import subprocess +import sys +from pathlib import Path + +# first-run.py ensures everything is set up right when running 4CAT for the first time +first_run = Path(__file__).parent.parent.joinpath("helper-scripts", "first-run.py") +result = subprocess.run([sys.executable, str(first_run)], stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + +if result.returncode != 0: + print("Unexpected error while preparing 4CAT. You may need to re-install 4CAT.") + print("stdout:\n".join([" " + line for line in result.stdout.decode("utf-8").split("\n")])) + print("stderr:\n".join([" " + line for line in result.stderr.decode("utf-8").split("\n")])) + exit(1) + from flask import Flask from flask_login import LoginManager from flask_limiter import Limiter