Skip to content

Commit a59cf27

Browse files
authored
Optionally ignore lock file (#458)
1 parent 70a1f48 commit a59cf27

File tree

7 files changed

+59
-22
lines changed

7 files changed

+59
-22
lines changed

gramps_webapi/__main__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@
2828
import click
2929
from whoosh.index import LockError
3030

31-
3231
from .api.util import get_db_manager, get_search_indexer, list_trees
33-
from .dbmanager import WebDbManager
3432
from .app import create_app
3533
from .auth import add_user, delete_user, fill_tree, user_db
3634
from .const import ENV_CONFIG_FILE, TREE_MULTI
35+
from .dbmanager import WebDbManager
3736

3837
logging.basicConfig()
3938
LOG = logging.getLogger("gramps_webapi")
@@ -128,7 +127,11 @@ def search(ctx, tree):
128127
if app.config["TREE"] == TREE_MULTI:
129128
raise ValueError("`tree` is required when multi-tree support is enabled.")
130129
# needed for backwards compatibility!
131-
dbmgr = WebDbManager(name=app.config["TREE"], create_if_missing=False)
130+
dbmgr = WebDbManager(
131+
name=app.config["TREE"],
132+
create_if_missing=False,
133+
ignore_lock=app.config["IGNORE_DB_LOCK"],
134+
)
132135
tree = dbmgr.dirname
133136
with app.app_context():
134137
ctx.obj["db_manager"] = get_db_manager(tree=tree)

gramps_webapi/api/resources/trees.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@
5050
def get_tree_details(tree_id: str) -> Dict[str, str]:
5151
"""Get details about a tree."""
5252
try:
53-
dbmgr = WebDbManager(dirname=tree_id, create_if_missing=False)
53+
dbmgr = WebDbManager(
54+
dirname=tree_id,
55+
create_if_missing=False,
56+
ignore_lock=current_app.config["IGNORE_DB_LOCK"],
57+
)
5458
except ValueError:
5559
abort(404)
5660
usage = get_tree_usage(tree_id) or {}
@@ -100,7 +104,7 @@ def post(self, args):
100104
require_permissions([PERM_ADD_TREE])
101105
tree_id = str(uuid.uuid4())
102106
backend = current_app.config["NEW_DB_BACKEND"]
103-
dbmgr = WebDbManager(
107+
WebDbManager(
104108
dirname=tree_id,
105109
name=args["name"],
106110
create_if_missing=True,
@@ -153,7 +157,11 @@ def put(self, args, tree_id: str):
153157
require_permissions([PERM_EDIT_OTHER_TREE])
154158
validate_tree_id(tree_id)
155159
try:
156-
dbmgr = WebDbManager(dirname=tree_id, create_if_missing=False)
160+
dbmgr = WebDbManager(
161+
dirname=tree_id,
162+
create_if_missing=False,
163+
ignore_lock=current_app.config["IGNORE_DB_LOCK"],
164+
)
157165
except ValueError:
158166
abort(404)
159167
rv = {}

gramps_webapi/api/util.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@
3737
from gramps.gen.const import GRAMPS_LOCALE
3838
from gramps.gen.db.base import DbReadBase
3939
from gramps.gen.db.dbconst import (
40+
CITATION_KEY,
4041
DBBACKEND,
41-
KEY_TO_NAME_MAP,
42-
PERSON_KEY,
43-
FAMILY_KEY,
44-
SOURCE_KEY,
4542
EVENT_KEY,
43+
FAMILY_KEY,
44+
KEY_TO_NAME_MAP,
4645
MEDIA_KEY,
46+
NOTE_KEY,
47+
PERSON_KEY,
4748
PLACE_KEY,
4849
REPOSITORY_KEY,
49-
NOTE_KEY,
50-
CITATION_KEY,
50+
SOURCE_KEY,
5151
)
5252
from gramps.gen.dbstate import DbState
5353
from gramps.gen.errors import HandleError
@@ -254,6 +254,7 @@ def get_db_manager(tree: Optional[str]) -> WebDbManager:
254254
username=current_app.config["POSTGRES_USER"],
255255
password=current_app.config["POSTGRES_PASSWORD"],
256256
create_if_missing=False,
257+
ignore_lock=current_app.config["IGNORE_DB_LOCK"],
257258
)
258259

259260

@@ -472,7 +473,11 @@ def get_tree_id(guid: str) -> str:
472473
# multi-tree support enabled but user has no tree ID: forbidden!
473474
abort_with_message(403, "Forbidden")
474475
# needed for backwards compatibility: single-tree mode but user without tree ID
475-
dbmgr = WebDbManager(name=current_app.config["TREE"], create_if_missing=False)
476+
dbmgr = WebDbManager(
477+
name=current_app.config["TREE"],
478+
create_if_missing=False,
479+
ignore_lock=current_app.config["IGNORE_DB_LOCK"],
480+
)
476481
tree_id = dbmgr.dirname
477482
return tree_id
478483

gramps_webapi/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ def create_app(config: Optional[Dict[str, Any]] = None):
105105

106106
if app.config["TREE"] != TREE_MULTI:
107107
# create database if missing (only in single-tree mode)
108-
WebDbManager(name=app.config["TREE"], create_if_missing=True)
108+
WebDbManager(
109+
name=app.config["TREE"],
110+
create_if_missing=True,
111+
ignore_lock=app.config["IGNORE_DB_LOCK"],
112+
)
109113

110114
if app.config["TREE"] == TREE_MULTI and not app.config["MEDIA_PREFIX_TREE"]:
111115
warnings.warn(

gramps_webapi/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class DefaultConfig(object):
4848
POSTGRES_PASSWORD = None
4949
POSTGRES_HOST = "localhost"
5050
POSTGRES_PORT = "5432"
51+
IGNORE_DB_LOCK = False
5152
CELERY_CONFIG: Dict[str, str] = {}
5253
MEDIA_BASE_DIR = ""
5354
MEDIA_PREFIX_TREE = False

gramps_webapi/dbloader.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
from gramps.gen.recentfiles import recent_files
3737
from gramps.gen.utils.config import get_researcher
3838

39-
4039
_ = glocale.translation.gettext
4140

4241
LOG = logging.getLogger(__name__)
@@ -106,9 +105,17 @@ def read_file(self, filename, mode: str, username: str, password: str):
106105
# set readonly correctly again
107106
self.dbstate.db.readonly = mode == DBMODE_R
108107

109-
def open_activate(self, filename, mode, username=None, password=None):
108+
def open_activate(
109+
self,
110+
filename,
111+
mode,
112+
username=None,
113+
password=None,
114+
ignore_lock: bool = False,
115+
):
110116
"""Open and make a family tree active."""
111-
check_lock(dir_name=filename, mode=mode)
117+
if not ignore_lock:
118+
check_lock(dir_name=filename, mode=mode)
112119
self.read_file(filename, mode, username, password)
113120
# Attempt to figure out the database title
114121
title = get_title(filename)

gramps_webapi/dbmanager.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@
2525

2626
import os
2727
import uuid
28-
2928
from typing import Optional, Tuple
3029

31-
from gramps.cli.clidbman import CLIDbManager, NAME_FILE
30+
from gramps.cli.clidbman import NAME_FILE, CLIDbManager
3231
from gramps.cli.user import User
3332
from gramps.gen.config import config
3433
from gramps.gen.db.dbconst import DBBACKEND, DBLOCKFN, DBMODE_R, DBMODE_W
35-
from gramps.gen.db.utils import make_database, get_dbid_from_path
34+
from gramps.gen.db.utils import get_dbid_from_path, make_database
3635
from gramps.gen.dbstate import DbState
3736

3837
from .dbloader import WebDbSessionManager
@@ -51,6 +50,7 @@ def __init__(
5150
password: Optional[str] = None,
5251
create_if_missing: bool = True,
5352
create_backend: str = "sqlite",
53+
ignore_lock: bool = False,
5454
) -> None:
5555
"""Initialize given a family tree name or subdirectory name (path)."""
5656
if dirname:
@@ -67,6 +67,7 @@ def __init__(
6767
self.password = password
6868
self.create_if_missing = create_if_missing
6969
self.create_backend = create_backend
70+
self.ignore_lock = ignore_lock
7071
self.path = self._get_path()
7172
self._check_backend()
7273

@@ -154,7 +155,11 @@ def break_lock(self) -> None:
154155
if os.path.exists(os.path.join(self.path, DBLOCKFN)):
155156
os.unlink(os.path.join(self.path, DBLOCKFN))
156157

157-
def get_db(self, readonly: bool = True, force_unlock: bool = False) -> DbState:
158+
def get_db(
159+
self,
160+
readonly: bool = True,
161+
force_unlock: bool = False,
162+
) -> DbState:
158163
"""Open the database and return a dbstate instance.
159164
160165
If `readonly` is `True` (default), write operations will fail (note,
@@ -170,7 +175,11 @@ def get_db(self, readonly: bool = True, force_unlock: bool = False) -> DbState:
170175
self.break_lock()
171176
mode = DBMODE_R if readonly else DBMODE_W
172177
smgr.open_activate(
173-
self.path, mode=mode, username=self.username, password=self.password
178+
self.path,
179+
mode=mode,
180+
username=self.username,
181+
password=self.password,
182+
ignore_lock=self.ignore_lock,
174183
)
175184
return dbstate
176185

0 commit comments

Comments
 (0)