Skip to content

pyflightstream.workspace.storage (stable)

Generated from the docstrings at build time: every one of the 22 names in the __all__ of pyflightstream.workspace.storage has an entry below. The names a guide or an example uses come first; the others follow under Advanced.

Workspace storage management and synchronisation.

Five commands of pyfs-matrix live here, and every one of them writes one entry to storage_management.json at the workspace root, the provenance of every storage call ever made in that workspace:

  • space-in-use reports the sizes on disk, by top-level folder, by sims/sim_* and by extension, in that order;
  • free-space <m<id>> runs a recipe from inputs/management/m<id>.toml: prune_step_exports (an unsteady point's per-step exports, all but the last step of each), compact_sims (a simulation folder becomes sims/sim_<id>.zip), delete_extensions (files of named extensions under sims/) and post_archives (the archive/<stamp>/ folders the post writes when it supersedes a product, compacted or deleted);
  • delete-sims <ids> deletes simulations: the folder, their post products and their records, leaving a note in runs.json and the full mention here;
  • sync <level> brings runs and results from the other workspaces named in inputs/sync-workspaces.toml into the main one.

THE RULES EVERY COMMAND KEEPS. A command that changes files previews by default and changes them only with apply=True. A recipe touches sims/ and the post archives only, never inputs/, a matrix, a saved .fsm, a script, a log, or a file a run record names or hashes: those are what a later post needs to extract the post-processing again. A compacted simulation is restored in place, automatically, by post, collect and a continuation (:func:ensure_sim_expanded). storage_management.json has the schema pyfs-storage/1 that the standalone fts_sync.py script started, and a file that script began is continued, never rewritten.

package B2: sync names every sims/sim_* folder of both sides, recorded or not, and with restore (off by default) rebuilds the records of the folders no record carries through :func:pyflightstream.run.records.rebuild, which the run layer registers here (:func:register_records_rebuild); it copies each file to a temporary name and renames it in place, skips every folder named archive unless include_archives, and holds the runs.json lease for the whole of its merge and copy (RST-6). A matrix is read from the root or inputs/matrices/, one stem in both homes once when the bytes are identical and refused naming both when they differ (RST-1). sync, free-space and delete-sims take runs, the manifest :func:pyflightstream.workspace.naming.resolve_manifest names (the function :mod:pyflightstream.run.records re-exports).

Advanced

pyflightstream.workspace.storage.COMPACTED_SUFFIX module-attribute

COMPACTED_SUFFIX = '.zip'

pyflightstream.workspace.storage.DELETED_SIM_KEY module-attribute

DELETED_SIM_KEY = 'deleted_sim'

pyflightstream.workspace.storage.MANAGEMENT_DIR module-attribute

MANAGEMENT_DIR = Path('inputs') / 'management'

pyflightstream.workspace.storage.MATRIX_PRODUCT_CHOICES module-attribute

MATRIX_PRODUCT_CHOICES = ('points-only', 'regenerate')

pyflightstream.workspace.storage.PRUNE_MODE module-attribute

PRUNE_MODE = 'prune_step_exports'

pyflightstream.workspace.storage.STEP_EXPORTS_PRUNED module-attribute

STEP_EXPORTS_PRUNED = (
    "Refused, per-step exports deleted by free-space: "
)

pyflightstream.workspace.storage.STORAGE_FILE module-attribute

STORAGE_FILE = 'storage_management.json'

pyflightstream.workspace.storage.STORAGE_SCHEMA module-attribute

STORAGE_SCHEMA = 'pyfs-storage/1'

pyflightstream.workspace.storage.SYNC_CONFIG module-attribute

SYNC_CONFIG = Path('inputs') / 'sync-workspaces.toml'

pyflightstream.workspace.storage.SYNC_LEVELS module-attribute

SYNC_LEVELS = ('runs', 'post', 'fsm', 'all')

pyflightstream.workspace.storage.StorageError

Bases: WorkspaceError

A storage command refused: the message names what and why.

pyflightstream.workspace.storage.delete_sims

delete_sims(
    root: str | Path,
    sim_ids: Sequence[str],
    *,
    matrix_products: str | None = None,
    apply: bool = False,
    caller: str | None = None,
    runs: str | None = None,
    force: bool = False,
    archive: bool = False,
) -> dict[str, Any]

Delete simulations, their post products and their records.

Preview by default. With apply, matrix_products must say what happens to a product that mixes the deleted simulation with others: points-only leaves it (marked stale), regenerate reruns the post of that matrix without the deleted points. The records leave runs.json (archived first); one note row per simulation stays there, and the full mention is the returned entry, recorded in storage_management.json.

runs names another manifest in the root (CLI: --runs NAME): the records are read from it and leave it, archived first as archive/<stem>-<stamp>.json, and runs.json is not touched (P0320-RUNS-NAME). regenerate is refused with it, because the post it reruns reads runs.json.

A simulation with a record still SUBMITTED is refused unless force (CLI: --force), which deletes it whatever the status of its records (FR-306); the entry then states force and each simulation's statuses.

Parameters:

Name Type Description Default
root str or Path

The workspace root.

required
sim_ids sequence of str

The simulations to delete.

required
matrix_products str

What happens to a product shared with other simulations, points-only or regenerate; required with apply when one is.

None
archive bool

Keep replaced matrix products during regeneration. False overwrites in place (FR-397).

False
apply bool

Write the files; without it no simulation, result or input file is written (the call is still recorded in storage_management.json) and the same refusals fire.

False
caller str

Who asked, recorded in the entry; the operating-system user when None.

None
runs str

Another manifest in the root, by name; runs.json when None.

None
force bool

Delete a simulation with a record still SUBMITTED.

False

Returns:

Type Description
dict of str to object

The entry recorded in storage_management.json.

Raises:

Type Description
StorageError

If no simulation is named, one is unknown or still SUBMITTED without force, matrix_products is missing or unknown where a product is shared, or regenerate is asked with runs.

pyflightstream.workspace.storage.disk_estimate

disk_estimate(
    workspace: CampaignWorkspace,
    pending_points: int,
    *,
    margin: float = 0.1,
) -> tuple[str, bool]

Say whether pending_points more points fit on the workspace's disk.

The estimate is the mean size of a recorded datapoint folder in this workspace times the number of points the plan will run; the answer is False when that, plus a margin of the disk's free space, exceeds it.

Parameters:

Name Type Description Default
workspace CampaignWorkspace

The workspace.

required
pending_points int

The points the plan will run.

required
margin float

The share of the free space held back.

0.1

Returns:

Type Description
tuple of (str, bool)

The line the plan prints, and whether the points fit.

pyflightstream.workspace.storage.ensure_sim_expanded

ensure_sim_expanded(
    workspace: CampaignWorkspace,
    sim_id: str,
    *,
    reason: str,
) -> bool

Restore sims/sim_<id>.zip in place when its folder is absent.

Called by post, collect, reconstruct and a continuation before they read a simulation, so a compacted simulation is read as if it had never been compacted. Returns True when a restore happened; the restore is recorded in storage_management.json and the folder stays expanded.

Parameters:

Name Type Description Default
workspace CampaignWorkspace

The workspace.

required
sim_id str

The simulation.

required
reason str

The command asking, recorded with the restore.

required

Returns:

Type Description
bool

True when a restore happened.

Raises:

Type Description
StorageError

If a member of the archive leaves the folder or does not match its recorded hash, or the folder and the archive both exist.

pyflightstream.workspace.storage.free_space

free_space(
    root: str | Path,
    recipe: str,
    *,
    apply: bool = False,
    runs: str | None = None,
) -> dict[str, Any]

Run a storage recipe: preview by default, change files only with apply.

Applying is not transactional: earlier steps may complete before a later archive collision or I/O failure. The storage call records completed steps and an interrupted reason; resolve that failure before retrying.

runs names another manifest in the root (CLI: --runs NAME), such as a rebuilt one, whose records the recipe reads IN ADDITION to runs.json's: a file either names is protected, and a simulation SUBMITTED in either is left alone, so naming a manifest never protects less than the default (P0320-RUNS-NAME). Returns the entry recorded in storage_management.json.

Parameters:

Name Type Description Default
root str or Path

The workspace root.

required
recipe str

The recipe's name, m<id>, under inputs/management/.

required
apply bool

Write the files; without it no simulation, result or input file is written (the call is still recorded in storage_management.json) and the same refusals fire.

False
runs str

Another manifest in the root whose records are read as well as runs.json's.

None

Returns:

Type Description
dict of str to object

The entry recorded in storage_management.json.

Raises:

Type Description
StorageError

If the recipe is invalid, would delete a saved simulation, or a run is in progress.

WorkspaceError

If post-archive compaction finds an existing archive or fails verification.

pyflightstream.workspace.storage.kept_phrase module-attribute

kept_phrase = step_prune.kept_phrase

pyflightstream.workspace.storage.pruned_step_refusal

pruned_step_refusal(
    sim_dir: Path,
    run_id: str,
    expected: Mapping[int, Sequence[Path]],
    *,
    product: str,
    window: tuple[int, int],
) -> str | None

Say why a product that needs a pruned step is refused, or None.

The post calls this with the steps of a window it did not find and, for each, the files it looked for. When free-space deleted one of them (mode prune_step_exports, applied, for this run), the answer names the missing steps, the first file and the recorded call, and opens with :data:STEP_EXPORTS_PRUNED: a product is never written from the steps that remain. A step no storage call deleted is left to the caller's own rule for a step that was never exported.

Parameters:

Name Type Description Default
sim_dir Path

The simulation folder, <workspace>/sims/sim_<id>.

required
run_id str

The point's run, as its record states it.

required
expected mapping of int to sequence of Path

Each missing step and the files the product looked for at that step.

required
product str

The product's name, as the refusal says it.

required
window tuple of int

The inclusive (first_step, last_step) the product needs.

required

Returns:

Type Description
str or None

The refusal, or None when no missing step was deleted by a storage call.

pyflightstream.workspace.storage.read_storage_calls

read_storage_calls(
    root: str | Path,
) -> list[dict[str, Any]]

Every call recorded in storage_management.json, oldest first.

Parameters:

Name Type Description Default
root str or Path

The workspace root.

required

Returns:

Type Description
list of dict

The recorded calls, oldest first; empty when the file does not exist.

Raises:

Type Description
StorageError

If the file is not a storage record.

pyflightstream.workspace.storage.record_storage_call

record_storage_call(
    root: str | Path, entry: dict[str, Any]
) -> int

Append one call to storage_management.json; return its index.

The write is atomic and serialised by the workspace's own manifest lock on the storage file, so two commands cannot interleave their entries.

Parameters:

Name Type Description Default
root str or Path

The workspace root.

required
entry dict of str to object

The call's entry.

required

Returns:

Type Description
int

The entry's index in the file.

pyflightstream.workspace.storage.space_in_use

space_in_use(root: str | Path) -> SpaceReport

Measure the workspace and record the call in storage_management.json.

No simulation, result or input file changes; the register is rewritten with the call appended, and its previous copy is archived.

Parameters:

Name Type Description Default
root str or Path

The workspace root.

required

Returns:

Type Description
SpaceReport

The space each part of the workspace uses.

pyflightstream.workspace.storage.sync_summary_lines

sync_summary_lines(entry: Mapping[str, Any]) -> list[str]

Return the lines pyfs-workspace sync prints for a synchronization entry.

The archive files and bytes skipped (P0320-SYNC-SKIP-ARCHIVES), the simulation folders of both sides and those no record carries (P0320-SYNC-ALL-FOLDERS), and what the restore did or would do (P0320-SYNC-RESTORE-OPTIN). An entry written in legacy releases gives none.

Parameters:

Name Type Description Default
entry mapping of str to object

One entry :func:sync_workspaces returned.

required

Returns:

Type Description
list of str

The lines; none for an entry written in legacy releases.

pyflightstream.workspace.storage.sync_workspaces

sync_workspaces(
    root: str | Path,
    level: str,
    *,
    source: str | None = None,
    apply: bool = False,
    prefer_other: bool = False,
    overwrite: bool = False,
    restore: bool = False,
    include_archives: bool = False,
    runs: str | None = None,
) -> list[dict[str, Any]]

Bring runs and results from the other workspaces into the main one.

level is cumulative: runs (runs.json and run provenance: each simulation's scripts/ and datapoint logs), post (+ post/), fsm (+ the datapoints' saved simulations), all (+ everything else under sims/; an inputs junction is never followed). Nothing in main is deleted, and inputs/ is never touched except a declared matrix under inputs/matrices/. MATRICES, at every level: each one is declared in sync-workspaces.toml by the ONE workspace that owns it (an undeclared matrix refuses the sync); a difference is always reported as a merge conflict and the owning workspace's copy is the one main keeps. Returns one recorded entry per source workspace.

Every sims/sim_* folder of both sides is compared, recorded or not, and each entry's sims names the folders no record carries. restore (CLI: --restore), off by default, rebuilds their records through :func:pyflightstream.run.records.rebuild once an applying sync has released the runs.json lease it holds for the whole of its merge and copy. Every file is copied to a temporary name and renamed in place. A folder named archive is skipped unless include_archives (CLI: --include-archives), and files.archives_skipped counts what was. runs (CLI: --runs NAME) names the manifest of main the records merge into; the other workspace's runs.json is the source.

Parameters:

Name Type Description Default
root str or Path

The main workspace.

required
level str

runs, post, fsm or all.

required
source str

The one workspace of sync-workspaces.toml to bring from; every other one when None.

None
apply bool

Write the files; without it no simulation, result or input file is written (the call is still recorded in storage_management.json) and the same refusals fire.

False
prefer_other bool

Where a record differs, keep the other workspace's; main's is kept otherwise, unless it is SUBMITTED.

False
overwrite bool

Replace a file of main that differs from the other workspace's instead of reporting a conflict.

False
restore bool

Rebuild the records of the simulation folders no record carries.

False
include_archives bool

Copy the folders named archive too.

False
runs str

The manifest of main the records merge into; runs.json when None.

None

Returns:

Type Description
list of dict

One recorded entry per source workspace.

Raises:

Type Description
RunsManifestError

runs is not a JSON file directly in the root.

StorageError

An unknown level or source; runs.json.lock present in main (a run, a collect, a restore or another sync is writing); restore with a manifest other than runs.json; a matrix no workspace declares, or one stem held in both homes of a workspace with different bytes.