* Bugfixes for tmpdir.

master
Wirawan Purwanto 10 years ago
parent c9fdeee86b
commit 86e4e8fd51
  1. 8
      file/tmpdir.py

@ -32,7 +32,7 @@ import os.path
import tempfile import tempfile
from warnings import warn from warnings import warn
from wpylib import shell_utils as sh from wpylib import shell_tools as sh
_g = globals() _g = globals()
_g.setdefault("TMPDIR", None) _g.setdefault("TMPDIR", None)
@ -46,24 +46,26 @@ def tmpdir():
and return the created temporary directory name. and return the created temporary directory name.
For subsequent calls, the same temporary directory name is given again. For subsequent calls, the same temporary directory name is given again.
""" """
from wpylib.file.file_utils import is_writable
import atexit import atexit
global TMPDIR, TMPDIR_ROOTS global TMPDIR, TMPDIR_ROOTS
if TMPDIR != None: return TMPDIR if TMPDIR != None: return TMPDIR
tmproot = None tmproot = None
# FIXME: site-specific hooks probably need to be set somewhere # FIXME: site-specific hooks probably need to be set somewhere
for d in list(TMPDIR_ROOTS) + [ os.getenv("TMP"), os.getenv("TMPDIR"), "/tmp", "/var/tmp" ]: for d in list(TMPDIR_ROOTS) + [ os.getenv("TMP"), os.getenv("TMPDIR"), "/tmp", "/var/tmp" ]:
if d != None and os.path.isdir(d) and : if d != None and os.path.isdir(d) and is_writable(d):
tmproot = d tmproot = d
break break
if not tmproot: if not tmproot:
warn("Cannot find suitable temporary directory root, using current directory.", warn("Cannot find suitable temporary directory root, using current directory.",
RuntimeWarning) RuntimeWarning)
tmproot = os.getcwd() tmproot = os.getcwd()
TMPDIR = tempfile.mkdtemp(prefix=("%s/chkgit-%d" % (tmproot, os.getpid()))) TMPDIR = tempfile.mkdtemp(prefix=("%s/pytmp-%d" % (tmproot, os.getpid())))
def tmpdir_exitfunc(): def tmpdir_exitfunc():
global TMPDIR global TMPDIR
global TMPDIR_CLEANUP global TMPDIR_CLEANUP
print "TMPDIR_CLEANUP = ", TMPDIR_CLEANUP
if TMPDIR != None and os.path.isdir(TMPDIR) and TMPDIR_CLEANUP: if TMPDIR != None and os.path.isdir(TMPDIR) and TMPDIR_CLEANUP:
try: try:
sh.rm("-rf", TMPDIR) sh.rm("-rf", TMPDIR)

Loading…
Cancel
Save