Skip to content

Lszecsi/refactoring #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
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
13 changes: 12 additions & 1 deletion .github/workflows/test-on-push-and-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
echo "$HOME/.local/bin" >> $GITHUB_PATH

- name: Run 'pr' target
run: make pr

Expand Down Expand Up @@ -45,4 +56,4 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Run ubuntu integration tests
run: DISTRO=ubuntu make test-integ
run: DISTRO=ubuntu make test-integ
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,6 @@ tmp*.py
deps/artifacts/
deps/aws-lambda-cpp-*/
deps/curl-*/

# local caches
.DS_Store
16 changes: 11 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
repos:
- repo: https://github.com/python/black
rev: 19.3b0
- repo: local
hooks:
- id: black
language_version: python3.9
exclude_types: ['markdown', 'ini', 'toml', 'rst']
- id: ruff-check
name: ruff
entry: poetry run ruff check --fix
language: system
types: [python]
- id: ruff-format
name: ruff format
entry: poetry run ruff format
language: system
types: [python]
54 changes: 29 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ target:

.PHONY: init
init:
pip3 install -r requirements/base.txt -r requirements/dev.txt
python3 scripts/dev.py init

.PHONY: test
test: check-format
pytest --cov awslambdaric --cov-report term-missing --cov-fail-under 90 tests
test:
python3 scripts/dev.py test

.PHONY: lint
lint:
python3 scripts/dev.py lint

.PHONY: clean
clean:
python3 scripts/dev.py clean

.PHONY: build
build: clean
python3 scripts/dev.py build

.PHONY: setup-codebuild-agent
setup-codebuild-agent:
Expand All @@ -25,48 +37,40 @@ test-integ: setup-codebuild-agent

.PHONY: check-security
check-security:
bandit -r awslambdaric
poetry run bandit -r awslambdaric

.PHONY: format
format:
black setup.py awslambdaric/ tests/
poetry run ruff format awslambdaric/ tests/

.PHONY: check-format
check-format:
black --check setup.py awslambdaric/ tests/
poetry run ruff format --check awslambdaric/ tests/

# Command to run everytime you make changes to verify everything works
.PHONY: dev
dev: init test

# Verifications to run before sending a pull request
.PHONY: pr
pr: init check-format check-security dev

.PHONY: codebuild
codebuild: setup-codebuild-agent
CODEBUILD_IMAGE_TAG=codebuild-agent DISTRO="$(DISTRO)" tests/integration/codebuild-local/test_all.sh tests/integration/codebuild

.PHONY: clean
clean:
rm -rf dist
rm -rf awslambdaric.egg-info

.PHONY: build
build: clean
BUILD=true python3 setup.py sdist

define HELP_MESSAGE

Usage: $ make [TARGETS]

TARGETS
check-security Run bandit to find security issues.
format Run black to automatically update your code to match our formatting.
build Builds the package.
clean Cleans the working directory by removing built artifacts.
dev Run all development tests after a change.
init Initialize and install the requirements and dev-requirements for this project.
format Run black to automatically update your code to match formatting.
build Build the package using scripts/dev.py.
clean Cleans the working directory using scripts/dev.py.
dev Run all development tests using scripts/dev.py.
init Install dependencies via scripts/dev.py.
pr Perform all checks before submitting a Pull Request.
test Run the Unit tests.

endef
test Run unit tests using scripts/dev.py.
lint Run all linters via scripts/dev.py.
test-smoke Run smoke tests inside Docker.
test-integ Run all integration tests.
endef
1 change: 1 addition & 0 deletions awslambdaric/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def handle_event_request(

if error_result is not None:

logging.warning(lambda_unhandled_exception_warning_message)
log_error(error_result, log_sink)
lambda_runtime_client.post_invocation_error(
invoke_id, to_json(error_result), to_json(xray_fault)
Expand Down
765 changes: 765 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[tool.poetry]
name = "awslambdaric"
version = "3.0.2"
description = "AWS Lambda Runtime Interface Client for Python"
authors = ["Amazon Web Services"]
license = "Apache-2.0"
readme = "README.md"
packages = [{ include = "awslambdaric" }]

[tool.poetry.dependencies]
python = ">=3.9"
simplejson = ">=3.20.1"
snapshot-restore-py = ">=1.0.0"

[tool.poetry.group.dev.dependencies]
coverage = ">=4.4.0"
pytest = ">=3.0.7"
mock = ">=2.0.0"
parameterized = ">=0.9.0"
ruff = "^0.1.0"
bandit = "^1.7.5"
pre-commit = "^3.0.0"

# Development scripts
[tool.poetry.scripts]
init = "scripts.dev:init"
test = "scripts.dev:test"
lint = "scripts.dev:lint"
format = "scripts.dev:format_code"
clean = "scripts.dev:clean"
build = "scripts.dev:build"
local-test = "scripts.dev:local_test"

[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0", "setuptools>=68", "wheel"]
build-backend = "poetry.core.masonry.api"

# Ruff configuration
[tool.ruff]
target-version = "py39"
line-length = 88

[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"UP", # pyupgrade
"C90", # mccabe complexity
]

# Ignore rules that are too strict for existing codebases
ignore = [
"E501", # Line too long (handled by formatter)
"PLR0913", # Too many arguments
"E722", # Bare except
"PLW0603", # Global statement
"UP031", # % formatting vs f-strings
"E402", # Module import not at top
]

[tool.ruff.format]
quote-style = "double"
2 changes: 0 additions & 2 deletions requirements/base.txt

This file was deleted.

12 changes: 0 additions & 12 deletions requirements/dev.txt

This file was deleted.

Empty file added scripts/__init__.py
Empty file.
88 changes: 88 additions & 0 deletions scripts/dev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env python3
import argparse
import subprocess
import shutil
import sys
import os
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent

def run(cmd, check=True, env=None):
print("\n$ {}".format(' '.join(cmd) if isinstance(cmd, list) else cmd))
result = subprocess.run(cmd, shell=isinstance(cmd, str), check=check, env=env)
if result.returncode != 0 and check:
sys.exit(result.returncode)


def init():
print("Initializing environment")
run(["poetry", "install"])


def test():
print("Running tests")
run(["poetry", "run", "pytest", "tests"])


def lint():
print("Running linters")
run(["poetry", "run", "ruff", "check", "awslambdaric/", "tests/"])


def format_code():
print("Formatting code")
run(["poetry", "run", "ruff", "format", "awslambdaric/", "tests/"])


def clean():
print("Cleaning build artifacts")
dirs_to_remove = ["build", "dist", "*.egg-info"]
for pattern in dirs_to_remove:
for path in ROOT.glob(pattern):
if path.is_dir():
shutil.rmtree(path)
print("Removed directory: {}".format(path))
elif path.is_file():
path.unlink()
print("Removed file: {}".format(path))


def build():
print("Building package")
env = os.environ.copy()

# Set BUILD=true on Linux for native compilation
import platform
if platform.system() == "Linux":
env["BUILD"] = "true"
elif os.getenv("BUILD") == "true":
env["BUILD"] = "true"

run([sys.executable, "setup.py", "sdist", "bdist_wheel"], env=env)


def main():
parser = argparse.ArgumentParser(description="Development scripts")
parser.add_argument("command", choices=[
"init", "test", "lint", "format", "clean", "build"
])

args = parser.parse_args()

command_map = {
"init": init,
"test": test,
"lint": lint,
"format": format_code,
"clean": clean,
"build": build,

}

command_map[args.command]()


if __name__ == "__main__":
main()

Loading
Loading