| #-*- coding: utf-8 -*- |
| |
| import os |
| import shutil |
| import tempfile |
| |
| import utils |
| |
| |
| class TempFiles(object): |
| def __init__(self): |
| self.logger = utils.logger.getChild('TempFiles') |
| self._temp_dirs = list() |
| self.empty_dir |
| |
| def __enter__(self): |
| return self |
| |
| def __exit__(self, type, value, traceback): |
| self.__del__() |
| |
| def __del__(self): |
| for d in self._temp_dirs: |
| if os.path.isdir(d): |
| shutil.rmtree(d) |
| self.logger.debug('Removed temporary directory "{}"'.format(d)) |
| |
| @property |
| def empty_dir(self): |
| if self.__dict__.get('_empty_dir') is None: |
| self._empty_dir = self.get_temp_dir() |
| return self._empty_dir |
| |
| def get_temp_dir(self, subdirs=None): |
| temp_dir = tempfile.mkdtemp() |
| self._temp_dirs.append(temp_dir) |
| msg = 'Created temporary directory "{}"'.format(temp_dir) |
| if subdirs is not None: |
| self.create_subdirs(subdirs, temp_dir) |
| msg += ' including subdirs "{}"'.format(subdirs) |
| self.logger.debug(msg) |
| return temp_dir |
| |
| @property |
| def last_temp_dir(self): |
| return self._temp_dirs[-1] |
| |
| def create_subdirs(self, subdirs, temp_dir): |
| if not os.path.isdir(temp_dir): |
| temp_dir = self.get_temp_dir() |
| if subdirs is not None: |
| if type(subdirs) == str: |
| _ = subdirs |
| subdirs = list() |
| subdirs.append(_) |
| if type(subdirs) in (list, tuple): |
| for s in subdirs: |
| os.makedirs(os.path.join(temp_dir, |
| s.strip(os.path.sep + '~'))) |
| else: |
| raise Exception('subdirs should be tuple or list of strings, ' |
| 'but currentry subdirs == {}'.format(subdirs)) |
| return temp_dir |
| |
| def get_symlink_to(self, target, temp_dir=None): |
| if temp_dir is None: |
| temp_dir = self.get_temp_dir() |
| linkname = tempfile.mktemp(dir=temp_dir) |
| os.symlink(target.strip(os.path.sep), linkname) |
| self.logger.debug('Creates temporary symlink "{} -> {}"' |
| ''.format(linkname, target)) |
| return linkname |
| |
| def get_file(self, content='', temp_dir=None): |
| if temp_dir is None: |
| temp_dir = self.get_temp_dir() |
| filename = tempfile.mktemp(dir=temp_dir) |
| with open(filename, 'w') as outfile: |
| outfile.write(content) |
| self.logger.debug('Creates temporary file "{}"'.format(filename)) |
| return filename |