From 4b20490ee60de3b2928600972a369dd43e5dd8bb Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Mon, 25 Nov 2024 21:43:50 +0100 Subject: [PATCH 1/2] Add a sysinspect result (initial) --- libsysinspect/src/pylang/pylib/pysystem.rs | 84 +++++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/libsysinspect/src/pylang/pylib/pysystem.rs b/libsysinspect/src/pylang/pylib/pysystem.rs index bdf12c8..fc1584a 100644 --- a/libsysinspect/src/pylang/pylib/pysystem.rs +++ b/libsysinspect/src/pylang/pylib/pysystem.rs @@ -10,8 +10,14 @@ pub mod syscore { traits::{self, systraits::SystemTraits}, util::dataconv, }; - use rustpython_vm::PyResult; - use rustpython_vm::{builtins::PyList, convert::ToPyObject, pyclass, PyObjectRef, PyPayload, VirtualMachine}; + use rustpython_vm::{ + builtins::{PyDict, PyList}, + common::lock::PyMutex, + convert::ToPyObject, + pyclass, PyObjectRef, PyPayload, PyRef, VirtualMachine, + }; + use rustpython_vm::{AsObject, PyResult}; + use std::collections::HashMap; #[derive(Debug, Clone)] struct StrVec(Vec); @@ -63,4 +69,78 @@ pub mod syscore { fn MinionTraits() -> PyResult { Ok(MinionTraits::new()) } + + #[derive(Debug)] + struct Inner { + retcode: usize, + warnings: Vec, + message: String, + data: PyRef, + } + + #[pyattr] + #[pyclass(module = "syscore", name = "__SysinspectReturn")] + #[derive(Debug, PyPayload)] + pub struct SysinspectReturn { + inner: PyMutex, + } + + #[pyclass] + impl SysinspectReturn { + fn new(_vm: &VirtualMachine) -> SysinspectReturn { + SysinspectReturn { + inner: PyMutex::new(Inner { retcode: 0, data: _vm.ctx.new_dict(), warnings: vec![], message: "".to_string() }), + } + } + + #[pygetset] + fn retcode(&self) -> usize { + self.inner.lock().retcode + } + + #[pymethod] + fn set_retcode(&self, retcode: usize, _vm: &VirtualMachine) -> PyObjectRef { + self.inner.lock().retcode = retcode; + _vm.ctx.none() + } + + #[pygetset] + fn message(&self) -> String { + self.inner.lock().message.to_owned() + } + + #[pymethod] + fn set_message(&self, message: String) { + self.inner.lock().message = message + } + + #[pygetset] + fn warnings(&self, _vm: &VirtualMachine) -> PyObjectRef { + let list = self.inner.lock().warnings.iter().map(|e| _vm.new_pyobj(e)).collect(); + PyList::new_ref(list, _vm.as_ref()).to_pyobject(_vm) + } + + #[pymethod] + fn add_warning(&self, warn: String, _vm: &VirtualMachine) { + self.inner.lock().warnings.push(warn); + } + + #[pygetset] + fn data(&self) -> PyRef { + self.inner.lock().data.to_owned() + } + + #[pymethod] + fn set_data(&self, data: PyRef) { + self.inner.lock().data = data; + } + } + + #[pyfunction] + #[allow(non_snake_case)] + /// This is a mimic of "MinionTraits" Python class, + /// which needs to be called for the init. + fn SysinspectReturn(_vm: &VirtualMachine) -> PyResult { + Ok(SysinspectReturn::new(_vm)) + } } From 21c8f85d01ade32e0e8ca42081470f30a51b0941 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Mon, 25 Nov 2024 21:44:20 +0100 Subject: [PATCH 2/2] Fix imports --- libsysinspect/src/pylang/pylib/pysystem.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libsysinspect/src/pylang/pylib/pysystem.rs b/libsysinspect/src/pylang/pylib/pysystem.rs index fc1584a..e106cd1 100644 --- a/libsysinspect/src/pylang/pylib/pysystem.rs +++ b/libsysinspect/src/pylang/pylib/pysystem.rs @@ -10,14 +10,13 @@ pub mod syscore { traits::{self, systraits::SystemTraits}, util::dataconv, }; + use rustpython_vm::PyResult; use rustpython_vm::{ builtins::{PyDict, PyList}, common::lock::PyMutex, convert::ToPyObject, pyclass, PyObjectRef, PyPayload, PyRef, VirtualMachine, }; - use rustpython_vm::{AsObject, PyResult}; - use std::collections::HashMap; #[derive(Debug, Clone)] struct StrVec(Vec);