-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreconfigure
executable file
·84 lines (68 loc) · 3.06 KB
/
reconfigure
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
#!/bin/python
import os
import fileinput
import shutil
import errno
import grp
import stat
def replace_line(filepath, search_text, replace_text):
# Iterate over the file and replace lines matching search_text with replace_text
for line in fileinput.input(filepath, inplace=True):
if line.startswith(search_text) or line.lstrip().startswith(search_text):
print(replace_text)
else:
print(line, end='')
def load_config(filepath):
if not os.path.isfile(filepath):
print(f"Configuration file '{filepath}' not found")
return {}
config = {}
with open(filepath, 'r') as file:
for line in file:
line = line.strip()
if line:
key, value = line.split('=', 1)
config[key.strip()] = value.strip().strip('"');
return config
def create_directory_with_user_group_and_permissions(directory_path, user_id, group_name):
try:
# Create the directory if it does not exist
os.makedirs(directory_path, exist_ok=True)
print(f"Directory '{directory_path}' created successfully.")
# Look up the GID for the group
gid = grp.getgrnam(group_name).gr_gid
# Change the ownership of the directory to user_id and group_id
os.chown(directory_path, user_id, gid)
print(f"Changed ownership of '{directory_path}' to user ID {user_id} and group '{group_name}'.")
# Set directory permissions to read, write, and execute for user and group
current_permissions = stat.S_IMODE(os.stat(directory_path).st_mode)
os.chmod(directory_path, current_permissions | stat.S_IRWXU | stat.S_IRWXG)
except KeyError:
print(f"Group '{group_name}' not found.")
except OSError as e:
if e.errno == errno.EEXIST:
print(f"Directory '{directory_path}' already exists.")
else:
print(f"Error creating/changing '{directory_path}'")
def reconfigure():
# Load configuration values from file
config = load_config('/etc/hifiberry.state')
# Reconfigure Shairport-sync
if not os.path.isfile('/etc/shairport-sync.conf'):
print("shairport-sync.conf not found")
return
# Replace lines in /etc/shairport-sync.conf
replace_line('/etc/shairport-sync.conf', 'mixer_control_name = ', ' mixer_control_name = "' + config.get("CURRENT_MIXER_CONTROL", "") + '";')
replace_line('/etc/shairport-sync.conf', 'name = ', ' name = "' + config.get("CURRENT_SYSNAME", "") + '";')
replace_line('/etc/shairport-sync.conf', 'run_this_before_play_begins = ', ' run_this_before_play_begins = "/pause-others.sh";')
# Print status messages
if config.get('MIXER_CONTROL') != config.get('CURRENT_MIXER_CONTROL'):
print("updated mixer")
if config.get('SYSNAME') != config.get('CURRENT_SYSNAME'):
print("updated system name")
directory_path = "/tmp/shairport-sync"
user_id = 2002
group_name = "audio"
create_directory_with_user_group_and_permissions(directory_path, user_id, group_name)
if __name__ == "__main__":
reconfigure()