GeezerVison is showing github.com — Merge pull request #127 from soapergem/master · luisaverdes/python-lambda@6dc00f3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -63,3 +63,5 @@ target/ | 63 | 63 |
| 64 | 64 | # vim swap files | 65 | 65 | .*.sw? | | 66 | +aws_lambda/.DS_Store | | 67 | +.DS_Store |
Collapse file Copy file name to clipboardExpand all lines: README.rst +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,7 @@ With python-lambda and `pytube <https://github.com/nficano/pytube/>`_ both conti | 19 | 19 | Description | 20 | 20 | =========== | 21 | 21 |
| 22 | | -AWS Lambda is a service that allows you to write Python, Java, Node.js, C# or Go code that gets executed in response to events like http requests or files uploaded to S3. | | 22 | +AWS Lambda is a service that allows you to write Python, Java, or Node.js code that gets executed in response to events like http requests or files uploaded to S3. | 23 | 23 |
| 24 | 24 | Working with Lambda is relatively easy, but the process of bundling and deploying your code is not as simple as it could be. | 25 | 25 |
| |
Collapse file Copy file name to clipboardExpand all lines: aws_lambda/aws_lambda.py +12-16Lines changed: 12 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 |
| 17 | 17 | import boto3 | 18 | 18 | import botocore | 19 | | -import pip | 20 | 19 | import yaml | | 20 | +import subprocess | 21 | 21 |
| 22 | 22 | from .helpers import archive | 23 | 23 | from .helpers import get_environment_variable_value | 24 | 24 | from .helpers import mkdir | 25 | 25 | from .helpers import read | 26 | 26 | from .helpers import timestamp | | 27 | +from .helpers import LambdaContext | 27 | 28 |
| 28 | 29 |
| 29 | 30 | ARN_PREFIXES = { | @@ -228,11 +229,14 @@ def invoke( | 228 | 229 | # into a function we can execute. | 229 | 230 | fn = get_callable_handler_function(src, handler) | 230 | 231 |
| 231 | | - # TODO: look into mocking the ``context`` variable, currently being passed | 232 | | - # as None. | | 232 | + timeout = cfg.get('timeout') | | 233 | + if timeout: | | 234 | + context = LambdaContext(cfg.get('function_name'),timeout) | | 235 | + else: | | 236 | + context = LambdaContext(cfg.get('function_name')) | 233 | 237 |
| 234 | 238 | start = time.time() | 235 | | - results = fn(event, None) | | 239 | + results = fn(event, context) | 236 | 240 | end = time.time() | 237 | 241 |
| 238 | 242 | print('{0}'.format(results)) | @@ -409,12 +413,8 @@ def _filter_blacklist(package): | 409 | 413 | package = package.replace('-e ', '') | 410 | 414 |
| 411 | 415 | print('Installing {package}'.format(package=package)) | 412 | | - pip_major_version = [int(v) for v in pip.__version__.split('.')][0] | 413 | | - if pip_major_version >= 10: | 414 | | - from pip._internal import main | 415 | | - main(['install', package, '-t', path, '--ignore-installed']) | 416 | | - else: | 417 | | - pip.main(['install', package, '-t', path, '--ignore-installed']) | | 416 | + subprocess.check_call([sys.executable, '-m', 'pip', 'install', package, '-t', path, '--ignore-installed']) | | 417 | + print ('Install directory contents are now: {directory}'.format(directory=os.listdir(path))) | 418 | 418 |
| 419 | 419 |
| 420 | 420 | def pip_install_to_target(path, requirements=None, local_package=None): | @@ -434,12 +434,8 @@ def pip_install_to_target(path, requirements=None, local_package=None): | 434 | 434 | packages = [] | 435 | 435 | if not requirements: | 436 | 436 | print('Gathering pip packages') | 437 | | - pip_major_version = [int(v) for v in pip.__version__.split('.')][0] | 438 | | - if pip_major_version >= 10: | 439 | | - from pip._internal import operations | 440 | | - packages.extend(operations.freeze.freeze()) | 441 | | - else: | 442 | | - packages.extend(pip.operations.freeze.freeze()) | | 437 | + pkgStr = subprocess.check_call([sys.executable, '-m', 'pip', 'freeze']) | | 438 | + packages.extend(pkgStr.decode('utf-8').splitlines()) | 443 | 439 | else: | 444 | 440 | if os.path.exists(requirements): | 445 | 441 | print('Gathering requirement packages') | |
Collapse file Copy file name to clipboardExpand all lines: aws_lambda/helpers.py +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | import datetime as dt | 3 | 3 | import os | 4 | 4 | import re | | 5 | +import time | 5 | 6 | import zipfile | 6 | 7 |
| 7 | 8 |
| @@ -41,3 +42,22 @@ def get_environment_variable_value(val): | 41 | 42 | if match is not None: | 42 | 43 | env_val = os.environ.get(match.group('environment_key_name')) | 43 | 44 | return env_val | | 45 | + | | 46 | +class LambdaContext: | | 47 | + current_milli_time = lambda x: int(round(time.time() * 1000)) | | 48 | + | | 49 | + def get_remaining_time_in_millis(self): | | 50 | + return max(0, self.timeout_millis - (self.current_milli_time() - self.start_time_millis)) | | 51 | + | | 52 | + def __init__(self,function_name, timeoutSeconds = 3): | | 53 | + self.function_name = function_name | | 54 | + self.function_version = None | | 55 | + self.invoked_function_arn = None | | 56 | + self.memory_limit_in_mb = None | | 57 | + self.aws_request_id = None | | 58 | + self.log_group_name = None | | 59 | + self.log_stream_name = None | | 60 | + self.identity = None | | 61 | + self.client_context = None | | 62 | + self.timeout_millis = timeoutSeconds * 1000 | | 63 | + self.start_time_millis = self.current_milli_time() |
Collapse file Copy file name to clipboardExpand all lines: setup.py +7-17Lines changed: 7 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | # -*- coding: utf-8 -*- | 3 | 3 | import sys | 4 | 4 |
| 5 | | -try: # for pip >= 10 | 6 | | - from pip._internal.req import parse_requirements | 7 | | -except ImportError: # for pip <= 9.0.3 | 8 | | - from pip.req import parse_requirements | 9 | | - | 10 | | -try: # for pip >= 10 | 11 | | - from pip._internal import download | 12 | | -except ImportError: # for pip <= 9.0.3 | 13 | | - from pip import download | 14 | | - | 15 | 5 | from setuptools import find_packages | 16 | 6 | from setuptools import setup | 17 | 7 |
| 18 | 8 | with open('README.rst') as readme_file: | 19 | 9 | readme = readme_file.read() | 20 | 10 |
| 21 | | -requirements = parse_requirements( | 22 | | - 'requirements.txt', session=download.PipSession(), | 23 | | -) | 24 | | - | 25 | | -pip_requirements = [str(r.req) for r in requirements] | | 11 | +requirements = [ | | 12 | + 'boto3', | | 13 | + 'click', | | 14 | + 'PyYAML', | | 15 | +] | 26 | 16 |
| 27 | 17 | # Only install futures package if using a Python version <= 2.7 | 28 | 18 | if sys.version_info < (3, 0): | 29 | | - pip_requirements.append('futures') | | 19 | + requirements.append('futures') | 30 | 20 |
| 31 | 21 | test_requirements = [ | 32 | 22 | # TODO: put package test requirements here | | 47 | 37 | }, | 48 | 38 | include_package_data=True, | 49 | 39 | scripts=['scripts/lambda'], | 50 | | - install_requires=pip_requirements, | | 40 | + install_requires=requirements, | 51 | 41 | license='ISCL', | 52 | 42 | zip_safe=False, | 53 | 43 | keywords='python-lambda', | |
Collapse file Copy file name to clipboard +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +from aws_lambda.helpers import LambdaContext | | 2 | +import time | | 3 | +import unittest | | 4 | + | | 5 | +class TestLambdaContext(unittest.TestCase): | | 6 | + | | 7 | + def test_get_remaining_time_in_millis(self): | | 8 | + context = LambdaContext('function_name',2000) | | 9 | + time.sleep(.5) | | 10 | + self.assertTrue(context.get_remaining_time_in_millis() < 2000) | | 11 | + | | 12 | + | | 13 | +if __name__ == '__main__': | | 14 | + unittest.main() |
|