Skip to content

pyflightstream.post.provenance (stable)

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

Write each recorded run's provenance as PROV-JSON.

Documents relate staged inputs, scripts and collected outputs to the run and its agents. The shared product archive helpers preserve existing documents and tables before a rebuild. Existing import spellings remain available through :mod:pyflightstream.post.products.

Advanced

pyflightstream.post.provenance.PRODUCT_ARCHIVE_DIR module-attribute

PRODUCT_ARCHIVE_DIR = ARCHIVE_DIR

pyflightstream.post.provenance.PRODUCT_ARCHIVE_STAMP module-attribute

PRODUCT_ARCHIVE_STAMP = ARCHIVE_STAMP

pyflightstream.post.provenance.PROVENANCE_DIR module-attribute

PROVENANCE_DIR = 'provenance'

pyflightstream.post.provenance.PROVENANCE_SUFFIX module-attribute

PROVENANCE_SUFFIX = '.prov.json'

pyflightstream.post.provenance.PROV_PREFIX module-attribute

PROV_PREFIX = {
    "prov": "http://www.w3.org/ns/prov#",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "pyfs": "urn:pyflightstream:",
}

pyflightstream.post.provenance.SCRIPT_SUFFIX module-attribute

SCRIPT_SUFFIX = '.txt'

pyflightstream.post.provenance.attributes

attributes(**pairs: object) -> dict[str, object]

Return the attributes of one PROV node, a None value left out rather than written.

Parameters:

Name Type Description Default
**pairs object

The attributes, by name.

{}

Returns:

Type Description
dict of str to object

The attributes whose value is not None.

pyflightstream.post.provenance.operator_agent

operator_agent(name: str | None) -> dict[str, object]

Return the prov:Person agent for the operator a record names.

A prov:Person and not a string on the activity, because the document is W3C PROV and already carries two prov:SoftwareAgents: the operator is the third agent the run was associated with, which another tool reads without being told anything about this package.

None means the operator was not recorded. submitted_by is a RUN-time fact that cannot be recovered afterward; no identity is invented. The field remains present as NA so absence is explicit.

Parameters:

Name Type Description Default
name str or None

The operator the record names; None for a run recorded before the field existed.

required

Returns:

Type Description
dict of str to object

The prov:Person node, its pyfs:submitted_by the name or NA.

pyflightstream.post.provenance.point_name_of

point_name_of(record: RunRecord) -> str | None

Return the point name a record's script carries, or None (FR-86).

THE SCRIPT IS THE SOURCE and not the naming template. The template a workspace is configured with today is not the one a record from last month was written under; the record names the file the run actually wrote, and that file's stem IS the convention every other generated file of the point carries. A record whose script is not named the way this package writes one answers None, and the caller keeps the run id, which is what every document was named in legacy releases.

Parameters:

Name Type Description Default
record RunRecord

The run's record.

required

Returns:

Type Description
str or None

The point name, or None where the script is not named the way this package writes one.

pyflightstream.post.provenance.product_archive_dir

product_archive_dir(
    path: Path, *, now: datetime | None = None
) -> Path

Where the product at path is archived to before it is rewritten.

<the product's own folder>/archive/<day and hour>/: the archive sits BESIDE the product it replaces, so a reader who has opened polars/ to compare two polar tables finds the old one in that same folder rather than in a tree somewhere above it.

THE STAMP IS THE REBUILD'S, passed in by the post stage, so every product a rebuild archives lands under one folder name even though the folders themselves are per-product.

Parameters:

Name Type Description Default
path Path

The product about to be rewritten.

required
now datetime

The rebuild's stamp; the current time when None.

None

Returns:

Type Description
Path

<the product's folder>/archive/<day and hour>/; the product's own folder when it already sits inside an archive.

pyflightstream.post.provenance.prov_document

prov_document(
    record: RunRecord, sim_dir: Path
) -> dict[str, object]

Build one run's PROV-JSON document from its record and the files it left.

W3C PROV, in the PROV-JSON serialization (design 68): the run record carried every fact a provenance document needs and lacked a shape another tool reads without reading this package's docs. ENTITIES are each staged input (inputs_sha256), the script (script_sha256) and each collected output, every one with its sha256 under pyfs:sha256; pyfs:sha256_from says where an output's hash came from. record is the digest the run recorded, used when the file is gone or its bytes differ. file is computed from the file on disk: it equals the recorded digest, or the run recorded none (an older record), in which case the file's digest is the only one there is and the label is what says so.

WHERE THE FILE'S BYTES ARE NOT THE RECORDED ONES the output entity keeps the RECORDED digest and the generation claim, because that claim is true, and what the file holds now becomes a second entity, pyfs:file/<name>, of type pyfs:ChangedOutput, listed under wasDerivedFrom and generated by no activity (PFS-2038.01). The document therefore never asserts that a run produced bytes it did not. It does not refuse: an edited or re-exported output is a workspace's business, and saying so correctly is the fix.

