Unverified Commit 2cc7c24b authored by Burak's avatar Burak Committed by GitHub
Browse files

Python: Use Maturin to build SDKs (#2025)

* Remove `lib` prefix from generated module names

* Build Pokemon service with `Maturin`

* Fix Maturin build on CI

* Generate minimal `pyproject.toml` for generated SDKs to build from source using Maturin

* Fix `ktlint` issues

* Bring back type stubs for Pokemon service

* Update instructions for Lambda

* Make `build-wheel` and `build-wheel-release` to depend on `codegen`
parent 40245a15
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@

package software.amazon.smithy.rust.codegen.server.python.smithy.customizations

import com.moandjiezana.toml.TomlWriter
import software.amazon.smithy.model.neighbor.Walker
import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
@@ -103,6 +104,30 @@ class PubUsePythonTypesDecorator : RustCodegenDecorator<ServerProtocolGenerator,
        clazz.isAssignableFrom(ServerCodegenContext::class.java)
}

/**
 * Generates `pyproject.toml` for the crate.
 *  - Configures Maturin as the build system
 */
class PyProjectTomlDecorator : RustCodegenDecorator<ServerProtocolGenerator, ServerCodegenContext> {
    override val name: String = "PyProjectTomlDecorator"
    override val order: Byte = 0

    override fun extras(codegenContext: ServerCodegenContext, rustCrate: RustCrate) {
        rustCrate.withFile("pyproject.toml") {
            val config = mapOf(
                "build-system" to listOfNotNull(
                    "requires" to listOfNotNull("maturin>=0.14,<0.15"),
                    "build-backend" to "maturin",
                ).toMap(),
            )
            writeWithNoFormatting(TomlWriter().write(config))
        }
    }

    override fun supportsCodegenContext(clazz: Class<out CodegenContext>): Boolean =
        clazz.isAssignableFrom(ServerCodegenContext::class.java)
}

val DECORATORS = listOf(
    /**
     * Add the [InternalServerError] error to all operations.
@@ -115,4 +140,6 @@ val DECORATORS = listOf(
    PubUsePythonTypesDecorator(),
    // Render the Python shared library export.
    PythonExportModuleDecorator(),
    // Generate `pyproject.toml` for the crate.
    PyProjectTomlDecorator(),
)
+1 −2
Original line number Diff line number Diff line
@@ -67,7 +67,7 @@ class PythonApplicationGenerator(
    private val operations: List<OperationShape>,
) {
    private val symbolProvider = codegenContext.symbolProvider
    private val libName = "lib${codegenContext.settings.moduleName.toSnakeCase()}"
    private val libName = codegenContext.settings.moduleName.toSnakeCase()
    private val runtimeConfig = codegenContext.runtimeConfig
    private val service = codegenContext.serviceShape
    private val serviceName = service.id.name.toPascalCase()
@@ -278,7 +278,6 @@ class PythonApplicationGenerator(
                    self.run_server(py, address, port, backlog, workers, tls)
                }
                /// Lambda entrypoint: start the server on Lambda.
                ##[cfg(feature = "aws-lambda")]
                ##[pyo3(text_signature = "(${'$'}self)")]
                pub fn run_lambda(
                    &mut self,
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ class PythonServerModuleGenerator(
        "pyo3" to PythonServerCargoDependency.PyO3.toType(),
    )
    private val symbolProvider = codegenContext.symbolProvider
    private val libName = "lib${codegenContext.settings.moduleName.toSnakeCase()}"
    private val libName = codegenContext.settings.moduleName.toSnakeCase()

    fun render() {
        rustCrate.withModule(
+16 −13
Original line number Diff line number Diff line
@@ -13,8 +13,8 @@ instead of the [Hyper](https://hyper.rs/) HTTP server.
In your `app.py`:

```diff
from libpokemon_service_server_sdk import App
from libpokemon_service_server_sdk.error import ResourceNotFoundException
from pokemon_service_server_sdk import App
from pokemon_service_server_sdk.error import ResourceNotFoundException

# ...

@@ -44,19 +44,19 @@ FROM public.ecr.aws/lambda/python:3.8-x86_64

# Copy your application code to `LAMBDA_TASK_ROOT`
COPY app.py ${LAMBDA_TASK_ROOT}
# When you build your Server SDK for your service you get a shared library
# that is importable in Python. You need to copy that shared library to same folder
# with your application code, so it can be imported by your application.
# Note that you need to build your library for Linux,
# if you are on a different platform you can consult to

# When you build your Server SDK for your service, you will get a Python wheel.
# You just need to copy that wheel and install it via `pip` inside your image.
# Note that you need to build your library for Linux, and Python version used to
# build your SDK should match with your image's Python version.
# For cross compiling, you can consult to:
# https://pyo3.rs/latest/building_and_distribution.html#cross-compiling
# for cross compiling.
COPY lib_pokemon_service_server_sdk.so ${LAMBDA_TASK_ROOT}
COPY wheels/ ${LAMBDA_TASK_ROOT}/wheels
RUN pip3 install ${LAMBDA_TASK_ROOT}/wheels/*.whl

# You can install your application's dependencies using file `requirements.txt`
# from your project folder, if you have any.
# COPY requirements.txt  .
# RUN  pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
# You can install your application's other dependencies listed in `requirements.txt`.
COPY requirements.txt .
RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"

# Create a symlink for your application's entrypoint,
# so we can use `/app.py` to refer it
@@ -69,6 +69,9 @@ ENTRYPOINT [ "/var/lang/bin/python3.8" ]
CMD [ "/app.py" ]
```

See [https://docs.aws.amazon.com/lambda/latest/dg/images-create.html#images-create-from-base](https://docs.aws.amazon.com/lambda/latest/dg/images-create.html#images-create-from-base) 
for more details on building your custom image.

<!-- anchor_start:footer -->
This crate is part of the [AWS SDK for Rust](https://awslabs.github.io/aws-sdk-rust/) and the [smithy-rs](https://github.com/awslabs/smithy-rs) code generator. In most cases, it should not be used directly.
<!-- anchor_end:footer -->
+0 −12
Original line number Diff line number Diff line
# See: https://pyo3.rs/latest/building_and_distribution.html?highlight=rustflags#macos
[target.x86_64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]

[target.aarch64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]
Loading