Created
February 19, 2014 17:59
-
-
Save liamstask/9097615 to your computer and use it in GitHub Desktop.
a script for deploying a middleman site to s3. any gzip'd files have their extension removed so that s3 can serve them directly without another server required to perform content negotiation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os, shutil, subprocess | |
SITE = 'build/' | |
BUCKET = 's3://mybucket.com/' | |
CMD = ['s3cmd', '-c', os.path.expanduser('~/.s3cfg-myconfig'), 'sync', '--acl-public', '--reduced-redundancy'] | |
CACHE_10_WEEKS = "Cache-Control: max-age=6048000" | |
# ASSETS are served directly, while | |
# COMPRESSABLE_ASSETS are served gzipped - need to specify their mime type | |
ASSETS = ['*.png', '*.jpg', '*.ico'] | |
COMPRESSABLE_ASSETS = [ | |
('*.css', 'text/css', CACHE_10_WEEKS), | |
('*.js', 'text/javascript', CACHE_10_WEEKS), | |
('*.html', 'text/html', "Cache-Control: max-age=86400, must-revalidate"), | |
] | |
def sync(params): | |
cmd = CMD + params + [SITE, BUCKET] | |
subprocess.check_call(cmd) | |
def replacegzip(p): | |
print "replacing uncompressed files with gzip'd versions..." | |
for root, dirs, files in os.walk(p): | |
for file in files: | |
name, ext = os.path.splitext(file) | |
if ext == '.gz': | |
shutil.move(os.path.join(root, file), os.path.join(root, name)) | |
def main(): | |
print "Deploying:", SITE, "to", BUCKET | |
# copy any gzip'd files to overwrite their original version | |
replacegzip(SITE) | |
for a in COMPRESSABLE_ASSETS: | |
print "sycing", a[0], "..." | |
cmd = ['--exclude', '*.*', '--include', a[0], '--mime-type=%s' % a[1], '--add-header=%s' % a[2], "--add-header=Content-Encoding:gzip"] | |
sync(cmd) | |
assetcmd = ['--exclude', '*.*', '--add-header=%s' % CACHE_10_WEEKS] | |
for a in ASSETS: | |
assetcmd.extend(['--include', a]) | |
print "syncing", ASSETS, "..." | |
sync(assetcmd) | |
cleancmd = ['--exclude', '.DS_Store', '--delete-removed'] | |
sync(cleancmd) | |
print "Done." | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment