_nwbfile_cleanup.py
Small, schema-independent analysis-file cleanup plan.
The cleanup contract intentionally matches the rest of Spyglass: take one
snapshot of database paths and the filesystem, classify the set difference,
then unlink it. A leaf *.nwb symlink inside the configured analysis
directory is a managed pointer, so its recorded target may be deleted even
when that target is on another volume.
FileSnapshot records one scanned leaf and CleanupPlan owns the scan,
classification, aggregate validation, and unlink pass.
CleanupPolicy
dataclass
¶
Validated, immutable deletion-safety limits for one cleanup run.
Construct this once -- before any insert-blocking trigger is acquired --
so an out-of-bounds or non-finite argument raises immediately, without
leaving triggers installed. Because the values are validated here,
CleanupPlan applies them at the deletion boundary without repeating
the raw numeric checks.
Source code in src/spyglass/common/_nwbfile_cleanup.py
FileSnapshot
dataclass
¶
One *.nwb path and the target it named during the scan.
Source code in src/spyglass/common/_nwbfile_cleanup.py
from_path(path)
classmethod
¶
Describe a regular file or dangling leaf symlink.
Non-regular targets (directories, sockets, devices, and FIFOs) are
outside the cleanup contract and return None.
Source code in src/spyglass/common/_nwbfile_cleanup.py
CleanupPlan
¶
Scan, classify, validate, and apply one cleanup snapshot.
This is deliberately a trust-the-disk plan. It does not retain inode identities, re-fetch database state per candidate, or attempt to detect changes between scan and unlink. Callers must not overlap cleanup with analysis-file writers.
Source code in src/spyglass/common/_nwbfile_cleanup.py
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 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 | |
walk_analysis_files(analysis_dir, logger)
staticmethod
¶
Yield *.nwb leaves within the analysis tree.
Directory symlinks are NOT traversed. Cleanup deletes files, so it
must not be able to follow a symlinked subdirectory out of
analysis_dir into an unrelated store (a raw/recording root, another
user's tree) and delete untracked *.nwb there -- there is no
protected-store denylist to stop it. Leaf *.nwb symlinks (managed
pointers, including to another volume) are still yielded: os.walk
lists file symlinks in files regardless of followlinks, and a
symlinked analysis_dir root is still scanned because os.walk
descends into its top directory either way. Because symlinked
subdirectories are never followed, no cycle can arise and no
cycle-detection is needed.
Source code in src/spyglass/common/_nwbfile_cleanup.py
files_to_delete
property
¶
Candidate targets captured by this plan.
candidate_bytes
property
¶
Logical bytes recorded for unique candidate target groups.
validate()
¶
Check the inexpensive aggregate deletion backstops.
Uses this plan's already-validated CleanupPolicy limits; the raw
numeric checks ran once when the policy was constructed.
Source code in src/spyglass/common/_nwbfile_cleanup.py
execute(*, dry_run=True)
¶
Report or unlink the candidates captured by this plan.
Destructive execution validates the plan against this plan's
CleanupPolicy immediately before unlinking, so callers cannot
accidentally bypass the aggregate deletion limits. A dry run still
reports a plan that would be refused, but warns that destructive
execution would not proceed.
The recorded target is used instead of resolving a symlink again at execution time. This keeps execution deterministic without adding inode, timestamp, or per-candidate database revalidation machinery. Ordinary unlink failures are logged and cleanup continues, matching existing Spyglass cleanup routines.