Skip to content

Commit fbf5006

Browse files
committed
Automatically move database from legacy location if it exists
1 parent 20b3b52 commit fbf5006

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

buku

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import html.parser as HTMLParser
2727
from http.client import HTTPConnection, HTTPSConnection
2828
from urllib.parse import urljoin, unquote
2929
import signal
30+
import shutil
3031

3132
# Import libraries needed for encryption
3233
try:
@@ -114,6 +115,30 @@ def getDataPath():
114115

115116

116117

118+
def moveOldDatabase():
119+
olddbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku')
120+
olddbfile = os.path.join(olddbpath, 'bookmarks.db')
121+
122+
if not os.path.exists(olddbfile):
123+
return
124+
125+
newdbpath = getDataPath()
126+
newdbfile = os.path.join(newdbpath, 'bookmarks.db')
127+
128+
if os.path.exists(newdbfile):
129+
print("Both old (%s) and new (%s) databases exist, need manual action" % (olddbfile, newdbfile))
130+
sys.exit(1)
131+
132+
if not os.path.exists(newdbpath):
133+
os.makedirs(newdbpath)
134+
135+
shutil.move(olddbfile, newdbfile)
136+
print("Database was moved from old (%s) to new (%s) location" % (olddbfile, newdbfile))
137+
138+
os.rmdir(olddbpath)
139+
140+
141+
117142
def initdb():
118143
"""Initialize the database connection. Create DB file and/or bookmarks table
119144
if they don't exist. Alert on encryption options on first execution.
@@ -1017,6 +1042,9 @@ if online == True and titleManual != None:
10171042
print("You can either fetch title from web or add/update title manually.\n")
10181043
usage()
10191044

1045+
# Move database to new location, if needed
1046+
moveOldDatabase()
1047+
10201048
# Handle encrypt/decrypt options at top priority
10211049
if encrypt == True:
10221050
encrypt_file()

0 commit comments

Comments
 (0)