def generate(self):
        if hasattr(self.dest, 'write'):
            self._do_generate()
        else:
            do_generate = False
            if self.cache_dir is not None:
                if isinstance(self.source, FieldFile) or \
                   isinstance(self.source, File):
                    source = force_unicode(self.source)
                elif not isinstance(self.source, basestring):
                    source = pickle.dumps(self.source.read())
                    self.source.seek(0)
                else:
                    source = smart_str(force_unicode(self.source))
                source = os.path.join(self.cache_dir,
                                      hashlib.md5(source).hexdigest())
                if not os.path.exists(source):
                    path = os.path.split(source)[0]
                    if not os.path.exists(path):
                        os.makedirs(path)
                    open(source, 'w').close()
                if not isinstance(self.dest, basestring):
                    dest = pickle.dumps(self.dest.read())
                    self.dest.seek(0)
                else:
                    dest = smart_str(force_unicode(self.dest))
                dest = os.path.join(self.cache_dir,
                                      hashlib.md5(dest).hexdigest())
            else:
                source = force_unicode(self.source)
                dest = self.dest

            if hasattr(default_storage, 'modified_time') and not self.cache_dir:
                try:
                    source_mod_time = default_storage.modified_time(source)
                except EnvironmentError:
                    # Means the source file doesn't exist, so nothing can be
                    # done.
                    do_generate = False
                else:
                    try:
                        dest_mod_time = default_storage.modified_time(dest)
                    except EnvironmentError:
                        # Means the destination file doesn't exist so it must be
                        # generated.
                        do_generate = True
                    else:
                        do_generate = source_mod_time > dest_mod_time
            else:
                if not self.cache_dir:
                    source_cache = os.path.join(settings.MEDIA_ROOT, source)
                    dest_cache = os.path.join(settings.MEDIA_ROOT, dest)
                else:
                    source_cache, dest_cache = source, dest
                try:
                    do_generate = os.path.getmtime(source_cache) > \
                            os.path.getmtime(dest_cache)
                except __HOLE__:
                    do_generate = True

            if do_generate:
                if self.cache_dir is not None:
                    path = os.path.split(dest)[0]
                    if not os.path.exists(path):
                        os.makedirs(path)
                    open(dest, 'w').close()
                try:
                    self._do_generate()
                except:
                    if self.cache_dir is not None:
                        if os.path.exists(dest):
                            os.remove(dest)
                    raise
dataset/ETHPy150Open kylemacfarlane/django-cb-thumbnail/src/cuddlybuddly/thumbnail/main.py/Thumbnail.generate