diff --git a/app/nullius/workspace.py b/app/nullius/workspace.py index d25e0f00b..5311b84d1 100644 --- a/app/nullius/workspace.py +++ b/app/nullius/workspace.py @@ -110,6 +110,16 @@ class Workspace: def imports_dir(self) -> str: return os.path.join(self.dir, "imported") + @property + def kept(self) -> list: + """Its job roots in its own ``imported/``: conversions kept from before.""" + return [r for r in self.job_roots if _inside(r, self.imports_dir)] + + @property + def walked(self) -> list: + """The folders its jobs are read from (``walked``).""" + return walked(self) + @property def jobs_dir(self) -> str: return os.path.join(self.dir, "jobs") @@ -123,6 +133,17 @@ def n_cached(self) -> int: return 0 +def _inside(path: str, folder: str) -> bool: + """*path* is *folder* or in it, both made absolute.""" + folder = os.path.abspath(folder) + return os.path.commonpath([os.path.abspath(path), folder]) == folder + + +def in_imports(ws: Workspace, path: str) -> bool: + """*path* is in *ws*'s own ``imported/``: a conversion kept from before, never deleted from disk alone.""" + return _inside(path, ws.imports_dir) + + def _path(home_dir: str, name: str) -> str: return os.path.join(home_dir, name, "workspace.json") @@ -314,8 +335,7 @@ def every(home_dir: str) -> list: def walked(ws: Workspace) -> list: """The folders *ws*'s jobs are read from, open or closed: its roots, then the job roots it kept in its own ``imported/``, converted before.""" - kept = [r for r in ws.job_roots if os.path.commonpath([os.path.abspath(r), ws.imports_dir]) == ws.imports_dir] - return list(dict.fromkeys([*ws.roots, *kept])) + return list(dict.fromkeys([*ws.roots, *ws.kept])) def last_open(home_dir: str) -> Workspace | None: @@ -373,11 +393,13 @@ def rename(home_dir: str, ws: Workspace, label: str) -> Workspace: with everything in it - parsed jobs, imports, judgements - so a workspace never carries one name on the page and another on disk. A label with nothing to slug keeps the name it has. The roots it reads - stay where they are.""" + stay where they are; those in its ``imported/`` are named where they + moved, then their parses dropped: a failure there leaves the rename made, + the next Refresh clears them.""" label = (label or "").strip() if not label or not os.path.isdir(ws.dir): return ws - name = slug(label) + name, moved = slug(label), {} with _locked(ws): # the lock moves with the folder now = _still(ws, load(home_dir, ws.name)) if now is None: @@ -387,16 +409,41 @@ def rename(home_dir: str, ws: Workspace, label: str) -> Workspace: taken = {w.name for w in list_all(home_dir)} | set(os.listdir(home_dir)) name = _free(name, taken - {ws.name}) new_dir = os.path.join(home_dir, name) + old_dir, old_imports = now.dir, os.path.abspath(now.imports_dir) os.rename(ws.dir, new_dir) from nullius.judge import store store.unmark(ws.dir, to=new_dir) now.name, now.dir = name, new_dir + + def there(r): + return os.path.join(os.path.abspath(now.imports_dir), os.path.relpath(os.path.abspath(r), old_imports)) if _inside(r, old_imports) else r + moved = {there(r): r for r in [*now.roots, *now.job_roots] if _inside(r, old_imports)} + now.roots, now.job_roots = [there(r) for r in now.roots], [there(r) for r in now.job_roots] + with _DECODED_LOCK: # parses held under the old folder are read nowhere now + for path in [p for p in _DECODED if _inside(p, old_dir)]: + del _DECODED[path] now.label = label save(now) - ws.name, ws.dir, ws.label, ws.roots = now.name, now.dir, now.label, now.roots + if moved: # under the lock: a rename after this one moves none of them + try: + _drop_moved(now, moved) + except OSError: + pass + ws.name, ws.dir, ws.label, ws.roots, ws.job_roots = now.name, now.dir, now.label, now.roots, now.job_roots return ws +def _drop_moved(ws: Workspace, moved: dict) -> None: + """The parses of the job folders under *moved*'s folders (new path: old path), kept by + their old path, dropped: in the index or not, as a load that began before wrote them.""" + old = [os.path.join(was, os.path.relpath(d, now)) for now, was in moved.items() for d in parse.find_job_dirs([now])] + + def drop(index): + for d in {*old, *(d for d in index if _under(d, moved.values()))}: + _drop(ws, index, d) + _update_index(ws, drop) + + def touch(ws: Workspace) -> None: """*ws* opened now: its state alone is written, its record is left as it is.""" _set_state(ws, opened=_now(), closed=False) diff --git a/app/nullius/workspace_cli.py b/app/nullius/workspace_cli.py index a690fde1f..7b4b36b90 100644 --- a/app/nullius/workspace_cli.py +++ b/app/nullius/workspace_cli.py @@ -86,7 +86,7 @@ def _list(args) -> int: opened = (ws.opened or "")[:16].replace("T", " ") print(f"{ws.name:<{width}} {ws.label}") print(f"{'':<{width}} cached runs {r['n_cached']} · opened {opened or '—'}") - for root in ws.roots: + for root in workspace.walked(ws): print(f"{'':<{width}} root {root}") for job in r["jobs"]: print(f"{'':<{width}} job {job['job_id']} ({job['n_trials']} trial(s))") diff --git a/app/templates/runs/start.html b/app/templates/runs/start.html index 9fdf46b5b..405fbfa70 100644 --- a/app/templates/runs/start.html +++ b/app/templates/runs/start.html @@ -25,7 +25,7 @@

Or open a workspace

  • diff --git a/app/templates/workspaces/workspaces.html b/app/templates/workspaces/workspaces.html index a5bbc922b..44fc164e9 100644 --- a/app/templates/workspaces/workspaces.html +++ b/app/templates/workspaces/workspaces.html @@ -14,7 +14,7 @@ workspace records it; never opened sorts last, newest first). #} {% macro launches(ws) %}{% set n = n_launches[ws.name] %}{% if n %}{{ n }} {%- else %}{{ greyed('Launches of ' ~ ws.label ~ ': 0', '', 'No launches in ' ~ ws.label, 'none-' ~ ws.name, icon='launches', class='btn ghost sm count-btn', text='0') }}{% endif %}{% endmacro %} -{% macro roots(ws) %}{% endmacro %} +{% macro roots(ws) %}{% set n = ws.walked|length %}{% endmacro %} {#- A workspace's rename: the pencil, and the form it swaps in for the name (the page's script); `view` the one to return to. #} {% macro pencil(ws) %}{% endmacro %} {% macro rename(ws, view=none) %}