🐛 Ported sqlite fix from main

This commit is contained in:
GeoffreyCoulaud
2023-06-13 10:32:07 +02:00
parent dbb96a166b
commit 6dd8e3965f
3 changed files with 32 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
from pathlib import Path
from shutil import rmtree
from sqlite3 import connect
from time import time
@@ -9,10 +10,8 @@ from src.importer.sources.source import (
SourceIterator,
URLExecutableSource,
)
from src.utils.decorators import (
replaced_by_path,
replaced_by_schema_key,
)
from src.utils.decorators import replaced_by_path, replaced_by_schema_key
from src.utils.sqlite import copy_db
class ItchSourceIterator(SourceIterator):
@@ -37,7 +36,8 @@ class ItchSourceIterator(SourceIterator):
caves.game_id = games.id
;
"""
connection = connect(self.source.location / "db" / "butler.db")
db_path = copy_db(self.source.location / "db" / "butler.db")
connection = connect(db_path)
cursor = connection.execute(db_request)
# Create games from the db results
@@ -54,6 +54,9 @@ class ItchSourceIterator(SourceIterator):
game = Game(values, allow_side_effects=False)
yield (game, additional_data)
# Cleanup
rmtree(str(db_path.parent))
class ItchSource(URLExecutableSource):
name = "Itch"

View File

@@ -1,3 +1,4 @@
from shutil import rmtree
from sqlite3 import connect
from time import time
@@ -9,6 +10,7 @@ from src.importer.sources.source import (
URLExecutableSource,
)
from src.utils.decorators import replaced_by_path, replaced_by_schema_key
from src.utils.sqlite import copy_db
class LutrisSourceIterator(SourceIterator):
@@ -30,7 +32,8 @@ class LutrisSourceIterator(SourceIterator):
;
"""
params = {"import_steam": shared.schema.get_boolean("lutris-import-steam")}
connection = connect(self.source.location / "pga.db")
db_path = copy_db(self.source.location / "pga.db")
connection = connect(db_path)
cursor = connection.execute(request, params)
# Create games from the DB results
@@ -56,6 +59,9 @@ class LutrisSourceIterator(SourceIterator):
# Produce game
yield (game, additional_data)
# Cleanup
rmtree(str(db_path.parent))
class LutrisSource(URLExecutableSource):
"""Generic lutris source"""

17
src/utils/sqlite.py Normal file
View File

@@ -0,0 +1,17 @@
from glob import escape
from pathlib import Path
from shutil import copyfile
from gi.repository import GLib
def copy_db(original_path: Path) -> Path:
"""
Copy a sqlite database to a cache dir and return its new path.
The caller in in charge of deleting the returned path's parent dir.
"""
tmp = Path(GLib.Dir.make_tmp())
for file in original_path.parent.glob(f"{escape(original_path.name)}*"):
copy = tmp / file.name
copyfile(str(file), str(copy))
return tmp / original_path.name