diff --git a/ChangeLog b/ChangeLog index 11f047c77..c5a120db9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2007-11-25 Jim Sack + * src/Utils.py : add get_empty_tempdir() and rm_tempdir() + * src/ArgHandler.py : use get_empty_tempdir, etc + * src/test/gramps_cli_test.py : update test for new import dir + - Note get_empty_tempdir forces location in (new) const.TEMP_DIR. + - Note rm_tempdir is paranoid in only removing subdirs under + TEMP_DIR -- if needed callers can code shutil.rmtree themselves. + - Also added FIXME comment in AH - about a prior comment and + code change that I feel needs re-examination. + 2007-11-25 Jim Sack * src/gramps_main.py : remove trial code left in on last commit the r9407 trial code may be of interest as it demos how I avoid diff --git a/src/ArgHandler.py b/src/ArgHandler.py index 760eb978c..1f43b8b13 100644 --- a/src/ArgHandler.py +++ b/src/ArgHandler.py @@ -64,16 +64,6 @@ from PluginUtils import Tool, cl_list, cli_tool_list from ReportBase import CATEGORY_BOOK, CATEGORY_CODE, CATEGORY_WEB, cl_report -def _rm_files(dirpath, pattern="*"): - """ Remove files in a directory which match the pattern - - The optional pattern can use shell-style wildcards - - """ - for fpath in glob.glob(os.path.join(dirpath, pattern)): - if os.path.isfile(fpath): - os.remove(fpath) - #------------------------------------------------------------------------- # ArgHandler #------------------------------------------------------------------------- @@ -406,23 +396,8 @@ class ArgHandler: if self.imports: self.cl = bool(self.exports or self.actions or self.cl) - # Create dir for imported database(s) - self.impdir_path = os.path.join(const.HOME_DIR,"import") - self.imp_db_path = os.path.join(self.impdir_path,"import_db.grdb") - if not os.path.isdir(self.impdir_path): - try: - os.mkdir(self.impdir_path,0700) - except: - print "Could not create import directory %s. Exiting." \ - % self.impdir_path - sys.exit(1) - elif not os.access(self.impdir_path,os.W_OK): - print "Import directory %s is not writable. Exiting." \ - % self.impdir_path - sys.exit(1) - # and clean it up before use - _rm_files(self.impdir_path) - _rm_files(self.imp_db_path) + # Create empty dir for imported database(s) + self.imp_db_path = Utils.get_empty_tempdir("import_dbdir") self.vm.db_loader.read_file(self.imp_db_path,const.APP_GRAMPS) @@ -450,8 +425,8 @@ class ArgHandler: print "Cleaning up." # remove files in import db subdir after use self.state.db.close() - if self.imports: - _rm_files(self.imp_db_path) + if self.imp_db_path: + Utils.rm_tempdir(self.imp_db_path) print "Exiting." sys.exit(0) @@ -513,23 +488,7 @@ class ArgHandler: print "Error importing %s" % filename sys.exit(1) elif format == 'gramps-pkg': - # Create tempdir, if it does not exist, then check for writability - tmpdir_path = os.path.join(const.HOME_DIR,"tmp") - if not os.path.isdir(tmpdir_path): - try: - os.mkdir(tmpdir_path,0700) - except: - print "Could not create temporary directory %s" \ - % tmpdir_path - sys.exit(1) - elif not os.access(tmpdir_path,os.W_OK): - print "Temporary directory %s is not writable" % tmpdir_path - sys.exit(1) - else: # tempdir exists and writable -- clean it up if not empty - files = os.listdir(tmpdir_path) ; - for fn in files: - os.remove( os.path.join(tmpdir_path,fn) ) - + tmpdir_path = Utils.get_empty_tempdir("imp_gpkgdir") try: import tarfile archive = tarfile.open(filename) @@ -557,6 +516,14 @@ class ArgHandler: # Clean up tempdir after ourselves # THIS HAS BEEN CHANGED, because now we want to keep images # stay after the import is over. Just delete the XML file. + ##jgs:FIXME for how long? just for debug? or this session? + ## must not be forever, since re-exec of this routine + ## clears dirfiles without asking + ## & expands nre tarball possibly overwriting subdirs + ## + ## if only debugging, could do Utils.rm_tempdir here + ## in any case, no real harm (exc. space) to leave stuff here + ## until next exec of this, which will discard all old stuff os.remove(dbname) ## files = os.listdir(tmpdir_path) ## for fn in files: diff --git a/src/Utils.py b/src/Utils.py index 2caad040c..29c69e8ff 100644 --- a/src/Utils.py +++ b/src/Utils.py @@ -51,6 +51,8 @@ import gen.lib import Errors from QuestionDialog import WarningDialog +from const import TEMP_DIR +import shutil #------------------------------------------------------------------------- # @@ -862,6 +864,25 @@ def get_new_filename(ext, folder='~/'): ix = ix + 1 return os.path.expanduser(_NEW_NAME_PATTERN % (folder, os.path.sep, ix, ext)) +def get_empty_tempdir(dirname): + """ Return path to TEMP_DIR/dirname, a guaranteed empty directory + + makes intervening directories if required + fails if _file_ by that name already exists, + or for inadequate permissions to delete dir/files or create dir(s) + + """ + dirpath = os.path.join(TEMP_DIR,dirname) + if os.path.isdir(dirpath): + shutil.rmtree(dirpath) + os.makedirs(dirpath) + return dirpath + +def rm_tempdir(path): + """Remove a tempdir created with get_empty_tempdir""" + if path.startswith(TEMP_DIR) and os.path.isdir(path): + shutil.rmtree(path) + def cast_to_bool(val): if val == str(True): return True diff --git a/src/test/gramps_cli_test.py b/src/test/gramps_cli_test.py index 42a34f8b0..67ae0f010 100644 --- a/src/test/gramps_cli_test.py +++ b/src/test/gramps_cli_test.py @@ -51,13 +51,13 @@ class Test(unittest.TestCase): g = re.search("INDI", content) s.assertTrue(g, "found 'INDI' in output file") - # this verifies that files in the "import dir" + # this verifies that files in the temporary "import dir" # get cleaned before (and after) running a CLI # (eg cleanout stale files from prior crash-runs) def test3_files_in_import_dir(s): import const - idir = os.path.join(const.HOME_DIR,"import") - ddir = os.path.join(idir, "import_db.grdb") + ddir = os.path.join(const.TEMP_DIR,"import_dbdir") + os.makedirs(ddir) bogofiles = [os.path.join(ddir,fn) for fn in ("family.db", "lock")] for fn in bogofiles: