Skip to content

Commit

Permalink
Script that does first-time setup
Browse files Browse the repository at this point in the history
  • Loading branch information
stijn-uva committed Sep 29, 2021
1 parent 503b961 commit 33fab04
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
15 changes: 15 additions & 0 deletions 4cat-daemon.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import subprocess
import argparse
import time
import sys
Expand All @@ -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
# ---------------------------------------------
Expand Down
9 changes: 0 additions & 9 deletions docker/docker_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
42 changes: 42 additions & 0 deletions helper-scripts/first-run.py
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions webtool/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 33fab04

Please sign in to comment.