forked from nebhead/PiFire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wizard.py
181 lines (150 loc) · 5.89 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
#!/usr/bin/env python3
'''
==============================================================================
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
'''
==============================================================================
Main Program
==============================================================================
'''
print('PiFire Module Wizard')
print('Copyright 2022, MIT License, Ben Parmeter')
settings = ReadSettings()
WizardData = ReadWizard()
WizardInstallInfo = LoadWizardInstallInfo()
percent = 5
status = 'Setting Up Modules...'
output = ' - Adding selected modules to the settings.json file. '
SetWizardInstallStatus(percent, status, output)
time.sleep(2)
settings['modules']['grillplat'] = WizardInstallInfo['modules']['grillplatform']['module_selected']
settings['modules']['adc'] = WizardInstallInfo['modules']['probes']['module_selected']
settings['modules']['display'] = WizardInstallInfo['modules']['display']['module_selected']
settings['modules']['dist'] = WizardInstallInfo['modules']['distance']['module_selected']
percent = 10
status = 'Updating Settings...'
output = ' - Adding selected settings to the settings.json file.'
SetWizardInstallStatus(percent, status, output)
time.sleep(2)
for module in WizardInstallInfo['modules']:
for setting in WizardInstallInfo['modules'][module]['settings']:
selected = WizardInstallInfo['modules'][module]['module_selected']
settingsLocation = WizardData['modules'][module][selected]['settings_dependencies'][setting]['settings']
selected_setting = WizardInstallInfo['modules'][module]['settings'][setting]
# Convert Number Strings to Int or Float
if selected_setting.isdigit():
selected_setting = int(selected_setting)
elif selected_setting.isdecimal():
selected_setting = float(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)
elif(len(settingsLocation) == 1):
settings[settingsLocation[0]] = selected_setting
elif(len(settingsLocation) == 2):
settings[settingsLocation[0]][settingsLocation[1]] = selected_setting
elif(len(settingsLocation) == 3):
settings[settingsLocation[0]][settingsLocation[1]][settingsLocation[2]] = selected_setting
output = f' + Set {setting} in settings.json'
SetWizardInstallStatus(percent, status, output)
# Commit Settings to JSON
WriteSettings(settings)
percent = 20
status = 'Calculating Python/Package Dependencies...'
output = ' - Calculating Python & Package Dependencies'
SetWizardInstallStatus(percent, status, output)
time.sleep(2)
# Get PyPi & Apt dependencies
py_dependencies = []
apt_dependencies = []
for module in WizardInstallInfo['modules']:
selected = WizardInstallInfo['modules'][module]['module_selected']
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)
# Calculate the percent done from remaining items to install
items_remaining = len(py_dependencies) + len(apt_dependencies)
if items_remaining == 0:
increment = 80
else:
increment = 80 / items_remaining
# Install Py dependencies
launchpip = ['pip3', 'install']
status = 'Installing Python Dependencies...'
output = ' - Installing Python Dependencies'
SetWizardInstallStatus(percent, status, output)
for py_item in py_dependencies:
command = []
command.extend(launchpip)
command.append(py_item)
if(isRaspberryPi()):
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:
SetWizardInstallStatus(percent, status, output.strip())
print(output.strip())
returncode = process.poll()
print(f'Return Code: {returncode}')
else:
# This path is for development/testing
time.sleep(2)
percent += increment
output = f' - Completed Install of {py_item}'
SetWizardInstallStatus(percent, status, output)
# Install Apt dependencies
launchapt = ['apt', 'install']
status = 'Installing Package Dependencies...'
output = ' - Installing APT Package Dependencies'
SetWizardInstallStatus(percent, status, output)
for apt_item in apt_dependencies:
command = []
command.extend(launchapt)
command.append(apt_item)
command.append('-y')
if(isRaspberryPi()):
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:
SetWizardInstallStatus(percent, status, output.strip())
print(output.strip())
returncode = process.poll()
print(f'Return Code: {returncode}')
else:
# This path is for development/testing
time.sleep(2)
percent += increment
output = f' - Completed Install of {apt_item}'
SetWizardInstallStatus(percent, status, output)
percent = 100
status = 'Finished!'
output = ' - Finished! Restarting Server...'
SetWizardInstallStatus(percent, status, output)
time.sleep(4)
percent = 101
SetWizardInstallStatus(percent, status, output)
# Clear First Time Setup Flag
settings['globals']['first_time_setup'] = False
# Commit Setting to JSON
WriteSettings(settings)