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,
                                      md5_constructor(source).hexdigest())
                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,
                                      md5_constructor(dest).hexdigest())
            else:
                source = force_unicode(self.source)
                dest = self.dest

            if hasattr(default_storage, 'getmtime') and not self.cache_dir:
                do_generate = default_storage.getmtime(source) > \
                        default_storage.getmtime(dest)
            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 OSError:
                    do_generate = True

            if do_generate:
                if self.cache_dir is not None:
                    for filename in (source, dest):
                        path = os.path.split(filename)[0]
                        if not os.path.exists(path):
                            os.makedirs(path)
                        open(filename, 'w').close()
                try:
                    self._do_generate()
                except:
                    if self.cache_dir is not None:
                        for filename in (source, dest):
                            if os.path.exists(filename):
                                os.remove(filename)
                    raise
    def run_test(self, filename, content='Lorem ipsum dolar sit amet'):
        content = UnicodeContentFile(content)
        filename = default_storage.save(filename, content)
        self.assert_(default_storage.exists(filename))

        self.assertEqual(default_storage.size(filename), content.size)
        now = datetime.utcnow()
        delta = timedelta(minutes=5)
        mtime = default_storage.getmtime(filename)
        self.assert_(mtime > mktime((now - delta).timetuple()))
        self.assert_(mtime < mktime((now + delta).timetuple()))
        file = default_storage.open(filename)
        self.assertEqual(file.size, content.size)
        fileurl = force_unicode(file).replace('\\', '/')
        fileurl = urlquote_plus(fileurl, '/')
        if fileurl.startswith('/'):
            fileurl = fileurl[1:]
        self.assertEqual(
            MEDIA_URL+fileurl,
            default_storage.url(filename)
        )
        file.close()

        default_storage.delete(filename)
        self.assert_(not default_storage.exists(filename))
def upload_to_s3(item, depth=0, force=False):
	upload_this = False
	download_this = False
	item_name = item.realpath().split(settings.MEDIA_ROOT)[1]
	item_path = os.path.basename(item.realpath())	
	if item_path[0] == '.':
		return
	if default_storage.exists(item_name):
		local_time = os.path.getmtime(item.realpath())
		server_time = default_storage.getmtime(item_name)
		if server_time < local_time:
			print '%s is newer locally' % item_name
			upload_this = True
	else:
		upload_this = True
	if force:
		upload_this = True
	if upload_this:
		filedata = open(item.realpath(), 'rb').read()
		content_type = mimetypes.guess_type(item.realpath())[0]
		print "Uploading %s as %s" % (item_name, content_type)		
		if not content_type:
			content_type = 'text/plain'
		io = StringIO()
		io.write(open(item.realpath(), 'rb').read())
		tenyrs = date.today() + timedelta(days=365*10)
		if content_type in GZIP_FILE_TYPES:
			print '... gzipping file'
			zbuffer = StringIO()
			zfile = gzip.GzipFile(mode='wb', compresslevel=6, fileobj=zbuffer)
			zfile.write(io.getvalue())
			zfile.close()
			file_object = ContentFile(zbuffer.getvalue())
			default_storage._save(item_name, file_object, {'Content-Encoding':'gzip',})
		else:
			file_object = ContentFile(io.getvalue())
			default_storage._save(item_name, file_object)
    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_image, FieldFile) or \
                   isinstance(self.source_image, File):
                    source_image = force_unicode(self.source_image)
                elif not isinstance(self.source_image, basestring):
                    source_image = pickle.dumps(self.source_image.read())
                    self.source_image.seek(0)
                else:
                    source_image = smart_str(force_unicode(self.source_image))

                source_image = os.path.join(self.cache_dir,
                                      md5_constructor(source_image).hexdigest())
                if not os.path.exists(source_image):
                    path = os.path.split(source_image)[0]
                    if not os.path.exists(path):
                        os.makedirs(path)
                    open(source_image, '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,
                                      md5_constructor(dest).hexdigest())
            else:
                source_image = force_unicode(self.source_image)
                dest = self.dest

            # If the destination file does not exist then generate it
            if not os.path.exists(dest):
                do_generate = True
            else:
                # otherwise do this hodge podge of time comparisons
                if hasattr(default_storage, 'modified_time') and not self.cache_dir:

                    do_generate = default_storage.modified_time(source_image) > \
                            default_storage.modified_time(dest)

                elif hasattr(default_storage, 'getmtime') and not self.cache_dir:
                    # An old custom method from before Django supported
                    # modified_time(). Kept around for backwards compatibility.
                    do_generate = default_storage.getmtime(source_image) > \
                            default_storage.getmtime(dest)
                else:
                    if not self.cache_dir:
                        source_image_cache = os.path.join(settings.MEDIA_ROOT, source_image)
                        dest_cache = os.path.join(settings.MEDIA_ROOT, dest)
                    else:
                        source_image_cache, dest_cache = source_image, dest
                    try:
                        do_generate = os.path.getmtime(source_image_cache) > \
                                os.path.getmtime(dest_cache)
                    except OSError:
                        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