From b39f0ad85c78ef141405462da0ed011998f69aa3 Mon Sep 17 00:00:00 2001 From: Fartash Haghani Date: Thu, 29 Jun 2017 12:14:39 -0400 Subject: [PATCH 1/6] add update method to be able to update new commited static files --- .gitignore | 2 ++ example/example/app.py | 5 +++-- flask_s3.py | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) 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..d4f5577 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -20,6 +20,7 @@ from flask import current_app from flask import url_for as flask_url_for import six +from git import Repo logger = logging.getLogger('flask_s3') @@ -173,8 +174,15 @@ 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 = None + if repo_path: + repo = Repo(repo_path) + diff = repo.git.diff('HEAD~1..HEAD', name_only=True) + changed_files = diff.split('\n') + dirs = [(six.u(app.static_folder), app.static_url_path)] if hasattr(app, 'blueprints'): blueprints = app.blueprints.values() @@ -200,7 +208,9 @@ 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(relative_folder, x) in changed_files) + )] if files: valid_files[(static_folder, static_url_loc)].extend(files) return valid_files @@ -338,6 +348,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 +438,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 +481,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. From 92a56900195f86e909527d296292d706ffc1c1b6 Mon Sep 17 00:00:00 2001 From: Fartash Haghani Date: Thu, 29 Jun 2017 12:26:56 -0400 Subject: [PATCH 2/6] add gitpython to setup.py --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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=[ From 8ebf037c2d9785f372619b942b77a18c8a5371fb Mon Sep 17 00:00:00 2001 From: Fartash Haghani Date: Thu, 29 Jun 2017 13:08:29 -0400 Subject: [PATCH 3/6] update method of getting last commited files --- flask_s3.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/flask_s3.py b/flask_s3.py index d4f5577..df82f91 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -177,11 +177,7 @@ def _bp_static_url(blueprint): def _gather_files(app, hidden, filepath_filter_regex=None, repo_path=None): """ Gets all files in static folders and returns in dict.""" - changed_files = None - if repo_path: - repo = Repo(repo_path) - diff = repo.git.diff('HEAD~1..HEAD', name_only=True) - changed_files = diff.split('\n') + changed_files = _get_last_commited_files(repo_path) dirs = [(six.u(app.static_folder), app.static_url_path)] if hasattr(app, 'blueprints'): @@ -215,6 +211,11 @@ def _gather_files(app, hidden, filepath_filter_regex=None, repo_path=None): valid_files[(static_folder, static_url_loc)].extend(files) return valid_files +def _get_last_commited_files(repo_path): + 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 """ From 32055c27d4da9f4b86e5bdb55bcc4c7dc7e17331 Mon Sep 17 00:00:00 2001 From: Fartash Haghani Date: Thu, 29 Jun 2017 13:21:35 -0400 Subject: [PATCH 4/6] modify matching condition --- flask_s3.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_s3.py b/flask_s3.py index df82f91..c208714 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -205,7 +205,7 @@ def _gather_files(app, hidden, filepath_filter_regex=None, repo_path=None): (filepath_filter_regex == None or re.search( filepath_filter_regex, os.path.join(relative_folder, x))) and - (changed_files is None or os.path.join(relative_folder, x) in changed_files) + (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) From 7fb03436db57027714cbfa916b4ac31af8d3bced Mon Sep 17 00:00:00 2001 From: Fartash Haghani Date: Thu, 29 Jun 2017 15:42:59 -0400 Subject: [PATCH 5/6] import when git is required --- flask_s3.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flask_s3.py b/flask_s3.py index c208714..1fed3d9 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -20,7 +20,7 @@ from flask import current_app from flask import url_for as flask_url_for import six -from git import Repo + logger = logging.getLogger('flask_s3') @@ -429,6 +429,7 @@ def _process(app, user=None, password=None, bucket_name=None, /latest/dev/BucketRestrictions.html """ + from git import Repo user = user or app.config.get('AWS_ACCESS_KEY_ID') password = password or app.config.get('AWS_SECRET_ACCESS_KEY') bucket_name = bucket_name or app.config.get('FLASKS3_BUCKET_NAME') From 64f146b7d2a7f1f312b9ee9f7123512efd4196c4 Mon Sep 17 00:00:00 2001 From: Fartash Haghani Date: Thu, 29 Jun 2017 15:47:17 -0400 Subject: [PATCH 6/6] import when git is required --- flask_s3.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_s3.py b/flask_s3.py index 1fed3d9..d67589c 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -212,6 +212,7 @@ def _gather_files(app, hidden, filepath_filter_regex=None, repo_path=None): 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] @@ -429,7 +430,6 @@ def _process(app, user=None, password=None, bucket_name=None, /latest/dev/BucketRestrictions.html """ - from git import Repo user = user or app.config.get('AWS_ACCESS_KEY_ID') password = password or app.config.get('AWS_SECRET_ACCESS_KEY') bucket_name = bucket_name or app.config.get('FLASKS3_BUCKET_NAME')