From fbf5006978e29a43b0bbfc594c195324a1a61693 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 8 Apr 2016 15:38:40 +0300 Subject: [PATCH] Automatically move database from legacy location if it exists --- buku | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/buku b/buku index 8a1805b..944d8bf 100755 --- a/buku +++ b/buku @@ -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()