Attach the nested storage command parser to PICurv's top-level parser.
1294def add_storage_parser(subparsers) -> argparse.ArgumentParser:
1295 """!
1296 @brief Attach the nested storage command parser to PICurv's top-level parser.
1297 @param[in] subparsers Value supplied through the `subparsers` argument.
1298 @return Result produced by this operation.
1299 """
1300 parser = subparsers.add_parser(
1301 "storage",
1302 help="Protect, offload, inspect, verify, and restore run/study artifacts.",
1303 formatter_class=argparse.RawTextHelpFormatter,
1304 description=(
1305 "Manage PICurv run and study data through a configured rclone remote.\n"
1306 "Remote archives are checksum-verified before local payload can be pruned.\n\n"
1307 "Examples:\n"
1308 " picurv storage setup --remote labstore:picurv-data\n"
1309 " picurv storage status --run-dir runs/my_run\n"
1310 " picurv storage protect --run-dir runs/my_run --label 'baseline'\n"
1311 " picurv storage offload --study-dir studies/my_study --case-id case_0003\n"
1312 " picurv storage list --search '64-grid'\n"
1313 " picurv storage restore --archive-id <id>"
1314 ),
1315 epilog="Use `picurv storage <action> --help` for action-specific controls.",
1316 )
1317 actions = parser.add_subparsers(dest="storage_action", required=True, help="Storage action")
1318
1319 setup = actions.add_parser("setup", help="Configure a non-secret rclone storage profile.")
1320 setup.add_argument("--remote", required=True, help="Rclone remote and base path, such as labstore:picurv-data.")
1321 setup.add_argument("--profile", default=DEFAULT_PROFILE_NAME, help="Profile name (default: archive).")
1322 setup.add_argument("--storage-config", help=f"Storage YAML path (default: ./{STORAGE_CONFIG_FILENAME}).")
1323 setup.add_argument(
"--compression", choices=
list(STORAGE_COMPRESSION_POLICIES), default=
"auto",
1324 help=COMPRESSION_HELP + " (default: auto).")
1325 setup.add_argument("--chunk-size-gib", type=float, default=DEFAULT_CHUNK_SIZE_GIB,
1326 help=f"Target archive chunk size in GiB (default: {DEFAULT_CHUNK_SIZE_GIB:g}); "
1327 "a larger file stays one chunk.")
1328 setup.add_argument("--workers", type=int, default=DEFAULT_STORAGE_WORKERS,
1329 help="CPU workers for compression and restoration.")
1330 setup.add_argument("--policy", "--offload-policy", dest="offload_policy",
1331 choices=
list(STORAGE_OFFLOAD_POLICIES), default=
"metadata-only",
1332 help=POLICY_HELP + " The profile default (default: metadata-only).")
1333 setup.add_argument("--keep-latest-checkpoint", action="store_true",
1334 help="Retain the newest committed checkpoint after offload.")
1335 setup.add_argument("--staging-directory", help="Optional local directory for one archive chunk at a time.")
1336 setup.add_argument("--dry-run", action="store_true", help="Print the profile without writing it.")
1337
1338 def add_profile_options(action_parser):
1339 """!
1340 @brief Attach shared storage-profile selectors to one action parser.
1341 @param[in] action_parser Value supplied through the `action_parser` argument.
1342 """
1343 action_parser.add_argument("--profile", help="Configured storage profile name.")
1344 action_parser.add_argument("--storage-config", help="Explicit storage YAML path.")
1345
1346 def add_local_target(action_parser, require=True, allow_workspace=True):
1347 """!
1348 @brief Attach the standard run/study/workspace target selectors to one parser.
1349 @param[in] action_parser Value supplied through the `action_parser` argument.
1350 @param[in] require Value supplied through the `require` argument.
1351 @param[in] allow_workspace Whether a whole workspace is a valid target here.
1352 """
1353 group = action_parser.add_mutually_exclusive_group(required=require)
1354 group.add_argument("--run-dir", help="Standalone run directory.")
1355 group.add_argument("--study-dir", help="Sweep study directory.")
1356 if allow_workspace:
1357 group.add_argument(
1358 "--workspace",
1359 help="Workspace root: its configuration, catalog, and assets, not its "
1360 "runs and studies, which are their own artifacts.",
1361 )
1362 action_parser.add_argument(
1363 "--case-id", dest="case_ids", action="append",
1364 help="One numbered study member, such as case_0003; repeat to select several.",
1365 )
1366 action_parser.add_argument(
1367 "--completed", action="store_true",
1368 help="With --study-dir, select every finished member and skip the rest.",
1369 )
1370
1371 def add_retention_options(parser):
1372 """!
1373 @brief Add the per-component local-retention overrides to one action parser.
1374 @param[in] parser Action parser being configured.
1375 @return None.
1376 """
1377 parser.add_argument(
1378 "--retain", action="append", metavar="COMPONENT",
1379 help="Keep this component local regardless of --policy; repeatable and\n"
1380 "comma-separated. One of: " + ", ".join(STORAGE_RETENTION_COMPONENTS) + ".",
1381 )
1382 parser.add_argument(
1383 "--drop", action="append", metavar="COMPONENT",
1384 help="Prune this component locally regardless of --policy; same names as --retain.",
1385 )
1386
1387 status = actions.add_parser("status", help="Show local, protected, cold, and busy artifact state.")
1388 add_local_target(status)
1389 status.add_argument(
"--format", dest=
"output_format", choices=
list(CLI_OUTPUT_FORMATS), default=
"text",
1390 help="Output format (default: text).")
1391
1392 plan = actions.add_parser("plan", help="Show packaging, dependencies, and safety checks without writing.")
1393 add_local_target(plan)
1394 add_profile_options(plan)
1395 plan.add_argument(
"--compression", choices=
list(STORAGE_COMPRESSION_POLICIES),
1396 help=COMPRESSION_HELP + "; defaults to the profile's.")
1397 plan.add_argument(
"--policy", choices=
list(STORAGE_OFFLOAD_POLICIES),
1398 help=POLICY_HELP + " Defaults to the profile's.")
1399 add_retention_options(plan)
1400 plan.add_argument("--workers", type=int, help="CPU workers; defaults to the profile's.")
1401 plan_checkpoint = plan.add_mutually_exclusive_group()
1402 plan_checkpoint.add_argument("--keep-latest-checkpoint", dest="keep_latest_checkpoint", action="store_true",
1403 help="Keep the newest committed checkpoint local under any policy.")
1404 plan_checkpoint.add_argument("--drop-all-checkpoints", dest="keep_latest_checkpoint", action="store_false",
1405 help="Keep no checkpoint local, overriding the profile.")
1406 plan.set_defaults(keep_latest_checkpoint=None)
1407
1408 for name, help_text in (
1409 ("protect", "Upload and verify an archive while retaining all local files."),
1410 ("offload", "Upload and verify an archive, then prune heavy local payload."),
1411 ):
1412 action_parser = actions.add_parser(name, help=help_text)
1413 add_local_target(action_parser)
1414 add_profile_options(action_parser)
1415 action_parser.add_argument(
1416 "--include-inputs", action="store_true",
1417 help="With --workspace, also archive user-supplied files under inputs/.",
1418 )
1419 action_parser.add_argument("--label", help="Human-readable searchable label.")
1420 action_parser.add_argument(
1421 "--notes", help="Free-text note recorded with the archive and shown by `show`."
1422 )
1423 action_parser.add_argument("--tag", dest="tags", action="append", help="Repeatable KEY=VALUE catalog tag.")
1424 action_parser.add_argument(
"--compression", choices=
list(STORAGE_COMPRESSION_POLICIES),
1425 help=COMPRESSION_HELP + "; defaults to the profile's.")
1426 action_parser.add_argument(
"--policy", choices=
list(STORAGE_OFFLOAD_POLICIES),
1427 help=POLICY_HELP + " Defaults to the profile's.")
1428 add_retention_options(action_parser)
1429 action_parser.add_argument("--workers", type=int, help="CPU workers; defaults to the profile's.")
1430 checkpoint_group = action_parser.add_mutually_exclusive_group()
1431 checkpoint_group.add_argument("--keep-latest-checkpoint", dest="keep_latest_checkpoint", action="store_true",
1432 help="Keep the newest committed checkpoint local under any policy.")
1433 checkpoint_group.add_argument("--drop-all-checkpoints", dest="keep_latest_checkpoint", action="store_false",
1434 help="Keep no checkpoint local, overriding the profile.")
1435 action_parser.set_defaults(keep_latest_checkpoint=None)
1436 action_parser.add_argument("--dry-run", action="store_true",
1437 help="Show the plan and safety checks without uploading.")
1438
1439 restore = actions.add_parser("restore", help="Restore a complete archive or selected checkpoints.")
1440 restore_source = restore.add_mutually_exclusive_group(required=True)
1441 restore_source.add_argument("--archive-id", help="Globally unique remote archive ID.")
1442 restore_source.add_argument(
1443 "--workspace-id", help="Restore a workspace archive by its recorded workspace identity."
1444 )
1445 restore_source.add_argument("--run-dir", help="Cold run containing a local storage marker.")
1446 restore_source.add_argument("--study-dir", help="Cold study containing a local storage marker.")
1447 restore.add_argument("--case-id", dest="case_ids", action="append",
1448 help="With --study-dir, one numbered member to restore; repeat to select several.")
1449 add_profile_options(restore)
1450 restore.add_argument("--to", dest="destination", help="Optional alternate restore destination.")
1451 restore.add_argument(
1452 "--checkpoint", dest="checkpoints", action="append", type=int,
1453 help="One committed step; repeat to select several.",
1454 )
1455 restore.add_argument(
1456 "--checkpoint-range", "--checkpoints", dest="checkpoint_ranges", action="append",
1457 help="An inclusive step range as START:END or START:END:STRIDE; repeatable.",
1458 )
1459 restore.add_argument(
1460 "--component", dest="components", action="append",
1461 choices=STORAGE_RESTORE_COMPONENTS,
1462 help="Restore one semantic component; repeat as needed.",
1463 )
1464 restore.add_argument("--force", action="store_true", help="Allow merge into a non-matching existing destination.")
1465 restore.add_argument("--workers", type=int, help="Parallel download/extraction workers.")
1466
1467 prune = actions.add_parser(
1468 "prune", help="Remove verified local asset objects that nothing local still needs."
1469 )
1470 prune.add_argument("--workspace", help="Workspace root; defaults to discovery from the cwd.")
1471 prune.add_argument(
1472 "--assets", action="store_true", required=True,
1473 help="Select the workspace asset store. Required: prune removes nothing else.",
1474 )
1475 prune.add_argument(
1476 "--unused-locally", action="store_true", required=True,
1477 help="Confirm that only objects with no active local run are removed.",
1478 )
1479 prune.add_argument("--dry-run", action="store_true", help="Report the decision only.")
1480 add_profile_options(prune)
1481
1482 verify = actions.add_parser("verify", help="Verify a remote archive completion marker and chunk checksums.")
1483 verify_source = verify.add_mutually_exclusive_group(required=True)
1484 verify_source.add_argument("--archive-id", help="Remote archive ID to verify.")
1485 verify_source.add_argument("--run-dir", help="Run whose recorded archive to verify.")
1486 verify_source.add_argument("--study-dir", help="Study whose recorded archive to verify.")
1487 verify_source.add_argument("--workspace", help="Workspace whose own archive to verify.")
1488 verify.add_argument("--case-id", dest="case_ids", action="append",
1489 help="With --study-dir, one numbered member to verify; repeat to select several.")
1490 add_profile_options(verify)
1491
1492 list_parser = actions.add_parser("list", help="List/search remote archives without local directories.")
1493 add_profile_options(list_parser)
1494 list_parser.add_argument("--search", help="Case-insensitive search across IDs, labels, identities, and tags.")
1495 list_parser.add_argument(
1496 "--workspace-label", help="Show only archives belonging to this workspace identity."
1497 )
1498 list_parser.add_argument(
"--format", dest=
"output_format", choices=
list(CLI_OUTPUT_FORMATS), default=
"text",
1499 help="Output format (default: text).")
1500
1501 show = actions.add_parser("show", help="Print the complete manifest for one archive.")
1502 show.add_argument("--archive-id", required=True, help="Remote archive ID to show.")
1503 add_profile_options(show)
1504 return parser