Automatically move database from legacy location if it exists

This commit is contained in:
Dmitry Marakasov 2016-04-08 15:38:40 +03:00
parent 20b3b52629
commit fbf5006978

28
buku
View File

@ -27,6 +27,7 @@ import html.parser as HTMLParser
from http.client import HTTPConnection, HTTPSConnection
from urllib.parse import urljoin, unquote
import signal
import shutil
# Import libraries needed for encryption
try:
@ -114,6 +115,30 @@ def getDataPath():
def moveOldDatabase():
olddbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku')
olddbfile = os.path.join(olddbpath, 'bookmarks.db')
if not os.path.exists(olddbfile):
return
newdbpath = getDataPath()
newdbfile = os.path.join(newdbpath, 'bookmarks.db')
if os.path.exists(newdbfile):
print("Both old (%s) and new (%s) databases exist, need manual action" % (olddbfile, newdbfile))
sys.exit(1)
if not os.path.exists(newdbpath):
os.makedirs(newdbpath)
shutil.move(olddbfile, newdbfile)
print("Database was moved from old (%s) to new (%s) location" % (olddbfile, newdbfile))
os.rmdir(olddbpath)
def initdb():
"""Initialize the database connection. Create DB file and/or bookmarks table
if they don't exist. Alert on encryption options on first execution.
@ -1017,6 +1042,9 @@ if online == True and titleManual != None:
print("You can either fetch title from web or add/update title manually.\n")
usage()
# Move database to new location, if needed
moveOldDatabase()
# Handle encrypt/decrypt options at top priority
if encrypt == True:
encrypt_file()