The ACTIVITY is the solver run, with prov:startTime and prov:endTime where the record carries them, the wall time, the status and the executor's argv. The AGENTS are the package at its version and commit and the solver build at its executable identity, both prov:SoftwareAgent. The activity used the inputs and the script, every output wasGeneratedBy it, it wasAssociatedWith both agents, the outputs are attributed to the solver and the script to the package. Standard library only: the document is a mapping :mod:json writes.

Parameters:

Name Type Description Default
record RunRecord

The run's record.

required
sim_dir Path

The simulation folder, where the outputs are hashed when still present.

required

Returns:

Type Description
dict of str to object

The PROV-JSON document.

pyflightstream.post.provenance.provenance_file_name

provenance_file_name(
    run_id: str, *, point_name: str | None = None
) -> str

Return the name of one run's provenance document (FR-86).

<point name>.prov.json when the point's own name is known, which is the convention the script and every export of the same point carry and what lets a reader sort scripts/ and provenance/ side by side. <run id with '/' replaced by '_'>.prov.json otherwise, which is what every document was named in legacy releases.

NOTHING RENAMES A RUN. The run id is unchanged: it keys the products manifest and it is a field inside the document, under pyfs:run_id, and it is the identifier of the activity. What moves is a file name, which this package never parses for meaning.

Parameters:

Name Type Description Default
run_id str

The run's id.

required
point_name str

The point's name, where it is known.

None

Returns:

Type Description
str

The document's file name.

pyflightstream.post.provenance.refuse_an_existing_product

refuse_an_existing_product(
    path: Path,
    *,
    archive: bool = True,
    stamp: datetime | None = None,
) -> Path

Return path, ARCHIVING an existing product rather than losing it.

A regenerated SUPER file can report different numbers: without an archive the previous table is gone and nothing says it ever said something else.

THREE BEHAVIOURS AND ONE FLAG, and the default is the first:

IT PREPARES THE DESTINATION AND WRITES NOTHING. The caller writes the replacement, at the path this returns.

  • the product exists and archive holds: it is MOVED into <its own folder>/archive/<day and hour>/, so the caller's write lands on a free path. Nothing is lost and nothing is refused.
  • the product exists and archive is false: it is LEFT IN PLACE for the caller's write to replace, and no copy is kept. That is the explicit escape, and the command line spells it --force-overwrite and asks for a confirmation, so it cannot be reached by habit. A caller that then writes nothing, as a refused or skipped product does, names the product under the post's skipped, and the post's retirement REMOVES the file, so no stale file stays current.
  • the product does not exist: nothing happens.

IT TOOK AN overwrite FLAG TOO, AND THAT WAS THE DEFECT. The guard read not archive and overwrite, so a caller asking for no archive with no overwrite, which is the documented do-not-archive request, fell through and archived anyway: one cell of a two-boolean truth table that no test named and no shipped path reached. Making them independent left overwrite deciding nothing at all, and a parameter that decides nothing is one the next caller will set and be surprised by, so it is gone rather than left dead (the QA lens, rounds one and two).

THE OLD REFUSAL IS GONE, which is the part worth saying plainly. It existed to stop a rebuild destroying a product silently, and archiving answers that better: a refusal makes the user delete the file, which destroys it just as thoroughly and puts the work on them.

Parameters:

Name Type Description Default
path Path

The product's destination.

required
archive bool

Move an existing product into the archive; when false it is left for the caller's write to replace.

True
stamp datetime

The rebuild's stamp, which names the archive folder.

None

Returns:

Type Description
Path

path, free where archive holds.

pyflightstream.post.provenance.run_provenance

run_provenance(
    workspace: CampaignWorkspace,
    records: Sequence[RunRecord],
    out: Path,
    *,
    overwrite: bool = False,
    archive: bool = True,
    archive_stamp: datetime | None = None,
    sims: Collection[str] | None = None,
) -> dict[str, str]

Write one PROV-JSON document per record under out/provenance.

Every recorded run, whatever its status: a failed run's provenance is evidence about the failure. Returns the manifest's provenance map, run id to the document's path relative to out. An existing document is archived by default. With archive=False, replacing it requires overwrite=True and keeps no copy; otherwise it is refused.

With sims (FR-307, post --sims) only the records of those simulations are written and returned; the names are still measured over every record given, so each document keeps the name a whole post gives it.

Parameters:

Name Type Description Default
workspace CampaignWorkspace

The workspace.

required
records sequence of RunRecord

The records to write a document for.

required
out Path

The products folder; the documents go under out/provenance.

required
overwrite bool

With archive false, replace an existing document.

False
archive bool

Archive an existing document before it is replaced.

True
archive_stamp datetime

The rebuild's stamp, which names the archive folder.

None
sims collection of str

Write only the records of these simulations.

None

Returns:

Type Description
dict of str to str

Run id to the document's path relative to out.

Raises:

Type Description
ProductExistsError

If a document exists, archive is false and overwrite is not set.