-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_raspberry.py
More file actions
522 lines (401 loc) · 13.4 KB
/
Copy pathbuild_raspberry.py
File metadata and controls
522 lines (401 loc) · 13.4 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
"""
Build Script for Raspberry Pi
Creates .deb package for Debian/Raspberry Pi OS
"""
import os
import sys
import shutil
import subprocess
from pathlib import Path
from datetime import datetime
# Build configuration
APP_NAME = "artnetcontroller"
APP_VERSION = "2.0.0"
MAINTAINER = "Your Name <your.email@example.com>"
DESCRIPTION = "Professional DMX Art-Net Controller for Raspberry Pi"
HOMEPAGE = "https://yourwebsite.com"
DIST_DIR = Path("dist")
BUILD_DIR = Path("build_deb")
class Colors:
HEADER = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
END = '\033[0m'
BOLD = '\033[1m'
def print_step(message):
"""Print step header"""
print(f"\n{Colors.BOLD}{Colors.BLUE}{'='*80}{Colors.END}")
print(f"{Colors.BOLD}{Colors.BLUE}{message}{Colors.END}")
print(f"{Colors.BOLD}{Colors.BLUE}{'='*80}{Colors.END}\n")
def print_success(message):
"""Print success message"""
print(f"{Colors.GREEN}✅ {message}{Colors.END}")
def print_error(message):
"""Print error message"""
print(f"{Colors.RED}❌ {message}{Colors.END}")
def print_warning(message):
"""Print warning message"""
print(f"{Colors.YELLOW}⚠️ {message}{Colors.END}")
def check_platform():
"""Check if running on Linux"""
if sys.platform != "linux":
print_warning("This script should be run on Linux/Raspberry Pi")
print_warning(f"Current platform: {sys.platform}")
response = input("Continue anyway? (y/N): ")
if response.lower() != 'y':
return False
return True
def check_dependencies():
"""Check if required tools are installed"""
print_step("Checking Dependencies")
# Check for dpkg-deb
try:
subprocess.run(["dpkg-deb", "--version"], capture_output=True, check=True)
print_success("dpkg-deb installed")
except (subprocess.CalledProcessError, FileNotFoundError):
print_error("dpkg-deb not found")
print("Install with: sudo apt-get install dpkg-dev")
return False
# Check Python packages
packages = ['PyQt6', 'psutil']
missing = []
for package in packages:
try:
__import__(package)
print_success(f"{package} installed")
except ImportError:
print_error(f"{package} not found")
missing.append(package)
if missing:
print_error(f"Missing packages: {', '.join(missing)}")
print(f"Install with: pip install {' '.join(missing)}")
return False
return True
def clean_build():
"""Clean previous build artifacts"""
print_step("Cleaning Build Directory")
if BUILD_DIR.exists():
shutil.rmtree(BUILD_DIR)
print_success(f"Removed: {BUILD_DIR}")
# Clean old .deb files
for deb in DIST_DIR.glob(f"{APP_NAME}_*.deb"):
deb.unlink()
print_success(f"Removed: {deb}")
def create_directory_structure():
"""Create Debian package directory structure"""
print_step("Creating Package Structure")
dirs = [
BUILD_DIR / "DEBIAN",
BUILD_DIR / "usr" / "local" / "bin",
BUILD_DIR / "usr" / "local" / "lib" / APP_NAME,
BUILD_DIR / "usr" / "share" / "applications",
BUILD_DIR / "usr" / "share" / "icons" / "hicolor" / "256x256" / "apps",
BUILD_DIR / "etc" / "systemd" / "system",
BUILD_DIR / "var" / "log" / APP_NAME,
]
for dir_path in dirs:
dir_path.mkdir(parents=True, exist_ok=True)
print(f" Created: {dir_path}")
print_success("Directory structure created")
def create_control_file():
"""Create DEBIAN/control file"""
print_step("Creating Control File")
# Detect architecture
try:
result = subprocess.run(
["dpkg", "--print-architecture"],
capture_output=True,
text=True,
check=True
)
arch = result.stdout.strip()
except:
arch = "armhf" # Default for Raspberry Pi
control_content = f"""Package: {APP_NAME}
Version: {APP_VERSION}
Section: electronics
Priority: optional
Architecture: {arch}
Depends: python3 (>= 3.9), python3-pyqt6, python3-psutil
Maintainer: {MAINTAINER}
Description: {DESCRIPTION}
Professional DMX Art-Net Controller with recording and playback
capabilities. Supports multiple universes, configuration management,
automatic updates, and crash reporting.
.
Features:
- Real-time DMX control via Art-Net protocol
- Record and playback shows
- Multi-universe support (up to 16 universes)
- Web-based remote control
- Automatic updates from GitHub
- Comprehensive logging and crash reporting
Homepage: {HOMEPAGE}
"""
control_file = BUILD_DIR / "DEBIAN" / "control"
with open(control_file, 'w') as f:
f.write(control_content)
print_success(f"Created: {control_file}")
print(f" Architecture: {arch}")
def create_postinst_script():
"""Create DEBIAN/postinst script"""
script_content = """#!/bin/bash
set -e
# Create log directory with correct permissions
mkdir -p /var/log/artnetcontroller
chown root:adm /var/log/artnetcontroller
chmod 755 /var/log/artnetcontroller
# Create config directory for users
mkdir -p /etc/artnetcontroller
# Reload systemd
systemctl daemon-reload
# Enable service (but don't start automatically)
# systemctl enable artnetcontroller.service
echo "ArtNetController installed successfully!"
echo "To start the service: sudo systemctl start artnetcontroller"
echo "To enable auto-start: sudo systemctl enable artnetcontroller"
exit 0
"""
script_file = BUILD_DIR / "DEBIAN" / "postinst"
with open(script_file, 'w') as f:
f.write(script_content)
# Make executable
script_file.chmod(0o755)
print_success(f"Created: {script_file}")
def create_prerm_script():
"""Create DEBIAN/prerm script"""
script_content = """#!/bin/bash
set -e
# Stop service if running
if systemctl is-active --quiet artnetcontroller; then
systemctl stop artnetcontroller
fi
# Disable service
if systemctl is-enabled --quiet artnetcontroller; then
systemctl disable artnetcontroller
fi
exit 0
"""
script_file = BUILD_DIR / "DEBIAN" / "prerm"
with open(script_file, 'w') as f:
f.write(script_content)
script_file.chmod(0o755)
print_success(f"Created: {script_file}")
def create_systemd_service():
"""Create systemd service file"""
service_content = f"""[Unit]
Description=ArtNetController - DMX Art-Net Controller
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/usr/local/lib/{APP_NAME}
ExecStart=/usr/bin/python3 /usr/local/lib/{APP_NAME}/main.py
Restart=on-failure
RestartSec=5s
# Environment
Environment="DISPLAY=:0"
Environment="PYTHONUNBUFFERED=1"
# Logging
StandardOutput=journal
StandardError=journal
SyslogIdentifier={APP_NAME}
[Install]
WantedBy=multi-user.target
"""
service_file = BUILD_DIR / "etc" / "systemd" / "system" / f"{APP_NAME}.service"
with open(service_file, 'w') as f:
f.write(service_content)
print_success(f"Created: {service_file}")
def create_desktop_entry():
"""Create .desktop file for application menu"""
desktop_content = f"""[Desktop Entry]
Version=1.0
Type=Application
Name=ArtNetController
Comment={DESCRIPTION}
Exec=/usr/local/bin/{APP_NAME}
Icon={APP_NAME}
Terminal=false
Categories=AudioVideo;Audio;Electronics;
Keywords=DMX;ArtNet;Lighting;Controller;
"""
desktop_file = BUILD_DIR / "usr" / "share" / "applications" / f"{APP_NAME}.desktop"
with open(desktop_file, 'w') as f:
f.write(desktop_content)
print_success(f"Created: {desktop_file}")
def copy_application_files():
"""Copy application files to package"""
print_step("Copying Application Files")
lib_dir = BUILD_DIR / "usr" / "local" / "lib" / APP_NAME
# Files and directories to copy
items_to_copy = [
("src", lib_dir / "src"),
("config.json", lib_dir / "config.json"),
("main.py", lib_dir / "main.py"), # Your main entry point
]
for src, dst in items_to_copy:
src_path = Path(src)
if not src_path.exists():
print_warning(f"Source not found: {src}")
continue
if src_path.is_dir():
shutil.copytree(src_path, dst, dirs_exist_ok=True)
else:
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src_path, dst)
print(f" Copied: {src} → {dst}")
# Create launcher script
launcher_script = BUILD_DIR / "usr" / "local" / "bin" / APP_NAME
launcher_content = f"""#!/bin/bash
cd /usr/local/lib/{APP_NAME}
python3 main.py "$@"
"""
with open(launcher_script, 'w') as f:
f.write(launcher_content)
launcher_script.chmod(0o755)
print_success("Application files copied")
def build_deb_package():
"""Build .deb package"""
print_step("Building .deb Package")
# Package filename
result = subprocess.run(
["dpkg", "--print-architecture"],
capture_output=True,
text=True
)
arch = result.stdout.strip() if result.returncode == 0 else "armhf"
deb_filename = f"{APP_NAME}_{APP_VERSION}_{arch}.deb"
deb_path = DIST_DIR / deb_filename
# Create dist directory
DIST_DIR.mkdir(exist_ok=True)
# Build package
cmd = [
"dpkg-deb",
"--build",
"--root-owner-group",
str(BUILD_DIR),
str(deb_path)
]
print(f"Running: {' '.join(cmd)}")
try:
result = subprocess.run(
cmd,
check=True,
capture_output=True,
text=True
)
print(result.stdout)
print_success(f"Package created: {deb_path}")
# Show size
size_mb = deb_path.stat().st_size / 1024 / 1024
print(f" Size: {size_mb:.2f} MB")
return deb_path
except subprocess.CalledProcessError as e:
print_error("Package build failed")
print(e.stdout)
print(e.stderr)
return None
def verify_package(deb_path):
"""Verify .deb package"""
print_step("Verifying Package")
# Show package info
cmd = ["dpkg-deb", "--info", str(deb_path)]
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
print(result.stdout)
print_success("Package verification passed")
return True
except subprocess.CalledProcessError as e:
print_error("Package verification failed")
print(e.stderr)
return False
def generate_checksum(deb_path):
"""Generate SHA256 checksum"""
print_step("Generating Checksum")
import hashlib
sha256 = hashlib.sha256()
with open(deb_path, 'rb') as f:
while True:
chunk = f.read(8192)
if not chunk:
break
sha256.update(chunk)
checksum = sha256.hexdigest()
print(f" {deb_path.name}")
print(f" SHA256: {checksum}")
# Save to file
checksum_file = DIST_DIR / f"{deb_path.stem}.sha256"
with open(checksum_file, 'w') as f:
f.write(f"{checksum} {deb_path.name}\n")
print_success(f"Checksum saved to: {checksum_file}")
def build_summary(deb_path):
"""Print build summary"""
print_step("Build Summary")
print(f"Build completed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"\nPackage: {deb_path.absolute()}")
size_mb = deb_path.stat().st_size / 1024 / 1024
print(f"Size: {size_mb:.2f} MB")
print("\nInstallation:")
print(f" sudo dpkg -i {deb_path.name}")
print(f" sudo apt-get install -f # Fix dependencies if needed")
print("\nUsage:")
print(f" {APP_NAME} # Run application")
print(f" sudo systemctl start {APP_NAME} # Start service")
print(f" sudo systemctl enable {APP_NAME} # Enable auto-start")
print("\n" + "="*80)
print_success("BUILD COMPLETE!")
print("="*80)
def main():
"""Main build process"""
print(f"""
{Colors.BOLD}{'='*80}
{APP_NAME.upper()} v{APP_VERSION} - Raspberry Pi Build Script
{'='*80}{Colors.END}
""")
# Step 1: Check platform
if not check_platform():
return 1
# Step 2: Check dependencies
if not check_dependencies():
print_error("Dependency check failed")
return 1
# Step 3: Clean build
clean_build()
# Step 4: Create directory structure
create_directory_structure()
# Step 5: Create control files
create_control_file()
create_postinst_script()
create_prerm_script()
# Step 6: Create systemd service
create_systemd_service()
# Step 7: Create desktop entry
create_desktop_entry()
# Step 8: Copy application files
copy_application_files()
# Step 9: Build package
deb_path = build_deb_package()
if not deb_path:
print_error("Build failed")
return 1
# Step 10: Verify package
verify_package(deb_path)
# Step 11: Generate checksum
generate_checksum(deb_path)
# Step 12: Summary
build_summary(deb_path)
return 0
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt:
print_warning("\nBuild cancelled by user")
sys.exit(1)
except Exception as e:
print_error(f"Unexpected error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)