From 18dc2584feac75aa369805be5ab2bcd1e391bc50 Mon Sep 17 00:00:00 2001 From: Giovanni Barillari Date: Wed, 24 Jan 2024 15:51:45 +0100 Subject: [PATCH] Use single source of truth for package version --- granian/__init__.py | 1 + granian/__version__.py | 1 - granian/_granian.pyi | 2 ++ granian/cli.py | 2 +- src/lib.rs | 11 +++++++++++ 5 files changed, 15 insertions(+), 2 deletions(-) delete mode 100644 granian/__version__.py diff --git a/granian/__init__.py b/granian/__init__.py index 37ebb755..e8dc2692 100644 --- a/granian/__init__.py +++ b/granian/__init__.py @@ -1 +1,2 @@ +from ._granian import __version__ # noqa from .server import Granian # noqa diff --git a/granian/__version__.py b/granian/__version__.py deleted file mode 100644 index cd7ca498..00000000 --- a/granian/__version__.py +++ /dev/null @@ -1 +0,0 @@ -__version__ = '1.0.1' diff --git a/granian/_granian.pyi b/granian/_granian.pyi index 5ad0d078..43c9002b 100644 --- a/granian/_granian.pyi +++ b/granian/_granian.pyi @@ -2,6 +2,8 @@ from typing import Any, Dict, List, Optional, Tuple from ._types import WebsocketMessage +__version__: str + class ASGIScope: def as_dict(self, root_path: str) -> Dict[str, Any]: ... diff --git a/granian/cli.py b/granian/cli.py index cda0aa6f..0cabb8c6 100644 --- a/granian/cli.py +++ b/granian/cli.py @@ -4,7 +4,7 @@ import typer -from .__version__ import __version__ +from . import __version__ from .constants import HTTPModes, Interfaces, Loops, ThreadModes from .http import HTTP1Settings, HTTP2Settings from .log import LogLevels diff --git a/src/lib.rs b/src/lib.rs index 335ebd9d..58d8295c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; use pyo3::prelude::*; +use std::sync::OnceLock; mod asgi; mod callbacks; @@ -17,8 +18,18 @@ mod workers; mod ws; mod wsgi; +pub fn get_granian_version() -> &'static str { + static GRANIAN_VERSION: OnceLock = OnceLock::new(); + + GRANIAN_VERSION.get_or_init(|| { + let version = env!("CARGO_PKG_VERSION"); + version.replace("-alpha", "a").replace("-beta", "b") + }) +} + #[pymodule] fn _granian(py: Python, module: &PyModule) -> PyResult<()> { + module.add("__version__", get_granian_version())?; asgi::init_pymodule(module)?; rsgi::init_pymodule(py, module)?; tcp::init_pymodule(module)?;