-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeta_update.py
executable file
·270 lines (207 loc) · 8.88 KB
/
meta_update.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
#!/usr/bin/env python3
import os
import pathlib
import shutil
import sqlite3
import subprocess
import sys
# Check the python version, minimum version needs to be 3.7
if sys.version_info < (3, 7):
sys.exit("You must use Python 3.7 or newer.")
# Installation type
installType = ""
# Installation and files/DB locations
pgbInstall = pathlib.Path("/opt/appdata/plex/database/")
plexInstall = pathlib.Path("/var/lib/plexmediaserver/")
cbInstall = pathlib.Path("/opt/plex/")
customInstall = ""
plexdb = "Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db"
plexdbBack = "Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db.back"
plexPref = "Library/Application Support/Plex Media Server/Preferences.xml"
plexPrefBack = "Library/Application Support/Plex Media Server/Preferences.xml.back"
backupFile = "database_path.txt"
metaTar = pathlib.Path("plex_linux.tar")
extMsg = "\nDo you want to extract the tar file? (Y/N): "
pathMsg = "\nIs the path correct? (Y/N): "
delMsg = "\nDo you want to delete the database backup? (Y/N):"
extractScript = pathlib.Path("extract.sh")
fixOwnerScript = pathlib.Path("fix-owner.sh")
# User input confirmation
def confirmation(msg):
check = str(input(msg)).lower().strip()
try:
if check[0] == 'y':
return True
elif check[0] == 'n':
return False
else:
print("Invalid Input.")
return confirmation(msg)
except Exception as error:
print("Please enter valid inputs.")
return confirmation(msg)
# Fix the ownership of the files
def fix_ownership(arg, arg2, arg3):
print("\nFixing the ownership on the extracted files, this might take a while...")
cmd = "./fix-owner.sh -d {} -u {} -g {}".format(arg, arg2, arg3)
subprocess.check_call(cmd, shell=True)
# Make sure the helper scripts are executable
def make_executable():
if extractScript.exists():
os.chmod(extractScript, 0o775)
if fixOwnerScript.exists():
os.chmod(fixOwnerScript, 0o775)
# Tar extraction
def extract_tar(arg):
# Rename the old Preferences.xml
if arg.joinpath(plexPref).exists():
os.rename(arg.joinpath(plexPref), arg.joinpath(plexPrefBack))
# Extract the tar file
if metaTar.exists():
print("\nExtracting the tar file, this might take a while...")
subprocess.check_call("./extract.sh -d '%s'" % str(arg), shell=True)
else:
print("\nError! Tar file not found.")
print("Please make sure that the plex.tar is in the same directory.")
sys.exit()
# Rename the Preferences.xml back
if arg.joinpath(plexPrefBack).exists():
os.rename(arg.joinpath(plexPrefBack), arg.joinpath(plexPref))
# Database Modification
def update_database(arg):
# Ask for the mount path
mount_path = input("\nEnter the path of your mount location (E.g. /mnt/plexcloudservers/): ")
# Confirm with the user if the path is correct
print("The path is: " + mount_path)
while not confirmation(pathMsg):
mount_path = input("\nEnter the path of your mount location (E.g. /mnt/plexcloudservers/): ")
# Check the mount path and make sure it ends with "/"
correct_path = mount_path.endswith('/')
if not correct_path:
mount_path = mount_path + "/"
# Backup Database
if arg.joinpath(plexdb).exists():
print("\nCreating Backup of the Database.")
shutil.copy2(arg.joinpath(plexdb), arg.joinpath(plexdbBack))
# Write the db path to a file for future reference
f = open(backupFile, "w")
f.write(str(arg.joinpath(plexdbBack)))
f.close()
print("\nUpdating the database to reflect the new mount path.")
connection = sqlite3.connect(arg.joinpath(plexdb))
cursor = connection.cursor()
sql_command = "DROP TRIGGER fts4_metadata_titles_before_update_icu;"
cursor.execute(sql_command)
sql_command = "DROP TRIGGER fts4_metadata_titles_after_update_icu;"
cursor.execute(sql_command)
sql_command = """UPDATE section_locations SET root_path= replace(root_path, "/mnt/unionfs/Media/", "{}") where root_path like "%/mnt/unionfs/Media/%";""".format(mount_path)
cursor.execute(sql_command)
sql_command = """UPDATE media_streams SET url= replace(url, "file:///mnt/unionfs/Media/", "file://{}") where url like "%file:///mnt/unionfs/Media/%";""".format(mount_path)
cursor.execute(sql_command)
sql_command = """UPDATE media_parts SET file= replace(file, "/mnt/unionfs/Media/", "{}") where file like "%/mnt/unionfs/Media/%";""".format(mount_path)
cursor.execute(sql_command)
sql_command = "CREATE TRIGGER fts4_metadata_titles_before_update_icu BEFORE UPDATE ON metadata_items BEGIN DELETE FROM fts4_metadata_titles_icu WHERE docid=old.rowid; END;"
cursor.execute(sql_command)
sql_command = "CREATE TRIGGER fts4_metadata_titles_after_update_icu AFTER UPDATE ON metadata_items BEGIN INSERT INTO fts4_metadata_titles_icu(docid, title, title_sort, original_title) VALUES(new.rowid, new.title, new.title_sort, new.original_title); END;"
cursor.execute(sql_command)
# Commit the changess
connection.commit()
# Close the connection
connection.close()
print("Database is updated.")
# Removing the database backup
def remove_database_backup():
if os.path.exists(backupFile):
f = open(backupFile, "r")
dbBackFile = f.read()
f.close()
print("\nDeleting Backup of the Database.")
os.remove(dbBackFile)
else:
sys.exit("\nNo backup file found (database_path.txt), exiting.")
# Menu
def make_menu():
global installType
global customInstall
options = "0"
while options == "0":
print("Please select your installation type:\n")
print(" 1. PGBlitz\n 2. CloudBox\n 3. Plex Media Server .deb file (Standard Install)\n 4. Custom path (Docker Install)\n 5. Remove database backup\n 6. Exit\n")
options = input("Option (1-5): ")
if options == "1":
if pgbInstall.exists():
installType = "pgblitz"
elif options == "2":
if cbInstall.exists():
installType = "cloudbox"
elif options == "3":
if os.geteuid() != 0:
print("Error! Please run the script as sudo.")
sys.exit()
if plexInstall.exists():
installType = "dpkg"
elif options == "4":
if os.geteuid() != 0:
print("Error! Please run the script as sudo.")
sys.exit()
usrPath = input("\nPlease enter the path of the custom installation: ")
print("The path is: " + usrPath)
while not confirmation(pathMsg):
usrPath = input("\nEnter the path of the custom installation: ")
if not pathlib.Path(usrPath).exists():
sys.exit("\nPath doesn't exist, please double check it!\n")
else:
customInstall = usrPath
installType = "custom"
elif options == "5":
if os.geteuid() != 0:
print("Error! Please run the script as sudo.")
sys.exit()
installType = "DeleteDatabaseBackup"
elif options == "6":
print("\nExiting")
sys.exit()
else:
print("\nIncorrect answer")
return make_menu()
# Main function
def main():
# Make the menu
make_menu()
if installType == "pgblitz": # PGBlitz Installation
if confirmation(extMsg):
extract_tar(pgbInstall)
# Database Modification
update_database(pgbInstall)
elif installType == "cloudbox": # Cloudbox Installation
if confirmation(extMsg):
extract_tar(cbInstall)
# Database Modification
update_database(cbInstall)
elif installType == "dpkg": # Normal Plex Installation
if confirmation(extMsg):
extract_tar(plexInstall)
# Database Modification
update_database(plexInstall)
# Fix the ownership
fix_ownership(plexInstall, "plex", "plex")
elif installType == "custom": # Plex Docker Installation
if confirmation(extMsg):
extract_tar(pathlib.Path(customInstall))
# Database Modification
update_database(pathlib.Path(customInstall))
# Custom user:group
print("\nSetting custom ownership for the files, e.g. chown user:user")
perm_user = input("\nEnter the user name: ")
perm_group = input("Enter the group name: ")
# Fixing the ownership
fix_ownership(customInstall, perm_user, perm_group)
elif installType == "DeleteDatabaseBackup": # Database Removal
if confirmation(delMsg):
# Remove Database Backup
remove_database_backup()
else:
sys.exit("\nWell then something went wrong here... Exiting")
# Main program
main()
print("\nDone. Enjoy!")