Skip to content

Commit 974cdd2

Browse files
committed
feat!: Migrate imports from glean to glean.api_client
Add automated tool to transform glean package structure from flat to namespace package layout with Speakeasy regeneration detection. Includes comprehensive documentation and dry-run capabilities.
1 parent 57edf6a commit 974cdd2

File tree

2 files changed

+585
-0
lines changed

2 files changed

+585
-0
lines changed

scripts/README.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
- Which files would be moved
26+
- Which import statements would be updated
27+
- Current state of the transformation
28+
29+
### Perform the restructuring
30+
31+
```bash
32+
python scripts/restructure_to_namespace.py
33+
```
34+
35+
This script:
36+
- **Detects Speakeasy regeneration** and automatically handles it
37+
- Creates a backup and moves all files
38+
- Uses implicit namespace packages (no `__init__.py` needed)
39+
- Can be run multiple times safely
40+
- Updates all import statements throughout the codebase
41+
42+
## Smart Speakeasy Integration
43+
44+
The script automatically detects when Speakeasy has regenerated files:
45+
46+
1. **First run**: Moves everything to `api_client/`
47+
2. **After Speakeasy regeneration**: Detects new files in `src/glean/`, removes old `api_client/`, and re-runs the transformation
48+
3. **Subsequent runs**: Detects already-transformed structure and skips
49+
50+
This means you can safely run the script as part of your build process!
51+
52+
## Examples
53+
54+
```bash
55+
# First, see what would be changed
56+
python scripts/restructure_to_namespace.py --dry-run
57+
58+
# If it looks good, perform the restructuring
59+
python scripts/restructure_to_namespace.py
60+
61+
# Safe to run multiple times - it will detect and handle various states
62+
python scripts/restructure_to_namespace.py # Skips if already done
63+
python scripts/restructure_to_namespace.py # Auto-detects Speakeasy regeneration
64+
```
65+
66+
## What the restructuring does
67+
68+
1. **Creates a backup** of the current `src/glean` directory
69+
2. **Moves all files** from `src/glean/` to `src/glean/api_client/`
70+
3. **Creates an implicit namespace package** (no `__init__.py` - Python 3.3+ feature)
71+
4. **Updates all import statements** in tests, examples, and internal files
72+
5. **Handles Speakeasy regeneration** automatically
73+
74+
## After restructuring
75+
76+
Users will need to update their imports:
77+
78+
### Before
79+
```python
80+
from glean import Glean, models, errors
81+
from glean.utils import parse_datetime
82+
```
83+
84+
### After
85+
```python
86+
from glean.api_client import Glean, models, errors
87+
from glean.api_client.utils import parse_datetime
88+
89+
# Or use an alias for convenience:
90+
import glean.api_client as glean
91+
client = glean.Glean(...)
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+
```

0 commit comments

Comments
 (0)