-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtools.py
425 lines (338 loc) · 12.3 KB
/
tools.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import pwd
import grp
import xml.etree.ElementTree as ET
import subprocess
PIBOY_CONFIGURATOR_ROOT_FOLDER = "/home/pi/PiBoy-Configurator/"
READERS_FOLDER = PIBOY_CONFIGURATOR_ROOT_FOLDER + "readers/"
CONFIGS_ROOT_FOLDER = "/opt/retropie/configs/"
EMULATIONSTATION_CONFIG_FOLDER = CONFIGS_ROOT_FOLDER + "all/emulationstation/"
RETROARCHJOYPADS_CONFIG_FOLDER = CONFIGS_ROOT_FOLDER + "all/retroarch-joypads/"
OVERLAYS_FOLDER = CONFIGS_ROOT_FOLDER + "all/retroarch/overlay/"
OVERLAYS_LCD_FOLDER = OVERLAYS_FOLDER + "LCD/"
OVERLAYS_TV_FOLDER = OVERLAYS_FOLDER + "TV/"
OVERLAYS_TV_CLEAN_FOLDER = OVERLAYS_TV_FOLDER + "clean/"
OVERLAYS_TV_GRID_FOLDER = OVERLAYS_TV_FOLDER + "grid/"
RETROPIE_MENU_FOLDER = "/home/pi/RetroPie/retropiemenu/"
RETROPIE_MENU_GAMELIST_FOLDER = "/opt/retropie/configs/all/emulationstation/gamelists/retropie/"
PIBOY_SCRIPTNAME_MENU = "./PiBoy-Configurator.sh"
PIBOY_NAME_MENU = "PiBoy Configuration"
PIBOY_DESC_MENU = "Change the PiBoy options."
PIBOY_IMAGE = RETROPIE_MENU_FOLDER + "icons/PiBoy-Configurator.png"
PIBOY_CONTROLLER_NAME = "PiBoy DMG Controller"
BOOT_CONFIG_FILE = "/boot/config.txt"
def setWiFi(on):
if (on == True):
action = 'unblock'
value = 'yes'
else:
action = 'block'
value = 'no'
p2 = subprocess.Popen("sudo rfkill " + action + " wifi", stdout=subprocess.PIPE, shell=True)
out, err = p2.communicate()
result = out.split('\n')
setOptionFromPiboyconfFile('wifi_enable', value)
def setBluetooth(on):
if (on == True):
action = 'unblock'
value = 'yes'
else:
action = 'block'
value = 'no'
p2 = subprocess.Popen("sudo rfkill " + action + " bluetooth", stdout=subprocess.PIPE, shell=True)
out, err = p2.communicate()
result = out.split('\n')
setOptionFromPiboyconfFile('bt_enable', value)
def getOptionFromPiboyconfFile(option):
path = PIBOY_CONFIGURATOR_ROOT_FOLDER + 'piboy.cfg'
# check if config file exits
if (not os.path.exists(path)):
return -1
treeOptions = ET.parse(path)
root = treeOptions.getroot()
for child in root:
if (child.tag == option):
value_returned = child.text
if value_returned is None:
value_returned = ""
return value_returned
def setOptionFromPiboyconfFile(option, value):
path = PIBOY_CONFIGURATOR_ROOT_FOLDER + 'piboy.cfg'
# check if config file exits
if (not os.path.exists(path)):
return -1
treeOptions = ET.parse(path)
root = treeOptions.getroot()
for child in root:
if (child.tag == option):
child.text = value
treeOptions.write(path)
return 0
def checkPiboyConfiguratorEntryExists():
path = RETROPIE_MENU_GAMELIST_FOLDER + 'gamelist.xml'
# check if config file exits
if (not os.path.exists(path)):
path = RETROPIE_MENU_FOLDER + 'gamelist.xml'
if (not os.path.exists(path)):
return -1
treeOptions = ET.parse(path)
gamelist = treeOptions.getroot()
for game in gamelist:
if (game.find('name').text == PIBOY_NAME_MENU):
return True
return False
def indent(elem, level=0):
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
def createPiboyCfg():
uid = 1000
gid = 1000
path = PIBOY_CONFIGURATOR_ROOT_FOLDER + 'piboy.cfg'
# check if config file exits
if (os.path.exists(path)):
return -1
piboycfg = ET.Element("config")
overlay_style = ET.Element("overlay_style")
overlay_style.text = "grid"
wifi_enable = ET.Element("wifi_enable")
bt_enable = ET.Element("bt_enable")
theme_lcd = ET.Element("theme_lcd")
theme_tv = ET.Element("theme_tv")
onboard_controller_hdmi = ET.Element("onboard_controller_hdmi")
onboard_controller_hdmi.text = "yes"
wifi_always_enable_hdmi = ET.Element("wifi_always_enable_hdmi")
wifi_always_enable_hdmi.text = "undefined"
bt_always_enable_hdmi = ET.Element("bt_always_enable_hdmi")
bt_always_enable_hdmi.text = "undefined"
wifi_always_enable_lcd = ET.Element("wifi_always_enable_lcd")
wifi_always_enable_lcd.text = "undefined"
bt_always_enable_lcd = ET.Element("bt_always_enable_lcd")
bt_always_enable_lcd.text = "undefined"
overscan_enable = ET.Element("overscan_enable")
overscan_enable.text = "no"
overscan_value = ET.Element("overscan_value")
overscan_value.text = "0"
vol_icon_set = ET.Element("vol_icon_set")
vol_icon_set.text = "original"
piboycfg.insert(0, overlay_style)
piboycfg.insert(0, wifi_enable)
piboycfg.insert(0, bt_enable)
piboycfg.insert(0, theme_lcd)
piboycfg.insert(0, theme_tv)
piboycfg.insert(0, onboard_controller_hdmi)
piboycfg.insert(0, wifi_always_enable_hdmi)
piboycfg.insert(0, bt_always_enable_hdmi)
piboycfg.insert(0, wifi_always_enable_lcd)
piboycfg.insert(0, bt_always_enable_lcd)
piboycfg.insert(0, overscan_enable)
piboycfg.insert(0, overscan_value)
piboycfg.insert(0, vol_icon_set)
indent(piboycfg)
tree = ET.ElementTree(piboycfg)
tree.write(path, encoding="utf-8", xml_declaration=True)
# set the owner to "pi"
os.chown(path, uid, gid)
def addRetropieMenuEntry():
path = RETROPIE_MENU_GAMELIST_FOLDER + 'gamelist.xml'
# check if config file exits
if (not os.path.exists(path)):
path = RETROPIE_MENU_FOLDER + 'gamelist.xml'
if (not os.path.exists(path)):
return -1
treeOptions = ET.parse(path)
gamelist = treeOptions.getroot()
piboyEntry = ET.Element("game")
pathEntry = ET.Element("path")
pathEntry.text = PIBOY_SCRIPTNAME_MENU
piboyEntry.insert(0, pathEntry)
name = ET.Element("name")
name.text = PIBOY_NAME_MENU
piboyEntry.insert(1, name)
desc = ET.Element("desc")
desc.text = PIBOY_DESC_MENU
piboyEntry.insert(2, desc)
image = ET.Element("image")
image.text = PIBOY_IMAGE
piboyEntry.insert(3, image)
print(ET.tostring(piboyEntry).decode())
gamelist.insert(0, piboyEntry)
indent(gamelist)
treeOptions.write(path, encoding="utf-8", xml_declaration=True)
def removeRetropieMenuEntry():
path = RETROPIE_MENU_GAMELIST_FOLDER + 'gamelist.xml'
# check if config file exits
if (not os.path.exists(path)):
path = RETROPIE_MENU_FOLDER + 'gamelist.xml'
if (not os.path.exists(path)):
return -1
treeOptions = ET.parse(path)
gamelist = treeOptions.getroot()
for game in gamelist:
if (game.find('name').text == PIBOY_NAME_MENU):
gamelist.remove(game)
indent(gamelist)
treeOptions.write(path, encoding="utf-8", xml_declaration=True)
def copyFileWithAnotherName(path, newPath):
# check if file exits
if (not os.path.exists(path)):
return -1
file = open(path, 'r')
lines = file.readlines()
file.close()
newFile = open(newPath, 'w')
newFile.writelines(lines)
os.chown(newPath, pwd.getpwnam("pi").pw_uid, grp.getgrnam("pi").gr_gid)
def createRetroarchCfg():
print("\nNow manage retroarch configurations files in " + CONFIGS_ROOT_FOLDER)
systems = next(os.walk(CONFIGS_ROOT_FOLDER))
for system in systems[1]:
if system <> 'all' and not system.startswith("auto-") and system <> 'retropie':
contents = next(os.walk(CONFIGS_ROOT_FOLDER + system + '/'))[2]
# check if retroarch.cfg exists
retroarchcfgExists = False
retroarchcfgLcdExists = False
retroarchcfgTvExists = False
for file in contents:
if file == "retroarch.cfg":
retroarchcfgExists = True
if file == "retroarch.cfg.lcd":
retroarchcfgLcdExists = True
if file == "retroarch.cfg.tv":
retroarchcfgTvExists = True
if retroarchcfgExists == True:
if retroarchcfgLcdExists == False:
# create retroarch.cfg.lcd
copyFileWithAnotherName(CONFIGS_ROOT_FOLDER + system + '/' + 'retroarch.cfg', CONFIGS_ROOT_FOLDER + system + '/' + 'retroarch.cfg.lcd')
print("File " + CONFIGS_ROOT_FOLDER + system + '/' + 'retroarch.cfg.lcd' + "was created")
else:
print("File " + CONFIGS_ROOT_FOLDER + system + '/' + 'retroarch.cfg.lcd' + "already exixts")
if retroarchcfgTvExists == False:
# create retroarch.cfg.tv
copyFileWithAnotherName(CONFIGS_ROOT_FOLDER + system + '/' + 'retroarch.cfg', CONFIGS_ROOT_FOLDER + system + '/' + 'retroarch.cfg.tv')
print("File " + CONFIGS_ROOT_FOLDER + system + '/' + 'retroarch.cfg.tv' + "was created")
else:
print("File " + CONFIGS_ROOT_FOLDER + system + '/' + 'retroarch.cfg.tv' + "already exixts")
else:
print("There is no retroarch.cfg in system " + system)
def createOverlaysFolders():
print("\nNow manage overlays folders in " + OVERLAYS_FOLDER)
# define the access rights
access_rights = 0o755
if (not os.path.exists(OVERLAYS_LCD_FOLDER)):
try:
os.mkdir(OVERLAYS_LCD_FOLDER, access_rights)
except OSError:
print ("Creation of the directory %s failed" % OVERLAYS_LCD_FOLDER)
else:
print ("Successfully created the directory %s" % OVERLAYS_LCD_FOLDER)
else:
print(OVERLAYS_LCD_FOLDER + " already exists")
if (not os.path.exists(OVERLAYS_TV_FOLDER)):
try:
os.mkdir(OVERLAYS_TV_FOLDER, access_rights)
except OSError:
print ("Creation of the directory %s failed" % OVERLAYS_TV_FOLDER)
else:
print ("Successfully created the directory %s" % OVERLAYS_TV_FOLDER)
else:
print(OVERLAYS_TV_FOLDER + " already exists")
if (not os.path.exists(OVERLAYS_TV_CLEAN_FOLDER)):
try:
os.mkdir(OVERLAYS_TV_CLEAN_FOLDER, access_rights)
except OSError:
print ("Creation of the directory %s failed" % OVERLAYS_TV_CLEAN_FOLDER)
else:
print ("Successfully created the directory %s" % OVERLAYS_TV_CLEAN_FOLDER)
else:
print(OVERLAYS_TV_CLEAN_FOLDER + " already exists")
if (not os.path.exists(OVERLAYS_TV_GRID_FOLDER)):
try:
os.mkdir(OVERLAYS_TV_GRID_FOLDER, access_rights)
except OSError:
print ("Creation of the directory %s failed" % OVERLAYS_TV_GRID_FOLDER)
else:
print ("Successfully created the directory %s" % OVERLAYS_TV_GRID_FOLDER)
else:
print(OVERLAYS_TV_GRID_FOLDER + " already exists")
def addAutoconfigToRcLocal():
print("\nNow manage to add autoconfig.py execution in rc.local")
rcLocalFile = open("/etc/rc.local", 'r')
lines = rcLocalFile.readlines()
rcLocalFile.close()
already_in = False
for line in lines:
if line.strip() == "sudo python /home/pi/PiBoy-Configurator/autoconfig.py":
already_in = True
if already_in == False:
newLines = []
for line in lines:
if line.strip() == "exit 0":
# insert
newLines.append('\n' + "sudo python /home/pi/PiBoy-Configurator/autoconfig.py" + '\n\n')
newLines.append(line.strip()+"\n")
try :
rcLocalFile = open("/etc/rc.local", 'w')
rcLocalFile.writelines(newLines)
rcLocalFile.close()
print("The line was successfully added in rc.local")
except IOError:
print("Please run this script as Root : sudo python install.py")
else:
print("The line is already in rc.local")
def removeAutoconfigToRcLocal():
print("\nNow manage to remove autoconfig.py execution in rc.local")
rcLocalFile = open("/etc/rc.local", 'r')
lines = rcLocalFile.readlines()
rcLocalFile.close()
newLines = []
for line in lines:
if line.strip() != "sudo python /home/pi/PiBoy-Configurator/autoconfig.py":
newLines.append(line.strip()+"\n")
try :
rcLocalFile = open("/etc/rc.local", 'w')
rcLocalFile.writelines(newLines)
rcLocalFile.close()
print("The line was successfully removed in rc.local")
except IOError:
print("Please run this script as Root : sudo python install.py")
def addOverscan():
bootConfigFile = open(BOOT_CONFIG_FILE, 'r')
lines = bootConfigFile.readlines()
bootConfigFile.close()
newLines = []
overscan_value = getOptionFromPiboyconfFile('overscan_value')
for line in lines:
if not line.startswith("overscan_"):
newLines.append(line.strip()+"\n")
newLines.append(("overscan_left=" + overscan_value).strip()+"\n")
newLines.append(("overscan_right=" + overscan_value).strip()+"\n")
newLines.append(("overscan_top=" + overscan_value).strip()+"\n")
newLines.append(("overscan_bottom=" + overscan_value).strip()+"\n")
bootConfigFile = open(BOOT_CONFIG_FILE, 'w')
bootConfigFile.writelines(newLines)
bootConfigFile.close()
def removeOverscan():
bootConfigFile = open(BOOT_CONFIG_FILE, 'r')
lines = bootConfigFile.readlines()
bootConfigFile.close()
newLines = []
overscan_value = getOptionFromPiboyconfFile('overscan_value')
for line in lines:
if not line.startswith("overscan_"):
newLines.append(line.strip()+"\n")
bootConfigFile = open(BOOT_CONFIG_FILE, 'w')
bootConfigFile.writelines(newLines)
bootConfigFile.close()