From 2f6d3ddd2f1259cc9452882e4df0dd764b9195e7 Mon Sep 17 00:00:00 2001 From: YANG Xudong Date: Wed, 10 May 2017 12:00:07 +0900 Subject: [PATCH 1/5] Add version. --- lambda_local/__init__.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lambda_local/__init__.py b/lambda_local/__init__.py index 9dfd854..17740d3 100644 --- a/lambda_local/__init__.py +++ b/lambda_local/__init__.py @@ -9,13 +9,20 @@ import argparse import sys from multiprocessing import Process +import pkg_resources from .main import run +__version__ = pkg_resources.require("python-lambda-local")[0].version + + def main(): args = parse_args() + print(args.version) + return + p = Process(target=run, args=(args,)) p.start() p.join() @@ -27,23 +34,28 @@ def parse_args(): parser = argparse.ArgumentParser(description="Run AWS Lambda function" + " written in Python on local machine.") parser.add_argument("file", metavar="FILE", type=str, - help="Lambda function file name") + help="lambda function file name") parser.add_argument("event", metavar="EVENT", type=str, - help="Event data file name.") + help="event data file name") parser.add_argument("-l", "--library", metavar="LIBRARY_PATH", - type=str, help="Path of 3rd party libraries.") + type=str, help="path of 3rd party libraries") parser.add_argument("-f", "--function", metavar="HANDLER_FUNCTION", type=str, default="handler", - help="Lambda function handler name. \ -Default: \"handler\".") + help="lambda function handler name, \ +default: \"handler\"") parser.add_argument("-t", "--timeout", metavar="TIMEOUT", type=int, default=3, - help="Seconds until lambda function timeout. \ -Default: 3") + help="seconds until lambda function timeout, \ +default: 3") parser.add_argument("-a", "--arn-string", metavar="ARN_STRING", type=str, - default="", help="arn string for function") + default="", help="ARN string for lambda function") parser.add_argument("-v", "--version-name", metavar="VERSION_NAME", - type=str, default="", help="function version name") + type=str, default="", + help="lambda function version name") + + parser.add_argument("--version", action="version", + version="%(prog)s " + __version__, + help="print the version of python-lambda-local and exit") return parser.parse_args() From 0739045595d41e41c4752a0690a8476d6d79f894 Mon Sep 17 00:00:00 2001 From: YANG Xudong Date: Wed, 10 May 2017 12:04:54 +0900 Subject: [PATCH 2/5] Update readme. --- README.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 844245a..416e8de 100644 --- a/README.md +++ b/README.md @@ -28,26 +28,28 @@ Run `python-lambda-local -h` to see the help. ``` usage: python-lambda-local [-h] [-l LIBRARY_PATH] [-f HANDLER_FUNCTION] [-t TIMEOUT] [-a ARN_STRING] [-v VERSION_NAME] + [--version] FILE EVENT Run AWS Lambda function written in Python on local machine. positional arguments: - FILE Lambda function file name - EVENT Event data file name. + FILE lambda function file name + EVENT event data file name optional arguments: -h, --help show this help message and exit -l LIBRARY_PATH, --library LIBRARY_PATH - Path of 3rd party libraries. + path of 3rd party libraries -f HANDLER_FUNCTION, --function HANDLER_FUNCTION - Lambda function handler name. Default: "handler". + lambda function handler name, default: "handler" -t TIMEOUT, --timeout TIMEOUT - Seconds until lambda function timeout. Default: 3 + seconds until lambda function timeout, default: 3 -a ARN_STRING, --arn-string ARN_STRING - arn string for function + ARN string for lambda function -v VERSION_NAME, --version-name VERSION_NAME - function version name + lambda function version name + --version print the version of python-lambda-local and exit ``` ### Prepare development directory From 95a4c529816db65448a99cc3b091a91caa4b626c Mon Sep 17 00:00:00 2001 From: YANG Xudong Date: Wed, 10 May 2017 12:06:41 +0900 Subject: [PATCH 3/5] Fix readme. --- README.rst | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 1b46963..deb21a0 100644 --- a/README.rst +++ b/README.rst @@ -33,26 +33,28 @@ Run ``python-lambda-local -h`` to see the help. usage: python-lambda-local [-h] [-l LIBRARY_PATH] [-f HANDLER_FUNCTION] [-t TIMEOUT] [-a ARN_STRING] [-v VERSION_NAME] + [--version] FILE EVENT Run AWS Lambda function written in Python on local machine. positional arguments: - FILE Lambda function file name - EVENT Event data file name. + FILE lambda function file name + EVENT event data file name optional arguments: -h, --help show this help message and exit -l LIBRARY_PATH, --library LIBRARY_PATH - Path of 3rd party libraries. + path of 3rd party libraries -f HANDLER_FUNCTION, --function HANDLER_FUNCTION - Lambda function handler name. Default: "handler". + lambda function handler name, default: "handler" -t TIMEOUT, --timeout TIMEOUT - Seconds until lambda function timeout. Default: 3 + seconds until lambda function timeout, default: 3 -a ARN_STRING, --arn-string ARN_STRING - arn string for function + ARN string for lambda function -v VERSION_NAME, --version-name VERSION_NAME - function version name + lambda function version name + --version print the version of python-lambda-local and exit Prepare development directory ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 8f2f6aa800ed285b04a17fc4a8a0f26b2d728e71 Mon Sep 17 00:00:00 2001 From: YANG Xudong Date: Wed, 10 May 2017 12:16:42 +0900 Subject: [PATCH 4/5] Remove debug code. --- lambda_local/__init__.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/lambda_local/__init__.py b/lambda_local/__init__.py index 17740d3..cb311b2 100644 --- a/lambda_local/__init__.py +++ b/lambda_local/__init__.py @@ -20,9 +20,6 @@ def main(): args = parse_args() - print(args.version) - return - p = Process(target=run, args=(args,)) p.start() p.join() From 587956176ced5681707c2901dee5b7c183604849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Cimpers=CC=8Cak?= Date: Wed, 20 Sep 2017 22:45:51 +0200 Subject: [PATCH 5/5] added an option to set env vars using json file --- README.md | 2 ++ lambda_local/__init__.py | 3 +++ lambda_local/environment_variables.py | 29 +++++++++++++++++++++++++++ lambda_local/main.py | 4 ++++ tests/environment_variables.json | 5 +++++ tests/test_environment_variables.py | 15 ++++++++++++++ 6 files changed, 58 insertions(+) create mode 100644 lambda_local/environment_variables.py create mode 100644 tests/environment_variables.json create mode 100644 tests/test_environment_variables.py diff --git a/README.md b/README.md index 416e8de..c314974 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ optional arguments: ARN string for lambda function -v VERSION_NAME, --version-name VERSION_NAME lambda function version name + -e ENVIRONMENT_VARIABLES, --environment-variables ENVIRONMENT_VARIABLES + path to flat json file with environment variables --version print the version of python-lambda-local and exit ``` diff --git a/lambda_local/__init__.py b/lambda_local/__init__.py index cb311b2..f56a061 100644 --- a/lambda_local/__init__.py +++ b/lambda_local/__init__.py @@ -49,6 +49,9 @@ def parse_args(): parser.add_argument("-v", "--version-name", metavar="VERSION_NAME", type=str, default="", help="lambda function version name") + parser.add_argument("-e", "--environment-variables", + metavar="ENVIRONMENT_VARIABLES", type=str, + help="path to flat json file with environment variables") parser.add_argument("--version", action="version", version="%(prog)s " + __version__, diff --git a/lambda_local/environment_variables.py b/lambda_local/environment_variables.py new file mode 100644 index 0000000..b518309 --- /dev/null +++ b/lambda_local/environment_variables.py @@ -0,0 +1,29 @@ +import json +import os + + +def set_environment_variables(json_file_path): + """ + Read and set environment variables from a flat json file. + + Bear in mind that env vars set this way and later on read using + `os.getenv` function will be strings since after all env vars are just + that - plain strings. + + Json file example: + ``` + { + "FOO": "bar", + "BAZ": true + } + ``` + + :param json_file_path: path to flat json file + :type json_file_path: str + """ + if json_file_path: + with open(json_file_path) as json_file: + env_vars = json.loads(json_file.read()) + + for env_name, env_value in env_vars.items(): + os.environ[str(env_name)] = str(env_value) diff --git a/lambda_local/main.py b/lambda_local/main.py index 3a46f94..95d4358 100644 --- a/lambda_local/main.py +++ b/lambda_local/main.py @@ -15,6 +15,7 @@ from . import event from . import context +from .environment_variables import set_environment_variables from .timeout import time_limit from .timeout import TimeoutException @@ -31,6 +32,9 @@ def run(args): + # set env vars if path to json file was given + set_environment_variables(args.environment_variables) + e = event.read_event(args.event) c = context.Context(args.timeout, args.arn_string, args.version_name) if args.library is not None: diff --git a/tests/environment_variables.json b/tests/environment_variables.json new file mode 100644 index 0000000..b9b81db --- /dev/null +++ b/tests/environment_variables.json @@ -0,0 +1,5 @@ +{ + "STR_KEY": "foo", + "INT_KEY": 100, + "BOOL_KEY": false +} diff --git a/tests/test_environment_variables.py b/tests/test_environment_variables.py new file mode 100644 index 0000000..6582779 --- /dev/null +++ b/tests/test_environment_variables.py @@ -0,0 +1,15 @@ +import os + +from lambda_local.environment_variables import set_environment_variables + + +def test_set_environment_variables(): + os.getenv('STR_KEY') is None + os.getenv('INT_KEY') is None + os.getenv('BOOL_KEY') is None + + set_environment_variables('tests/environment_variables.json') + + assert os.getenv('STR_KEY') == 'foo' + assert os.getenv('INT_KEY') == '100' + assert os.getenv('BOOL_KEY') == 'False'