11# -*- coding: utf-8 -*-
22from __future__ import print_function
33
4+ import hashlib
45import json
56import logging
67import os
1516import botocore
1617import pip
1718import yaml
18- import hashlib
1919
2020from .helpers import archive
21+ from .helpers import get_environment_variable_value
2122from .helpers import mkdir
2223from .helpers import read
2324from .helpers import timestamp
24- from .helpers import get_environment_variable_value
2525
2626
2727log = logging .getLogger (__name__ )
@@ -47,11 +47,13 @@ def cleanup_old_versions(src, keep_last_versions):
4747 aws_access_key_id = cfg .get ('aws_access_key_id' )
4848 aws_secret_access_key = cfg .get ('aws_secret_access_key' )
4949
50- client = get_client ('lambda' , aws_access_key_id , aws_secret_access_key ,
51- cfg .get ('region' ))
50+ client = get_client (
51+ 'lambda' , aws_access_key_id , aws_secret_access_key ,
52+ cfg .get ('region' ),
53+ )
5254
5355 response = client .list_versions_by_function (
54- FunctionName = cfg .get ('function_name' )
56+ FunctionName = cfg .get ('function_name' ),
5557 )
5658 versions = response .get ('Versions' )
5759 if len (response .get ('Versions' )) < keep_last_versions :
@@ -63,7 +65,7 @@ def cleanup_old_versions(src, keep_last_versions):
6365 try :
6466 client .delete_function (
6567 FunctionName = cfg .get ('function_name' ),
66- Qualifier = version_number
68+ Qualifier = version_number ,
6769 )
6870 except botocore .exceptions .ClientError as e :
6971 print ('Skipping Version {}: {}'
@@ -95,6 +97,7 @@ def deploy(src, requirements=False, local_package=None):
9597 else :
9698 create_function (cfg , path_to_zip_file )
9799
100+
98101def upload (src , requirements = False , local_package = None ):
99102 """Uploads a new function to AWS S3.
100103
@@ -117,6 +120,7 @@ def upload(src, requirements=False, local_package=None):
117120
118121 upload_s3 (cfg , path_to_zip_file )
119122
123+
120124def invoke (src , alt_event = None , verbose = False ):
121125 """Simulates a call to your function.
122126
@@ -132,7 +136,8 @@ def invoke(src, alt_event=None, verbose=False):
132136 path_to_config_file = os .path .join (src , 'config.yaml' )
133137 cfg = read (path_to_config_file , loader = yaml .load )
134138
135- # Load environment variables from the config file into the actual environment.
139+ # Load environment variables from the config file into the actual
140+ # environment.
136141 for key , value in cfg .get ('environment_variables' ).items ():
137142 os .environ [key ] = value
138143
@@ -177,7 +182,8 @@ def init(src, minimal=False):
177182 """
178183
179184 templates_path = os .path .join (
180- os .path .dirname (os .path .abspath (__file__ )), 'project_templates' )
185+ os .path .dirname (os .path .abspath (__file__ )), 'project_templates' ,
186+ )
181187 for filename in os .listdir (templates_path ):
182188 if (minimal and filename == 'event.json' ) or filename .endswith ('.pyc' ):
183189 continue
@@ -213,22 +219,28 @@ def build(src, requirements=False, local_package=None):
213219 output_filename = '{0}-{1}.zip' .format (timestamp (), function_name )
214220
215221 path_to_temp = mkdtemp (prefix = 'aws-lambda' )
216- pip_install_to_target (path_to_temp ,
217- requirements = requirements ,
218- local_package = local_package )
222+ pip_install_to_target (
223+ path_to_temp ,
224+ requirements = requirements ,
225+ local_package = local_package ,
226+ )
219227
220228 # Hack for Zope.
221229 if 'zope' in os .listdir (path_to_temp ):
222- print ('Zope packages detected; fixing Zope package paths to '
223- 'make them importable.' )
230+ print (
231+ 'Zope packages detected; fixing Zope package paths to '
232+ 'make them importable.' ,
233+ )
224234 # Touch.
225235 with open (os .path .join (path_to_temp , 'zope/__init__.py' ), 'wb' ):
226236 pass
227237
228238 # Gracefully handle whether ".zip" was included in the filename or not.
229- output_filename = ('{0}.zip' .format (output_filename )
230- if not output_filename .endswith ('.zip' )
231- else output_filename )
239+ output_filename = (
240+ '{0}.zip' .format (output_filename )
241+ if not output_filename .endswith ('.zip' )
242+ else output_filename
243+ )
232244
233245 files = []
234246 for filename in os .listdir (src ):
@@ -364,7 +376,7 @@ def get_client(client, aws_access_key_id, aws_secret_access_key, region=None):
364376 client ,
365377 aws_access_key_id = aws_access_key_id ,
366378 aws_secret_access_key = aws_secret_access_key ,
367- region_name = region
379+ region_name = region ,
368380 )
369381
370382
@@ -379,8 +391,10 @@ def create_function(cfg, path_to_zip_file):
379391 account_id = get_account_id (aws_access_key_id , aws_secret_access_key )
380392 role = get_role_name (account_id , cfg .get ('role' , 'lambda_basic_execution' ))
381393
382- client = get_client ('lambda' , aws_access_key_id , aws_secret_access_key ,
383- cfg .get ('region' ))
394+ client = get_client (
395+ 'lambda' , aws_access_key_id , aws_secret_access_key ,
396+ cfg .get ('region' ),
397+ )
384398
385399 # Do we prefer development variable over config?
386400 func_name = (
@@ -396,7 +410,7 @@ def create_function(cfg, path_to_zip_file):
396410 'Description' : cfg .get ('description' ),
397411 'Timeout' : cfg .get ('timeout' , 15 ),
398412 'MemorySize' : cfg .get ('memory_size' , 512 ),
399- 'Publish' : True
413+ 'Publish' : True ,
400414 }
401415
402416 if 'environment_variables' in cfg :
@@ -406,8 +420,8 @@ def create_function(cfg, path_to_zip_file):
406420 key : get_environment_variable_value (value )
407421 for key , value
408422 in cfg .get ('environment_variables' ).items ()
409- }
410- }
423+ },
424+ },
411425 )
412426
413427 client .create_function (** kwargs )
@@ -424,13 +438,15 @@ def update_function(cfg, path_to_zip_file):
424438 account_id = get_account_id (aws_access_key_id , aws_secret_access_key )
425439 role = get_role_name (account_id , cfg .get ('role' , 'lambda_basic_execution' ))
426440
427- client = get_client ('lambda' , aws_access_key_id , aws_secret_access_key ,
428- cfg .get ('region' ))
441+ client = get_client (
442+ 'lambda' , aws_access_key_id , aws_secret_access_key ,
443+ cfg .get ('region' ),
444+ )
429445
430446 client .update_function_code (
431447 FunctionName = cfg .get ('function_name' ),
432448 ZipFile = byte_stream ,
433- Publish = True
449+ Publish = True ,
434450 )
435451
436452 kwargs = {
@@ -442,8 +458,8 @@ def update_function(cfg, path_to_zip_file):
442458 'MemorySize' : cfg .get ('memory_size' , 512 ),
443459 'VpcConfig' : {
444460 'SubnetIds' : cfg .get ('subnet_ids' , []),
445- 'SecurityGroupIds' : cfg .get ('security_group_ids' , [])
446- }
461+ 'SecurityGroupIds' : cfg .get ('security_group_ids' , []),
462+ },
447463 }
448464
449465 if 'environment_variables' in cfg :
@@ -453,29 +469,32 @@ def update_function(cfg, path_to_zip_file):
453469 key : get_environment_variable_value (value )
454470 for key , value
455471 in cfg .get ('environment_variables' ).items ()
456- }
457- }
472+ },
473+ },
458474 )
459475
460476 client .update_function_configuration (** kwargs )
461477
478+
462479def upload_s3 (cfg , path_to_zip_file ):
463480 """Upload a function to AWS S3."""
464481
465482 print ('Uploading your new Lambda function' )
466483 aws_access_key_id = cfg .get ('aws_access_key_id' )
467484 aws_secret_access_key = cfg .get ('aws_secret_access_key' )
468- account_id = get_account_id ( aws_access_key_id , aws_secret_access_key )
469- client = get_client ( 's3' , aws_access_key_id , aws_secret_access_key ,
470- cfg .get ('region' ))
471- role = get_role_name ( account_id , cfg . get ( 'role' , 'basic_s3_upload' ) )
485+ client = get_client (
486+ 's3' , aws_access_key_id , aws_secret_access_key ,
487+ cfg .get ('region' ),
488+ )
472489 byte_stream = b''
473490 with open (path_to_zip_file , mode = 'rb' ) as fh :
474491 byte_stream = fh .read ()
475492 s3_key_prefix = cfg .get ('s3_key_prefix' , '/dist' )
476493 checksum = hashlib .new ('md5' , byte_stream ).hexdigest ()
477494 timestamp = str (time .time ())
478- filename = '{prefix}{checksum}-{ts}.zip' .format (prefix = s3_key_prefix , checksum = checksum , ts = timestamp )
495+ filename = '{prefix}{checksum}-{ts}.zip' .format (
496+ prefix = s3_key_prefix , checksum = checksum , ts = timestamp ,
497+ )
479498
480499 # Do we prefer development variable over config?
481500 buck_name = (
@@ -487,19 +506,22 @@ def upload_s3(cfg, path_to_zip_file):
487506 kwargs = {
488507 'Bucket' : '{}' .format (buck_name ),
489508 'Key' : '{}' .format (filename ),
490- 'Body' : byte_stream
509+ 'Body' : byte_stream ,
491510 }
492511
493512 client .put_object (** kwargs )
494513 print ('Finished uploading {} to S3 bucket {}' .format (func_name , buck_name ))
495514
515+
496516def function_exists (cfg , function_name ):
497517 """Check whether a function exists or not"""
498518
499519 aws_access_key_id = cfg .get ('aws_access_key_id' )
500520 aws_secret_access_key = cfg .get ('aws_secret_access_key' )
501- client = get_client ('lambda' , aws_access_key_id , aws_secret_access_key ,
502- cfg .get ('region' ))
521+ client = get_client (
522+ 'lambda' , aws_access_key_id , aws_secret_access_key ,
523+ cfg .get ('region' ),
524+ )
503525 functions = client .list_functions ().get ('Functions' , [])
504526 for fn in functions :
505527 if fn .get ('FunctionName' ) == function_name :
0 commit comments