-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_macos_usb.py
More file actions
150 lines (122 loc) · 4.52 KB
/
Copy pathtest_macos_usb.py
File metadata and controls
150 lines (122 loc) · 4.52 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
#!/usr/bin/env python3
"""
Test macOS USB Detection
This script tests the USB detection functionality on macOS.
Run this to verify that USB drives are being detected properly.
Usage:
python3 test_macos_usb.py
"""
import sys
import platform
def test_usb_detection():
"""Test USB device detection"""
print("=" * 60)
print("macOS USB Detection Test")
print("=" * 60)
# Check OS
current_os = platform.system()
print(f"\nCurrent OS: {current_os}")
if current_os != 'Darwin':
print("⚠️ Warning: Not running on macOS (Darwin)")
print(" This test is designed for macOS")
# Import USB manager
try:
from src.usb import USBManager
print("✓ Successfully imported USBManager")
except ImportError as e:
print(f"✗ Failed to import USBManager: {e}")
return False
# Create manager instance
try:
usb = USBManager()
print(f"✓ Created USBManager instance")
print(f" - System: {usb.system}")
print(f" - is_windows: {usb.is_windows}")
print(f" - is_macos: {usb.is_macos}")
print(f" - is_linux: {usb.is_linux}")
except Exception as e:
print(f"✗ Failed to create USBManager: {e}")
return False
# Check permissions
print("\n" + "-" * 60)
print("Checking permissions...")
try:
has_perms = usb.check_permissions()
if has_perms:
print("✓ Have necessary permissions")
else:
print("⚠️ May need elevated permissions for some operations")
except Exception as e:
print(f"✗ Error checking permissions: {e}")
# Detect USB devices
print("\n" + "-" * 60)
print("Detecting USB devices...")
try:
devices = usb.detect_usb_devices()
print(f"✓ Detection completed")
print(f" Found {len(devices)} USB device(s)")
if devices:
print("\nDetected Devices:")
for i, dev in enumerate(devices, 1):
print(f"\n Device {i}:")
print(f" Path: {dev.get('device', 'Unknown')}")
print(f" Size: {dev.get('size', 'Unknown')}")
print(f" Model: {dev.get('model', 'Unknown')}")
print(f" Mountpoint: {dev.get('mountpoint', 'Not mounted')}")
partitions = dev.get('partitions', [])
if partitions:
print(f" Partitions: {len(partitions)}")
for j, part in enumerate(partitions, 1):
print(f" {j}. {part.get('device', 'Unknown')} - {part.get('size', 'Unknown')}")
if part.get('mountpoint'):
print(f" Mounted at: {part['mountpoint']}")
else:
print("\n⚠️ No USB devices detected")
print(" Please plug in a USB drive and try again")
except Exception as e:
print(f"✗ Error detecting USB devices: {e}")
import traceback
traceback.print_exc()
return False
print("\n" + "=" * 60)
print("Test completed!")
print("=" * 60)
return True
def test_diskutil_available():
"""Test if diskutil is available"""
import subprocess
print("\n" + "-" * 60)
print("Testing diskutil availability...")
try:
result = subprocess.run(
['diskutil', 'list'],
capture_output=True,
text=True,
timeout=10
)
if result.returncode == 0:
print("✓ diskutil is available and working")
# Count disks
disk_count = result.stdout.count('/dev/disk')
print(f" Found {disk_count} disk(s) in system")
return True
else:
print(f"⚠️ diskutil returned error: {result.stderr}")
return False
except FileNotFoundError:
print("✗ diskutil not found - not running on macOS?")
return False
except Exception as e:
print(f"✗ Error running diskutil: {e}")
return False
if __name__ == "__main__":
print("\n")
# Test diskutil
diskutil_ok = test_diskutil_available()
if not diskutil_ok and platform.system() == 'Darwin':
print("\n⚠️ WARNING: diskutil not working properly")
print(" USB detection may not work")
# Test USB detection
print("\n")
success = test_usb_detection()
sys.exit(0 if success else 1)