-
Notifications
You must be signed in to change notification settings - Fork 102
/
vagrant_setup.py
139 lines (116 loc) · 4.7 KB
/
vagrant_setup.py
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
import re
from shutil import which
import subprocess
import os
def find_line_items(identifier, input_string):
pattern = re.compile(fr"(?<={re.escape(identifier)}: ).*.")
matches = pattern.findall(input_string)
matches = list(map(str.strip, matches))
return matches
def get_usb_devices():
usb_string = subprocess.check_output(['VBoxManage', 'list', 'usbhost'])
usb_string = usb_string.decode("utf-8").replace('\r', '')
usb_devices = usb_string.split("\n\n")
devices = []
for device in usb_devices:
productid = find_line_items("ProductId", device)
vendorid = find_line_items("VendorId", device)
manufacturer = find_line_items("Manufacturer", device)
product = find_line_items("Product", device)
if (len(productid) < 1 or len(vendorid) < 1 or
len(manufacturer) < 1 or len(product) < 1):
continue
productid = productid[0]
vendorid = vendorid[0]
manufacturer = manufacturer[0]
product = product[0]
if len(productid) != 13 or len(vendorid) != 13:
continue
devices.append({
'product': product,
'manufacturer': manufacturer,
'productid': productid[8:12],
'vendorid': vendorid[8:12]
})
return devices
def is_cli(cli_string):
return which(cli_string) is not None
def check_cli(name, cli_string, msg=None):
print(name, end="")
if is_cli(cli_string):
print(" [OK]")
else:
print(" [ERROR]")
print(f" -> {name} wasn't found on your system")
if msg is not None:
print(msg)
exit(1)
GH_SHELL_CONFIG = """cd /vagrant
pip3 install -e ."""
PYPI_SHELL_CONFIG = """pip3 install nxbt"""
if __name__ == "__main__":
print("Checking for the required utilities...")
vg_msg = (" Please ensure that Vagrant is installed and available on\n"
" your system path.")
check_cli("Vagrant", "vagrant", msg=vg_msg)
vb_msg = (" VBoxManage (part of the VirtualBox CLI) wasn't found\n"
" on your system path. Please ensure that VirtualBox is\n"
" installed and VBoxManage is on your system path.")
check_cli("VirtualBox", "VBoxManage", msg=vb_msg)
print("")
print("---")
print("Welcome to the nxbt-vagrant setup.")
print("As part of the first step in this process, you will "
"select the USB Bluetooth adapter that will be used with NXBT.")
print("Please ensure that your adapter is plugged into this computer.")
print("---")
input("Press the enter key to continue.")
print("")
print("USB Devices:")
print("---")
devices = get_usb_devices()
for i, device in enumerate(devices):
print(f"{i:3}. {device['product']} ({device['manufacturer']})")
print()
# Choose a USB Bluetooth adapter
invalid_choice = True
while invalid_choice:
usb_choice = input(
f"Please choose your Bluetooth USB Adapter from the above list [0-{len(devices)-1}]: ")
if usb_choice.isdigit() and int(usb_choice) < len(devices):
invalid_choice = False
else:
print(f"Invalid choice. Please choose a number from 0 to {len(devices)-1}.")
adapter_info = devices[int(usb_choice)]
print("")
# Choose how to install NXBT (PyPi or Github)
invalid_choice = True
while invalid_choice:
install_choice = input(
"Would you like to install NXBT from (1) PyPi or (2) install from local files? (1/2) ")
if install_choice in ['1', '2']:
invalid_choice = False
else:
print("Invalid choice. Please choose PyPi (1) or Github clone/install (2)")
print("")
print("Configuring...")
with open("template_vagrantfile", "r") as f:
vagrantfile = f.read()
vb_usb_filter = f"""vb.customize ["usbfilter", "add", "0",
"--target", :id,
"--name", "{adapter_info['product']} ({adapter_info['manufacturer']})",
"--product", "{adapter_info['product']}",
"--manufacturer", "{adapter_info['manufacturer']}",
"--productid", "{adapter_info['productid']}",
"--vendorid", "{adapter_info['vendorid']}",]"""
vagrantfile = vagrantfile.replace("{{USB_FILTER}}", vb_usb_filter)
if install_choice == '1':
vagrantfile = vagrantfile.replace("{{SHELL_CONFIG}}", PYPI_SHELL_CONFIG)
else:
vagrantfile = vagrantfile.replace("{{SHELL_CONFIG}}", GH_SHELL_CONFIG)
with open("Vagrantfile", "w") as f:
f.write(vagrantfile)
print("Done!")
print("")
print("You can now start the NXBT Vagrant Box with 'vagrant up'.")
print("After booting up, the Vagrant Box can be access with 'vagrant ssh'.")