text
stringlengths
0
14.1k
# Load and parse event file.
path_to_event_file = os.path.join(src, event_file)
event = read(path_to_event_file, loader=json.loads)
# Tweak to allow module to import local modules
try:
sys.path.index(src)
except ValueError:
sys.path.append(src)
handler = cfg.get(""handler"")
# Inspect the handler string (<module>.<function name>) and translate it
# into a function we can execute.
fn = get_callable_handler_function(src, handler)
timeout = cfg.get(""timeout"")
if timeout:
context = LambdaContext(cfg.get(""function_name""), timeout)
else:
context = LambdaContext(cfg.get(""function_name""))
start = time.time()
results = fn(event, context)
end = time.time()
print(""{0}"".format(results))
if verbose:
print(
""\nexecution time: {:.8f}s\nfunction execution ""
""timeout: {:2}s"".format(end - start, cfg.get(""timeout"", 15))
)
def init(src, minimal=False):
""""""Copies template files to a given directory.
:param str src:
The path to output the template lambda project files.
:param bool minimal:
Minimal possible template files (excludes event.json).
""""""
templates_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), ""project_templates"",
)
for filename in os.listdir(templates_path):
if (minimal and filename == ""event.json"") or filename.endswith("".pyc""):
continue
dest_path = os.path.join(templates_path, filename)
if not os.path.isdir(dest_path):
copy(dest_path, src)
def build(
src,
requirements=None,
local_package=None,
config_file=""config.yaml"",
profile_name=None,
):
""""""Builds the file bundle.
:param str src:
The path to your Lambda ready project (folder must contain a valid
config.yaml and handler module (e.g.: service.py).
:param str local_package:
The path to a local package with should be included in the deploy as
well (and/or is not available on PyPi)
""""""
# Load and parse the config file.
path_to_config_file = os.path.join(src, config_file)
cfg = read_cfg(path_to_config_file, profile_name)
# Get the absolute path to the output directory and create it if it doesn't
# already exist.
dist_directory = cfg.get(""dist_directory"", ""dist"")
path_to_dist = os.path.join(src, dist_directory)
mkdir(path_to_dist)
# Combine the name of the Lambda function with the current timestamp to use
# for the output filename.
function_name = cfg.get(""function_name"")
output_filename = ""{0}-{1}.zip"".format(timestamp(), function_name)
path_to_temp = mkdtemp(prefix=""aws-lambda"")
pip_install_to_target(
path_to_temp, requirements=requirements, local_package=local_package,
)
# Hack for Zope.
if ""zope"" in os.listdir(path_to_temp):
print(
""Zope packages detected; fixing Zope package paths to ""
""make them importable."",
)
# Touch.
with open(os.path.join(path_to_temp, ""zope/__init__.py""), ""wb""):
pass