From c1f690be93862256e8d231f548ed656ab7b2030f Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sun, 14 Oct 2018 16:53:18 +0200 Subject: github-lambda: support python2. return error from blogc_handler --- src/blogc-github-lambda/lambda_function.py.in | 32 ++++++++++++++++++--------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/blogc-github-lambda/lambda_function.py.in b/src/blogc-github-lambda/lambda_function.py.in index 8c55402..a6cdc6f 100644 --- a/src/blogc-github-lambda/lambda_function.py.in +++ b/src/blogc-github-lambda/lambda_function.py.in @@ -7,8 +7,13 @@ # See the license for details. # +from __future__ import print_function + from contextlib import closing -from StringIO import StringIO +try: + from io import StringIO +except ImportError: + from StringIO import StringIO import base64 import boto3 @@ -19,8 +24,11 @@ import mimetypes import os import subprocess import tarfile -import urllib2 import shutil +try: + import urllib.request as urllib2 +except ImportError: + import urllib2 BLOGC_VERSION = '@PACKAGE_VERSION@' @@ -48,7 +56,7 @@ def get_tarball(repo_name): auth = base64.b64encode(GITHUB_AUTH) request.add_header("Authorization", "Basic %s" % auth) - with closing(urllib2.urlopen(request)) as fp: + with closing(urllib2(request)) as fp: tarball = fp.read() rootdir = None @@ -148,23 +156,23 @@ def sync_s3(src, dest, settings_file): to_delete.append(filename) for data in to_upload: - print 'Uploading file: %s; content-type: "%s"' % ( + print('Uploading file: %s; content-type: "%s"' % ( data['Key'], data.get('ContentType'), - ) + )) bucket.put_object(**data) for filename in to_delete: - print 'Deleting file:', filename + print('Deleting file:', filename) remote_files[filename].delete() def blogc_handler(message): - print 'blogc-github-lambda %s' % BLOGC_VERSION + print('blogc-github-lambda %s' % BLOGC_VERSION) payload = json.loads(message) if payload['ref'] == 'refs/heads/master': - print 'Building: %s' % payload['repository']['full_name'] + print('Building: %s' % payload['repository']['full_name']) debug = 'DEBUG' in os.environ env = os.environ.copy() @@ -195,7 +203,7 @@ def blogc_handler(message): os.path.join(rootdir, 's3.json')) else: - print "Commit not for master branch, skipping: %s" % payload['ref'] + print("Commit not for master branch, skipping: %s" % payload['ref']) def api_gateway_response(code, message): @@ -229,7 +237,11 @@ def api_gateway_handler(event): if not hmac.compare_digest(digest.hexdigest(), pieces[1]): return api_gateway_response(400, 'BAD_SIGNATURE') - blogc_handler(body) + try: + blogc_handler(body) + except Exception as err: + return api_gateway_response(500, 'ERROR: %s' % err) + return api_gateway_response(201, 'ACCEPTED') -- cgit v1.2.3-18-g5258