Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rwlock: require info keys to be present in schema #3283

Merged
merged 1 commit into from Feb 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions dvc/rwlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
from collections import defaultdict
from contextlib import contextmanager

from voluptuous import Schema, Invalid
from voluptuous import Schema, Invalid, Required, Optional

from .exceptions import DvcException
from .lock import LockError
from .utils import relpath

INFO_SCHEMA = {"pid": int, "cmd": str}
INFO_SCHEMA = {Required("pid"): int, Required("cmd"): str}

SCHEMA = Schema({"write": {str: INFO_SCHEMA}, "read": {str: [INFO_SCHEMA]}})
SCHEMA = Schema(
{
Optional("write", default={}): {str: INFO_SCHEMA},
Optional("read", default={}): {str: [INFO_SCHEMA]},
}
)


class RWLockFileCorruptedError(DvcException):
Expand All @@ -35,15 +40,13 @@ def _edit_rwlock(lock_dir):
path = os.path.join(lock_dir, "rwlock")
try:
with open(path, "r") as fobj:
lock = json.load(fobj)
lock = SCHEMA(lock)
lock = SCHEMA(json.load(fobj))
except FileNotFoundError:
lock = SCHEMA({})
except json.JSONDecodeError as exc:
raise RWLockFileCorruptedError(path) from exc
except Invalid as exc:
raise RWLockFileFormatError(path) from exc
lock = defaultdict(dict, lock)
lock["read"] = defaultdict(list, lock["read"])
lock["write"] = defaultdict(dict, lock["write"])
yield lock
Expand Down