diff --git a/typedb/localstack_typedb/extension.py b/typedb/localstack_typedb/extension.py index 21d8c81..bb67bc0 100644 --- a/typedb/localstack_typedb/extension.py +++ b/typedb/localstack_typedb/extension.py @@ -2,7 +2,7 @@ import shlex from localstack.config import is_env_not_false -from localstack_typedb.utils.docker import ProxiedDockerContainerExtension +from localstack_extensions.utils import ProxiedDockerContainerExtension from rolo import Request from werkzeug.datastructures import Headers diff --git a/typedb/localstack_typedb/utils/__init__.py b/typedb/localstack_typedb/utils/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/typedb/pyproject.toml b/typedb/pyproject.toml index 6e8f703..0d36012 100644 --- a/typedb/pyproject.toml +++ b/typedb/pyproject.toml @@ -14,9 +14,7 @@ authors = [ keywords = ["LocalStack", "TypeDB"] classifiers = [] dependencies = [ - "httpx", - "h2", - "priority", + "localstack-extensions-utils", ] [project.urls] diff --git a/typedb/tests/test_extension.py b/typedb/tests/test_extension.py index efe3fc4..ef56cf3 100644 --- a/typedb/tests/test_extension.py +++ b/typedb/tests/test_extension.py @@ -1,7 +1,7 @@ import requests import httpx from localstack.utils.strings import short_uid -from localstack_typedb.utils.h2_proxy import ( +from localstack_extensions.utils import ( get_frames_from_http2_stream, get_headers_from_frames, ) diff --git a/utils/Makefile b/utils/Makefile new file mode 100644 index 0000000..c80a78c --- /dev/null +++ b/utils/Makefile @@ -0,0 +1,40 @@ +VENV_BIN = python3 -m venv +VENV_DIR ?= .venv +VENV_ACTIVATE = $(VENV_DIR)/bin/activate +VENV_RUN = . $(VENV_ACTIVATE) + +usage: ## Show usage for this Makefile + @cat Makefile | grep -E '^[a-zA-Z_-]+:.*?## .*$$' | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}' + +venv: $(VENV_ACTIVATE) + +$(VENV_ACTIVATE): pyproject.toml + test -d .venv || $(VENV_BIN) .venv + $(VENV_RUN); pip install --upgrade pip setuptools wheel build + $(VENV_RUN); pip install -e .[dev] + touch $(VENV_DIR)/bin/activate + +clean: ## Clean up build artifacts and virtual environment + rm -rf .venv/ + rm -rf build/ + rm -rf .eggs/ + rm -rf *.egg-info/ + +install: venv ## Install the package in development mode + +dist: venv ## Create distribution package + $(VENV_RUN); python -m build + +publish: clean-dist venv dist ## Publish package to PyPI + $(VENV_RUN); pip install --upgrade twine; twine upload dist/* + +format: venv ## Run ruff to format and fix the code + $(VENV_RUN); python -m ruff format .; python -m ruff check --fix . + +lint: venv ## Run ruff to lint the code + $(VENV_RUN); python -m ruff check --output-format=full . + +clean-dist: clean + rm -rf dist/ + +.PHONY: clean clean-dist dist install publish usage venv format lint diff --git a/utils/README.md b/utils/README.md new file mode 100644 index 0000000..f55ed59 --- /dev/null +++ b/utils/README.md @@ -0,0 +1,40 @@ +LocalStack Extensions Utils +=========================== + +A utility library providing common functionality for building [LocalStack Extensions](https://github.com/localstack/localstack-extensions). + +## Usage + +To use this library in your LocalStack extension, add it to the `dependencies` in your extension's `pyproject.toml`: + +```toml +[project] +dependencies = [ + "localstack-extensions-utils", +] +``` + +Or, to install directly from the GitHub repository: + +```toml +[project] +dependencies = [ + "localstack-extensions-utils @ git+https://github.com/localstack/localstack-extensions.git#subdirectory=utils", +] +``` + +Then import the utilities in your extension code, for example: + +```python +from localstack_extensions.utils import ProxiedDockerContainerExtension + +... +``` + +## Dependencies + +This library requires LocalStack to be installed as it uses various LocalStack utilities for Docker management and networking. + +## License + +The code in this repo is available under the Apache 2.0 license. diff --git a/utils/localstack_extensions/__init__.py b/utils/localstack_extensions/__init__.py new file mode 100644 index 0000000..154f76e --- /dev/null +++ b/utils/localstack_extensions/__init__.py @@ -0,0 +1 @@ +# LocalStack Extensions utilities package diff --git a/utils/localstack_extensions/utils/__init__.py b/utils/localstack_extensions/utils/__init__.py new file mode 100644 index 0000000..42a1e62 --- /dev/null +++ b/utils/localstack_extensions/utils/__init__.py @@ -0,0 +1,23 @@ +from localstack_extensions.utils.docker import ( + ProxiedDockerContainerExtension, + ProxyResource, +) +from localstack_extensions.utils.h2_proxy import ( + TcpForwarder, + apply_http2_patches_for_grpc_support, + get_headers_from_data_stream, + get_headers_from_frames, + get_frames_from_http2_stream, + ProxyRequestMatcher, +) + +__all__ = [ + "ProxiedDockerContainerExtension", + "ProxyResource", + "TcpForwarder", + "apply_http2_patches_for_grpc_support", + "get_headers_from_data_stream", + "get_headers_from_frames", + "get_frames_from_http2_stream", + "ProxyRequestMatcher", +] diff --git a/typedb/localstack_typedb/utils/docker.py b/utils/localstack_extensions/utils/docker.py similarity index 94% rename from typedb/localstack_typedb/utils/docker.py rename to utils/localstack_extensions/utils/docker.py index 7a78c60..8050242 100644 --- a/typedb/localstack_typedb/utils/docker.py +++ b/utils/localstack_extensions/utils/docker.py @@ -5,9 +5,8 @@ from typing import Callable import requests -from localstack import config from localstack.config import is_env_true -from localstack_typedb.utils.h2_proxy import ( +from localstack_extensions.utils.h2_proxy import ( apply_http2_patches_for_grpc_support, ) from localstack.utils.docker_utils import DOCKER_CLIENT @@ -22,10 +21,6 @@ from werkzeug.datastructures import Headers LOG = logging.getLogger(__name__) -logging.getLogger("localstack_typedb").setLevel( - logging.DEBUG if config.DEBUG else logging.INFO -) -logging.basicConfig() class ProxiedDockerContainerExtension(Extension): @@ -130,8 +125,8 @@ def start_container(self) -> None: ) except Exception as e: LOG.debug("Failed to start container %s: %s", self.container_name, e) - # allow running TypeDB in a local server in dev mode, if TYPEDB_DEV_MODE is enabled - if not is_env_true("TYPEDB_DEV_MODE"): + # allow running the container in a local server in dev mode + if not is_env_true(f"{self.name.upper().replace('-', '_')}_DEV_MODE"): raise def _ping_endpoint(): diff --git a/typedb/localstack_typedb/utils/h2_proxy.py b/utils/localstack_extensions/utils/h2_proxy.py similarity index 100% rename from typedb/localstack_typedb/utils/h2_proxy.py rename to utils/localstack_extensions/utils/h2_proxy.py diff --git a/utils/pyproject.toml b/utils/pyproject.toml new file mode 100644 index 0000000..a9dbed3 --- /dev/null +++ b/utils/pyproject.toml @@ -0,0 +1,40 @@ +[build-system] +requires = ["setuptools", "wheel", "plux>=1.3.1"] +build-backend = "setuptools.build_meta" + +[project] +name = "localstack-extensions-utils" +version = "0.1.0" +description = "Utility library for LocalStack Extensions" +readme = {file = "README.md", content-type = "text/markdown; charset=UTF-8"} +requires-python = ">=3.10" +authors = [ + { name = "LocalStack Team" } +] +keywords = ["LocalStack", "Extensions", "Utils"] +classifiers = [] +dependencies = [ + "httpx", + "h2", + "hpack", + "hyperframe", + "priority", + "requests", + "rolo", + "twisted", +] + +[project.urls] +Homepage = "https://github.com/localstack/localstack-extensions" + +[project.optional-dependencies] +dev = [ + "boto3", + "build", + "localstack", + "pytest", + "ruff", +] + +[tool.setuptools.packages.find] +include = ["localstack_extensions*"]