| 1 |
# Copyright (C) 2024 Johannes Sasongko <johannes sasongko org> |
| 2 |
# |
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 2 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License along |
| 14 |
# with this program; if not, write to the Free Software Foundation, Inc., |
| 15 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 |
# |
| 17 |
# |
| 18 |
# The developers of the Exaile media player hereby grant permission |
| 19 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 20 |
# distributed together with GStreamer and Exaile. This permission is |
| 21 |
# above and beyond the permissions granted by the GPL license by which |
| 22 |
# Exaile is covered. If you modify this code, you may extend this |
| 23 |
# exception to your version of the code, but you are not obligated to |
| 24 |
# do so. If you do not wish to do so, delete this exception statement |
| 25 |
# from your version. |
| 26 |
|
| 27 |
import dbm |
| 28 |
import glob |
| 29 |
import os |
| 30 |
import os.path |
| 31 |
import pickle |
| 32 |
import shelve |
| 33 |
from typing import List |
| 34 |
|
| 35 |
try: |
| 36 |
import berkeleydb |
| 37 |
except ImportError: |
| 38 |
import bsddb3 as berkeleydb |
| 39 |
|
| 40 |
from xl import common, sqlitedbm |
| 41 |
|
| 42 |
|
| 43 |
def migrate(old_path: str, new_path: str): |
| 44 |
""" |
| 45 |
Migrate to SQLite-based shelf database. |
| 46 |
|
| 47 |
The source can be a BsdDbShelf but can also be any of the built-in shelves, |
| 48 |
because there was a time (probably only during 4.0 beta?) when Exaile could |
| 49 |
produce non-bsddb shelves. |
| 50 |
It even supports old databases created on Python 2. |
| 51 |
|
| 52 |
:return: Whether a migration is performed |
| 53 |
""" |
| 54 |
|
| 55 |
class Utf8Unpickler(pickle.Unpickler): |
| 56 |
def __init__(self, *args, **kwargs): |
| 57 |
kwargs['encoding'] = 'utf-8' |
| 58 |
super().__init__(*args, **kwargs) |
| 59 |
|
| 60 |
# Change shelve's unpickler to use UTF-8, for compatibility with shelves |
| 61 |
# created by Python 2. |
| 62 |
# This is safe to remove if we don't want the compatibility anymore. |
| 63 |
shelve.Unpickler = Utf8Unpickler |
| 64 |
|
| 65 |
# Read data from old db |
| 66 |
try: |
| 67 |
old_db = berkeleydb.hashopen(old_path, 'r') |
| 68 |
except (berkeleydb.db.DBNoSuchFileError, berkeleydb.db.DBInvalidArgError): |
| 69 |
try: |
| 70 |
old_db = dbm.open(old_path, 'r') |
| 71 |
except dbm.error: |
| 72 |
return False |
| 73 |
old_shelf = shelve.Shelf(old_db, protocol=common.PICKLE_PROTOCOL) |
| 74 |
del old_db # Don't accidentally use the raw database |
| 75 |
items = list(old_shelf.items()) |
| 76 |
old_shelf.close() |
| 77 |
|
| 78 |
# Set aside the old files as temporary backup |
| 79 |
bak_path = old_path + '.before-sqlite' |
| 80 |
old_group = _get_file_group(old_path) |
| 81 |
bak_group = [bak_path + old[len(old_path) :] for old in old_group] |
| 82 |
for old, bak in zip(old_group, bak_group): |
| 83 |
os.rename(old, bak) |
| 84 |
|
| 85 |
# Write data to new db |
| 86 |
new_shelf = shelve.Shelf( |
| 87 |
sqlitedbm.SqliteDbm(new_path, autocommit=False), protocol=common.PICKLE_PROTOCOL |
| 88 |
) |
| 89 |
for key, value in items: |
| 90 |
new_shelf[key] = value |
| 91 |
new_shelf.close() |
| 92 |
|
| 93 |
# # Remove the backup files |
| 94 |
# for bak in _get_file_group(bak_path): |
| 95 |
# try: |
| 96 |
# os.remove(bak) |
| 97 |
# except Exception: |
| 98 |
# pass |
| 99 |
|
| 100 |
return True |
| 101 |
|
| 102 |
|
| 103 |
def _get_file_group(path: str) -> List[str]: |
| 104 |
""" |
| 105 |
Get `path` (if exists) and `path.*` files. |
| 106 |
|
| 107 |
Databases like to create multiple files; this tries to get all of them. |
| 108 |
""" |
| 109 |
group = glob.glob(path + '.*') |
| 110 |
if os.path.exists(path): |
| 111 |
group.insert(0, path) |
| 112 |
return group |