---
type: "Source File"
title: "!/usr/bin/env python3"
description: "#!/usr/bin/env python3 \"\"\"Flag (never delete) layoff records whose `company` is a descriptive placeholder. Adds two keys to each affected entry: company_valid: false company_flag_reason: \"<rule that fired>\" Entries."
resource: "https://layoffs.copecheck.com"
tags: ["copecheck", "layoffs", "source-file", "py"]
timestamp: "2026-09-25T22:06:33Z"
site: "layoffs"
source_path: "copecheck-layoffs/flag_bad_companies.py"
---

# !/usr/bin/env python3

Source path: `copecheck-layoffs/flag_bad_companies.py`  
Index snapshot generated: `2026-11-24T13:27:52Z`  
File modified: `2026-09-25T22:06:33Z`

```py
#!/usr/bin/env python3
"""Flag (never delete) layoff records whose `company` is a descriptive placeholder.

Adds two keys to each affected entry:
    company_valid: false
    company_flag_reason: "<rule that fired>"
Entries that pass validation get company_valid: true. Nothing else changes.

Run:  python3 flag_bad_companies.py [--dry-run]
"""
from __future__ import annotations

import argparse
import json
import shutil
from collections import Counter
from datetime import datetime, timezone
from pathlib import Path

from company_validation import validate_company

DATA = Path("/home/ben/infra/copecheck-layoffs/data/layoffs.json")
TS = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
LISTS = ["scanned_2026", "scanned_2025", "major_2026", "major_2025", "uk_layoffs"]


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--dry-run", action="store_true")
    args = ap.parse_args()

    data = json.loads(DATA.read_text())
    reasons: Counter = Counter()
    flagged: list[dict] = []
    total = 0

    for key in LISTS:
        entries = data.get(key)
        if not isinstance(entries, list):
            continue
        for e in entries:
            if not isinstance(e, dict) or "company" not in e:
                continue
            total += 1
            ok, reason = validate_company(e.get("company"))
            e["company_valid"] = bool(ok)
            if ok:
                e.pop("company_flag_reason", None)
                continue
            e["company_flag_reason"] = reason
            reasons[reason] += 1
            flagged.append({"list": key, "company": e.get("company"), "reason": reason})

    if not args.dry_run:
        bak = DATA.with_name(f"{DATA.name}.bak-companyflag-{TS}")
        shutil.copy2(DATA, bak)
        tmp = DATA.with_suffix(".json.tmp")
        tmp.write_text(json.dumps(data, indent=1))
        tmp.replace(DATA)
        print(f"WROTE {DATA} (backup {bak.name})")
    else:
        print("DRY RUN - nothing written")

    report = Path(f"/home/ben/infra/copecheck-layoffs/data/company_flag_report-{TS}.json")
    if not args.dry_run:
        report.write_text(json.dumps(
            {"timestamp": TS, "checked": total, "flagged": len(flagged),
             "by_reason": dict(reasons), "entries": flagged}, indent=1))
        print(f"REPORT {report}")

    print(f"checked={total} flagged={len(flagged)}")
    for r, n in reasons.most_common():
        print(f"  {r}: {n}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
```
