2024-03-07 14:49:04 +00:00
|
|
|
from flask import current_app
|
2023-05-15 10:00:13 +00:00
|
|
|
import shutil
|
2023-05-15 10:22:38 +00:00
|
|
|
from app import db
|
2024-03-07 14:49:04 +00:00
|
|
|
from app.models import Corpus, CorpusStatus
|
2023-05-15 10:00:13 +00:00
|
|
|
from . import bp
|
|
|
|
|
|
|
|
|
|
|
|
@bp.cli.command('reset')
|
|
|
|
def reset():
|
|
|
|
''' Reset built corpora. '''
|
|
|
|
status = [
|
|
|
|
CorpusStatus.QUEUED,
|
|
|
|
CorpusStatus.BUILDING,
|
|
|
|
CorpusStatus.BUILT,
|
|
|
|
CorpusStatus.STARTING_ANALYSIS_SESSION,
|
|
|
|
CorpusStatus.RUNNING_ANALYSIS_SESSION,
|
|
|
|
CorpusStatus.CANCELING_ANALYSIS_SESSION
|
|
|
|
]
|
|
|
|
for corpus in [x for x in Corpus.query.all() if x.status in status]:
|
2023-05-15 10:22:38 +00:00
|
|
|
print(f'Resetting corpus {corpus}')
|
2024-03-07 14:49:04 +00:00
|
|
|
corpus_cwb_dir = corpus.path / 'cwb'
|
|
|
|
corpus_cwb_data_dir = corpus_cwb_dir / 'data'
|
|
|
|
corpus_cwb_registry_dir = corpus_cwb_dir / 'registry'
|
|
|
|
try:
|
|
|
|
shutil.rmtree(corpus.path / 'cwb', ignore_errors=True)
|
|
|
|
corpus_cwb_dir.mkdir()
|
|
|
|
corpus_cwb_data_dir.mkdir()
|
|
|
|
corpus_cwb_registry_dir.mkdir()
|
|
|
|
except OSError as e:
|
|
|
|
current_app.logger.error(e)
|
|
|
|
raise
|
2023-05-15 10:22:38 +00:00
|
|
|
corpus.status = CorpusStatus.UNPREPARED
|
2023-05-15 10:00:13 +00:00
|
|
|
corpus.num_analysis_sessions = 0
|
2023-05-15 10:22:38 +00:00
|
|
|
db.session.commit()
|