245 lines
6.6 KiB
Python
245 lines
6.6 KiB
Python
|
|
"""
|
|||
|
|
Migration Script: Update all files to use centralized config.py
|
|||
|
|
|
|||
|
|
Run this script once to migrate all hardcoded paths to use config.py
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import re
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
def update_nx_updater():
|
|||
|
|
"""Update nx_updater.py to use config"""
|
|||
|
|
file_path = Path("optimization_engine/nx_updater.py")
|
|||
|
|
content = file_path.read_text()
|
|||
|
|
|
|||
|
|
# Add config import after other imports
|
|||
|
|
if "from config import" not in content:
|
|||
|
|
import_section = """from pathlib import Path
|
|||
|
|
from typing import Dict, List, Optional
|
|||
|
|
import re
|
|||
|
|
import shutil
|
|||
|
|
import subprocess
|
|||
|
|
from datetime import datetime"""
|
|||
|
|
|
|||
|
|
new_import = """from pathlib import Path
|
|||
|
|
from typing import Dict, List, Optional
|
|||
|
|
import re
|
|||
|
|
import shutil
|
|||
|
|
import subprocess
|
|||
|
|
from datetime import datetime
|
|||
|
|
import sys
|
|||
|
|
|
|||
|
|
# Import centralized configuration
|
|||
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|||
|
|
from config import NX_RUN_JOURNAL"""
|
|||
|
|
|
|||
|
|
content = content.replace(import_section, new_import)
|
|||
|
|
|
|||
|
|
# Replace hardcoded path with config
|
|||
|
|
old_default = 'self.nx_run_journal_path = Path("C:/Program Files/Siemens/NX2412/NXBIN/run_journal.exe")'
|
|||
|
|
new_default = 'self.nx_run_journal_path = NX_RUN_JOURNAL'
|
|||
|
|
|
|||
|
|
content = content.replace(old_default, new_default)
|
|||
|
|
|
|||
|
|
file_path.write_text(content)
|
|||
|
|
print(f"✓ Updated {file_path}")
|
|||
|
|
|
|||
|
|
def update_dashboard_app():
|
|||
|
|
"""Update dashboard/api/app.py to use config"""
|
|||
|
|
file_path = Path("dashboard/api/app.py")
|
|||
|
|
content = file_path.read_text()
|
|||
|
|
|
|||
|
|
# Add config import at top
|
|||
|
|
if "from config import" not in content:
|
|||
|
|
# Find Flask imports
|
|||
|
|
flask_import_idx = content.find("from flask import")
|
|||
|
|
if flask_import_idx > 0:
|
|||
|
|
# Insert before Flask imports
|
|||
|
|
config_import = """import sys
|
|||
|
|
from pathlib import Path
|
|||
|
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
|||
|
|
from config import NX_RUN_JOURNAL
|
|||
|
|
|
|||
|
|
"""
|
|||
|
|
content = config_import + content
|
|||
|
|
|
|||
|
|
# Replace hardcoded path
|
|||
|
|
old_path = r'nx_executable = r"C:\Program Files\Siemens\NX2412\NXBIN\run_journal.exe"'
|
|||
|
|
new_path = 'nx_executable = str(NX_RUN_JOURNAL)'
|
|||
|
|
|
|||
|
|
content = content.replace(old_path, new_path)
|
|||
|
|
|
|||
|
|
file_path.write_text(content)
|
|||
|
|
print(f"✓ Updated {file_path}")
|
|||
|
|
|
|||
|
|
def update_vscode_settings():
|
|||
|
|
"""Update .vscode/settings.json to use dynamic path"""
|
|||
|
|
file_path = Path(".vscode/settings.json")
|
|||
|
|
|
|||
|
|
# Note: VSCode settings.json can't use Python variables
|
|||
|
|
# Instead, create a note in SYSTEM_CONFIGURATION.md
|
|||
|
|
print(f"⚠ {file_path} - Manual update needed (VSCode doesn't support dynamic paths)")
|
|||
|
|
print(f" Current stub path will work, but update manually if NX version changes")
|
|||
|
|
|
|||
|
|
def create_migration_doc():
|
|||
|
|
"""Create documentation for the migration"""
|
|||
|
|
doc_content = """# Configuration Migration Guide
|
|||
|
|
|
|||
|
|
## Centralized Configuration
|
|||
|
|
|
|||
|
|
All NX and environment paths are now managed in `config.py`.
|
|||
|
|
|
|||
|
|
### To Update NX Version:
|
|||
|
|
|
|||
|
|
**OLD WAY** (required editing multiple files):
|
|||
|
|
1. Edit `optimization_engine/nx_updater.py`
|
|||
|
|
2. Edit `dashboard/api/app.py`
|
|||
|
|
3. Edit `README.md`
|
|||
|
|
4. Edit `docs/SYSTEM_CONFIGURATION.md`
|
|||
|
|
5. Edit `.vscode/settings.json`
|
|||
|
|
6. Search for other hardcoded paths...
|
|||
|
|
|
|||
|
|
**NEW WAY** (edit ONE file):
|
|||
|
|
1. Open `config.py`
|
|||
|
|
2. Change `NX_VERSION = "2412"` to your version (e.g., `"2506"`)
|
|||
|
|
3. Done! All modules automatically use new paths.
|
|||
|
|
|
|||
|
|
### Configuration Variables
|
|||
|
|
|
|||
|
|
Import from `config.py`:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from config import (
|
|||
|
|
NX_VERSION, # "2412"
|
|||
|
|
NX_INSTALLATION_DIR, # Path to NX installation
|
|||
|
|
NX_RUN_JOURNAL, # Path to run_journal.exe
|
|||
|
|
NX_MATERIAL_LIBRARY, # Path to physicalmateriallibrary.xml
|
|||
|
|
NX_PYTHON_STUBS, # Path to Python stubs
|
|||
|
|
PYTHON_ENV_NAME, # "atomizer"
|
|||
|
|
PYTHON_ENV_PATH, # Path to Python executable
|
|||
|
|
PROJECT_ROOT, # Project root directory
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Helper Functions
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from config import get_nx_journal_command
|
|||
|
|
|
|||
|
|
# Generate journal command automatically
|
|||
|
|
cmd = get_nx_journal_command(
|
|||
|
|
journal_script=Path("optimization_engine/import_expressions.py"),
|
|||
|
|
prt_file,
|
|||
|
|
exp_file
|
|||
|
|
)
|
|||
|
|
# Returns: "C:/Program Files/Siemens/NX2412/NXBIN/run_journal.exe" "..." -args "..." "..."
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Validation
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from config import validate_config, print_config
|
|||
|
|
|
|||
|
|
# Validate all paths exist
|
|||
|
|
validate_config() # Raises FileNotFoundError if paths missing
|
|||
|
|
|
|||
|
|
# Print current configuration
|
|||
|
|
print_config() # Shows all paths for debugging
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Files Migrated
|
|||
|
|
|
|||
|
|
- ✅ `optimization_engine/nx_updater.py` - Uses `NX_RUN_JOURNAL`
|
|||
|
|
- ✅ `dashboard/api/app.py` - Uses `NX_RUN_JOURNAL`
|
|||
|
|
- ⚠️ `.vscode/settings.json` - Manual update needed (VSCode limitation)
|
|||
|
|
- ✅ `README.md` - References `config.py`
|
|||
|
|
- ✅ `docs/SYSTEM_CONFIGURATION.md` - References `config.py`
|
|||
|
|
|
|||
|
|
### Testing Configuration
|
|||
|
|
|
|||
|
|
Run:
|
|||
|
|
```bash
|
|||
|
|
python config.py
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
Expected output:
|
|||
|
|
```
|
|||
|
|
================================================================================
|
|||
|
|
ATOMIZER CONFIGURATION
|
|||
|
|
================================================================================
|
|||
|
|
|
|||
|
|
NX Configuration:
|
|||
|
|
Version: 2412
|
|||
|
|
Installation: C:\\Program Files\\Siemens\\NX2412
|
|||
|
|
...
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Future NX Updates
|
|||
|
|
|
|||
|
|
When NX 2506 (or any version) is released:
|
|||
|
|
|
|||
|
|
1. Edit `config.py`:
|
|||
|
|
```python
|
|||
|
|
NX_VERSION = "2506" # Changed from "2412"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
2. Verify installation exists:
|
|||
|
|
```bash
|
|||
|
|
python config.py
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
3. Update VSCode stubs path in `.vscode/settings.json`:
|
|||
|
|
```json
|
|||
|
|
{
|
|||
|
|
"python.analysis.extraPaths": [
|
|||
|
|
"C:\\\\Program Files\\\\Siemens\\\\NX2506\\\\ugopen\\\\pythonStubs"
|
|||
|
|
]
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
That's it! All Python code automatically uses NX2506.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
**Created**: 2025-11-17
|
|||
|
|
**Last Updated**: 2025-11-17
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
doc_path = Path("docs/CONFIG_MIGRATION.md")
|
|||
|
|
doc_path.write_text(doc_content)
|
|||
|
|
print(f"✓ Created {doc_path}")
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
print("=" * 80)
|
|||
|
|
print("MIGRATING TO CENTRALIZED CONFIGURATION")
|
|||
|
|
print("=" * 80)
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
update_nx_updater()
|
|||
|
|
update_dashboard_app()
|
|||
|
|
update_vscode_settings()
|
|||
|
|
create_migration_doc()
|
|||
|
|
|
|||
|
|
print()
|
|||
|
|
print("=" * 80)
|
|||
|
|
print("✓ MIGRATION COMPLETE")
|
|||
|
|
print("=" * 80)
|
|||
|
|
print()
|
|||
|
|
print("Next Steps:")
|
|||
|
|
print("1. Test configuration: python config.py")
|
|||
|
|
print("2. Run a test optimization to verify paths work")
|
|||
|
|
print("3. Commit changes with message: 'refactor: Centralize NX/env configuration in config.py'")
|
|||
|
|
print()
|
|||
|
|
print("To update NX version in future:")
|
|||
|
|
print(" - Edit config.py: NX_VERSION = \"<new_version>\"")
|
|||
|
|
print(" - Run: python config.py (to verify)")
|
|||
|
|
print(" - Done!")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"✗ Migration failed: {e}")
|
|||
|
|
raise
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|