Skip to content

Commit

Permalink
Merge pull request #1637 from UlrichB22/cli_checks
Browse files Browse the repository at this point in the history
Add wiki index checks in CLI.
  • Loading branch information
RogerHaase authored Mar 9, 2024
2 parents 0fa0a5c + 83a154d commit 1847c89
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
21 changes: 14 additions & 7 deletions src/moin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright: 2002-2011 MoinMoin:ThomasWaldmann
# Copyright: 2008 MoinMoin:FlorianKrupicka
# Copyright: 2010 MoinMoin:DiogenesAugusto
# Copyright: 2023 MoinMoin project
# Copyright: 2023-2024 MoinMoin:UlrichB
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Expand All @@ -25,6 +25,7 @@
from flask_theme import setup_themes

from jinja2 import ChoiceLoader, FileSystemLoader
from whoosh.index import EmptyIndexError

from moin.utils import monkeypatch # noqa
from moin.utils.clock import Clock
Expand Down Expand Up @@ -158,7 +159,17 @@ class ItemNameConverter(PathConverter):
clock.stop('create_app flask-cache')
# init storage
clock.start('create_app init backends')
init_backends(app)
try:
init_backends(app)
except EmptyIndexError:
# create-instance has no index at start and index-* subcommands check the index individually
if (info_name not in ['create-instance', 'build-instance', ]
and not info_name.startswith('index-')):
clock.stop('create_app init backends')
clock.stop('create_app total')
logging.error("Error: Wiki index not found. Try 'moin help' or 'moin --help' to get further information.")
raise SystemExit(1)
logging.debug("Wiki index not found.")
clock.stop('create_app init backends')
clock.start('create_app flask-babel')
i18n_init(app)
Expand Down Expand Up @@ -190,8 +201,6 @@ def init_backends(app, create_backend=False):
# mountpoint, unprotected backend
# Just initialize with unprotected backends.
logging.debug("running init_backends")
c = get_current_context(silent=True)
info_name = getattr(c, 'info_name', '')
app.router = routing.Backend(app.cfg.namespace_mapping, app.cfg.backend_mapping)
if create_backend or getattr(app.cfg, 'create_backend', False):
app.router.create()
Expand All @@ -203,9 +212,7 @@ def init_backends(app, create_backend=False):
logging.debug("create_backend: %s ", str(create_backend))
if create_backend or getattr(app.cfg, 'create_backend', False): # 2. call of init_backends
app.storage.create()
app.storage.open()
if info_name not in ['create-instance', 'build-instance', 'index-create']:
app.storage.open()
app.storage.open()


def deinit_backends(app):
Expand Down
18 changes: 10 additions & 8 deletions src/moin/cli/maint/create_instance.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright: 2020 MoinMoin:RogerHaase
# Copyright: 2023-2024 MoinMoin project
# Copyright: 2023-2024 MoinMoin:UlrichB
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Expand Down Expand Up @@ -99,10 +99,12 @@ def cli_BuildInstance():
'''
logging.info('Build Instance started.')
logging.debug('CWD: %s', os.getcwd())
index.IndexCreate()
modify_item.LoadHelp(namespace='help-en', path_to_help=None)
modify_item.LoadHelp(namespace='help-common', path_to_help=None)
modify_item.LoadWelcome()
index.IndexOptimize(tmp=False)
logging.info('Full instance setup finished.')
logging.info('You can now use "moin run" to start the builtin server.')
if index.IndexCreate():
modify_item.LoadHelp(namespace='help-en', path_to_help=None)
modify_item.LoadHelp(namespace='help-common', path_to_help=None)
modify_item.LoadWelcome()
index.IndexOptimize(tmp=False)
logging.info('Full instance setup finished.')
logging.info('You can now use "moin run" to start the builtin server.')
else:
logging.error('Build Instance failed.')
25 changes: 23 additions & 2 deletions src/moin/cli/maint/index.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright: 2011 MoinMoin:MichaelMayorov
# Copyright: 2023 MoinMoin project
# Copyright: 2023-2024 MoinMoin:UlrichB
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Expand All @@ -14,11 +14,14 @@

from moin.app import create_app, init_backends
from moin.constants.keys import LATEST_REVS, ALL_REVS
from moin.utils.filesys import wiki_index_exists


from moin import log

logging = log.getLogger(__name__)

ERR_NO_INDEX = "Error: Wiki index does not exist."

@click.group(cls=FlaskGroup, create_app=create_app)
def cli():
Expand All @@ -35,25 +38,31 @@ def cli_IndexCreate(tmp, index_create, storage_create):
logging.info("options -i or --index-create are obsolete and will be ignored")
if storage_create:
logging.info("options -s or --storage-create are obsolete and will be ignored")
logging.info("Index creation started")
return IndexCreate(tmp=tmp)


def IndexCreate(**kwargs):
"""
Create empty indexes
"""
if wiki_index_exists():
logging.error("Error: wiki index exists. Please check and destroy index before running index-create")
return False
logging.info("Index creation started")
init_backends(app, create_backend=True)
tmp = kwargs.get('tmp')
app.storage.create(tmp=tmp)
logging.info("Index creation finished")
return True


@cli.command('index-destroy', help='Destroy the indexes')
@click.option('--tmp', is_flag=True, required=False, default=False,
help='use the temporary location.')
def IndexDestroy(tmp):
if not wiki_index_exists():
logging.error(ERR_NO_INDEX)
raise SystemExit(1)
logging.info("Index destroy started")
app.storage.destroy(tmp=tmp)
logging.info("Index destroy finished")
Expand All @@ -68,6 +77,9 @@ def IndexDestroy(tmp):
@click.option('--index-create', '-i', is_flag=True, required=False, default=False)
@click.option('--storage-create', '-s', is_flag=True, required=False, default=False)
def IndexBuild(tmp, procs, limitmb, **kwargs):
if not wiki_index_exists():
logging.error("{} Run 'moin index-create' first.".format(ERR_NO_INDEX))
raise SystemExit(1)
logging.info("Index build started")
flaskg.add_lineno_attr = False # no need to add lineno attributes while building indexes
app.storage.rebuild(tmp=tmp, procs=procs, limitmb=limitmb)
Expand All @@ -77,6 +89,9 @@ def IndexBuild(tmp, procs, limitmb, **kwargs):
@cli.command('index-update', help='Update the indexes')
@click.option('--tmp', is_flag=True, required=False, default=False, help='use the temporary location.')
def IndexUpdate(tmp):
if not wiki_index_exists():
logging.error(ERR_NO_INDEX)
raise SystemExit(1)
logging.info("Index update started")
app.storage.update(tmp=tmp)
logging.info("Index update started")
Expand All @@ -99,6 +114,9 @@ def IndexOptimize(tmp):
"""
Optimize the indexes
"""
if not wiki_index_exists():
logging.error(ERR_NO_INDEX)
raise SystemExit(1)
logging.info("Index optimization started")
app.storage.optimize_index(tmp=tmp)
logging.info("Index optimization finished")
Expand All @@ -108,6 +126,9 @@ def IndexOptimize(tmp):
@click.option('--tmp', is_flag=True, required=False, default=False, help='use the temporary location.')
@click.option('--truncate/--no-truncate', default=True, help='truncate long entries')
def IndexDump(tmp, truncate):
if not wiki_index_exists():
logging.error(ERR_NO_INDEX)
raise SystemExit(1)
logging.info("Index dump started")
for idx_name in [LATEST_REVS, ALL_REVS]:
print(" {0} {1} {2}".format("-" * 10, idx_name, "-" * 60))
Expand Down
10 changes: 9 additions & 1 deletion src/moin/utils/filesys.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright: 2002 Juergen Hermann <[email protected]>
# Copyright: 2006-2010 MoinMoin:ThomasWaldmann
# Copyright: 2024 MoinMoin:UlrichB
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Expand All @@ -12,8 +13,9 @@
import shutil
import time
import errno
from stat import S_ISDIR, ST_MODE, S_IMODE

from glob import glob
from stat import S_ISDIR, ST_MODE, S_IMODE
from os import replace as rename

from moin import log
Expand Down Expand Up @@ -181,3 +183,9 @@ def copytree(src, dst, symlinks=False):
errors.append((srcname, dstname, why))
if errors:
raise EnvironmentError(str(errors))


def wiki_index_exists():
"""Return true if a wiki index exists."""
logging.debug('CWD: %s', os.getcwd())
return bool(glob('wiki/index/_all_revs_*.toc'))

0 comments on commit 1847c89

Please sign in to comment.