-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreconfigure
executable file
·122 lines (102 loc) · 4.54 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
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
#!/usr/bin/python
import re
import os
import shutil
import subprocess
import filecmp
def is_multiuser():
result = subprocess.run(["systemctl", "is-system-running"], capture_output=True, text=True)
return result.stdout.strip() == "multi-user"
def get_script_directory():
# Get the absolute path of the current Python script
script_path = os.path.abspath(__file__)
# Get the directory containing the script
script_directory = os.path.dirname(script_path)
return script_directory
def parse_config_file(file_path):
config = {}
with open(file_path, 'r') as file:
for line in file:
# Strip whitespace and newline characters from each line
line = line.strip()
# Skip empty lines or lines that start with a comment
if not line or line.startswith('#'):
continue
# Split each line at the first equal sign
key, value = line.split('=', 1)
# Remove quotes from the value if present
value = value.strip('"')
# Assign the key-value pair to the dictionary
config[key] = value
return config
def update_spotifyd_config(config_file_path, replacements):
file_changed = False # Flag to track if the file has been modified
# Read the current configuration
with open(config_file_path, 'r') as file:
lines = file.readlines()
# Prepare the file for writing
with open(config_file_path, 'w') as file:
for line in lines:
updated_line = line # Assume no change to the line initially
for key, new_value in replacements.items():
# Create a regex pattern to match the key with any amount of whitespace around '='
pattern = re.compile(rf'^\s*{key}\s*=\s*".*?"\s*(#.*)?$|^\s*{key}\s*=\s*.*?\s*(#.*)?$', re.IGNORECASE)
match = pattern.match(line)
# If a match is found and needs update, prepare the updated line
if match and f'{key} = "{new_value}"' not in line:
updated_line = f'{key} = "{new_value}" # Automatically updated by reconfigure script\n'
file_changed = True # Indicate a change has been made
file.write(updated_line) # Write the updated (or original) line to the file
return file_changed
def fix_default_config_file(configFile, defaultFile):
# Initialize a flag to track if the configFile was removed
removed = False
# Check if the configFile exists and if it's empty or a directory
if os.path.exists(configFile):
if os.path.isfile(configFile) and os.path.getsize(configFile) == 0:
# If it's an empty file, remove it
os.remove(configFile)
removed = True
elif os.path.isdir(configFile) and not os.listdir(configFile):
# If it's an empty directory, remove it recursively
shutil.rmtree(configFile)
removed = True
else:
removed = True
# Check if the defaultFile exists and if configFile was removed
if os.path.exists(defaultFile) and removed:
# Copy the defaultFile to the configFile
shutil.copy(defaultFile, configFile)
print(f"File '{defaultFile}' copied to '{configFile}'")
elif not os.path.exists(defaultFile):
print(f"Source file '{defaultFile}' does not exist.")
sys.exit(1)
def copy_if_different(source_file, destination_file):
if not os.path.exists(destination_file) or not filecmp.cmp(source_file, destination_file):
shutil.copy(source_file, destination_file)
print(f"File '{source_file}' copied to '{destination_file}'")
return True
else:
return False
# DBUS Configuration
if copy_if_different(get_script_directory()+"/dbus-spotifyd.conf","/etc/dbus-1/system.d/spotifyd.conf"):
if is_multiuser():
result = subprocess.run(["systemctl", "restart", "dbus"])
if result.returncode != 0:
print("Could not restart dbus")
sys.exit(result.returncode)
else:
with open("/tmp/reboot", "a") as f:
f.write("squeezelite dbus configuration")
# Spotfiyd config file
configfile = "/etc/spotifyd.conf"
default_configfile = get_script_directory()+"/spotifyd.conf.default"
fix_default_config_file(configfile, default_configfile)
config_dict = parse_config_file("/etc/hifiberry.state")
replacements = {
"mixer": config_dict["CURRENT_MIXER_CONTROL"],
"device_name": config_dict["CURRENT_SYSNAME"]
}
changed = update_spotifyd_config(configfile, replacements)
if changed:
print("CHANGED")