-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_to_new_structure.py
More file actions
229 lines (190 loc) · 7.22 KB
/
migrate_to_new_structure.py
File metadata and controls
229 lines (190 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python3
"""
Migrate plots from legacy structure to new structure.
Legacy structure:
- spec.md
- metadata.yaml (monolithic with all library data)
New structure:
- specification.md
- specification.yaml (spec-level only: tags, created, issue, suggested, updates)
- metadata/{library}.yaml (per-library: preview_url, current, history)
Usage:
python scripts/migrate_to_new_structure.py [--dry-run]
"""
import argparse
import logging
import shutil
import sys
from pathlib import Path
import yaml
# Configuration
BASE_DIR = Path(__file__).parent.parent.parent
PLOTS_DIR = BASE_DIR / "plots"
# Logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
def migrate_plot_directory(plot_dir: Path, dry_run: bool = False) -> bool:
"""
Migrate a single plot directory to new structure.
Args:
plot_dir: Path to the plot directory
dry_run: If True, only log what would be done
Returns:
True if migration successful, False otherwise
"""
spec_id = plot_dir.name
logger.info(f"Migrating: {spec_id}")
# Check if already migrated
if (plot_dir / "specification.md").exists():
logger.info(" Already migrated (specification.md exists)")
return True
# Check for legacy files
legacy_spec = plot_dir / "spec.md"
legacy_metadata = plot_dir / "metadata.yaml"
if not legacy_spec.exists():
logger.warning(" No spec.md found, skipping")
return False
# Step 1: Rename spec.md -> specification.md
new_spec = plot_dir / "specification.md"
if dry_run:
logger.info(" [DRY RUN] Would rename: spec.md -> specification.md")
else:
shutil.move(legacy_spec, new_spec)
logger.info(" Renamed: spec.md -> specification.md")
# Step 2: Split metadata.yaml
if not legacy_metadata.exists():
logger.info(" No metadata.yaml found, creating empty specification.yaml")
if not dry_run:
spec_yaml = {
"specification_id": spec_id,
"title": spec_id.replace("-", " ").title(),
"created": None,
"issue": None,
"suggested": None,
"updates": [],
"tags": {
"plot_type": [],
"domain": ["general"],
"features": ["basic"],
"audience": ["beginner"],
"data_type": ["numeric"],
},
}
with open(plot_dir / "specification.yaml", "w") as f:
yaml.dump(spec_yaml, f, default_flow_style=False, sort_keys=False)
return True
# Load legacy metadata
try:
with open(legacy_metadata) as f:
legacy_data = yaml.safe_load(f) or {}
except Exception as e:
logger.error(f" Failed to parse metadata.yaml: {e}")
return False
# Create specification.yaml (spec-level only)
spec_yaml = {
"specification_id": spec_id,
"title": legacy_data.get("title", spec_id.replace("-", " ").title()),
"created": legacy_data.get("created"),
"issue": legacy_data.get("issue"),
"suggested": legacy_data.get("suggested"),
"updates": legacy_data.get("updates", []),
"tags": legacy_data.get(
"tags",
{
"plot_type": [],
"domain": ["general"],
"features": ["basic"],
"audience": ["beginner"],
"data_type": ["numeric"],
},
),
}
if dry_run:
logger.info(" [DRY RUN] Would create: specification.yaml")
else:
with open(plot_dir / "specification.yaml", "w") as f:
yaml.dump(spec_yaml, f, default_flow_style=False, sort_keys=False)
logger.info(" Created: specification.yaml")
# Create metadata/{library}.yaml for each implementation
implementations = legacy_data.get("implementations", {})
if implementations:
metadata_dir = plot_dir / "metadata"
if dry_run:
logger.info(" [DRY RUN] Would create: metadata/ directory")
else:
metadata_dir.mkdir(exist_ok=True)
logger.info(" Created: metadata/ directory")
for library_id, impl_data in implementations.items():
library_yaml = {
"library": library_id,
"specification_id": spec_id,
"preview_url": impl_data.get("preview_url"),
"preview_html": impl_data.get("preview_html"),
"current": impl_data.get("current"),
"history": impl_data.get("history", []),
}
if dry_run:
logger.info(f" [DRY RUN] Would create: metadata/{library_id}.yaml")
else:
with open(metadata_dir / f"{library_id}.yaml", "w") as f:
yaml.dump(library_yaml, f, default_flow_style=False, sort_keys=False)
logger.info(f" Created: metadata/{library_id}.yaml")
# Step 3: Delete legacy metadata.yaml
if dry_run:
logger.info(" [DRY RUN] Would delete: metadata.yaml")
else:
legacy_metadata.unlink()
logger.info(" Deleted: metadata.yaml")
return True
def main() -> int:
"""Main entry point for the migration script."""
parser = argparse.ArgumentParser(description="Migrate plots to new structure")
parser.add_argument("--dry-run", action="store_true", help="Show what would be done without making changes")
parser.add_argument("--spec-id", type=str, help="Migrate only a specific spec ID")
args = parser.parse_args()
logger.info("=" * 60)
logger.info("Plot Structure Migration")
logger.info("=" * 60)
logger.info(f"Plots directory: {PLOTS_DIR}")
logger.info(f"Dry run: {args.dry_run}")
logger.info("")
if not PLOTS_DIR.exists():
logger.error(f"Plots directory not found: {PLOTS_DIR}")
return 1
# Collect directories to migrate
if args.spec_id:
plot_dirs = [PLOTS_DIR / args.spec_id]
if not plot_dirs[0].exists():
logger.error(f"Spec not found: {args.spec_id}")
return 1
else:
plot_dirs = sorted([d for d in PLOTS_DIR.iterdir() if d.is_dir() and not d.name.startswith(".")])
logger.info(f"Found {len(plot_dirs)} plot directories")
logger.info("")
# Migrate each directory
success_count = 0
fail_count = 0
for plot_dir in plot_dirs:
try:
if migrate_plot_directory(plot_dir, dry_run=args.dry_run):
success_count += 1
else:
fail_count += 1
except Exception as e:
logger.error(f"Failed to migrate {plot_dir.name}: {e}")
fail_count += 1
# Summary
logger.info("")
logger.info("=" * 60)
logger.info("Migration Summary")
logger.info("=" * 60)
logger.info(f"Successful: {success_count}")
logger.info(f"Failed: {fail_count}")
logger.info(f"Total: {len(plot_dirs)}")
if args.dry_run:
logger.info("")
logger.info("This was a DRY RUN. No changes were made.")
logger.info("Run without --dry-run to apply changes.")
return 0 if fail_count == 0 else 1
if __name__ == "__main__":
sys.exit(main())