climate_ref_core.output_files
#
Raw-file operations on diagnostic execution outputs.
This module groups every operation that manipulates the files produced by a diagnostic execution.
Execution output is written to the scratch directory.
When the execution is ingested,
a curated subset of outputs are copied from the scratch directory to the results directory:
- logs
- the metric bundle
- the output bundle
- the files the output bundle references (plots/data/html)
- the series bundle
Only files in the results directory are accessed by the API/public.
For some tests we must sanitise paths to files as well as the contents of text files
(:class:PlaceholderMap).
This ensures that the regression data is machine independent.
PLACEHOLDER_OUTPUT_DIR = '<OUTPUT_DIR>'
module-attribute
#
Placeholder substituted for the absolute execution output directory.
PLACEHOLDER_SOFTWARE_ROOT_DIR = '<SOFTWARE_ROOT_DIR>'
module-attribute
#
Placeholder substituted for the absolute shared-software root directory.
Provider command lines stamped into CMEC provenance reference the installed software environment (e.g. a conda prefix) under this root.
PLACEHOLDER_TEST_DATA_DIR = '<TEST_DATA_DIR>'
module-attribute
#
Placeholder substituted for the absolute provider test-data directory.
SANITISED_FILE_GLOBS = ('*.json', '*.txt', '*.yaml', '*.yml', '*.html', '*.xml')
module-attribute
#
Text artefacts whose absolute paths are rewritten for portability.
Binary outputs (.nc, .png, ...) are never rewritten.
PlaceholderMap
#
Map between absolute runtime directories and portable <TOKEN> placeholders.
A committed regression bundle is made machine-independent by replacing host-specific absolute paths with stable tokens. The same map drives every direction:
- :meth:
sanitiserewrites absolute paths to tokens (capture / mint). - :meth:
hydraterewrites tokens back to absolute paths (replay / rebuild). - :meth:
as_replacementsyields the{absolute: token}mapping the bundle and series comparators apply to a freshly regenerated artefact before diffing.
The two configuration-stable tokens (<TEST_DATA_DIR> / <SOFTWARE_ROOT_DIR>)
are fixed for a whole run,
while the per-execution <OUTPUT_DIR> is late-bound with :meth:with_output.
Source code in packages/climate-ref-core/src/climate_ref_core/output_files.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
is_output_bound
property
#
Whether <OUTPUT_DIR> has been bound via :meth:with_output.
The committed-bundle writer requires this: an unbound map would leave execution-specific output paths in the committed bundle.
pairs
instance-attribute
#
Ordered (token, absolute_directory) pairs.
Application order is not significant:
all rewriting goes through :func:rewrite_tree,
which re-sorts longest-match-first so an overlapping shorter path cannot shadow a longer one.
as_replacements()
#
Return the {absolute_directory: token} mapping (real path -> placeholder).
This is what the bundle and series comparators apply to a regenerated artefact before diffing it against the committed (already-placeholdered) baseline.
Source code in packages/climate-ref-core/src/climate_ref_core/output_files.py
for_baseline(*, test_data_dir, software_root_dir=None)
classmethod
#
Build the configuration-stable placeholder set for a committed baseline.
Holds every token except the per-execution output directory,
which is added with :meth:with_output.
software_root_dir is optional:
when None no <SOFTWARE_ROOT_DIR> substitution is applied --
a verification context that does not know the shared-software root relies on this,
and the omission is explicit and declared once.
Source code in packages/climate-ref-core/src/climate_ref_core/output_files.py
hydrate(directory, globs=SANITISED_FILE_GLOBS)
#
Inverse of :meth:sanitise: rewrite <TOKEN> placeholders back to absolute paths.
Binary files are never touched. In-place.
Source code in packages/climate-ref-core/src/climate_ref_core/output_files.py
sanitise(directory, globs=SANITISED_FILE_GLOBS)
#
Rewrite absolute paths to <TOKEN> placeholders in every text artefact under directory.
Binary files are never touched. In-place.
Source code in packages/climate-ref-core/src/climate_ref_core/output_files.py
with_output(output_dir)
#
Return a new map that also binds <OUTPUT_DIR> to the per-execution output_dir.
Rebinding replaces any existing <OUTPUT_DIR> entry,
so a second call hydrates to the latest directory rather than the first.
Source code in packages/climate-ref-core/src/climate_ref_core/output_files.py
copy_execution_outputs(scratch_directory, results_directory, fragment, result, *, include_log=False, extra_globs=())
#
Copy the curated set of persisted outputs from scratch to results.
This is the canonical definition of what REF persists for a successful execution,
- the metric bundle
- the output bundle
- every file it references (plots/data/html)
- the series bundle
- the execution log (if
include_log=True) - any files matching
extra_globs(a diagnostic's declared reconstruction inputs)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scratch_directory
|
Path
|
Base scratch directory the diagnostic wrote into. |
required |
results_directory
|
Path
|
Base results directory to copy the curated subset into. |
required |
fragment
|
Path | str
|
The per-execution fragment under both base directories. |
required |
result
|
ExecutionResult
|
The successful execution result (must carry a metric bundle filename). |
required |
include_log
|
bool
|
If True, copy the execution log. |
False
|
extra_globs
|
tuple[str, ...]
|
Additional output globs (relative to the execution directory) to persist beyond the
bundle-referenced files, e.g. a diagnostic's
:attr: |
()
|
Returns:
| Type | Description |
|---|---|
list[Path]
|
The copied files, each relative to |
Source code in packages/climate-ref-core/src/climate_ref_core/output_files.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
copy_output_file(scratch_directory, results_directory, fragment, filename)
#
Copy a single output file from the scratch directory to the results directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scratch_directory
|
Path
|
The base directory where the file is currently located. |
required |
results_directory
|
Path
|
The base directory where the file should be copied to. |
required |
fragment
|
Path | str
|
The per-execution fragment under both base directories. |
required |
filename
|
Path | str
|
The file to copy, relative to |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The copied file's path, relative to |
Source code in packages/climate-ref-core/src/climate_ref_core/output_files.py
ordered_replacements(replacements)
#
Order replacements longest-key-first.
This is the canonical replacement ordering for all sanitisation: an overlapping shorter key cannot partially shadow a longer one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
replacements
|
dict[str, str]
|
Mapping of substring to replacement. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, str]]
|
The |
Source code in packages/climate-ref-core/src/climate_ref_core/output_files.py
rewrite_tree(directory, replacements, globs=SANITISED_FILE_GLOBS)
#
Apply replacements to the text content of every matching file under directory.
Keys are applied longest-first so that an overlapping shorter path cannot
partially shadow a longer one.
Only files matching globs are rewritten while binary artefacts are never touched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Path
|
The tree of files to rewrite in place. |
required |
replacements
|
dict[str, str]
|
Mapping of substring to replacement, applied to every matching file. |
required |
globs
|
tuple[str, ...]
|
File globs whose contents are rewritten. |
SANITISED_FILE_GLOBS
|