-
Notifications
You must be signed in to change notification settings - Fork 19
/
run.py
53 lines (47 loc) · 1.72 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
import argparse
import importlib
import logging
import sys
import json
import traceback
from tests import MockContext
from lib import common
import lib
# Parse CLI arguments
parser = argparse.ArgumentParser(description='Run a Lambda function locally.')
parser.add_argument('--verbose', '-v', action='count', default=0,
help='Show more output (one -v for WARNING, two for INFO, three for DEBUG)')
parser.add_argument('name', metavar='NAME', type=str, nargs='?',
help='Name of the function to be run')
parser.add_argument('input', metavar='FILE', type=argparse.FileType('r'), nargs='?', default=sys.stdin,
help='File to get input from, or "-" for stdin')
args = parser.parse_args()
# Set up context
response = ''
context = MockContext.MockContext(args.name, '$LATEST')
logging.basicConfig(
stream=sys.stderr,
format=("[%(levelname)s]\t%(asctime)s\t{req}\t%(message)s").format(req=context.aws_request_id),
datefmt="%Y-%m-%dT%H:%M:%S%Z",
level=(logging.ERROR - args.verbose * 10)
)
try:
# Run the function
module = importlib.import_module('src.{name}.index'.format(name=args.name))
event = common.get_payload(args.input, context.identity.cognito_identity_id, '{}')
event = json.loads(event);
response = module.handler(event, context)
except Exception as exc:
exc_type, exc_value, exc_traceback = sys.exc_info()
response = {
'errorMessage': str(exc_value),
'stackTrace': traceback.extract_tb(exc_traceback),
'errorType': exc_type.__name__
}
del exc_traceback
print("\nOutput:\n--------")
try:
print(json.dumps(response, indent=4, separators=(',', ': ')))
except Exception as e:
print(response)