Skip to content

Instantly share code, notes, and snippets.

@liamstask
Created February 19, 2014 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liamstask/9097615 to your computer and use it in GitHub Desktop.
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.
#!/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