diff --git a/ChangeLog b/ChangeLog index 2e9f61299..cd7d92c67 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2007-06-24 Alex Roitman + * src/plugins/ReadGrdb.py (importData): Rework to upgrade and + import a copy of grdb file, leaving the original file untouched. + * src/GrampsDb/_GrampsBSDDB.py (make_env_name): Add method to + determine the environment name. + 2007-06-24 Brian Matherly * src/plugins/BookReport.py (BookReportDialog::__init__): Fix non text report styles diff --git a/src/GrampsDb/_GrampsBSDDB.py b/src/GrampsDb/_GrampsBSDDB.py index bb8682a80..ba1cffc6f 100644 --- a/src/GrampsDb/_GrampsBSDDB.py +++ b/src/GrampsDb/_GrampsBSDDB.py @@ -417,14 +417,7 @@ class GrampsBSDDB(GrampsDbBase, UpdateCallback): if os.path.isfile(self.full_name): env_flags = env_flags | db.DB_RECOVER - # Environment name is now based on the filename - drive, tmp_name = os.path.splitdrive(self.full_name) - tmp_name = tmp_name.lstrip(os.sep) - env_name = os.path.join(os.path.expanduser(const.env_dir), tmp_name) - else: - env_flags = db.DB_CREATE | db.DB_PRIVATE | db.DB_INIT_MPOOL - env_name = os.path.expanduser('~') - + env_name = self.make_env_name(self.full_name) self.env.open(env_name, env_flags) if self.UseTXN: self.env.txn_checkpoint() @@ -501,6 +494,17 @@ class GrampsBSDDB(GrampsDbBase, UpdateCallback): db_copy(other_database, self, callback) return 1 + def make_env_name(self,full_name): + if self.UseTXN: + # Environment name is now based on the filename + drive, tmp_name = os.path.splitdrive(full_name) + tmp_name = tmp_name.lstrip(os.sep) + env_name = os.path.join(os.path.expanduser(const.env_dir),tmp_name) + else: + env_flags = db.DB_CREATE | db.DB_PRIVATE | db.DB_INIT_MPOOL + env_name = os.path.expanduser('~') + return env_name + def __load_metadata(self): # name display formats self.name_formats = self.metadata.get('name_formats', default=[]) diff --git a/src/plugins/ReadGrdb.py b/src/plugins/ReadGrdb.py index 05eb6f8cc..3cd7ec664 100644 --- a/src/plugins/ReadGrdb.py +++ b/src/plugins/ReadGrdb.py @@ -29,8 +29,9 @@ # #------------------------------------------------------------------------- import os +import shutil +import tempfile from gettext import gettext as _ -import sets #------------------------------------------------------------------------- # @@ -58,16 +59,31 @@ from PluginUtils import register_import #------------------------------------------------------------------------- def importData(database, filename, callback=None,cl=0,use_trans=True): - filename = os.path.normpath(filename) - other_database = GrampsBSDDB() + + # Since we don't want to modify the file being imported, + # we create new temp file into which we will copy the imported file + orig_filename = os.path.normpath(filename) + new_filename = tempfile.mkstemp()[1] + new_env_name = tempfile.mkdtemp() + + # determine old env dir and make db work with new env dir + orig_env_name = other_database.make_env_name(orig_filename) + other_database.make_env_name = lambda x: new_env_name + + # copy data (and env if using TXN) + shutil.copyfile(orig_filename,new_filename) + if other_database.UseTXN: + shutil.rmtree(new_env_name) + shutil.copytree(orig_env_name,new_env_name) + try: - other_database.load(filename,callback) + other_database.load(new_filename,callback) except: if cl: - print "Error: %s could not be opened. Exiting." % filename + print "Error: %s could not be opened. Exiting." % new_filename else: - ErrorDialog(_("%s could not be opened") % filename) + ErrorDialog(_("%s could not be opened") % new_filename) return if not other_database.version_supported(): @@ -216,6 +232,10 @@ def importData(database, filename, callback=None,cl=0,use_trans=True): # close the other database and clean things up other_database.close() + # Remove temp file and env dir + os.unlink(new_filename) + shutil.rmtree(new_env_name) + database.transaction_commit(trans,_("Import database")) database.enable_signals() database.request_rebuild() @@ -224,8 +244,8 @@ def check_common_handles(table,other_table,msg): # Check for duplicate handles. At the moment we simply exit here, # before modifying any data. In the future we will need to handle # this better. How? - handles = sets.Set(table.keys()) - other_handles = sets.Set(other_table.keys()) + handles = set(table.keys()) + other_handles = set(other_table.keys()) if handles.intersection(other_handles): raise HandleError(msg) return len(other_handles)