2022-02-03 11:39:16 +00:00
|
|
|
from flask import current_app
|
2021-09-15 10:31:53 +00:00
|
|
|
from flask_migrate import upgrade
|
2022-04-04 11:31:09 +00:00
|
|
|
import click
|
2022-02-03 11:39:16 +00:00
|
|
|
import os
|
2022-10-12 08:23:05 +00:00
|
|
|
from app.models import (
|
2023-02-23 12:05:04 +00:00
|
|
|
CorpusFollowerRole,
|
2022-10-12 08:23:05 +00:00
|
|
|
Role,
|
2023-02-23 12:05:04 +00:00
|
|
|
SpaCyNLPPipelineModel,
|
2022-10-13 13:05:54 +00:00
|
|
|
TesseractOCRPipelineModel,
|
2023-02-23 12:05:04 +00:00
|
|
|
User
|
2022-10-12 08:23:05 +00:00
|
|
|
)
|
2022-02-03 11:39:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2021-09-15 10:31:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def register(app):
|
|
|
|
@app.cli.command()
|
|
|
|
def deploy():
|
2021-12-07 13:18:05 +00:00
|
|
|
''' Run deployment tasks. '''
|
2022-02-03 11:39:16 +00:00
|
|
|
# Make default directories
|
2023-02-23 12:05:04 +00:00
|
|
|
print('Make default directories')
|
2022-02-03 11:39:16 +00:00
|
|
|
_make_default_dirs()
|
|
|
|
|
2021-09-15 10:31:53 +00:00
|
|
|
# migrate database to latest revision
|
2023-02-23 12:05:04 +00:00
|
|
|
print('Migrate database to latest revision')
|
2021-09-15 10:31:53 +00:00
|
|
|
upgrade()
|
2022-02-03 11:39:16 +00:00
|
|
|
|
|
|
|
# Insert/Update default database values
|
2023-02-23 12:05:04 +00:00
|
|
|
print('Insert/Update default Roles')
|
2022-02-03 11:39:16 +00:00
|
|
|
Role.insert_defaults()
|
2023-02-23 12:05:04 +00:00
|
|
|
print('Insert/Update default Users')
|
2022-02-03 11:39:16 +00:00
|
|
|
User.insert_defaults()
|
2023-02-23 12:05:04 +00:00
|
|
|
print('Insert/Update default CorpusFollowerRoles')
|
|
|
|
CorpusFollowerRole.insert_defaults()
|
|
|
|
print('Insert/Update default SpaCyNLPPipelineModels')
|
2022-10-13 13:05:54 +00:00
|
|
|
SpaCyNLPPipelineModel.insert_defaults()
|
2023-02-23 12:05:04 +00:00
|
|
|
print('Insert/Update default TesseractOCRPipelineModels')
|
2022-10-12 08:23:05 +00:00
|
|
|
TesseractOCRPipelineModel.insert_defaults()
|
2021-09-15 10:31:53 +00:00
|
|
|
|
2022-04-04 11:31:09 +00:00
|
|
|
@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)
|
|
|
|
|
2021-09-22 11:53:39 +00:00
|
|
|
@app.cli.group()
|
2021-09-15 10:31:53 +00:00
|
|
|
def test():
|
2021-12-07 13:18:05 +00:00
|
|
|
''' Test commands. '''
|
2021-09-22 11:53:39 +00:00
|
|
|
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)
|