11# -*- coding: utf-8 -*-
22from __future__ import print_function
3+
34import json
45import logging
56import os
67import sys
78import time
89from imp import load_source
9- from shutil import copy , copyfile
10+ from shutil import copy
11+ from shutil import copyfile
1012from tempfile import mkdtemp
1113
12- import botocore
1314import boto3
15+ import botocore
1416import pip
1517import yaml
1618
17- from .helpers import mkdir , read , archive , timestamp
19+ from .helpers import archive
20+ from .helpers import mkdir
21+ from .helpers import read
22+ from .helpers import timestamp
1823
1924
2025log = logging .getLogger (__name__ )
@@ -44,22 +49,22 @@ def cleanup_old_versions(src, keep_last_versions):
4449 cfg .get ('region' ))
4550
4651 response = client .list_versions_by_function (
47- FunctionName = cfg .get (" function_name" )
52+ FunctionName = cfg .get (' function_name' )
4853 )
49- versions = response .get (" Versions" )
50- if len (response .get (" Versions" )) < keep_last_versions :
51- print (" Nothing to delete. (Too few versions published)" )
54+ versions = response .get (' Versions' )
55+ if len (response .get (' Versions' )) < keep_last_versions :
56+ print (' Nothing to delete. (Too few versions published)' )
5257 else :
53- version_numbers = [elem .get (" Version" ) for elem in
58+ version_numbers = [elem .get (' Version' ) for elem in
5459 versions [1 :- keep_last_versions ]]
5560 for version_number in version_numbers :
5661 try :
5762 client .delete_function (
58- FunctionName = cfg .get (" function_name" ),
63+ FunctionName = cfg .get (' function_name' ),
5964 Qualifier = version_number
6065 )
6166 except botocore .exceptions .ClientError as e :
62- print (" Skipping Version {}: {}"
67+ print (' Skipping Version {}: {}'
6368 .format (version_number , e .message ))
6469
6570
@@ -111,7 +116,7 @@ def invoke(src, alt_event=None, verbose=False):
111116 path_to_event_file = os .path .join (src , 'event.json' )
112117 event = read (path_to_event_file , loader = json .loads )
113118
114- #Tweak to allow module to import local modules
119+ # Tweak to allow module to import local modules
115120 try :
116121 sys .path .index (src )
117122 except :
@@ -129,10 +134,10 @@ def invoke(src, alt_event=None, verbose=False):
129134 results = fn (event , None )
130135 end = time .time ()
131136
132- print (" {0}" .format (results ))
137+ print (' {0}' .format (results ))
133138 if verbose :
134- print (" \n execution time: {:.8f}s\n function execution "
135- " timeout: {:2}s" .format (end - start , cfg .get ('timeout' , 15 )))
139+ print (' \n execution time: {:.8f}s\n function execution '
140+ ' timeout: {:2}s' .format (end - start , cfg .get ('timeout' , 15 )))
136141
137142
138143def init (src , minimal = False ):
@@ -145,7 +150,7 @@ def init(src, minimal=False):
145150 """
146151
147152 templates_path = os .path .join (
148- os .path .dirname (os .path .abspath (__file__ )), " project_templates" )
153+ os .path .dirname (os .path .abspath (__file__ )), ' project_templates' )
149154 for filename in os .listdir (templates_path ):
150155 if (minimal and filename == 'event.json' ) or filename .endswith ('.pyc' ):
151156 continue
@@ -178,18 +183,19 @@ def build(src, requirements=False, local_package=None):
178183 # Combine the name of the Lambda function with the current timestamp to use
179184 # for the output filename.
180185 function_name = cfg .get ('function_name' )
181- output_filename = " {0}-{1}.zip" .format (timestamp (), function_name )
186+ output_filename = ' {0}-{1}.zip' .format (timestamp (), function_name )
182187
183188 path_to_temp = mkdtemp (prefix = 'aws-lambda' )
184189 pip_install_to_target (path_to_temp ,
185190 requirements = requirements ,
186191 local_package = local_package )
187192
188193 # Hack for Zope.
189- if "zope" in os .listdir (path_to_temp ):
190- print ("Zope packages detected; fixing Zope package paths to make them importable." )
194+ if 'zope' in os .listdir (path_to_temp ):
195+ print ('Zope packages detected; fixing Zope package paths to '
196+ 'make them importable.' )
191197 # Touch.
192- with open (os .path .join (path_to_temp , " zope/__init__.py" ), "wb" ):
198+ with open (os .path .join (path_to_temp , ' zope/__init__.py' ), 'wb' ):
193199 pass
194200
195201 # Gracefully handle whether ".zip" was included in the filename or not.
@@ -204,7 +210,7 @@ def build(src, requirements=False, local_package=None):
204210 continue
205211 if filename == 'config.yaml' :
206212 continue
207- print (" Bundling: %r" % filename )
213+ print (' Bundling: %r' % filename )
208214 files .append (os .path .join (src , filename ))
209215
210216 # "cd" into `temp_path` directory.
@@ -264,7 +270,7 @@ def _install_packages(path, packages):
264270 A list of packages to be installed via pip.
265271 """
266272 def _filter_blacklist (package ):
267- blacklist = ["-i" , "#" , " Python==" , " python-lambda==" ]
273+ blacklist = ['-i' , '#' , ' Python==' , ' python-lambda==' ]
268274 return all (package .startswith (entry ) is False for entry in blacklist )
269275 filtered_packages = filter (_filter_blacklist , packages )
270276 for package in filtered_packages :
@@ -296,16 +302,16 @@ def pip_install_to_target(path, requirements=False, local_package=None):
296302 print ('Gathering pip packages' )
297303 packages .extend (pip .operations .freeze .freeze ())
298304 else :
299- if os .path .exists (" requirements.txt" ):
305+ if os .path .exists (' requirements.txt' ):
300306 print ('Gathering requirement packages' )
301- data = read (" requirements.txt" )
307+ data = read (' requirements.txt' )
302308 packages .extend (data .splitlines ())
303309
304310 if not packages :
305311 print ('No dependency packages installed!' )
306312
307313 if local_package is not None :
308- if not isinstance (local_package , (list , tuple ) ):
314+ if not isinstance (local_package , (list , tuple )):
309315 local_package = [local_package ]
310316 for l_package in local_package :
311317 packages .append (l_package )
@@ -314,7 +320,7 @@ def pip_install_to_target(path, requirements=False, local_package=None):
314320
315321def get_role_name (account_id , role ):
316322 """Shortcut to insert the `account_id` and `role` into the iam string."""
317- return " arn:aws:iam::{0}:role/{1}" .format (account_id , role )
323+ return ' arn:aws:iam::{0}:role/{1}' .format (account_id , role )
318324
319325
320326def get_account_id (aws_access_key_id , aws_secret_access_key ):
@@ -337,7 +343,7 @@ def get_client(client, aws_access_key_id, aws_secret_access_key, region=None):
337343def create_function (cfg , path_to_zip_file ):
338344 """Register and upload a function to AWS Lambda."""
339345
340- print (" Creating your new Lambda function" )
346+ print (' Creating your new Lambda function' )
341347 byte_stream = read (path_to_zip_file )
342348 aws_access_key_id = cfg .get ('aws_access_key_id' )
343349 aws_secret_access_key = cfg .get ('aws_secret_access_key' )
@@ -375,7 +381,7 @@ def create_function(cfg, path_to_zip_file):
375381def update_function (cfg , path_to_zip_file ):
376382 """Updates the code of an existing Lambda function"""
377383
378- print (" Updating your Lambda function" )
384+ print (' Updating your Lambda function' )
379385 byte_stream = read (path_to_zip_file )
380386 aws_access_key_id = cfg .get ('aws_access_key_id' )
381387 aws_secret_access_key = cfg .get ('aws_secret_access_key' )
0 commit comments