|
3 | 3 | This directory contains a script to restructure the glean package from: |
4 | 4 |
|
5 | 5 | ``` |
6 | | -src/glean/ # All implementation files |
| 6 | +src/glean/ # All implementation files |
7 | 7 | ``` |
8 | 8 |
|
9 | 9 | To: |
10 | 10 |
|
11 | 11 | ``` |
12 | | -src/glean/ # Implicit namespace package (no __init__.py) |
| 12 | +src/glean/ # Implicit namespace package (no __init__.py) |
13 | 13 | src/glean/api_client/ # All implementation files moved here |
14 | 14 | ``` |
15 | 15 |
|
16 | 16 | ## Usage |
17 | 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 | 18 | ```bash |
33 | 19 | python scripts/restructure_to_namespace.py |
34 | 20 | ``` |
35 | 21 |
|
36 | 22 | This script: |
37 | 23 |
|
38 | | -- **Detects Speakeasy regeneration** and automatically handles it |
| 24 | +- Detects Speakeasy regeneration and automatically handles it |
39 | 25 | - Creates a backup and moves all files |
40 | 26 | - Uses implicit namespace packages (no `__init__.py` needed) |
41 | 27 | - Can be run multiple times safely |
42 | 28 | - Updates all import statements throughout the codebase |
43 | 29 |
|
44 | | -## Smart Speakeasy Integration |
| 30 | +## Speakeasy Integration |
45 | 31 |
|
46 | 32 | The script automatically detects when Speakeasy has regenerated files: |
47 | 33 |
|
48 | 34 | 1. **First run**: Moves everything to `api_client/` |
49 | 35 | 2. **After Speakeasy regeneration**: Detects new files in `src/glean/`, removes old `api_client/`, and re-runs the transformation |
50 | 36 | 3. **Subsequent runs**: Detects already-transformed structure and skips |
51 | 37 |
|
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 | 38 | ## What the restructuring does |
69 | 39 |
|
70 | 40 | 1. **Creates a backup** of the current `src/glean` directory |
71 | 41 | 2. **Moves all files** from `src/glean/` to `src/glean/api_client/` |
72 | 42 | 3. **Creates an implicit namespace package** (no `__init__.py` - Python 3.3+ feature) |
73 | 43 | 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 | 44 |
|
94 | 45 | ## Workflow Integration |
95 | 46 |
|
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 |
| 47 | +The repository includes a GitHub Actions workflow (`.github/workflows/patch-speakeasy-pr.yml`) that automatically runs the restructuring script. |
105 | 48 |
|
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 | | -``` |
| 49 | +- **Triggers**: Runs on PRs with "Update SDK - Generate" in the title (Speakeasy PRs) or manual dispatch |
| 50 | +- **Process**: Automatically runs the restructuring script when Speakeasy regenerates the SDK |
| 51 | +- **Auto-commit**: Commits and pushes the restructured files back to the same PR branch |
122 | 52 |
|
| 53 | +This means Speakeasy PRs are automatically transformed to the namespace |
| 54 | +structure without manual intervention. |
0 commit comments