-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathwizard.py
280 lines (230 loc) · 9.28 KB
/
wizard.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
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
'''
==============================================================================
PiFire Module Wizard
==============================================================================
Description: This script used during install to configure modules and settings
==============================================================================
'''
'''
==============================================================================
Imported Libraries
==============================================================================
'''
from common import * # Common Library for writing settings
import subprocess
def _convert_value(value):
"""
Converts a given value to an appropriate data type based on its string representation.
Parameters:
- value (str): The string representation of the value to be converted.
Returns:
- int or float or bool or list or str or None: The converted value in the appropriate data type.
The function checks if the value is a string representation of a number and converts it to an integer or float if possible.
If the value is the string representation of a boolean, it is converted to a boolean value.
If the value is the string representation of None, it is converted to None.
If the value is a string representation of a list, it is converted to a list.
Otherwise, the original value is returned.
Note:
- The function assumes that the value parameter is a string.
- The function does not perform any validation on the input value.
"""
if value.isdigit():
return int(value)
if value.isdecimal():
return float(value)
# Convert Boolean String to Standard Boolean
if value == 'True' or value == 'False':
return value == 'True'
# Convert 'None' to None
if value == 'None':
return None
# Covert String List to List
if type(value) == str:
if value.startswith('[') and value.endswith(']'):
value = value.replace('\'', '').replace('\"', '').replace(' ', '')
return value[1:-1].split(',')
return value
'''
==============================================================================
Main Program
==============================================================================
'''
print('PiFire Module Wizard')
print('Copyright 2022, MIT License, Ben Parmeter')
settings = read_settings()
WizardData = read_wizard()
WizardInstallInfo = load_wizard_install_info()
percent = 5
status = 'Setting Up Modules...'
output = ' - Adding selected modules to the settings.json file. '
set_wizard_install_status(percent, status, output)
time.sleep(2)
display_selected = WizardInstallInfo['modules']['display']['profile_selected'][0]
settings['modules']['display'] = WizardData['modules']['display'][display_selected]['filename']
settings['display']['selected'] = display_selected
distance_selected = WizardInstallInfo['modules']['distance']['profile_selected'][0]
settings['modules']['dist'] = WizardData['modules']['distance'][distance_selected]['filename']
''' Configuring Probes Data '''
settings['probe_settings']['probe_map'] = WizardInstallInfo['probe_map']
''' Update History Page Config with Latest Probe Config '''
settings['history_page']['probe_config'] = default_probe_config(settings)
percent = 10
status = 'Updating Settings...'
output = ' - Adding selected settings to the settings.json file.'
set_wizard_install_status(percent, status, output)
time.sleep(2)
for module in WizardInstallInfo['modules']:
for setting in WizardInstallInfo['modules'][module]['settings']:
selected = WizardInstallInfo['modules'][module]['profile_selected'][0]
settingsLocation = WizardData['modules'][module][selected]['settings_dependencies'][setting]['settings']
selected_setting = WizardInstallInfo['modules'][module]['settings'][setting]
# Convert Strings to the correct type
selected_setting = _convert_value(selected_setting)
# Special Handling for Units
if setting == 'units':
units = WizardInstallInfo['modules'][module]['settings'][setting]
if units == 'C' and settings['globals']['units'] == 'F':
settings = convert_settings_units('C', settings)
elif(units == 'F') and (settings['globals']['units'] == 'C'):
settings = convert_settings_units('F', settings)
else:
settings = set_nested_key_value(settings, settingsLocation, selected_setting)
output = f' + Set {setting} in settings.json'
set_wizard_install_status(percent, status, output)
if module == 'display':
selected = WizardInstallInfo['modules'][module]['profile_selected'][0]
settings['display']['config'][selected] = WizardInstallInfo['modules']['display']['config']
for key in settings['display']['config'][selected]:
# Convert Strings to the correct type
settings['display']['config'][selected][key] = _convert_value(settings['display']['config'][selected][key])
percent = 15
status = 'Installing Dependencies...'
output = ' - Installing Dependencies'
set_wizard_install_status(percent, status, output)
time.sleep(2)
''' Set the grillplatform module per the system_type '''
settings['modules']['grillplat'] = 'prototype'
if settings['platform']['system_type'] == 'raspberry_pi_all':
settings['modules']['grillplat'] = 'raspberry_pi_all'
# Commit Settings to JSON
write_settings(settings)
percent = 20
status = 'Calculating Python/Package Dependencies...'
output = ' - Calculating Python, APT Package, and General Dependencies'
set_wizard_install_status(percent, status, output)
time.sleep(2)
# Get PyPi & Apt dependencies
py_dependencies = []
apt_dependencies = []
command_list = []
reboot_required = False
for module in WizardInstallInfo['modules']:
for selected in WizardInstallInfo['modules'][module]['profile_selected']:
if module == 'grillplatform':
selected = WizardInstallInfo['modules'][module]['settings']['current']
for py_dependency in WizardData['modules'][module][selected]['py_dependencies']:
py_dependencies.append(py_dependency)
for apt_dependency in WizardData['modules'][module][selected]['apt_dependencies']:
apt_dependencies.append(apt_dependency)
for command in WizardData['modules'][module][selected]['command_list']:
command_list.append(command)
if WizardData['modules'][module][selected]['reboot_required']:
reboot_required = True
# Calculate the percent done from remaining items to install
items_remaining = len(py_dependencies) + len(apt_dependencies) + len(command_list)
if items_remaining == 0:
increment = 80
else:
increment = 80 / items_remaining
# Install Py dependencies
if settings['globals']['venv']:
python_exec = 'bin/python'
else:
python_exec = 'python'
launch_pip = [python_exec, '-m', 'pip', 'install']
status = 'Installing Python Dependencies...'
output = ' - Installing Python Dependencies'
set_wizard_install_status(percent, status, output)
for py_item in py_dependencies:
command = []
command.extend(launch_pip)
command.append(py_item)
if is_real_hardware():
process = subprocess.Popen(command, stdout=subprocess.PIPE, encoding='utf-8')
while True:
output = process.stdout.readline()
if process.poll() is not None:
break
if output:
set_wizard_install_status(percent, status, output.strip())
print(output.strip())
return_code = process.poll()
print(f'Return Code: {return_code}')
else:
# This path is for development/testing
time.sleep(2)
percent += increment
output = f' - Completed Install of {py_item}'
set_wizard_install_status(percent, status, output)
# Install Apt dependencies
launch_apt = ['sudo', 'apt', 'install']
status = 'Installing Package Dependencies...'
output = ' - Installing APT Package Dependencies'
set_wizard_install_status(percent, status, output)
for apt_item in apt_dependencies:
command = []
command.extend(launch_apt)
command.append(apt_item)
command.append('-y')
if is_real_hardware():
process = subprocess.Popen(command, stdout=subprocess.PIPE, encoding='utf-8')
while True:
output = process.stdout.readline()
if process.poll() is not None:
break
if output:
set_wizard_install_status(percent, status, output.strip())
print(output.strip())
return_code = process.poll()
print(f'Return Code: {return_code}')
else:
# This path is for development/testing
time.sleep(2)
percent += increment
output = f' - Completed Install of {apt_item}'
set_wizard_install_status(percent, status, output)
# Run system commands dependencies
status = 'Installing General Dependencies...'
output = ' - Installing General Dependencies'
set_wizard_install_status(percent, status, output)
for command in command_list:
if is_real_hardware():
process = subprocess.Popen(command, stdout=subprocess.PIPE, encoding='utf-8')
while True:
output = process.stdout.readline()
if process.poll() is not None:
break
if output:
set_wizard_install_status(percent, status, output.strip())
print(f'command output: {output.strip()}')
#return_code = process.poll()
else:
# This path is for development/testing
time.sleep(2)
percent += increment
output = f' - Completed General Dependency Item'
set_updater_install_status(percent, status, output)
percent = 100
status = 'Finished!'
if reboot_required:
output = ' - Finished! Rebooting Server...'
else:
output = ' - Finished! Restarting Server...'
set_wizard_install_status(percent, status, output)
time.sleep(4)
percent = 142 if reboot_required else 101
set_wizard_install_status(percent, status, output)
# Clear First Time Setup Flag
settings['globals']['first_time_setup'] = False
# Commit Setting to JSON
write_settings(settings)