Skip to content

Instantly share code, notes, and snippets.

@jpdillingham
Last active March 18, 2018 16:05
Show Gist options
  • Save jpdillingham/ab607f15894df266860f04ef855425a8 to your computer and use it in GitHub Desktop.
Save jpdillingham/ab607f15894df266860f04ef855425a8 to your computer and use it in GitHub Desktop.
Quick Python script to zip a directory. Useful for AWS Lambda deployments from an npm script, among other things.
#!/usr/bin/env python
import sys
import os
import shutil
import tempfile
import uuid
# python zip.py 'folder to zip' 'output file'
# e.g. python zip.py ./ ./build/build.zip
if __name__ == '__main__':
srcDir = os.path.abspath(sys.argv[1])
outputFile = os.path.abspath(sys.argv[2])
tempDir = os.path.join(tempfile.gettempdir(), 'zippy-builds')
tempFile = os.path.join(tempDir, str(uuid.uuid4()))
print('creating deployable zip from ' + srcDir + '...')
try:
print ('creating zip in \'' + tempDir + '\'...')
shutil.make_archive(tempFile, 'zip', srcDir)
if os.path.exists(outputFile):
print('output file exists. deleting \'' + outputFile + '\'')
os.remove(outputFile)
print('copying \'' + tempFile + '.zip\' to \'' + outputFile + '\'...')
shutil.move(tempFile + '.zip', outputFile)
print('zip succeeded.')
except:
e = sys.exc_info()[0]
print('error: ' + e)
print('cleaning up...')
shutil.rmtree(tempDir)
print('zip failed.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment