diff --git a/.gitignore b/.gitignore index 5672901..0f86e9d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ dist /.idea _build +env +env/ diff --git a/example/example/app.py b/example/example/app.py index 8abcfeb..d0f31ef 100644 --- a/example/example/app.py +++ b/example/example/app.py @@ -3,7 +3,7 @@ from flask_s3 import FlaskS3, create_all app = Flask(__name__) -app.config['S3_BUCKET_NAME'] = 'mybucketname' +app.config['FLASKS3_BUCKET_NAME'] = 'mybucketname' app.config['USE_S3_DEBUG'] = True s3 = FlaskS3(app) @@ -13,8 +13,9 @@ def index(): template_str = """{{ url_for('static', filename="foo.js") }}""" return render_template_string(template_str) +@app.route('/upload') def upload_all(): create_all(app, user='MY_AWS_ID', password='MY_AWS_SECRET') if __name__ == '__main__': - app.run(debug=True) \ No newline at end of file + app.run(debug=True) diff --git a/flask_s3.py b/flask_s3.py index d0825f8..d67589c 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -21,6 +21,7 @@ from flask import url_for as flask_url_for import six + logger = logging.getLogger('flask_s3') # Mapping for Header names to S3 parameters @@ -173,8 +174,11 @@ def _bp_static_url(blueprint): return u -def _gather_files(app, hidden, filepath_filter_regex=None): +def _gather_files(app, hidden, filepath_filter_regex=None, repo_path=None): """ Gets all files in static folders and returns in dict.""" + + changed_files = _get_last_commited_files(repo_path) + dirs = [(six.u(app.static_folder), app.static_url_path)] if hasattr(app, 'blueprints'): blueprints = app.blueprints.values() @@ -200,11 +204,19 @@ def _gather_files(app, hidden, filepath_filter_regex=None): # negative match. (filepath_filter_regex == None or re.search( filepath_filter_regex, - os.path.join(relative_folder, x))))] + os.path.join(relative_folder, x))) and + (changed_files is None or os.path.join('static',relative_folder, x) in changed_files) + )] if files: valid_files[(static_folder, static_url_loc)].extend(files) return valid_files +def _get_last_commited_files(repo_path): + from git import Repo + repo = Repo(repo_path) + last_commit = repo.iter_commits().next() + last_commit_parent = last_commit.parents[0] + return [x.b_path for x in last_commit.diff(last_commit_parent)] def _path_to_relative_url(path): """ Converts a folder and filename into a ralative url path """ @@ -338,6 +350,22 @@ def get_setting(name, app=None): def create_all(app, user=None, password=None, bucket_name=None, location=None, include_hidden=False, filepath_filter_regex=None, put_bucket_acl=True): + _process(app, user=None, password=None, bucket_name=None, + location=None, include_hidden=False, + filepath_filter_regex=None, put_bucket_acl=True) + + +def update(app, user=None, password=None, bucket_name=None, + location=None, include_hidden=False, + filepath_filter_regex=None, put_bucket_acl=True, repo_path=None): + _process(app, user=None, password=None, bucket_name=None, + location=None, include_hidden=False, + filepath_filter_regex=None, put_bucket_acl=True, repo_path=repo_path) + + +def _process(app, user=None, password=None, bucket_name=None, + location=None, include_hidden=False, + filepath_filter_regex=None, put_bucket_acl=True, repo_path=None): """ Uploads of the static assets associated with a Flask application to Amazon S3. @@ -412,7 +440,7 @@ def create_all(app, user=None, password=None, bucket_name=None, # build list of static files all_files = _gather_files(app, include_hidden, - filepath_filter_regex=filepath_filter_regex) + filepath_filter_regex=filepath_filter_regex, repo_path=repo_path) logger.debug("All valid files: %s" % all_files) # connect to s3 @@ -455,6 +483,7 @@ def create_all(app, user=None, password=None, bucket_name=None, else: _upload_files(s3, app, all_files, bucket_name) + class FlaskS3(object): """ The FlaskS3 object allows your application to use Flask-S3. diff --git a/setup.py b/setup.py index f84076a..5f3b736 100644 --- a/setup.py +++ b/setup.py @@ -42,7 +42,8 @@ def parse_version(asignee): install_requires=[ 'Flask', 'Boto3>=1.1.1', - 'six' + 'six', + 'gitpython' ], tests_require=['nose', 'mock'], classifiers=[