-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbuild.py
More file actions
506 lines (418 loc) · 19.1 KB
/
build.py
File metadata and controls
506 lines (418 loc) · 19.1 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
"""
OrthoRoute Build System - Creates manual installation package for KiCad IPC plugins
Follows the working example from layout_stamp (https://github.com/hraftery/layout_stamp)
"""
import os
import sys
import json
import shutil
import zipfile
import logging
from pathlib import Path
from datetime import datetime
from typing import Dict, List, Optional
logging.basicConfig(level=logging.INFO, format='%(message)s')
logger = logging.getLogger(__name__)
class OrthoRouteBuildSystem:
"""Build system for OrthoRoute manual installation package"""
def __init__(self, project_root: Path = None):
self.project_root = project_root or Path(__file__).parent
self.build_dir = self.project_root / "build"
self.version = "1.0.0"
self.plugin_identifier = "com.github.bbenchoff.orthoroute"
self.plugin_name = "OrthoRoute"
def clean_build_directory(self):
"""Clean the build directory"""
logger.info("Cleaning build directory...")
if self.build_dir.exists():
shutil.rmtree(self.build_dir)
self.build_dir.mkdir(exist_ok=True)
logger.info(f"[OK] Build directory cleaned: {self.build_dir}")
def create_plugin_json(self) -> Dict:
"""Create plugin.json using modern schema v1 (strict compliance)"""
return {
"$schema": "https://go.kicad.org/api/schemas/v1",
"identifier": self.plugin_identifier,
"name": self.plugin_name,
"description": "GPU-accelerated PCB autorouter with Manhattan routing and real-time visualization",
"runtime": {
"type": "python",
"min_version": "3.10"
},
"actions": [
{
"identifier": "orthoroute.route",
"name": "OrthoRoute",
"description": "Launch OrthoRoute GPU-accelerated autorouter",
"scopes": ["pcb"],
"entrypoint": "main.py",
"show-button": True,
"icons-light": ["icon-24.png", "icon-48.png"]
}
]
}
def copy_core_files(self, package_dir: Path):
"""Copy core plugin files for manual installation"""
logger.info(f"Copying core files to {package_dir.name}...")
# Copy the orthoroute package directory
orthoroute_src = self.project_root / "orthoroute"
if orthoroute_src.exists():
orthoroute_dst = package_dir / "orthoroute"
shutil.copytree(orthoroute_src, orthoroute_dst)
py_files = len(list(orthoroute_src.rglob('*.py')))
logger.info(f" [OK] Copied orthoroute package: {py_files} Python files")
# Copy main.py entry point
main_file = self.project_root / "main.py"
if main_file.exists():
shutil.copy2(main_file, package_dir / "main.py")
logger.info(f" [OK] Copied main.py entry point")
# Create plugin.json
plugin_json = self.create_plugin_json()
with open(package_dir / "plugin.json", 'w', encoding='utf-8') as f:
json.dump(plugin_json, f, indent=2)
logger.info(f" [OK] Created plugin.json (modern schema v1)")
# Copy icons (rename to match plugin.json)
graphics_src = self.project_root / "graphics"
if graphics_src.exists():
# Copy and rename icon24.png -> icon-24.png
icon24_src = graphics_src / "icon24.png"
if icon24_src.exists():
shutil.copy2(icon24_src, package_dir / "icon-24.png")
logger.info(f" [OK] Copied icon-24.png")
# Copy and rename icon64.png -> icon-48.png (or create if needed)
icon48_src = graphics_src / "icon64.png"
if icon48_src.exists():
shutil.copy2(icon48_src, package_dir / "icon-48.png")
logger.info(f" [OK] Copied icon-48.png")
# Copy requirements.txt
requirements_file = self.project_root / "requirements.txt"
if requirements_file.exists():
shutil.copy2(requirements_file, package_dir / "requirements.txt")
logger.info(" [OK] Copied requirements.txt")
# Copy LICENSE
license_file = self.project_root / "LICENSE"
if license_file.exists():
shutil.copy2(license_file, package_dir / "LICENSE")
logger.info(" [OK] Copied LICENSE")
def create_installation_instructions(self, package_dir: Path):
"""Create detailed installation instructions"""
instructions = f"""# OrthoRoute {self.version} - Manual Installation Instructions
## ⚠️ Important: IPC API Must Be Enabled
Before installing this plugin, you MUST enable the IPC API in KiCad:
1. Open KiCad
2. Go to Preferences → Plugins
3. Check the box "Enable Python API"
4. Click OK
5. Restart KiCad
## Installation Instructions
### Windows
1. Extract the `{self.plugin_identifier}` folder from this ZIP
2. Copy it to: `C:\\Users\\<your-username>\\Documents\\KiCad\\9.0\\plugins\\`
3. Restart KiCad
4. The OrthoRoute button should appear in the PCB Editor toolbar
### macOS
1. Extract the `{self.plugin_identifier}` folder from this ZIP
2. Copy it to: `/Users/<your-username>/Documents/KiCad/9.0/plugins/`
3. Restart KiCad
4. The OrthoRoute button should appear in the PCB Editor toolbar
### Linux
1. Extract the `{self.plugin_identifier}` folder from this ZIP
2. Copy it to: `~/.local/share/KiCad/9.0/plugins/`
3. Restart KiCad
4. The OrthoRoute button should appear in the PCB Editor toolbar
## Dependency Management
KiCad will automatically create a virtual environment and install dependencies
from requirements.txt when you first run the plugin.
The virtual environment is located at:
- Windows: `C:\\Users\\<username>\\AppData\\Local\\KiCad\\9.0\\python-environments\\{self.plugin_identifier}\\`
- macOS: `/Users/<username>/Library/Caches/KiCad/9.0/python-environments/{self.plugin_identifier}/`
- Linux: `~/.cache/KiCad/9.0/python-environments/{self.plugin_identifier}/`
## Requirements
- KiCad 9.0 or later
- Python 3.10 or later (usually included with KiCad)
- Dependencies listed in requirements.txt (auto-installed by KiCad)
## Troubleshooting
### Plugin button doesn't appear
- Verify IPC API is enabled (Preferences → Plugins)
- Check that the folder name matches: `{self.plugin_identifier}`
- Restart KiCad after installation
- Check KiCad logs for errors: `Documents/KiCad/9.0/logs/`
### Dependencies not installing
- Check your internet connection
- Look at: `Documents/KiCad/9.0/logs/api.log` for installation errors
- Manually activate the venv and install: `pip install -r requirements.txt`
### Can't find plugins directory
- Create the directory if it doesn't exist
- The path varies by KiCad version (use 9.0, 9.1, etc.)
## More Information
- Project: https://github.com/bbenchoff/OrthoRoute
- KiCad IPC API Docs: https://dev-docs.kicad.org/en/apis-and-binding/ipc-api/
- Plugin Reference: https://github.com/hraftery/layout_stamp
## Why Manual Installation?
PCM (Plugin and Content Manager) support for IPC plugins is currently broken
on Windows (GitLab issue #19465). Manual installation is the only reliable
method for now. We'll add PCM support once KiCad fixes the issue.
---
Generated: {datetime.now().strftime('%Y-%m-%d')}
Version: {self.version}
"""
with open(package_dir / "INSTALL.txt", 'w', encoding='utf-8') as f:
f.write(instructions)
logger.info(" [OK] Created INSTALL.txt")
def create_package_zip(self, package_dir: Path) -> Path:
"""Create ZIP package for manual installation"""
zip_name = f"{self.plugin_identifier}-{self.version}.zip"
zip_path = self.build_dir / zip_name
logger.info(f"\nCreating installation package: {zip_name}")
# Create ZIP with the plugin folder inside
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
# Add INSTALL.txt at root of ZIP
install_file = package_dir / "INSTALL.txt"
if install_file.exists():
zipf.write(install_file, "INSTALL.txt")
# Add all plugin files under the plugin identifier folder
for file_path in package_dir.rglob('*'):
if file_path.is_file() and file_path.name != "INSTALL.txt":
# Create archive path as: com.github.bbenchoff.orthoroute/...
arcname = self.plugin_identifier + "/" + str(file_path.relative_to(package_dir))
zipf.write(file_path, arcname)
# Calculate size
size_mb = zip_path.stat().st_size / (1024 * 1024)
logger.info(f"[OK] Package created: {zip_name} ({size_mb:.2f} MB)")
return zip_path
def build_package(self) -> Optional[Path]:
"""Build the manual installation package"""
logger.info(f"Building OrthoRoute {self.version} manual installation package")
logger.info(f"Project root: {self.project_root}\n")
# Create package directory
package_dir = self.build_dir / self.plugin_identifier
package_dir.mkdir(parents=True, exist_ok=True)
# Copy files
self.copy_core_files(package_dir)
# Create installation instructions
self.create_installation_instructions(package_dir)
# Create ZIP package
zip_path = self.create_package_zip(package_dir)
return zip_path
def show_completion_message(self, zip_path: Path):
"""Show completion message with installation instructions"""
size_mb = zip_path.stat().st_size / (1024 * 1024)
print("\n" + "="*70)
print("BUILD COMPLETE!")
print("="*70)
print(f"\nPackage: {zip_path.name} ({size_mb:.2f} MB)")
print(f"Location: {zip_path.parent}")
print("\n" + "="*70)
print("INSTALLATION INSTRUCTIONS")
print("="*70)
print("\n1. First, enable IPC API in KiCad:")
print(" - Open KiCad -> Preferences -> Plugins")
print(" - Check 'Enable Python API'")
print(" - Restart KiCad")
print("\n2. Install the plugin:")
print(f" - Extract the ZIP file")
print(f" - Copy the '{self.plugin_identifier}' folder to your plugins directory:")
print(f" * Windows: C:\\Users\\<username>\\Documents\\KiCad\\9.0\\plugins\\")
print(f" * macOS: /Users/<username>/Documents/KiCad/9.0/plugins/")
print(f" * Linux: ~/.local/share/KiCad/9.0/plugins/")
print("\n3. Restart KiCad")
print("\n4. Look for the OrthoRoute button in PCB Editor toolbar")
print("\n" + "="*70)
print("Full instructions included in INSTALL.txt inside the ZIP")
print("="*70 + "\n")
def create_pcm_metadata(self) -> Dict:
"""Create metadata.json for PCM installation (SWIG plugin)"""
return {
"$schema": "https://go.kicad.org/pcm/schemas/v1",
"name": self.plugin_name,
"description": "GPU-accelerated PCB autorouter with Manhattan routing",
"description_full": "Advanced PCB autorouter that leverages GPU acceleration for high-performance routing. Features intelligent pathfinding, real-time visualization, and seamless KiCad integration.",
"identifier": self.plugin_identifier,
"type": "plugin",
"author": {
"name": "OrthoRoute Team",
"contact": {
"web": "https://github.com/bbenchoff/OrthoRoute"
}
},
"maintainer": {
"name": "OrthoRoute Team",
"contact": {
"web": "https://github.com/bbenchoff/OrthoRoute"
}
},
"license": "MIT",
"resources": {
"homepage": "https://github.com/bbenchoff/OrthoRoute",
"repository": "https://github.com/bbenchoff/OrthoRoute",
"issues": "https://github.com/bbenchoff/OrthoRoute/issues",
"icon": "resources/icon.png"
},
"tags": [
"autorouter",
"routing",
"pcb",
"gpu",
"automation"
],
"versions": [
{
"version": self.version,
"status": "stable",
"kicad_version": "9.0",
"platforms": ["windows", "macos", "linux"],
"runtime": "swig"
}
]
}
def create_swig_init_py(self) -> str:
"""Create __init__.py for SWIG ActionPlugin registration"""
return '''"""OrthoRoute KiCad SWIG Plugin"""
import os
import sys
# Add plugin directory to path
plugin_dir = os.path.dirname(os.path.abspath(__file__))
if plugin_dir not in sys.path:
sys.path.insert(0, plugin_dir)
try:
import pcbnew
class OrthoRoutePlugin(pcbnew.ActionPlugin):
"""KiCad Action Plugin wrapper for OrthoRoute."""
def defaults(self):
"""Set plugin defaults."""
self.name = "OrthoRoute"
self.category = "Routing"
self.description = "GPU-accelerated PCB autorouter"
self.show_toolbar_button = True
self.icon_file_name = os.path.join(os.path.dirname(__file__), "icon.png")
def Run(self):
"""Run the plugin."""
try:
# Import the main plugin class
from orthoroute.presentation.plugin.kicad_plugin import KiCadPlugin
# Create and run plugin
plugin = KiCadPlugin()
result = plugin.run_with_gui()
if result:
pcbnew.Refresh()
except Exception as e:
import traceback
import wx
error_msg = f"OrthoRoute error: {e}\n\n{traceback.format_exc()}"
wx.LogError(error_msg) # KiCad 9 uses wx.LogError, not pcbnew.LogError
print(error_msg) # Also print to console
# Register the plugin
OrthoRoutePlugin().register()
except ImportError:
# pcbnew not available - running outside KiCad
pass
'''
def build_pcm_package(self) -> Optional[Path]:
"""Build PCM-installable SWIG plugin package"""
logger.info(f"Building OrthoRoute {self.version} PCM package (SWIG)")
logger.info(f"Project root: {self.project_root}\n")
# Create package directory structure
package_dir = self.build_dir / "pcm_package"
package_dir.mkdir(parents=True, exist_ok=True)
# NOTE: Files must be at root level, NOT in plugins/ subdirectory
resources_dir = package_dir / "resources"
resources_dir.mkdir(exist_ok=True)
logger.info("Copying files for PCM package...")
# Copy orthoroute package to root level
orthoroute_src = self.project_root / "orthoroute"
if orthoroute_src.exists():
orthoroute_dst = package_dir / "orthoroute"
shutil.copytree(orthoroute_src, orthoroute_dst)
py_files = len(list(orthoroute_src.rglob('*.py')))
logger.info(f" [OK] Copied orthoroute package: {py_files} Python files")
# Copy main.py to root level
main_file = self.project_root / "main.py"
if main_file.exists():
shutil.copy2(main_file, package_dir / "main.py")
logger.info(f" [OK] Copied main.py")
# Create __init__.py for SWIG registration at root level
init_content = self.create_swig_init_py()
with open(package_dir / "__init__.py", 'w', encoding='utf-8') as f:
f.write(init_content)
logger.info(f" [OK] Created __init__.py (SWIG ActionPlugin)")
# Copy 24x24 icon to root level
graphics_src = self.project_root / "graphics"
if graphics_src.exists():
icon24_src = graphics_src / "icon24.png"
if icon24_src.exists():
shutil.copy2(icon24_src, package_dir / "icon.png")
logger.info(f" [OK] Copied toolbar icon (24x24)")
# Copy 64x64 icon to resources/
icon64_src = graphics_src / "icon64.png"
if icon64_src.exists():
shutil.copy2(icon64_src, resources_dir / "icon.png")
logger.info(f" [OK] Copied catalog icon (64x64)")
# Create metadata.json at package root
metadata = self.create_pcm_metadata()
with open(package_dir / "metadata.json", 'w', encoding='utf-8') as f:
json.dump(metadata, f, indent=2)
logger.info(f" [OK] Created metadata.json (PCM)")
# Create ZIP package
zip_name = f"{self.plugin_identifier}-pcm-{self.version}.zip"
zip_path = self.build_dir / zip_name
logger.info(f"\nCreating PCM package: {zip_name}")
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for file_path in package_dir.rglob('*'):
if file_path.is_file():
arcname = str(file_path.relative_to(package_dir))
zipf.write(file_path, arcname)
size_mb = zip_path.stat().st_size / (1024 * 1024)
logger.info(f"[OK] PCM package created: {zip_name} ({size_mb:.2f} MB)")
return zip_path
def show_pcm_completion_message(self, zip_path: Path):
"""Show completion message for PCM package"""
size_mb = zip_path.stat().st_size / (1024 * 1024)
print("\n" + "="*70)
print("BUILD COMPLETE - PCM PACKAGE")
print("="*70)
print(f"\nPackage: {zip_path.name} ({size_mb:.2f} MB)")
print(f"Location: {zip_path.parent}")
print("\n" + "="*70)
print("INSTALLATION INSTRUCTIONS")
print("="*70)
print("\n1. Open KiCad")
print("2. Go to Tools -> Plugin and Content Manager")
print("3. Click 'Install from File'")
print(f"4. Select: {zip_path}")
print("5. Restart KiCad")
print("6. Look for OrthoRoute button in PCB Editor toolbar")
print("\n" + "="*70)
print("NOTE: This is a SWIG plugin using KiCad's embedded Python")
print("Some features may differ from the IPC version")
print("="*70 + "\n")
def main():
"""Main build script entry point"""
import argparse
parser = argparse.ArgumentParser(description='OrthoRoute Build System')
parser.add_argument('--version', default='1.0.0', help='Version number')
parser.add_argument('--clean', action='store_true', help='Clean build directory only')
parser.add_argument('--pcm', action='store_true', help='Build PCM package (SWIG) instead of manual IPC package')
args = parser.parse_args()
builder = OrthoRouteBuildSystem()
builder.version = args.version
if args.clean:
builder.clean_build_directory()
return 0
# Clean and build
builder.clean_build_directory()
if args.pcm:
# Build PCM-installable SWIG package
zip_path = builder.build_pcm_package()
if zip_path and zip_path.exists():
builder.show_pcm_completion_message(zip_path)
return 0
else:
# Build manual installation IPC package
zip_path = builder.build_package()
if zip_path and zip_path.exists():
builder.show_completion_message(zip_path)
return 0
logger.error("[ERROR] Build failed!")
return 1
if __name__ == "__main__":
sys.exit(main())