From: Michael Tremer Date: Sun, 27 Feb 2011 13:22:39 +0000 (+0100) Subject: Simulate the in-memory database on disk because the backup seems to be broken. X-Git-Tag: 0.9.3~108 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5cc35aa427843869a42081a0002adc3ff7b4cf41;p=pakfire.git Simulate the in-memory database on disk because the backup seems to be broken. --- diff --git a/pakfire/repository/database.py b/pakfire/repository/database.py index 02a13eacd..c2f7d015f 100644 --- a/pakfire/repository/database.py +++ b/pakfire/repository/database.py @@ -2,6 +2,7 @@ import logging import os +import random import shutil import sqlite3 import time @@ -19,9 +20,18 @@ class Cursor(sqlite3.Cursor): class Database(object): def __init__(self, pakfire, filename): self.pakfire = pakfire - self.filename = filename self._db = None + self._tmp = False + + if filename == ":memory:": + self._tmp = True + + filename = "/tmp/.%s-%s" % \ + (random.randint(0, 1024**2), os.path.basename(filename)) + + self.filename = filename + self.open() def __del__(self): @@ -36,14 +46,11 @@ class Database(object): if not self._db: logging.debug("Open database %s" % self.filename) - database_exists = False + dirname = os.path.dirname(self.filename) + if not os.path.exists(dirname): + os.makedirs(dirname) - if not self.filename == ":memory:": - dirname = os.path.dirname(self.filename) - if not os.path.exists(dirname): - os.makedirs(dirname) - - database_exists = os.path.exists(self.filename) + database_exists = os.path.exists(self.filename) # Make a connection to the database. self._db = sqlite3.connect(self.filename) @@ -57,6 +64,9 @@ class Database(object): self._db.close() self._db = None + if self._tmp: + os.unlink(self.filename) + def commit(self): self._db.commit() @@ -70,16 +80,9 @@ class Database(object): """ Save a copy of this database to a new one located at path. """ - db2 = Database(self.pakfire, path) - - script = "" - for line in self._db.iterdump(): - script += "%s\n" % line - - db2.executescript(script) - db2.commit() + self.commit() - db2.close() + shutil.copy2(self.filename, path) class PackageDatabase(Database):