"""WINC machine adoption - UNDO (reverse an applied import batch). A thin CLI over the shared, import-agnostic journal.undo_batch. It is WINC-named only for discoverability; the actual work is generic, so the same reversal serves any importer that wrote through the journal. Reverting is conservative: a cell a human changed since the import is left as-is and reported, and a created business unit is deleted only when nothing outside the batch still references it. """ import argparse import os import sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname( os.path.dirname(os.path.abspath(__file__)))))) from dotenv import load_dotenv from pathlib import Path load_dotenv(Path(__file__).resolve().parents[3] / '.env') from scripts.site_imports import journal from scripts.site_imports.winc.dry_run import build_app_connection from scripts.site_imports.winc.writer import _UNDO_REFERENCES def main(argv=None): parser = argparse.ArgumentParser( description='Undo an applied WINC import batch (reversible journal).') parser.add_argument('--batch', required=True, help='The batch id to reverse.') parser.add_argument('--commit', action='store_true', help='Actually reverse. Without it, this is a no-op.') args = parser.parse_args(argv) if not args.commit: print('DRY: pass --commit to reverse batch %s' % args.batch) return 0 _app, connection = build_app_connection() from shopdb.api import db try: result = journal.undo_batch(connection, args.batch, references=_UNDO_REFERENCES) db.session.commit() except Exception: db.session.rollback() raise print(journal.format_undo_result(result)) return 0 if __name__ == '__main__': sys.exit(main())