Replace TEMP_DIR by tempfile object
This commit is contained in:
parent
1dd36390ff
commit
fd26b5f066
@ -48,7 +48,7 @@ from glob import glob
|
|||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
from gramps.gen.const import (LONGOPTS, SHORTOPTS, USER_PLUGINS, VERSION_DIR,
|
from gramps.gen.const import (LONGOPTS, SHORTOPTS, USER_PLUGINS, VERSION_DIR,
|
||||||
HOME_DIR, TEMP_DIR, THUMB_DIR, ENV_DIR, USER_CSS)
|
HOME_DIR, THUMB_DIR, ENV_DIR, USER_CSS)
|
||||||
from gramps.gen.utils.cast import get_type_converter
|
from gramps.gen.utils.cast import get_type_converter
|
||||||
from gramps.gen.const import GRAMPS_LOCALE as glocale
|
from gramps.gen.const import GRAMPS_LOCALE as glocale
|
||||||
_ = glocale.translation.gettext
|
_ = glocale.translation.gettext
|
||||||
@ -423,7 +423,6 @@ class ArgParser:
|
|||||||
for fil in glob(os.path.join(HOME_DIR, "*.zip")):
|
for fil in glob(os.path.join(HOME_DIR, "*.zip")):
|
||||||
os.remove(fil)
|
os.remove(fil)
|
||||||
if 'E' in value: # Everything else
|
if 'E' in value: # Everything else
|
||||||
rmtree(TEMP_DIR)
|
|
||||||
rmtree(THUMB_DIR)
|
rmtree(THUMB_DIR)
|
||||||
rmtree(USER_CSS)
|
rmtree(USER_CSS)
|
||||||
rmtree(ENV_DIR)
|
rmtree(ENV_DIR)
|
||||||
|
@ -26,7 +26,6 @@ import unittest
|
|||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from gramps.gen.const import TEMP_DIR
|
|
||||||
from gramps.gen.dbstate import DbState
|
from gramps.gen.dbstate import DbState
|
||||||
from gramps.test.test_util import Gramps
|
from gramps.test.test_util import Gramps
|
||||||
|
|
||||||
@ -113,34 +112,6 @@ class Test(unittest.TestCase):
|
|||||||
g = re.search("INDI", content)
|
g = re.search("INDI", content)
|
||||||
self.assertTrue(g, "found 'INDI' in output file")
|
self.assertTrue(g, "found 'INDI' in output file")
|
||||||
|
|
||||||
# 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(self):
|
|
||||||
ddir = os.path.join(TEMP_DIR, "import_dbdir")
|
|
||||||
try:
|
|
||||||
os.makedirs(ddir)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
bogofiles = [os.path.join(ddir, fn) for fn in ("family.db", "lock")]
|
|
||||||
for fn in bogofiles:
|
|
||||||
with open(fn, "w") as f:
|
|
||||||
f.write("garbage")
|
|
||||||
|
|
||||||
# ~same as test 2
|
|
||||||
ifile = min1r
|
|
||||||
ofile = out_ged
|
|
||||||
gcmd = [sys.executable, "Gramps.py", "-i", ifile, "-e", ofile]
|
|
||||||
process = subprocess.Popen(gcmd,
|
|
||||||
stdin=subprocess.PIPE,
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.PIPE)
|
|
||||||
result_str, err_str = process.communicate()
|
|
||||||
self.assertEqual(process.returncode, 0,
|
|
||||||
"executed CLI command %r" % gcmd)
|
|
||||||
|
|
||||||
for fn in bogofiles:
|
|
||||||
self.assertFalse(os.path.exists(fn))
|
|
||||||
|
|
||||||
class UnicodeTest(unittest.TestCase):
|
class UnicodeTest(unittest.TestCase):
|
||||||
|
|
||||||
|
@ -122,14 +122,13 @@ TOOL_OPTIONS = os.path.join(HOME_DIR, "tool_options.xml")
|
|||||||
PLACE_FORMATS = os.path.join(HOME_DIR, "place_formats.xml")
|
PLACE_FORMATS = os.path.join(HOME_DIR, "place_formats.xml")
|
||||||
|
|
||||||
ENV_DIR = os.path.join(HOME_DIR, "env")
|
ENV_DIR = os.path.join(HOME_DIR, "env")
|
||||||
TEMP_DIR = os.path.join(HOME_DIR, "temp")
|
|
||||||
THUMB_DIR = os.path.join(HOME_DIR, "thumb")
|
THUMB_DIR = os.path.join(HOME_DIR, "thumb")
|
||||||
THUMB_NORMAL = os.path.join(THUMB_DIR, "normal")
|
THUMB_NORMAL = os.path.join(THUMB_DIR, "normal")
|
||||||
THUMB_LARGE = os.path.join(THUMB_DIR, "large")
|
THUMB_LARGE = os.path.join(THUMB_DIR, "large")
|
||||||
USER_PLUGINS = os.path.join(VERSION_DIR, "plugins")
|
USER_PLUGINS = os.path.join(VERSION_DIR, "plugins")
|
||||||
USER_CSS = os.path.join(HOME_DIR, "css")
|
USER_CSS = os.path.join(HOME_DIR, "css")
|
||||||
# dirs checked/made for each Gramps session
|
# dirs checked/made for each Gramps session
|
||||||
USER_DIRLIST = (USER_HOME, HOME_DIR, VERSION_DIR, ENV_DIR, TEMP_DIR, THUMB_DIR,
|
USER_DIRLIST = (USER_HOME, HOME_DIR, VERSION_DIR, ENV_DIR, THUMB_DIR,
|
||||||
THUMB_NORMAL, THUMB_LARGE, USER_PLUGINS, USER_CSS)
|
THUMB_NORMAL, THUMB_LARGE, USER_PLUGINS, USER_CSS)
|
||||||
|
|
||||||
|
|
||||||
@ -199,7 +198,6 @@ ENV = {
|
|||||||
"major_version": major_version,
|
"major_version": major_version,
|
||||||
"VERSION_DIR": VERSION_DIR,
|
"VERSION_DIR": VERSION_DIR,
|
||||||
"ENV_DIR": ENV_DIR,
|
"ENV_DIR": ENV_DIR,
|
||||||
"TEMP_DIR": TEMP_DIR,
|
|
||||||
"THUMB_DIR": THUMB_DIR,
|
"THUMB_DIR": THUMB_DIR,
|
||||||
"THUMB_NORMAL": THUMB_NORMAL,
|
"THUMB_NORMAL": THUMB_NORMAL,
|
||||||
"THUMB_LARGE": THUMB_LARGE,
|
"THUMB_LARGE": THUMB_LARGE,
|
||||||
|
@ -29,11 +29,12 @@ from io import BytesIO
|
|||||||
import difflib
|
import difflib
|
||||||
import copy
|
import copy
|
||||||
import lxml.etree as ET
|
import lxml.etree as ET
|
||||||
|
import tempfile
|
||||||
|
|
||||||
from gramps.plugins.lib.libgrampsxml import GRAMPS_XML_VERSION
|
from gramps.plugins.lib.libgrampsxml import GRAMPS_XML_VERSION
|
||||||
from gramps.test.test_util import Gramps
|
from gramps.test.test_util import Gramps
|
||||||
from gramps.gen.user import User
|
from gramps.gen.user import User
|
||||||
from gramps.gen.const import DATA_DIR, USER_PLUGINS, TEMP_DIR
|
from gramps.gen.const import DATA_DIR, USER_PLUGINS
|
||||||
from gramps.version import VERSION
|
from gramps.version import VERSION
|
||||||
from gramps.gen.lib import Name, Surname
|
from gramps.gen.lib import Name, Surname
|
||||||
from gramps.gen.const import GRAMPS_LOCALE as glocale
|
from gramps.gen.const import GRAMPS_LOCALE as glocale
|
||||||
@ -104,11 +105,8 @@ class BaseMergeCheck(unittest.TestCase):
|
|||||||
|
|
||||||
def check_results(self, input_doc, expect_doc, result_str, err_str,
|
def check_results(self, input_doc, expect_doc, result_str, err_str,
|
||||||
test_error_str=''):
|
test_error_str=''):
|
||||||
input_file = os.path.join(TEMP_DIR, "merge_test_input.gramps")
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||||
try:
|
input_file = os.path.join(tmpdirname, "merge_test_input.gramps")
|
||||||
os.remove(input_file)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if err_str:
|
if err_str:
|
||||||
if test_error_str:
|
if test_error_str:
|
||||||
@ -122,17 +120,10 @@ class BaseMergeCheck(unittest.TestCase):
|
|||||||
inpt.close()
|
inpt.close()
|
||||||
raise Exception(err_str)
|
raise Exception(err_str)
|
||||||
result = self.canonicalize(result_str)
|
result = self.canonicalize(result_str)
|
||||||
result_file = os.path.join(TEMP_DIR, "merge_test_result.gramps")
|
result_file = os.path.join(tmpdirname, "merge_test_result.gramps")
|
||||||
try:
|
|
||||||
os.remove(result_file)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
expect = self.canonicalize(expect_doc)
|
expect = self.canonicalize(expect_doc)
|
||||||
expect_file = os.path.join(TEMP_DIR, "merge_test_expected.gramps")
|
expect_file = os.path.join(tmpdirname, "merge_test_expected.gramps")
|
||||||
try:
|
|
||||||
os.remove(expect_file)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
if result != expect:
|
if result != expect:
|
||||||
res = open(result_file, mode='wb')
|
res = open(result_file, mode='wb')
|
||||||
res.write(result)
|
res.write(result)
|
||||||
@ -180,11 +171,8 @@ class BaseMergeCheck(unittest.TestCase):
|
|||||||
msg = '\n***** result:\n' + result_str + \
|
msg = '\n***** result:\n' + result_str + \
|
||||||
'\n***** expect:\n' + expect_str
|
'\n***** expect:\n' + expect_str
|
||||||
inp = self.canonicalize(input_doc)
|
inp = self.canonicalize(input_doc)
|
||||||
input_file = os.path.join(TEMP_DIR, "merge_test_input.gramps")
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||||
try:
|
input_file = os.path.join(tmpdirname, "merge_test_input.gramps")
|
||||||
os.remove(input_file)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
inpt = open(input_file, mode='wb')
|
inpt = open(input_file, mode='wb')
|
||||||
inpt.write(inp)
|
inpt.write(inp)
|
||||||
inpt.close()
|
inpt.close()
|
||||||
|
@ -32,6 +32,7 @@ File and folder related utility functions
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import shutil
|
import shutil
|
||||||
|
import tempfile
|
||||||
import hashlib
|
import hashlib
|
||||||
import logging
|
import logging
|
||||||
LOG = logging.getLogger(".gen.utils.file")
|
LOG = logging.getLogger(".gen.utils.file")
|
||||||
@ -42,7 +43,7 @@ LOG = logging.getLogger(".gen.utils.file")
|
|||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
from ..constfunc import win, mac, get_env_var
|
from ..constfunc import win, mac, get_env_var
|
||||||
from ..const import TEMP_DIR, USER_HOME, ENV, GRAMPS_LOCALE as glocale
|
from ..const import USER_HOME, ENV, GRAMPS_LOCALE as glocale
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
@ -94,15 +95,12 @@ def get_empty_tempdir(dirname):
|
|||||||
or for inadequate permissions to delete dir/files or create dir(s)
|
or for inadequate permissions to delete dir/files or create dir(s)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
dirpath = os.path.join(TEMP_DIR, str(dirname))
|
dirpath = tempfile.mkdtemp(prefix='gramps-', suffix='-'+dirname)
|
||||||
if os.path.isdir(dirpath):
|
|
||||||
shutil.rmtree(dirpath)
|
|
||||||
os.makedirs(dirpath)
|
|
||||||
return dirpath
|
return dirpath
|
||||||
|
|
||||||
def rm_tempdir(path):
|
def rm_tempdir(path):
|
||||||
"""Remove a tempdir created with get_empty_tempdir"""
|
"""Remove a tempdir created with get_empty_tempdir"""
|
||||||
if path.startswith(TEMP_DIR) and os.path.isdir(path):
|
if path.startswith(tempfile.gettempdir()) and os.path.isdir(path):
|
||||||
shutil.rmtree(path)
|
shutil.rmtree(path)
|
||||||
|
|
||||||
def relative_path(original, base):
|
def relative_path(original, base):
|
||||||
|
@ -31,6 +31,7 @@ File tests.
|
|||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
import os
|
import os
|
||||||
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
@ -38,8 +39,8 @@ import unittest
|
|||||||
# Gramps modules
|
# Gramps modules
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
from ...const import TEMP_DIR, USER_HOME, USER_PLUGINS, VERSION
|
from ...const import USER_HOME, USER_PLUGINS, VERSION
|
||||||
from ...utils.file import media_path, get_empty_tempdir
|
from ...utils.file import media_path
|
||||||
from ...db.utils import make_database
|
from ...db.utils import make_database
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
@ -56,10 +57,11 @@ class FileTest(unittest.TestCase):
|
|||||||
"""
|
"""
|
||||||
Test media path variables.
|
Test media path variables.
|
||||||
"""
|
"""
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||||
# Create database
|
# Create database
|
||||||
db = make_database("sqlite")
|
db = make_database("sqlite")
|
||||||
path = get_empty_tempdir("utils_file_test")
|
path = os.path.join(tmpdirname, "utils_file_test")
|
||||||
|
os.makedirs(path)
|
||||||
db.load(path)
|
db.load(path)
|
||||||
|
|
||||||
# Test without db.mediapath set
|
# Test without db.mediapath set
|
||||||
@ -75,7 +77,7 @@ class FileTest(unittest.TestCase):
|
|||||||
# Test with relative db.mediapath
|
# Test with relative db.mediapath
|
||||||
db.set_mediapath("test_rel")
|
db.set_mediapath("test_rel")
|
||||||
self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(
|
self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(
|
||||||
os.path.abspath(TEMP_DIR + "/utils_file_test/test_rel"))))
|
os.path.abspath(tmpdirname + "/utils_file_test/test_rel"))))
|
||||||
|
|
||||||
# Test with environment variable
|
# Test with environment variable
|
||||||
db.set_mediapath("/test/{VERSION}/test_var")
|
db.set_mediapath("/test/{VERSION}/test_var")
|
||||||
@ -86,7 +88,7 @@ class FileTest(unittest.TestCase):
|
|||||||
os.path.abspath(USER_PLUGINS + "/test_var"))))
|
os.path.abspath(USER_PLUGINS + "/test_var"))))
|
||||||
db.set_mediapath("{VERSION}/test_var")
|
db.set_mediapath("{VERSION}/test_var")
|
||||||
self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(
|
self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(
|
||||||
os.path.abspath(TEMP_DIR + "/utils_file_test/" + VERSION +
|
os.path.abspath(tmpdirname + "/utils_file_test/" + VERSION +
|
||||||
"/test_var"))))
|
"/test_var"))))
|
||||||
|
|
||||||
# Test with $GRAMPSHOME environment variable not set
|
# Test with $GRAMPSHOME environment variable not set
|
||||||
|
@ -24,9 +24,10 @@ import os
|
|||||||
import difflib
|
import difflib
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
from time import localtime, strptime
|
from time import localtime, strptime
|
||||||
|
import tempfile
|
||||||
|
|
||||||
from gramps.test.test_util import Gramps
|
from gramps.test.test_util import Gramps
|
||||||
from gramps.gen.const import TEMP_DIR, DATA_DIR
|
from gramps.gen.const import DATA_DIR
|
||||||
from gramps.gen.datehandler import set_format
|
from gramps.gen.datehandler import set_format
|
||||||
from gramps.gen.user import User
|
from gramps.gen.user import User
|
||||||
from gramps.gen.utils.config import config
|
from gramps.gen.utils.config import config
|
||||||
@ -57,7 +58,8 @@ def do_it(srcfile, tstfile, dfilter=None):
|
|||||||
"""
|
"""
|
||||||
tst_file = os.path.join(TEST_DIR, srcfile)
|
tst_file = os.path.join(TEST_DIR, srcfile)
|
||||||
expect_file = os.path.join(TEST_DIR, tstfile)
|
expect_file = os.path.join(TEST_DIR, tstfile)
|
||||||
result_file = os.path.join(TEMP_DIR, tstfile)
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||||
|
result_file = os.path.join(tmpdirname, tstfile)
|
||||||
err = call("-C", TREE_NAME, "-q",
|
err = call("-C", TREE_NAME, "-q",
|
||||||
"--import", tst_file,
|
"--import", tst_file,
|
||||||
"--export", result_file)[1]
|
"--export", result_file)[1]
|
||||||
|
@ -37,7 +37,7 @@ from gramps.gen.merge.diff import diff_dbs, to_struct
|
|||||||
from gramps.gen.simple import SimpleAccess
|
from gramps.gen.simple import SimpleAccess
|
||||||
from gramps.gen.utils.id import set_det_id
|
from gramps.gen.utils.id import set_det_id
|
||||||
from gramps.gen.user import User
|
from gramps.gen.user import User
|
||||||
from gramps.gen.const import TEMP_DIR, DATA_DIR
|
from gramps.gen.const import DATA_DIR
|
||||||
from gramps.test.test_util import capture
|
from gramps.test.test_util import capture
|
||||||
from gramps.plugins.export.exportxml import XmlWriter
|
from gramps.plugins.export.exportxml import XmlWriter
|
||||||
|
|
||||||
@ -212,8 +212,6 @@ def make_tst_function(tstfile, file_name):
|
|||||||
mockdtime.side_effect = mock_time
|
mockdtime.side_effect = mock_time
|
||||||
fn1 = os.path.join(TEST_DIR, tstfile)
|
fn1 = os.path.join(TEST_DIR, tstfile)
|
||||||
fn2 = os.path.join(TEST_DIR, (file_name + ".gramps"))
|
fn2 = os.path.join(TEST_DIR, (file_name + ".gramps"))
|
||||||
fres = os.path.join(TEMP_DIR, (file_name + ".difs"))
|
|
||||||
fout = os.path.join(TEMP_DIR, (file_name + ".gramps"))
|
|
||||||
if "_dfs" in tstfile:
|
if "_dfs" in tstfile:
|
||||||
config.set('preferences.default-source', True)
|
config.set('preferences.default-source', True)
|
||||||
config.set('preferences.tag-on-import-format', "Imported")
|
config.set('preferences.tag-on-import-format', "Imported")
|
||||||
@ -223,11 +221,6 @@ def make_tst_function(tstfile, file_name):
|
|||||||
skp_imp_adds = True
|
skp_imp_adds = True
|
||||||
config.set('preferences.default-source', False)
|
config.set('preferences.default-source', False)
|
||||||
config.set('preferences.tag-on-import', False)
|
config.set('preferences.tag-on-import', False)
|
||||||
try:
|
|
||||||
os.remove(fres)
|
|
||||||
os.remove(fout)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
#logger.info("\n**** %s ****", tstfile)
|
#logger.info("\n**** %s ****", tstfile)
|
||||||
set_det_id(True)
|
set_det_id(True)
|
||||||
with capture(None) as output:
|
with capture(None) as output:
|
||||||
@ -251,6 +244,9 @@ def make_tst_function(tstfile, file_name):
|
|||||||
# Also we save the .gramps file from the import so user can perform
|
# Also we save the .gramps file from the import so user can perform
|
||||||
# Diff himself for better context.
|
# Diff himself for better context.
|
||||||
if deltas:
|
if deltas:
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||||
|
fres = os.path.join(tmpdirname, (file_name + ".difs"))
|
||||||
|
fout = os.path.join(tmpdirname, (file_name + ".gramps"))
|
||||||
writer = XmlWriter(self.database1, self.user,
|
writer = XmlWriter(self.database1, self.user,
|
||||||
strip_photos=0, compress=0)
|
strip_photos=0, compress=0)
|
||||||
writer.write(fout)
|
writer.write(fout)
|
||||||
|
Loading…
Reference in New Issue
Block a user