|
| 1 | +# Restructuring Script |
| 2 | + |
| 3 | +This directory contains a script to restructure the glean package from: |
| 4 | + |
| 5 | +``` |
| 6 | +src/glean/ # All implementation files |
| 7 | +``` |
| 8 | + |
| 9 | +To: |
| 10 | + |
| 11 | +``` |
| 12 | +src/glean/ # Implicit namespace package (no __init__.py) |
| 13 | +src/glean/api_client/ # All implementation files moved here |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +### Analyze what would change (recommended first step) |
| 19 | + |
| 20 | +```bash |
| 21 | +python scripts/restructure_to_namespace.py --dry-run |
| 22 | +``` |
| 23 | + |
| 24 | +This shows you: |
| 25 | + |
| 26 | +- Which files would be moved |
| 27 | +- Which import statements would be updated |
| 28 | +- Current state of the transformation |
| 29 | + |
| 30 | +### Perform the restructuring |
| 31 | + |
| 32 | +```bash |
| 33 | +python scripts/restructure_to_namespace.py |
| 34 | +``` |
| 35 | + |
| 36 | +This script: |
| 37 | + |
| 38 | +- **Detects Speakeasy regeneration** and automatically handles it |
| 39 | +- Creates a backup and moves all files |
| 40 | +- Uses implicit namespace packages (no `__init__.py` needed) |
| 41 | +- Can be run multiple times safely |
| 42 | +- Updates all import statements throughout the codebase |
| 43 | + |
| 44 | +## Smart Speakeasy Integration |
| 45 | + |
| 46 | +The script automatically detects when Speakeasy has regenerated files: |
| 47 | + |
| 48 | +1. **First run**: Moves everything to `api_client/` |
| 49 | +2. **After Speakeasy regeneration**: Detects new files in `src/glean/`, removes old `api_client/`, and re-runs the transformation |
| 50 | +3. **Subsequent runs**: Detects already-transformed structure and skips |
| 51 | + |
| 52 | +This means you can safely run the script as part of your build process! |
| 53 | + |
| 54 | +## Examples |
| 55 | + |
| 56 | +```bash |
| 57 | +# First, see what would be changed |
| 58 | +python scripts/restructure_to_namespace.py --dry-run |
| 59 | + |
| 60 | +# If it looks good, perform the restructuring |
| 61 | +python scripts/restructure_to_namespace.py |
| 62 | + |
| 63 | +# Safe to run multiple times - it will detect and handle various states |
| 64 | +python scripts/restructure_to_namespace.py # Skips if already done |
| 65 | +python scripts/restructure_to_namespace.py # Auto-detects Speakeasy regeneration |
| 66 | +``` |
| 67 | + |
| 68 | +## What the restructuring does |
| 69 | + |
| 70 | +1. **Creates a backup** of the current `src/glean` directory |
| 71 | +2. **Moves all files** from `src/glean/` to `src/glean/api_client/` |
| 72 | +3. **Creates an implicit namespace package** (no `__init__.py` - Python 3.3+ feature) |
| 73 | +4. **Updates all import statements** in tests, examples, and internal files |
| 74 | +5. **Handles Speakeasy regeneration** automatically |
| 75 | + |
| 76 | +## After restructuring |
| 77 | + |
| 78 | +Users will need to update their imports: |
| 79 | + |
| 80 | +### Before |
| 81 | + |
| 82 | +```python |
| 83 | +from glean import Glean, models, errors |
| 84 | +from glean.utils import parse_datetime |
| 85 | +``` |
| 86 | + |
| 87 | +### After |
| 88 | + |
| 89 | +```python |
| 90 | +from glean.api_client import Glean, models, errors |
| 91 | +from glean.api_client.utils import parse_datetime |
| 92 | +``` |
| 93 | + |
| 94 | +## Workflow Integration |
| 95 | + |
| 96 | +You can integrate this into your build process: |
| 97 | + |
| 98 | +```bash |
| 99 | +# In your build script or CI |
| 100 | +speakeasy generate # Regenerates files to src/glean/ |
| 101 | +python scripts/restructure_to_namespace.py # Automatically detects and re-transforms |
| 102 | +``` |
| 103 | + |
| 104 | +## Recovery |
| 105 | + |
| 106 | +If something goes wrong, the script provides the path to the backup directory: |
| 107 | + |
| 108 | +```bash |
| 109 | +rm -rf src/glean |
| 110 | +cp -r /path/to/backup/glean src/glean |
| 111 | +``` |
| 112 | + |
| 113 | +## Testing after restructuring |
| 114 | + |
| 115 | +```bash |
| 116 | +# Run tests |
| 117 | +python -m pytest |
| 118 | + |
| 119 | +# Try importing |
| 120 | +python -c "from glean.api_client import Glean; print('Success!')" |
| 121 | +``` |
| 122 | + |
0 commit comments