Skip to content

Commit 4bd9927

Browse files
authored
Merge pull request #39 from HDE/master
Release v0.1.7
2 parents 0a8e44d + 2de2a72 commit 4bd9927

13 files changed

+105
-18
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ target/
5858

5959
# Python environment
6060
.python-version
61+
62+
# Vitual Environments
63+
venv/

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015-2017 HDE, Inc.
3+
Copyright (c) 2015-2018 HDE, Inc.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Run lambda function on local machine
88

99
## Prepare development environment
1010

11-
Please use a newly created virtualenv of Python 2.7 or Python 3.6 .
11+
Please use a newly created virtualenv of Python 2.7 or Python 3.6.
1212

1313
## Installation
1414

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Run lambda function on local machine
99
Prepare development environment
1010
-------------------------------
1111

12-
Please use a newly created virtualenv of Python 2.7 or Python 3.6 .
12+
Please use a newly created virtualenv of Python 2.7 or Python 3.6.
1313

1414
Installation
1515
------------

lambda_local/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'''
22
python-lambda-local: Main module
33
4-
Copyright 2015-2017 HDE, Inc.
4+
Copyright 2015-2018 HDE, Inc.
55
Licensed under MIT.
66
'''
77

lambda_local/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
Copyright 2015-2017 HDE, Inc.
2+
Copyright 2015-2018 HDE, Inc.
33
Licensed under MIT.
44
'''
55

lambda_local/environment_variables.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
import os
33

44

5+
def export_variables(environment_variables):
6+
for env_name, env_value in environment_variables.items():
7+
os.environ[str(env_name)] = str(env_value)
8+
9+
510
def set_environment_variables(json_file_path):
611
"""
712
Read and set environment variables from a flat json file.
@@ -25,5 +30,4 @@ def set_environment_variables(json_file_path):
2530
with open(json_file_path) as json_file:
2631
env_vars = json.loads(json_file.read())
2732

28-
for env_name, env_value in env_vars.items():
29-
os.environ[str(env_name)] = str(env_value)
33+
export_variables(env_vars)

lambda_local/event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
Copyright 2015-2017 HDE, Inc.
2+
Copyright 2015-2018 HDE, Inc.
33
Licensed under MIT.
44
'''
55

lambda_local/main.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
Copyright 2015-2017 HDE, Inc.
2+
Copyright 2015-2018 HDE, Inc.
33
Licensed under MIT.
44
'''
55

@@ -15,7 +15,7 @@
1515

1616
from . import event
1717
from . import context
18-
from .environment_variables import set_environment_variables
18+
from .environment_variables import set_environment_variables, export_variables
1919
from .timeout import time_limit
2020
from .timeout import TimeoutException
2121

@@ -31,6 +31,17 @@
3131
EXITCODE_ERR = 1
3232

3333

34+
def call(func, event, timeout, environment_variables={}, arn_string="", version_name="", library=None):
35+
export_variables(environment_variables)
36+
e = json.loads(event)
37+
c = context.Context(timeout, arn_string, version_name)
38+
if library is not None:
39+
load_lib(library)
40+
request_id = uuid.uuid4()
41+
42+
return _runner(request_id, e, c, func)
43+
44+
3445
def run(args):
3546
# set env vars if path to json file was given
3647
set_environment_variables(args.environment_variables)
@@ -41,16 +52,23 @@ def run(args):
4152
load_lib(args.library)
4253
request_id = uuid.uuid4()
4354
func = load(request_id, args.file, args.function)
55+
56+
(result, err_type) = _runner(request_id, e, c, func)
57+
58+
if err_type is not None:
59+
sys.exit(EXITCODE_ERR)
60+
4461

62+
def _runner(request_id, event, context, func):
4563
logger = logging.getLogger()
4664
result = None
4765

48-
logger.info("Event: {}".format(e))
66+
logger.info("Event: {}".format(event))
4967

5068
logger.info("START RequestId: {}".format(request_id))
5169

5270
start_time = timeit.default_timer()
53-
result, err_type = execute(func, e, c)
71+
result, err_type = execute(func, event, context)
5472
end_time = timeit.default_timer()
5573

5674
logger.info("END RequestId: {}".format(request_id))
@@ -64,8 +82,7 @@ def run(args):
6482
logger.info("REPORT RequestId: {}\tDuration: {}".format(
6583
request_id, duration))
6684

67-
if err_type is not None:
68-
sys.exit(EXITCODE_ERR)
85+
return (result, err_type)
6986

7087

7188
def load_lib(path):

lambda_local/timeout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
Copyright 2015-2017 HDE, Inc.
2+
Copyright 2015-2018 HDE, Inc.
33
Licensed under MIT.
44
'''
55

setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'''
22
python-lambda-local: Run lambda function in python on local machine.
33
4-
Copyright 2015-2017 HDE, Inc.
4+
Copyright 2015-2018 HDE, Inc.
55
Licensed under MIT.
66
'''
7+
import io
78
import sys
89
from setuptools import setup, find_packages
910
from setuptools.command.test import test as TestCommand
@@ -22,12 +23,12 @@ def run_tests(self):
2223
sys.exit(pytest.main(self.test_args))
2324

2425

25-
version = "0.1.6"
26+
version = "0.1.7"
2627

2728
setup(name="python-lambda-local",
2829
version=version,
2930
description="Run lambda function in python on local machine.",
30-
long_description=open("README.rst").read(),
31+
long_description=io.open("README.rst", encoding="utf-8").read(),
3132
classifiers=[
3233
'Development Status :: 3 - Alpha',
3334
'Operating System :: POSIX',

tests/test_direct_invocations.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'''
2+
python-lambda-local: Test Direct Inovactions
3+
(command-line and direct).
4+
5+
Meant for use with py.test.
6+
7+
Copyright 2015 HDE, Inc.
8+
Licensed under MIT
9+
'''
10+
import json
11+
import argparse
12+
from multiprocessing import Process
13+
import os
14+
from lambda_local.main import run as lambda_run
15+
from lambda_local.main import call as lambda_call
16+
17+
18+
def my_lambda_function(event, context):
19+
print("Hello World from My Lambda Function!")
20+
return 42
21+
22+
def test_function_call_for_pytest():
23+
request = json.dumps({})
24+
(result, error_type) = lambda_call(func=my_lambda_function, event=request, timeout=1)
25+
26+
assert error_type is None
27+
28+
assert result == 42
29+
30+
31+
def test_check_command_line():
32+
request = json.dumps({})
33+
request_file = 'check_command_line_event.json'
34+
with open (request_file, "w") as f:
35+
f.write(request)
36+
37+
args = argparse.Namespace(event=request_file,
38+
file='tests/test_direct_invocations.py',
39+
function='my_lambda_function',
40+
timeout=1,
41+
environment_variables='',
42+
library=None,
43+
version_name='',
44+
arn_string=''
45+
)
46+
p = Process(target=lambda_run, args=(args,))
47+
p.start()
48+
p.join()
49+
50+
os.remove(request_file)
51+
assert p.exitcode == 0
52+

wercker.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ build-py2:
2626
code: |
2727
python setup.py sdist bdist_wheel
2828
29+
- script:
30+
name: test
31+
code: |
32+
python setup.py test
33+
2934
build-py3:
3035
box: python:3.6-slim
3136
steps:
@@ -49,6 +54,11 @@ build-py3:
4954
code: |
5055
python setup.py sdist bdist_wheel
5156
57+
- script:
58+
name: test
59+
code: |
60+
python setup.py test
61+
5262
deploy:
5363
pypi:
5464
- script:

0 commit comments

Comments
 (0)