Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ target/

# Python environment
.python-version

# Vitual Environments
venv/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

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

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Run lambda function on local machine

## Prepare development environment

Please use a newly created virtualenv of Python 2.7 or Python 3.6 .
Please use a newly created virtualenv of Python 2.7 or Python 3.6.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Run lambda function on local machine
Prepare development environment
-------------------------------

Please use a newly created virtualenv of Python 2.7 or Python 3.6 .
Please use a newly created virtualenv of Python 2.7 or Python 3.6.

Installation
------------
Expand Down
2 changes: 1 addition & 1 deletion lambda_local/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''
python-lambda-local: Main module

Copyright 2015-2017 HDE, Inc.
Copyright 2015-2018 HDE, Inc.
Licensed under MIT.
'''

Expand Down
2 changes: 1 addition & 1 deletion lambda_local/context.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'''
Copyright 2015-2017 HDE, Inc.
Copyright 2015-2018 HDE, Inc.
Licensed under MIT.
'''

Expand Down
8 changes: 6 additions & 2 deletions lambda_local/environment_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import os


def export_variables(environment_variables):
for env_name, env_value in environment_variables.items():
os.environ[str(env_name)] = str(env_value)


def set_environment_variables(json_file_path):
"""
Read and set environment variables from a flat json file.
Expand All @@ -25,5 +30,4 @@ def set_environment_variables(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)
export_variables(env_vars)
2 changes: 1 addition & 1 deletion lambda_local/event.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'''
Copyright 2015-2017 HDE, Inc.
Copyright 2015-2018 HDE, Inc.
Licensed under MIT.
'''

Expand Down
29 changes: 23 additions & 6 deletions lambda_local/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'''
Copyright 2015-2017 HDE, Inc.
Copyright 2015-2018 HDE, Inc.
Licensed under MIT.
'''

Expand All @@ -15,7 +15,7 @@

from . import event
from . import context
from .environment_variables import set_environment_variables
from .environment_variables import set_environment_variables, export_variables
from .timeout import time_limit
from .timeout import TimeoutException

Expand All @@ -31,6 +31,17 @@
EXITCODE_ERR = 1


def call(func, event, timeout, environment_variables={}, arn_string="", version_name="", library=None):
export_variables(environment_variables)
e = json.loads(event)
c = context.Context(timeout, arn_string, version_name)
if library is not None:
load_lib(library)
request_id = uuid.uuid4()

return _runner(request_id, e, c, func)


def run(args):
# set env vars if path to json file was given
set_environment_variables(args.environment_variables)
Expand All @@ -41,16 +52,23 @@ def run(args):
load_lib(args.library)
request_id = uuid.uuid4()
func = load(request_id, args.file, args.function)

(result, err_type) = _runner(request_id, e, c, func)

if err_type is not None:
sys.exit(EXITCODE_ERR)


def _runner(request_id, event, context, func):
logger = logging.getLogger()
result = None

logger.info("Event: {}".format(e))
logger.info("Event: {}".format(event))

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

start_time = timeit.default_timer()
result, err_type = execute(func, e, c)
result, err_type = execute(func, event, context)
end_time = timeit.default_timer()

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

if err_type is not None:
sys.exit(EXITCODE_ERR)
return (result, err_type)


def load_lib(path):
Expand Down
2 changes: 1 addition & 1 deletion lambda_local/timeout.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'''
Copyright 2015-2017 HDE, Inc.
Copyright 2015-2018 HDE, Inc.
Licensed under MIT.
'''

Expand Down
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'''
python-lambda-local: Run lambda function in python on local machine.

Copyright 2015-2017 HDE, Inc.
Copyright 2015-2018 HDE, Inc.
Licensed under MIT.
'''
import io
import sys
from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand
Expand All @@ -22,12 +23,12 @@ def run_tests(self):
sys.exit(pytest.main(self.test_args))


version = "0.1.6"
version = "0.1.7"

setup(name="python-lambda-local",
version=version,
description="Run lambda function in python on local machine.",
long_description=open("README.rst").read(),
long_description=io.open("README.rst", encoding="utf-8").read(),
classifiers=[
'Development Status :: 3 - Alpha',
'Operating System :: POSIX',
Expand Down
52 changes: 52 additions & 0 deletions tests/test_direct_invocations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'''
python-lambda-local: Test Direct Inovactions
(command-line and direct).

Meant for use with py.test.

Copyright 2015 HDE, Inc.
Licensed under MIT
'''
import json
import argparse
from multiprocessing import Process
import os
from lambda_local.main import run as lambda_run
from lambda_local.main import call as lambda_call


def my_lambda_function(event, context):
print("Hello World from My Lambda Function!")
return 42

def test_function_call_for_pytest():
request = json.dumps({})
(result, error_type) = lambda_call(func=my_lambda_function, event=request, timeout=1)

assert error_type is None

assert result == 42


def test_check_command_line():
request = json.dumps({})
request_file = 'check_command_line_event.json'
with open (request_file, "w") as f:
f.write(request)

args = argparse.Namespace(event=request_file,
file='tests/test_direct_invocations.py',
function='my_lambda_function',
timeout=1,
environment_variables='',
library=None,
version_name='',
arn_string=''
)
p = Process(target=lambda_run, args=(args,))
p.start()
p.join()

os.remove(request_file)
assert p.exitcode == 0

10 changes: 10 additions & 0 deletions wercker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ build-py2:
code: |
python setup.py sdist bdist_wheel

- script:
name: test
code: |
python setup.py test

build-py3:
box: python:3.6-slim
steps:
Expand All @@ -49,6 +54,11 @@ build-py3:
code: |
python setup.py sdist bdist_wheel

- script:
name: test
code: |
python setup.py test

deploy:
pypi:
- script:
Expand Down