def test__gather_files(self, path_mock, os_mock): """ Tests the _gather_files function """ self.app.static_folder = '/home' self.app.static_url_path = '/static' bp_a = Mock(static_folder='/home/bar', static_url_path='/a/bar', url_prefix=None) bp_b = Mock(static_folder='/home/zoo', static_url_path='/b/bar', url_prefix=None) bp_c = Mock(static_folder=None) self.app.blueprints = {'a': bp_a, 'b': bp_b, 'c': bp_c} dirs = {'/home': [('/home', None, ['.a'])], '/home/bar': [('/home/bar', None, ['b'])], '/home/zoo': [('/home/zoo', None, ['c']), ('/home/zoo/foo', None, ['d', 'e'])]} os_mock.side_effect = dirs.get path_mock.return_value = True expected = {('/home/bar', six.u('/a/bar')): ['/home/bar/b'], ('/home/zoo', six.u('/b/bar')): ['/home/zoo/c', '/home/zoo/foo/d', '/home/zoo/foo/e']} actual = flask_s3._gather_files(self.app, False) self.assertEqual(expected, actual) expected[('/home', six.u('/static'))] = ['/home/.a'] actual = flask_s3._gather_files(self.app, True) self.assertEqual(expected, actual)
def test__gather_files(self, path_mock, os_mock): """ Tests the _gather_files function """ self.app.static_folder = '/home' self.app.static_url_path = '/static' bp_a = Mock(static_folder='/home/bar', static_url_path='/a/bar', url_prefix=None) bp_b = Mock(static_folder='/home/zoo', static_url_path='/b/bar', url_prefix=None) bp_c = Mock(static_folder=None) self.app.blueprints = {'a': bp_a, 'b': bp_b, 'c': bp_c} dirs = { '/home': [('/home', None, ['.a'])], '/home/bar': [('/home/bar', None, ['b'])], '/home/zoo': [('/home/zoo', None, ['c']), ('/home/zoo/foo', None, ['d', 'e'])] } os_mock.side_effect = dirs.get path_mock.return_value = True expected = { ('/home/bar', u'/a/bar'): ['/home/bar/b'], ('/home/zoo', u'/b/bar'): ['/home/zoo/c', '/home/zoo/foo/d', '/home/zoo/foo/e'] } actual = flask_s3._gather_files(self.app, False) self.assertEqual(expected, actual) expected[('/home', u'/static')] = ['/home/.a'] actual = flask_s3._gather_files(self.app, True) self.assertEqual(expected, actual)
def test__gather_files(self, path_mock, os_mock): """ Tests the _gather_files function """ self.app.static_folder = "/home" self.app.static_url_path = "/static" bp_a = Mock(static_folder="/home/bar", static_url_path="/a/bar", url_prefix=None) bp_b = Mock(static_folder="/home/zoo", static_url_path="/b/bar", url_prefix=None) bp_c = Mock(static_folder=None) self.app.blueprints = {"a": bp_a, "b": bp_b, "c": bp_c} dirs = { "/home": [("/home", None, [".a"])], "/home/bar": [("/home/bar", None, ["b"])], "/home/zoo": [("/home/zoo", None, ["c"]), ("/home/zoo/foo", None, ["d", "e"])], } os_mock.side_effect = dirs.get path_mock.return_value = True expected = { ("/home/bar", u"/a/bar"): ["/home/bar/b"], ("/home/zoo", u"/b/bar"): ["/home/zoo/c", "/home/zoo/foo/d", "/home/zoo/foo/e"], } actual = flask_s3._gather_files(self.app, False) self.assertEqual(expected, actual) expected[("/home", u"/static")] = ["/home/.a"] actual = flask_s3._gather_files(self.app, True) self.assertEqual(expected, actual)
def test__gather_files_bad_folder(self, path_mock, os_mock): """ Tests that _gather_files when static folder is not valid folder """ self.app.static_folder = '/bad' dirs = {'/bad': []} os_mock.side_effect = dirs.get path_mock.return_value = False actual = flask_s3._gather_files(self.app, False) self.assertEqual({}, actual)
def test__gather_files_no_blueprints_no_files(self, path_mock, os_mock): """ Tests that _gather_files works when there are no blueprints and no files available in the static folder """ self.app.static_folder = '/foo' dirs = {'/foo': [('/foo', None, [])]} os_mock.side_effect = dirs.get path_mock.return_value = True actual = flask_s3._gather_files(self.app, False) self.assertEqual({}, actual)
#!/usr/bin/env python # Copyright 2013 Globo.com. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. import os import re import flask_s3 from boto.s3.connection import S3Connection import app location = None endpoint = os.environ.get("TSURU_S3_ENDPOINT") if endpoint: location_regexp = re.compile("^https://([a-z0-9-]+)\.amazonaws\.com$") m = location_regexp.match(endpoint) if m: location = m.groups()[0] all_files = flask_s3._gather_files(app.app, False) conn = S3Connection(os.environ.get("TSURU_S3_ACCESS_KEY_ID"), os.environ.get("TSURU_S3_SECRET_KEY")) bucket = conn.get_bucket(os.environ.get("TSURU_S3_BUCKET")) print "Uploading static files to S3... ", flask_s3._upload_files(app.app, all_files, bucket) print "ok"