Skip to content
Merged
Show file tree
Hide file tree
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
57 changes: 49 additions & 8 deletions client/cli/corpus_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,42 @@ def _warn_once(message):
print(message, file=sys.stderr)


def _read_registry(path):
try:
with open(path) as f:
raw = json.load(f)
except (OSError, json.JSONDecodeError):
return {}

return raw if isinstance(raw, dict) else {}


def user_projects_file():
"""Where the user's own corpora are kept when the registry is not theirs
to write: a container ships it in the image, and what a session registers
there lands in the state directory instead. None when the state directory
is the install itself."""
from cli import chat_settings

if os.path.realpath(chat_settings.STATE_DIR) == os.path.realpath(APP_DIR):
return None

return os.path.join(chat_settings.STATE_DIR, "projects.json")


def load_projects():
"""Return {name: {"path","kind", ...}}. Tolerates the legacy {name: path} format (treated as
generic). Extra keys (exclude, include_build — see reindex_options) are
PRESERVED: rebuilding specs from path/kind alone erased them at the first
save_projects()."""

try:
with open(PROJECTS_FILE) as f:
raw = json.load(f)
except (OSError, json.JSONDecodeError):
raw = {}
raw = _read_registry(PROJECTS_FILE)
own = user_projects_file()

# The shipped registry wins on a name: the user's file only adds.

if own:
raw = {**_read_registry(own), **raw}

projects = {}

Expand Down Expand Up @@ -122,9 +147,25 @@ def save_projects(projects):
with open(PROJECTS_FILE, "w") as f:
json.dump(projects, f, indent=2)
except OSError as exc:
# The registry is shipped configuration, not session state: in a
# container it lives in the image and is not the user's to change.
# Say where it has to be done instead of raising a stack trace.
# The registry is shipped configuration: in a container it lives in
# the image and is not the user's to change. What the session adds to
# it goes to the user's own file; without one, say where it has to be
# done instead of raising a stack trace.

own = user_projects_file()
shipped = _read_registry(PROJECTS_FILE)

if own:
try:
os.makedirs(os.path.dirname(own), exist_ok=True)

with open(own, "w") as f:
json.dump({name: spec for name, spec in projects.items()
if name not in shipped}, f, indent=2)

return
except OSError:
pass

raise SystemExit(
f"cannot write the corpus registry ({PROJECTS_FILE}): {exc}\n"
Expand Down
36 changes: 36 additions & 0 deletions client/tests/test_corpus_behaviour_is_declared.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,42 @@ def register(self, **spec):
return spec


class AShippedRegistryTakesTheUsersAdditionsElsewhere(Registry):
def setUp(self):
super().setUp()
from cli import chat_settings

self.addCleanup(setattr, chat_settings, "STATE_DIR", chat_settings.STATE_DIR)
chat_settings.STATE_DIR = f"{self.root}/state"

with open(corpus_registry.PROJECTS_FILE, "w") as f:
json.dump({"shipped": {"path": f"{self.root}/tree"}}, f)

os.chmod(corpus_registry.PROJECTS_FILE, 0o444)

def test_a_new_corpus_goes_to_the_state_directory(self):
projects = corpus_registry.load_projects()
projects["mine"] = {"path": f"{self.root}/tree", "kind": "generic"}
corpus_registry.save_projects(projects)

with open(f"{self.root}/state/projects.json") as f:
self.assertEqual(list(json.load(f)), ["mine"])

with open(corpus_registry.PROJECTS_FILE) as f:
self.assertEqual(list(json.load(f)), ["shipped"])

self.assertEqual(sorted(corpus_registry.load_projects()), ["mine", "shipped"])

def test_the_shipped_entry_wins_on_its_name(self):
os.makedirs(f"{self.root}/state")

with open(f"{self.root}/state/projects.json", "w") as f:
json.dump({"shipped": {"path": "/elsewhere"}}, f)

self.assertEqual(corpus_registry.load_projects()["shipped"]["path"],
f"{self.root}/tree")


class TheIndexerIsDeclared(Registry):
def test_generic_by_default(self):
self.register(kind="generic")
Expand Down
7 changes: 7 additions & 0 deletions client/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,13 @@ def test_scan_takes_min_as_a_value_not_as_a_directory(self):
def test_a_read_only_registry_does_not_kill_the_session(self):
"""save_projects raises SystemExit there — right for a CLI, fatal in a
chat loop, which would lose the conversation to a typo."""

# An install that is its own state directory: nowhere else to write.

self.addCleanup(setattr, self.corpus_registry, "user_projects_file",
self.corpus_registry.user_projects_file)
self.corpus_registry.user_projects_file = lambda: None

os.chmod(self.corpus_registry.PROJECTS_FILE, 0o444)
os.chmod(self.root, 0o555)
self.addCleanup(os.chmod, self.root, 0o755)
Expand Down
Loading