Package omero :: Package util :: Module temp_files :: Class TempFileManager
[hide private]
[frames] | no frames]

Class TempFileManager

source code


Creates temporary files and folders and makes a best effort to remove them on exit (or sooner). Typically only a single instance of this class will exist ("manager" variable in this module below)

Instance Methods [hide private]
 
__init__(self, prefix="omero")
Initializes a TempFileManager instance with a userDir containing the given prefix value, or "omero" by default.
source code
 
cleanup(self)
Releases self.lock and deletes self.dir.
source code
 
tmpdir(self)
Returns a platform-specific user-writable temporary directory
source code
 
username(self)
Returns the current OS-user's name
source code
 
pid(self)
Returns some representation of the current process's id
source code
 
access(self, dir)
Returns True if the current user can write to the given directory
source code
 
create(self, dir)
If the given directory doesn't exist, creates it (with mode 0700) and returns True.
source code
 
gettempdir(self)
Returns the directory under which all temporary files and folders will be created.
source code
 
create_path(self, prefix, suffix, folder=False, text=False, mode="r+")
Uses tempfile.mkdtemp and tempfile.mkstemp to create temporary folders and files, respectively, under self.dir
source code
 
remove_path(self, name)
If the given path is under self.dir, then it is deleted whether file or folder.
source code
 
clean_tempdir(self)
Deletes self.dir
source code
 
clean_userdir(self)
Attempts to delete all directories under self.userdir other than the one owned by this process.
source code
 
on_rmtree(self, func, name, exc) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Instance Variables [hide private]
  userdir
User-accessible directory of the form $TMPDIR/omero_$USERNAME.
  dir
Directory under which all temporary files and folders will be created.
  lock
.lock file under self.dir which is used to prevent other TempFileManager instances (also in other languages) from cleaning up this directory.
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, prefix="omero")
(Constructor)

source code 

Initializes a TempFileManager instance with a userDir containing the given prefix value, or "omero" by default. Also registers an atexit callback to call self.cleanup() on exit.

Overrides: object.__init__

cleanup(self)

source code 

Releases self.lock and deletes self.dir. The lock is released first since on some platforms like Windows the lock file cannot be deleted even by the owner of the lock.

tmpdir(self)

source code 

Returns a platform-specific user-writable temporary directory

First, the value of "OMERO_TEMPDIR" is attempted (if available),
then user's home directory, then the global temp director.

Typical errors for any of the possible temp locations are:
 * non-existence
 * inability to lock

See: https://trac.openmicroscopy.org.uk/omero/ticket/1653

create(self, dir)

source code 

If the given directory doesn't exist, creates it (with mode 0700) and returns True. Otherwise False.

remove_path(self, name)

source code 

If the given path is under self.dir, then it is deleted whether file or folder. Otherwise, an exception is thrown.

clean_userdir(self)

source code 

Attempts to delete all directories under self.userdir other than the one owned by this process. If a directory is locked, it is skipped.


Instance Variable Details [hide private]

userdir

User-accessible directory of the form $TMPDIR/omero_$USERNAME. If the given directory is not writable, an attempt is made to use an alternative

dir

Directory under which all temporary files and folders will be created. An attempt to remove a path not in this directory will lead to an exception.