nautilus_core/python/
version.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Functions for introspecting the running Python interpreter & installed packages.
17
18#![allow(clippy::manual_let_else)]
19use pyo3::{prelude::*, types::PyTuple};
20
21/// Retrieves the Python interpreter version as a string.
22///
23/// # Panics
24///
25/// Panics if `version_info` cannot be downcast to a tuple or if tuple elements are missing.
26#[must_use]
27pub fn get_python_version() -> String {
28    Python::with_gil(|py| {
29        let sys = match py.import("sys") {
30            Ok(mod_sys) => mod_sys,
31            Err(_) => return "Unavailable (failed to import sys)".to_string(),
32        };
33
34        let version_info = match sys.getattr("version_info") {
35            Ok(info) => info,
36            Err(_) => return "Unavailable (version_info not found)".to_string(),
37        };
38
39        let version_tuple: &Bound<'_, PyTuple> = version_info
40            .downcast::<PyTuple>()
41            .expect("Failed to extract version_info");
42
43        let major = version_tuple
44            .get_item(0)
45            .expect("Failed to get major version")
46            .extract::<i32>()
47            .unwrap_or(-1);
48        let minor = version_tuple
49            .get_item(1)
50            .expect("Failed to get minor version")
51            .extract::<i32>()
52            .unwrap_or(-1);
53        let micro = version_tuple
54            .get_item(2)
55            .expect("Failed to get micro version")
56            .extract::<i32>()
57            .unwrap_or(-1);
58
59        if major == -1 || minor == -1 || micro == -1 {
60            "Unavailable (failed to extract version components)".to_string()
61        } else {
62            format!("{major}.{minor}.{micro}")
63        }
64    })
65}
66
67#[must_use]
68/// Attempt to retrieve the `__version__` attribute of a *Python* package.
69///
70/// When the requested package cannot be imported, or when it does not define a `__version__`
71/// attribute, the function returns a human-readable fallback string that starts with
72/// `"Unavailable"` so that downstream code can distinguish *real* version strings from error
73/// cases.
74///
75/// This helper is primarily intended for diagnostic/logging purposes inside the PoseiTrader
76/// Python bindings.
77pub fn get_python_package_version(package_name: &str) -> String {
78    Python::with_gil(|py| match py.import(package_name) {
79        Ok(package) => match package.getattr("__version__") {
80            Ok(version_attr) => match version_attr.extract::<String>() {
81                Ok(version) => version,
82                Err(_) => "Unavailable (failed to extract version)".to_string(),
83            },
84            Err(_) => "Unavailable (__version__ attribute not found)".to_string(),
85        },
86        Err(_) => "Unavailable (failed to import package)".to_string(),
87    })
88}