Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Deploy a ZIP archive to Netlify
#!/usr/bin/python
# -*- coding:utf-8 -*-
import zipfile, urllib2, argparse, os
try:
from BytesIO import BytesIO
except ImportError:
from io import BytesIO
def deploy(archive):
data = open(archive, 'rb')
req = urllib2.Request('https://api.netlify.com/api/v1/sites/{SITE_ID}.netlify.com/deploys', data.read())
req.get_method = lambda: 'POST'
req.add_header('Content-Length', len(data.getvalue()))
req.add_header('Content-Type', 'application/zip')
req.add_header('Authorization', 'Bearer {YOUR_API_KEY}')
res = urllib2.urlopen(req)
if res.getcode() == 200:
print("Deploy successful")
exit(0)
print("An error occured!")
print(res.read())
exit(1)
def is_valid_file(parser, arg):
if not os.path.exists(arg):
parser.error("The file %s does not exist!" % arg)
else:
return arg
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-f", dest="file", required=True, help="Path to the ZIP archive", metavar="FILE", type=lambda x: is_valid_file(parser, x))
args = parser.parse_args()
deploy(args.file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment