Skip to content

Commit 1847c89

Browse files
authored
Merge pull request #1637 from UlrichB22/cli_checks
Add wiki index checks in CLI.
2 parents 0fa0a5c + 83a154d commit 1847c89

File tree

4 files changed

+56
-18
lines changed

4 files changed

+56
-18
lines changed

src/moin/app.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright: 2002-2011 MoinMoin:ThomasWaldmann
33
# Copyright: 2008 MoinMoin:FlorianKrupicka
44
# Copyright: 2010 MoinMoin:DiogenesAugusto
5-
# Copyright: 2023 MoinMoin project
5+
# Copyright: 2023-2024 MoinMoin:UlrichB
66
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
77

88
"""
@@ -25,6 +25,7 @@
2525
from flask_theme import setup_themes
2626

2727
from jinja2 import ChoiceLoader, FileSystemLoader
28+
from whoosh.index import EmptyIndexError
2829

2930
from moin.utils import monkeypatch # noqa
3031
from moin.utils.clock import Clock
@@ -158,7 +159,17 @@ class ItemNameConverter(PathConverter):
158159
clock.stop('create_app flask-cache')
159160
# init storage
160161
clock.start('create_app init backends')
161-
init_backends(app)
162+
try:
163+
init_backends(app)
164+
except EmptyIndexError:
165+
# create-instance has no index at start and index-* subcommands check the index individually
166+
if (info_name not in ['create-instance', 'build-instance', ]
167+
and not info_name.startswith('index-')):
168+
clock.stop('create_app init backends')
169+
clock.stop('create_app total')
170+
logging.error("Error: Wiki index not found. Try 'moin help' or 'moin --help' to get further information.")
171+
raise SystemExit(1)
172+
logging.debug("Wiki index not found.")
162173
clock.stop('create_app init backends')
163174
clock.start('create_app flask-babel')
164175
i18n_init(app)
@@ -190,8 +201,6 @@ def init_backends(app, create_backend=False):
190201
# mountpoint, unprotected backend
191202
# Just initialize with unprotected backends.
192203
logging.debug("running init_backends")
193-
c = get_current_context(silent=True)
194-
info_name = getattr(c, 'info_name', '')
195204
app.router = routing.Backend(app.cfg.namespace_mapping, app.cfg.backend_mapping)
196205
if create_backend or getattr(app.cfg, 'create_backend', False):
197206
app.router.create()
@@ -203,9 +212,7 @@ def init_backends(app, create_backend=False):
203212
logging.debug("create_backend: %s ", str(create_backend))
204213
if create_backend or getattr(app.cfg, 'create_backend', False): # 2. call of init_backends
205214
app.storage.create()
206-
app.storage.open()
207-
if info_name not in ['create-instance', 'build-instance', 'index-create']:
208-
app.storage.open()
215+
app.storage.open()
209216

210217

211218
def deinit_backends(app):

src/moin/cli/maint/create_instance.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Copyright: 2020 MoinMoin:RogerHaase
2-
# Copyright: 2023-2024 MoinMoin project
2+
# Copyright: 2023-2024 MoinMoin:UlrichB
33
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
44

55
"""
@@ -99,10 +99,12 @@ def cli_BuildInstance():
9999
'''
100100
logging.info('Build Instance started.')
101101
logging.debug('CWD: %s', os.getcwd())
102-
index.IndexCreate()
103-
modify_item.LoadHelp(namespace='help-en', path_to_help=None)
104-
modify_item.LoadHelp(namespace='help-common', path_to_help=None)
105-
modify_item.LoadWelcome()
106-
index.IndexOptimize(tmp=False)
107-
logging.info('Full instance setup finished.')
108-
logging.info('You can now use "moin run" to start the builtin server.')
102+
if index.IndexCreate():
103+
modify_item.LoadHelp(namespace='help-en', path_to_help=None)
104+
modify_item.LoadHelp(namespace='help-common', path_to_help=None)
105+
modify_item.LoadWelcome()
106+
index.IndexOptimize(tmp=False)
107+
logging.info('Full instance setup finished.')
108+
logging.info('You can now use "moin run" to start the builtin server.')
109+
else:
110+
logging.error('Build Instance failed.')

src/moin/cli/maint/index.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Copyright: 2011 MoinMoin:MichaelMayorov
2-
# Copyright: 2023 MoinMoin project
2+
# Copyright: 2023-2024 MoinMoin:UlrichB
33
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
44

55
"""
@@ -14,11 +14,14 @@
1414

1515
from moin.app import create_app, init_backends
1616
from moin.constants.keys import LATEST_REVS, ALL_REVS
17+
from moin.utils.filesys import wiki_index_exists
18+
1719

1820
from moin import log
1921

2022
logging = log.getLogger(__name__)
2123

24+
ERR_NO_INDEX = "Error: Wiki index does not exist."
2225

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

4143

4244
def IndexCreate(**kwargs):
4345
"""
4446
Create empty indexes
4547
"""
48+
if wiki_index_exists():
49+
logging.error("Error: wiki index exists. Please check and destroy index before running index-create")
50+
return False
4651
logging.info("Index creation started")
4752
init_backends(app, create_backend=True)
4853
tmp = kwargs.get('tmp')
4954
app.storage.create(tmp=tmp)
5055
logging.info("Index creation finished")
56+
return True
5157

5258

5359
@cli.command('index-destroy', help='Destroy the indexes')
5460
@click.option('--tmp', is_flag=True, required=False, default=False,
5561
help='use the temporary location.')
5662
def IndexDestroy(tmp):
63+
if not wiki_index_exists():
64+
logging.error(ERR_NO_INDEX)
65+
raise SystemExit(1)
5766
logging.info("Index destroy started")
5867
app.storage.destroy(tmp=tmp)
5968
logging.info("Index destroy finished")
@@ -68,6 +77,9 @@ def IndexDestroy(tmp):
6877
@click.option('--index-create', '-i', is_flag=True, required=False, default=False)
6978
@click.option('--storage-create', '-s', is_flag=True, required=False, default=False)
7079
def IndexBuild(tmp, procs, limitmb, **kwargs):
80+
if not wiki_index_exists():
81+
logging.error("{} Run 'moin index-create' first.".format(ERR_NO_INDEX))
82+
raise SystemExit(1)
7183
logging.info("Index build started")
7284
flaskg.add_lineno_attr = False # no need to add lineno attributes while building indexes
7385
app.storage.rebuild(tmp=tmp, procs=procs, limitmb=limitmb)
@@ -77,6 +89,9 @@ def IndexBuild(tmp, procs, limitmb, **kwargs):
7789
@cli.command('index-update', help='Update the indexes')
7890
@click.option('--tmp', is_flag=True, required=False, default=False, help='use the temporary location.')
7991
def IndexUpdate(tmp):
92+
if not wiki_index_exists():
93+
logging.error(ERR_NO_INDEX)
94+
raise SystemExit(1)
8095
logging.info("Index update started")
8196
app.storage.update(tmp=tmp)
8297
logging.info("Index update started")
@@ -99,6 +114,9 @@ def IndexOptimize(tmp):
99114
"""
100115
Optimize the indexes
101116
"""
117+
if not wiki_index_exists():
118+
logging.error(ERR_NO_INDEX)
119+
raise SystemExit(1)
102120
logging.info("Index optimization started")
103121
app.storage.optimize_index(tmp=tmp)
104122
logging.info("Index optimization finished")
@@ -108,6 +126,9 @@ def IndexOptimize(tmp):
108126
@click.option('--tmp', is_flag=True, required=False, default=False, help='use the temporary location.')
109127
@click.option('--truncate/--no-truncate', default=True, help='truncate long entries')
110128
def IndexDump(tmp, truncate):
129+
if not wiki_index_exists():
130+
logging.error(ERR_NO_INDEX)
131+
raise SystemExit(1)
111132
logging.info("Index dump started")
112133
for idx_name in [LATEST_REVS, ALL_REVS]:
113134
print(" {0} {1} {2}".format("-" * 10, idx_name, "-" * 60))

src/moin/utils/filesys.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright: 2002 Juergen Hermann <[email protected]>
22
# Copyright: 2006-2010 MoinMoin:ThomasWaldmann
3+
# Copyright: 2024 MoinMoin:UlrichB
34
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
45

56
"""
@@ -12,8 +13,9 @@
1213
import shutil
1314
import time
1415
import errno
15-
from stat import S_ISDIR, ST_MODE, S_IMODE
1616

17+
from glob import glob
18+
from stat import S_ISDIR, ST_MODE, S_IMODE
1719
from os import replace as rename
1820

1921
from moin import log
@@ -181,3 +183,9 @@ def copytree(src, dst, symlinks=False):
181183
errors.append((srcname, dstname, why))
182184
if errors:
183185
raise EnvironmentError(str(errors))
186+
187+
188+
def wiki_index_exists():
189+
"""Return true if a wiki index exists."""
190+
logging.debug('CWD: %s', os.getcwd())
191+
return bool(glob('wiki/index/_all_revs_*.toc'))

0 commit comments

Comments
 (0)