|
3524 | 3524 | "import sys\n", |
3525 | 3525 | "from pathlib import Path\n", |
3526 | 3526 | "\n", |
| 3527 | + "if \"get_project_root\" not in globals():\n", |
| 3528 | + " NOTEBOOK_FILENAME = \"complete_setup_guide.ipynb\"\n", |
| 3529 | + " PROJECT_ROOT_MARKERS = ((\"src\", \"api\"), (\"scripts\", \"setup\"))\n", |
| 3530 | + " NOTEBOOK_SETUP_DIR = \"setup\"\n", |
| 3531 | + " NOTEBOOKS_DIR = \"notebooks\"\n", |
| 3532 | + "\n", |
| 3533 | + " def _has_markers(path: Path) -> bool:\n", |
| 3534 | + " return (\n", |
| 3535 | + " (path / PROJECT_ROOT_MARKERS[0][0] / PROJECT_ROOT_MARKERS[0][1]).exists()\n", |
| 3536 | + " and (path / PROJECT_ROOT_MARKERS[1][0] / PROJECT_ROOT_MARKERS[1][1]).exists()\n", |
| 3537 | + " )\n", |
| 3538 | + "\n", |
| 3539 | + " def get_project_root():\n", |
| 3540 | + " \"\"\"Detect repo root (same rules as Step 2) if that cell was not run.\"\"\"\n", |
| 3541 | + " import builtins\n", |
| 3542 | + " import os\n", |
| 3543 | + "\n", |
| 3544 | + " if hasattr(builtins, \"__project_root__\"):\n", |
| 3545 | + " cached = Path(builtins.__project_root__)\n", |
| 3546 | + " if _has_markers(cached):\n", |
| 3547 | + " return cached\n", |
| 3548 | + " del builtins.__project_root__\n", |
| 3549 | + "\n", |
| 3550 | + " current = Path.cwd()\n", |
| 3551 | + " if _has_markers(current):\n", |
| 3552 | + " project_root = current\n", |
| 3553 | + " elif current.name == NOTEBOOK_SETUP_DIR and current.parent.name == NOTEBOOKS_DIR:\n", |
| 3554 | + " project_root = current.parent.parent\n", |
| 3555 | + " elif current.name == NOTEBOOKS_DIR:\n", |
| 3556 | + " project_root = current.parent\n", |
| 3557 | + " else:\n", |
| 3558 | + " project_root = current\n", |
| 3559 | + " for parent in current.parents:\n", |
| 3560 | + " if _has_markers(parent):\n", |
| 3561 | + " project_root = parent\n", |
| 3562 | + " break\n", |
| 3563 | + "\n", |
| 3564 | + " if not _has_markers(project_root):\n", |
| 3565 | + " try:\n", |
| 3566 | + " for child in sorted(current.iterdir(), key=lambda p: p.name.lower()):\n", |
| 3567 | + " if child.is_dir() and _has_markers(child):\n", |
| 3568 | + " project_root = child\n", |
| 3569 | + " break\n", |
| 3570 | + " except (OSError, PermissionError):\n", |
| 3571 | + " pass\n", |
| 3572 | + "\n", |
| 3573 | + " os.chdir(project_root)\n", |
| 3574 | + " builtins.__project_root__ = project_root\n", |
| 3575 | + " return project_root\n", |
| 3576 | + "\n", |
3527 | 3577 | "def generate_demo_data():\n", |
3528 | 3578 | " \"\"\"Generate demo data for testing.\"\"\"\n", |
3529 | 3579 | " print(\"📊 Generating Demo Data\")\n", |
3530 | 3580 | " print(\"=\" * 60)\n", |
3531 | | - " \n", |
3532 | | - " # Determine Python path\n", |
| 3581 | + "\n", |
| 3582 | + " project_root = get_project_root()\n", |
3533 | 3583 | " if sys.platform == \"win32\":\n", |
3534 | | - " python_path = Path(\"env\") / \"Scripts\" / \"python.exe\"\n", |
| 3584 | + " python_path = project_root / \"env\" / \"Scripts\" / \"python.exe\"\n", |
3535 | 3585 | " else:\n", |
3536 | | - " python_path = Path(\"env\") / \"bin\" / \"python\"\n", |
3537 | | - " \n", |
| 3586 | + " python_path = project_root / \"env\" / \"bin\" / \"python\"\n", |
| 3587 | + "\n", |
3538 | 3588 | " if not python_path.exists():\n", |
3539 | 3589 | " print(f\"❌ Python not found at: {python_path}\")\n", |
| 3590 | + " print(\" Make sure virtual environment is set up (Step 3)\")\n", |
3540 | 3591 | " return False\n", |
3541 | | - " \n", |
| 3592 | + "\n", |
3542 | 3593 | " scripts = [\n", |
3543 | 3594 | " (\"scripts/data/quick_demo_data.py\", \"Quick demo data (equipment, inventory)\"),\n", |
3544 | 3595 | " (\"scripts/data/generate_historical_demand.py\", \"Historical demand data (for forecasting)\"),\n", |
3545 | 3596 | " ]\n", |
3546 | | - " \n", |
3547 | | - " for script_path, description in scripts:\n", |
3548 | | - " script = Path(script_path)\n", |
| 3597 | + "\n", |
| 3598 | + " for script_rel, description in scripts:\n", |
| 3599 | + " script = project_root / script_rel\n", |
3549 | 3600 | " if not script.exists():\n", |
3550 | | - " print(f\"⚠️ Script not found: {script_path} (skipping)\")\n", |
| 3601 | + " print(f\"⚠️ Script not found: {script} (skipping)\")\n", |
3551 | 3602 | " continue\n", |
3552 | | - " \n", |
| 3603 | + "\n", |
3553 | 3604 | " print(f\"\\n🔄 {description}...\")\n", |
3554 | 3605 | " result = subprocess.run(\n", |
3555 | 3606 | " [str(python_path), str(script)],\n", |
3556 | 3607 | " capture_output=True,\n", |
3557 | | - " text=True\n", |
| 3608 | + " text=True,\n", |
| 3609 | + " cwd=str(project_root),\n", |
3558 | 3610 | " )\n", |
3559 | | - " \n", |
| 3611 | + "\n", |
3560 | 3612 | " if result.returncode == 0:\n", |
3561 | 3613 | " print(f\"✅ {description} generated\")\n", |
3562 | 3614 | " else:\n", |
3563 | 3615 | " print(f\"⚠️ {description} had issues: {result.stderr[:200]}\")\n", |
3564 | | - " \n", |
| 3616 | + "\n", |
3565 | 3617 | " print(\"\\n\" + \"=\" * 60)\n", |
3566 | 3618 | " print(\"✅ Demo data generation complete!\")\n", |
3567 | 3619 | " print(\"\\n💡 You can skip this step if you don't need demo data.\")\n", |
|
0 commit comments