From b3a60de68ffef2846181f0dd84e80488a06632fe Mon Sep 17 00:00:00 2001 From: cos Date: Wed, 3 Jul 2024 01:07:49 +0200 Subject: Clarify variable names Mainly make variables containing file names indicate they do just that, rather than confusingly masquerading as directory paths. --- AntennaPodDbFixer.py | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/AntennaPodDbFixer.py b/AntennaPodDbFixer.py index dfc83ad..e5a9835 100755 --- a/AntennaPodDbFixer.py +++ b/AntennaPodDbFixer.py @@ -13,9 +13,9 @@ if len(sys.argv) > 1: print(usage()) exit(0) else: - inputPath = sys.argv[1] + inputFilePath = sys.argv[1] else: - inputPath = input("Enter file path to your corrupted AntennaPod database: ") + inputFilePath = input("Enter file path to your corrupted AntennaPod database: ") DBPAGE_SYM = "ENABLE_DBPAGE_VTAB" dbpage_ext = subprocess.run([ @@ -29,7 +29,7 @@ if dbpage_ext != DBPAGE_SYM: exit(1) corruptedVersion = subprocess.run( - ["sqlite3", inputPath, "PRAGMA user_version;"], + ["sqlite3", inputFilePath, "PRAGMA user_version;"], capture_output=True, text=True ).stdout.strip() @@ -38,12 +38,12 @@ if corruptedVersion == "0": exit() print("Corrupted file version: " + corruptedVersion) -emptyPath = "empty/" + corruptedVersion + ".db" -if not os.path.isfile(emptyPath): +emptyFilePath = "empty/" + corruptedVersion + ".db" +if not os.path.isfile(emptyFilePath): print("If needed, you can download old app versions on F-Droid and export an empty database.") - emptyPath = input("Enter file path to an EMPTY AntennaPod database with the same version: ") + emptyFilePath = input("Enter file path to an EMPTY AntennaPod database with the same version: ") emptyVersion = subprocess.run( - ["sqlite3", emptyPath, "PRAGMA user_version;"], + ["sqlite3", emptyFilePath, "PRAGMA user_version;"], capture_output=True, text=True ).stdout.strip() @@ -53,30 +53,30 @@ if not os.path.isfile(emptyPath): exit() print() -outputPath = inputPath + "-repaired.db" -sqlPath = inputPath + ".sql.tmp" -corruptedPath = inputPath + ".tmp" +repairedFilePath = inputFilePath + "-repaired.db" +sqlFilePath = inputFilePath + ".sql.tmp" +workingcopyFilePath = inputFilePath + ".tmp" -shutil.copyfile(emptyPath, outputPath, follow_symlinks=True) -if os.path.exists(sqlPath): os.remove(sqlPath) -if os.path.exists(corruptedPath): os.remove(corruptedPath) +shutil.copyfile(emptyFilePath, repairedFilePath, follow_symlinks=True) +if os.path.exists(sqlFilePath): os.remove(sqlFilePath) +if os.path.exists(workingcopyFilePath): os.remove(workingcopyFilePath) # Recover to SQL commands and insert back into a database print("Recovering database.") subprocess.run( - ["sqlite3", inputPath, ".recover --ignore-freelist"], + ["sqlite3", inputFilePath, ".recover --ignore-freelist"], check=True, - stdout=open(sqlPath, 'w') + stdout=open(sqlFilePath, 'w') ) -f = open(sqlPath,'r') +f = open(sqlFilePath,'r') filedata = f.read() f.close() -f = open(sqlPath,'w') +f = open(sqlFilePath,'w') # Avoid a warning that could be confusing to users f.write(filedata.replace("CREATE TABLE sqlite_sequence(name,seq);","")) f.close() -subprocess.run(["sqlite3", corruptedPath], stdin=open(sqlPath, 'r')) +subprocess.run(["sqlite3", workingcopyFilePath], stdin=open(sqlFilePath, 'r')) print() @@ -97,24 +97,24 @@ def query(db, query): except json.decoder.JSONDecodeError as err: return result -tables = query(emptyPath, "SELECT name FROM sqlite_schema WHERE type='table';") +tables = query(emptyFilePath, "SELECT name FROM sqlite_schema WHERE type='table';") for table in tables: table = table["name"] print("Copying " + table + "...") columns = query( - emptyPath, + emptyFilePath, "SELECT GROUP_CONCAT(NAME,',') AS columns FROM PRAGMA_TABLE_INFO('" + table + "')" )[0]["columns"] - query(outputPath, "DELETE FROM " + table) + query(repairedFilePath, "DELETE FROM " + table) query( - outputPath, - "attach '" + corruptedPath + "' AS old; INSERT INTO main." + table + " SELECT " + + repairedFilePath, + "attach '" + workingcopyFilePath + "' AS old; INSERT INTO main." + table + " SELECT " + columns + " from old." + table + ";" ) print() # Cleanup -os.remove(sqlPath) -os.remove(corruptedPath) -print("Done. Output file: " + outputPath) +os.remove(sqlFilePath) +os.remove(workingcopyFilePath) +print("Done. Output file: " + repairedFilePath) -- cgit v1.2.3