nopaque/app/cli.py

83 lines
2.3 KiB
Python
Raw Normal View History

from flask import current_app
from flask_migrate import upgrade
from . import db
from .models import Corpus, Job, Role, User, TesseractOCRModel
import click
import os
def _make_default_dirs():
base_dir = current_app.config['NOPAQUE_DATA_DIR']
default_directories = [
os.path.join(base_dir, 'tmp'),
os.path.join(base_dir, 'users')
]
for directory in default_directories:
if os.path.exists(directory):
if not os.path.isdir(directory):
raise NotADirectoryError(f'{directory} is not a directory')
else:
os.mkdir(directory)
def register(app):
@app.cli.command()
def deploy():
2021-12-07 13:18:05 +00:00
''' Run deployment tasks. '''
# Make default directories
_make_default_dirs()
# migrate database to latest revision
upgrade()
# Insert/Update default database values
current_app.logger.info('Insert/Update default roles')
Role.insert_defaults()
current_app.logger.info('Insert/Update default users')
User.insert_defaults()
current_app.logger.info('Insert/Update default tesseract ocr models')
TesseractOCRModel.insert_defaults()
@app.cli.group()
def daemon():
2021-12-07 13:18:05 +00:00
''' Daemon commands. '''
pass
2021-09-22 12:13:59 +00:00
@daemon.command('run')
2021-09-22 11:58:46 +00:00
def run_daemon():
2021-12-07 13:18:05 +00:00
''' Run daemon '''
corpus: Corpus
2021-11-16 14:23:57 +00:00
for corpus in Corpus.query.filter(Corpus.num_analysis_sessions > 0):
corpus.num_analysis_sessions = 0
db.session.commit()
from app.daemon import Daemon
2021-12-07 13:18:05 +00:00
daemon: Daemon = Daemon()
daemon.run()
@app.cli.group()
def converter():
''' Converter commands. '''
pass
@converter.command()
@click.argument('json_db')
@click.argument('data_dir')
def sandpaper(json_db, data_dir):
''' Sandpaper converter '''
from app.converters.sandpaper import convert
convert(json_db, data_dir)
@app.cli.group()
def test():
2021-12-07 13:18:05 +00:00
''' Test commands. '''
pass
2021-09-22 12:13:59 +00:00
@test.command('run')
2021-09-22 11:58:46 +00:00
def run_test():
2021-12-07 13:18:05 +00:00
''' Run unit tests. '''
from unittest import TestLoader, TextTestRunner
from unittest.suite import TestSuite
tests: TestSuite = TestLoader().discover('tests')
TextTestRunner(verbosity=2).run(tests)