diff --git a/sdk/cosmos/azure-cosmos/.gitignore b/sdk/cosmos/azure-cosmos/.gitignore new file mode 100644 index 000000000000..6e2fb1273a02 --- /dev/null +++ b/sdk/cosmos/azure-cosmos/.gitignore @@ -0,0 +1,2 @@ +/target/ +/azure/cosmos/_rust*.so diff --git a/sdk/cosmos/azure-cosmos/Cargo.lock b/sdk/cosmos/azure-cosmos/Cargo.lock new file mode 100644 index 000000000000..82e2e9c896c5 --- /dev/null +++ b/sdk/cosmos/azure-cosmos/Cargo.lock @@ -0,0 +1,212 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "azure_cosmos_rust" +version = "0.1.0" +dependencies = [ + "pyo3", +] + +[[package]] +name = "cc" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89588d05638b5b4594a3348a2d6c20277e43a7f5c5202b05cc56888475a47b8" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "find-msvc-tools" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "indoc" +version = "2.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706" +dependencies = [ + "rustversion", +] + +[[package]] +name = "libc" +version = "0.2.186" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" + +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "portable-atomic" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "pyo3" +version = "0.22.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f402062616ab18202ae8319da13fa4279883a2b8a9d9f83f20dbade813ce1884" +dependencies = [ + "cfg-if", + "indoc", + "libc", + "memoffset", + "once_cell", + "portable-atomic", + "pyo3-build-config", + "pyo3-ffi", + "pyo3-macros", + "unindent", +] + +[[package]] +name = "pyo3-build-config" +version = "0.22.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b14b5775b5ff446dd1056212d778012cbe8a0fbffd368029fd9e25b514479c38" +dependencies = [ + "once_cell", + "python3-dll-a", + "target-lexicon", +] + +[[package]] +name = "pyo3-ffi" +version = "0.22.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ab5bcf04a2cdcbb50c7d6105de943f543f9ed92af55818fd17b660390fc8636" +dependencies = [ + "libc", + "pyo3-build-config", +] + +[[package]] +name = "pyo3-macros" +version = "0.22.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fd24d897903a9e6d80b968368a34e1525aeb719d568dba8b3d4bfa5dc67d453" +dependencies = [ + "proc-macro2", + "pyo3-macros-backend", + "quote", + "syn", +] + +[[package]] +name = "pyo3-macros-backend" +version = "0.22.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36c011a03ba1e50152b4b394b479826cad97e7a21eb52df179cd91ac411cbfbe" +dependencies = [ + "heck", + "proc-macro2", + "pyo3-build-config", + "quote", + "syn", +] + +[[package]] +name = "python3-dll-a" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d80ba7540edb18890d444c5aa8e1f1f99b1bdf26fb26ae383135325f4a36042b" +dependencies = [ + "cc", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "target-lexicon" +version = "0.12.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "unindent" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3" diff --git a/sdk/cosmos/azure-cosmos/Cargo.toml b/sdk/cosmos/azure-cosmos/Cargo.toml new file mode 100644 index 000000000000..02050ea25062 --- /dev/null +++ b/sdk/cosmos/azure-cosmos/Cargo.toml @@ -0,0 +1,16 @@ +[workspace] +members = ["azure_cosmos_rust"] +resolver = "2" + +[workspace.package] +authors = ["Microsoft "] +edition = "2021" +license = "MIT" +repository = "https://github.com/Azure/azure-sdk-for-python" +rust-version = "1.97" + +[workspace.lints.rust] +unsafe_code = "forbid" + +[workspace.dependencies] +pyo3 = "0.22" diff --git a/sdk/cosmos/azure-cosmos/azure_cosmos_rust/Cargo.toml b/sdk/cosmos/azure-cosmos/azure_cosmos_rust/Cargo.toml new file mode 100644 index 000000000000..e2eead3ea80c --- /dev/null +++ b/sdk/cosmos/azure-cosmos/azure_cosmos_rust/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "azure_cosmos_rust" +version = "0.1.0" +description = "Compiled extension for the Azure Cosmos DB Python SDK." +authors.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true +rust-version.workspace = true +publish = false + +[lints] +workspace = true + +[lib] +crate-type = ["cdylib"] + +[dependencies] +pyo3 = { workspace = true, features = ["extension-module", "abi3-py310", "generate-import-lib"] } diff --git a/sdk/cosmos/azure-cosmos/azure_cosmos_rust/src/lib.rs b/sdk/cosmos/azure-cosmos/azure_cosmos_rust/src/lib.rs new file mode 100644 index 000000000000..dad8989ffdfe --- /dev/null +++ b/sdk/cosmos/azure-cosmos/azure_cosmos_rust/src/lib.rs @@ -0,0 +1,9 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +use pyo3::prelude::*; + +#[pymodule] +fn _rust(_module: &Bound<'_, PyModule>) -> PyResult<()> { + Ok(()) +} diff --git a/sdk/cosmos/azure-cosmos/pyproject.toml b/sdk/cosmos/azure-cosmos/pyproject.toml index 52c35fbc61c2..ce5b966133d7 100644 --- a/sdk/cosmos/azure-cosmos/pyproject.toml +++ b/sdk/cosmos/azure-cosmos/pyproject.toml @@ -1,3 +1,87 @@ +[build-system] +requires = ["maturin==1.15.0"] +build-backend = "maturin" + +[project] +name = "azure-cosmos" +version = "4.17.2" +description = "Microsoft Azure Cosmos Client Library for Python" +readme = "README.md" +# Target Python 3.10 now because the V5 Python code being migrated in later PRs +# uses the dataclass options kw_only and slots, both introduced in Python 3.10. +requires-python = ">=3.10" +license = { text = "MIT License" } +authors = [{ name = "Microsoft Corporation", email = "askdocdb@microsoft.com" }] +maintainers = [{ name = "Microsoft", email = "askdocdb@microsoft.com" }] +keywords = ["azure", "azure sdk"] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Intended Audience :: Developers", + "Natural Language :: English", + "Programming Language :: Python", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "License :: OSI Approved :: MIT License", +] +dependencies = ["azure-core>=1.30.0", "typing-extensions>=4.6.0"] + +[project.optional-dependencies] +aio = ["azure-core[aio]>=1.30.0"] + +[project.urls] +Homepage = "https://github.com/Azure/azure-sdk-for-python" + +[tool.maturin] +manifest-path = "azure_cosmos_rust/Cargo.toml" +module-name = "azure.cosmos._rust" +python-source = "." +locked = true +include = [ + { path = "LICENSE", format = "sdist" }, + { path = "rust-toolchain.toml", format = "sdist" }, + { path = "scripts/*.sh", format = "sdist" }, + { path = "samples/**/*", format = "sdist" }, + { path = "tests/**/*", format = "sdist" }, + { path = "doc/**/*.rst", format = "sdist" }, + { path = "*.md", format = "sdist" }, +] +exclude = [ + { path = "azure/__init__.py", format = "wheel" }, + "**/__pycache__/**", + "**/*.py[co]", +] + +[tool.cibuildwheel] +build = ["cp310-*"] +skip = ["*-musllinux*"] +test-command = "python -I -c \"from azure.cosmos import CosmosClient, _rust; from importlib.machinery import EXTENSION_SUFFIXES; assert _rust.__file__.endswith(tuple(EXTENSION_SUFFIXES))\"" + +[tool.cibuildwheel.windows] +archs = ["AMD64"] + +[tool.cibuildwheel.macos] +archs = ["arm64"] +environment = { PATH = "$HOME/.msrustup/multiplexers/bin:$PATH" } + +[tool.cibuildwheel.linux] +archs = ["x86_64"] +manylinux-x86_64-image = "manylinux_2_28" +environment-pass = [ + "PIP_INDEX_URL", + "MSRUSTUP_ACCESS_TOKEN", + "MSRUSTUP_PAT", + "MSRUSTUP_FEED_URL", + "CARGO_REGISTRIES_AZURE_SDK_FOR_RUST_PUBLIC_INDEX", + "CARGO_REGISTRIES_AZURE_SDK_FOR_RUST_PUBLIC_TOKEN", + "CARGO_REGISTRIES_AZURE_SDK_FOR_RUST_PUBLIC_CREDENTIAL_PROVIDER", +] +environment = { MSRUSTUP_HOME = "/msrustup", CARGO_HOME = "/cargo", PATH = "/msrustup/multiplexers/bin:$PATH" } +before-all = "dnf install -y jq && mkdir -p $MSRUSTUP_HOME/bootstrap && cd $MSRUSTUP_HOME/bootstrap && sh /project/scripts/install-msrustup.sh && cd /project && $MSRUSTUP_HOME/bootstrap/msrustup toolchain install && sh /project/scripts/configure-cargo-feed.sh" + [tool.azure-sdk-build] mypy = true pyright = false diff --git a/sdk/cosmos/azure-cosmos/rust-toolchain.toml b/sdk/cosmos/azure-cosmos/rust-toolchain.toml new file mode 100644 index 000000000000..ea4a76a98c25 --- /dev/null +++ b/sdk/cosmos/azure-cosmos/rust-toolchain.toml @@ -0,0 +1,4 @@ +# Microsoft toolchains require msrustup rather than public rustup. +[toolchain] +channel = "ms-prod-1.97" +profile = "minimal" diff --git a/sdk/cosmos/azure-cosmos/scripts/configure-cargo-feed.sh b/sdk/cosmos/azure-cosmos/scripts/configure-cargo-feed.sh new file mode 100644 index 000000000000..697dfa5b5119 --- /dev/null +++ b/sdk/cosmos/azure-cosmos/scripts/configure-cargo-feed.sh @@ -0,0 +1,31 @@ +#!/bin/sh +# Point cargo inside the manylinux container at the authenticated pipeline feed. +set -eu + +REGISTRY_NAME=azure-sdk-for-rust-public +INDEX="${CARGO_REGISTRIES_AZURE_SDK_FOR_RUST_PUBLIC_INDEX:-}" + +if [ -z "$INDEX" ]; then + echo "No cargo registry index forwarded, leaving crates.io source unchanged." + exit 0 +fi + +if [ -z "${CARGO_REGISTRIES_AZURE_SDK_FOR_RUST_PUBLIC_TOKEN:-}" ]; then + echo "Cargo registry index is set but its token is missing; the feed requires authentication." >&2 + exit 1 +fi + +CARGO_CONFIG_HOME="${CARGO_HOME:-$HOME/.cargo}" +mkdir -p "$CARGO_CONFIG_HOME" + +# Cargo reads the registry index and credentials from the environment, but +# crates.io source replacement must be specified in a configuration file. +cat > "$CARGO_CONFIG_HOME/config.toml" </dev/null 2>&1 || { echo >&2 "install-msrustup requires uname to detect host."; exit 1; } +command -v curl >/dev/null 2>&1 || { echo >&2 "install-msrustup requires curl to download msrustup."; exit 1; } +command -v jq >/dev/null 2>&1 || { echo >&2 "install-msrustup requires jq to parse Azure Artifact response."; exit 1; } +command -v unzip >/dev/null 2>&1 || { echo >&2 "install-msrustup requires unzip to unzip msrustup."; exit 1; } + +if [ -z "$MSRUSTUP_ACCESS_TOKEN" ] && [ -z "$MSRUSTUP_PAT" ]; then + if $(command -v azureauth >/dev/null 2>&1); then + MSRUSTUP_ACCESS_TOKEN=$(azureauth ado token) + elif $(command -v azureauth.exe >/dev/null 2>&1); then + MSRUSTUP_ACCESS_TOKEN=$(azureauth.exe ado token) + else + echo "MSRUSTUP_ACCESS_TOKEN or MSRUSTUP_PAT must be set or azureauth must be present." + exit 1 + fi +fi + +if [ -z "$MSRUSTUP_ACCESS_TOKEN" ]; then + accessMethod=pat +else + accessMethod=token +fi + +if [ -z "$MSRUSTUP_FEED_URL" ]; then + MSRUSTUP_FEED_URL='https://devdiv.pkgs.visualstudio.com/DevDiv/_packaging/Rust.Sdk%40Release/nuget/v3/index.json' +fi + +# Now that we've tested for missing required variables, treat unset variables as an error. +set -eu + +cleanup() { + rm -f msrustup.zip +} +trap cleanup EXIT + +do_curl() { + if [ "$accessMethod" = "token" ]; then + curl -sSfLH "Authorization: Bearer $MSRUSTUP_ACCESS_TOKEN" --retry 5 $@ + else + curl -sSfLu :$MSRUSTUP_PAT --retry 5 $@ + fi +} + +target_arch='' +target_rest='' + +# We intentionally use the msvc host toolchain for variations of Windows (CYGWIN, MINGW, etc) +# since no other Windows host toolchain is supported. +unameOut="$(uname -s)" +case "${unameOut}" in + Linux*) target_rest="-unknown-linux-gnu";; + Darwin*) target_rest="-apple-darwin";; + CYGWIN*) target_rest="-pc-windows-msvc";; + MINGW*) target_rest="-pc-windows-msvc";; + MSYS_NT*) target_rest="-pc-windows-msvc";; + *) { echo "host environment could not be determined: ${unameOut}"; exit 1; } +esac + +# Detect x86_64 or aarch64 (Apple devices report as "arm64", Linux as "aarch64" usually). +arch="$(uname -m)" +case "${arch}" in + x86_64*) target_arch="x86_64";; + aarch64*) target_arch="aarch64";; + arm64*) target_arch="aarch64";; + *) { echo "unknown host arch: ${arch}"; exit 1; } +esac + +echo "Host is ${target_arch}${target_rest}" +package="rust.msrustup-${target_arch}${target_rest}" + +response=$(do_curl $MSRUSTUP_FEED_URL) +base=$(echo $response | jq -r '.resources[] | select(."@type"=="PackageBaseAddress/3.0.0") | .["@id"]') +version=$(do_curl "$base$package/index.json" | jq -r '.versions[0]') +latest="${base}${package}/$version/$package.$version.nupkg" + +echo "Downloading msrustup $version from $latest" +do_curl "$latest" -o msrustup.zip + +if [ "$target_rest" = "-pc-windows-msvc" ]; then + unzip -jqo msrustup.zip tools/msrustup.exe +else + unzip -jqo msrustup.zip tools/msrustup + chmod +x msrustup +fi diff --git a/sdk/cosmos/azure-cosmos/setup.py b/sdk/cosmos/azure-cosmos/setup.py index 1d9aaa9726a0..5af65a105416 100644 --- a/sdk/cosmos/azure-cosmos/setup.py +++ b/sdk/cosmos/azure-cosmos/setup.py @@ -9,7 +9,18 @@ import re import os from io import open -from setuptools import find_packages, setup +from setuptools import Distribution, find_packages, setup +from setuptools.errors import SetupError + + +class MetadataOnlyDistribution(Distribution): + """Keep legacy metadata commands without producing wheels missing the Rust extension.""" + + def run_commands(self): + unsupported = set(self.commands) - {"egg_info", "dist_info"} + if unsupported: + raise SetupError("setup.py is metadata-only; use python -m build to build the package.") + super().run_commands() # Change the PACKAGE_NAME only to change folder and different name PACKAGE_NAME = "azure-cosmos" @@ -41,6 +52,7 @@ ] setup( + distclass=MetadataOnlyDistribution, name=PACKAGE_NAME, version=version, include_package_data=True, @@ -58,11 +70,9 @@ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "Natural Language :: English", - "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3 :: Only", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", @@ -75,7 +85,7 @@ "azure.cosmos": ["py.typed"], "azure.cosmos._query_advisor": ["query_advice_rules.json"], }, - python_requires=">=3.9", + python_requires=">=3.10", install_requires=[ "azure-core>=1.30.0", "typing-extensions>=4.6.0" diff --git a/sdk/cosmos/ci.yml b/sdk/cosmos/ci.yml index 9565a7aeb76e..b9a8b9683cf3 100644 --- a/sdk/cosmos/ci.yml +++ b/sdk/cosmos/ci.yml @@ -28,6 +28,9 @@ extends: template: /eng/pipelines/templates/stages/cosmos-sdk-client.yml parameters: ServiceDirectory: cosmos + InstallMsRustToolchain: true + MsRustWorkingDirectory: $(Build.SourcesDirectory)/sdk/cosmos/azure-cosmos + MsRustToolchainFeed: https://pkgs.dev.azure.com/azure-sdk/_packaging/azure-sdk-for-rust-org-test/nuget/v3/index.json Artifacts: - name: azure-cosmos safeName: azurecosmos