Skip to content

Git support #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
dist
/.idea
_build
env
env/
5 changes: 3 additions & 2 deletions example/example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
app.run(debug=True)
35 changes: 32 additions & 3 deletions flask_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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 """
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def parse_version(asignee):
install_requires=[
'Flask',
'Boto3>=1.1.1',
'six'
'six',
'gitpython'
],
tests_require=['nose', 'mock'],
classifiers=[
Expand Down