code formatting

This commit is contained in:
Caelan Sayler
2025-06-04 16:03:10 +01:00
committed by Caelan
parent aff7ade8a9
commit 23661f7d6b
5 changed files with 110 additions and 146 deletions

View File

@@ -3,28 +3,8 @@ use std::fs;
use std::path::Path;
fn main() {
// Get the workspace version
let version = get_workspace_version().unwrap_or_else(|| {
env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "0.1.0".to_string())
});
let version = get_workspace_version().unwrap_or_else(|| env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "0.1.0".to_string()));
let python_version = convert_to_python_version(&version);
@@ -44,8 +24,8 @@ fn get_workspace_version() -> Option<String> {
// Navigate up to workspace root and read Cargo.toml
let manifest_dir = env::var("CARGO_MANIFEST_DIR").ok()?;
let workspace_toml = Path::new(&manifest_dir)
.parent()? // src
.parent()? // velopack root
.parent()? // src
.parent()? // velopack root
.join("Cargo.toml");
if !workspace_toml.exists() {
@@ -65,8 +45,7 @@ fn get_workspace_version() -> Option<String> {
}
if trimmed.starts_with('[') && trimmed != "[workspace.package]" {
if in_workspace_package {
}
if in_workspace_package {}
in_workspace_package = false;
continue;
}

View File

@@ -85,8 +85,7 @@ impl VelopackAppWrapper {
/// Runs the Velopack startup logic
pub fn run(&mut self, _py: Python) -> PyResult<()> {
// Create the Rust VelopackApp with our stored configuration
let mut app = VelopackAppRust::build()
.set_auto_apply_on_startup(self.auto_apply);
let mut app = VelopackAppRust::build().set_auto_apply_on_startup(self.auto_apply);
// Set args if provided
if let Some(ref args) = self.args {
@@ -175,4 +174,3 @@ impl VelopackAppWrapper {
Ok(())
}
}

View File

@@ -1,5 +1,5 @@
use pyo3::prelude::*;
use pyo3::exceptions::PyException;
use pyo3::prelude::*;
#[pyclass(name="VelopackError", extends=PyException, module="velopack.exceptions")]
#[derive(Debug)]
@@ -18,4 +18,3 @@ impl VelopackError {
self.message.clone()
}
}

View File

@@ -1,38 +1,35 @@
use asset::PyUpdateInfo;
use pyo3::prelude::*;
use pyo3::types::PyModule;
mod asset;
pub use asset::PyVelopackAsset;
mod types;
use types::*;
mod exceptions;
pub use exceptions::VelopackError;
use exceptions::VelopackError;
mod app;
pub use app::VelopackAppWrapper;
use app::VelopackAppWrapper;
mod manager;
pub use manager::UpdateManagerWrapper;
use manager::UpdateManagerWrapper;
#[pymodule]
fn velopack(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<VelopackError>()?;
// auto-generated DTO's
m.add_class::<PyVelopackAsset>()?;
m.add_class::<PyUpdateInfo>()?;
m.add_class::<PyUpdateOptions>()?;
m.add_class::<PyVelopackLocatorConfig>()?;
// concrete classes
m.add_class::<VelopackAppWrapper>()?;
m.add_class::<UpdateManagerWrapper>()?;
// add __version__ attribute
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
// add __author__ attribute
m.add("__author__", env!("CARGO_PKG_AUTHORS"))?;

View File

@@ -1,13 +1,12 @@
use pyo3::prelude::*;
use std::sync::mpsc;
use std::thread;
use pyo3::prelude::*;
use velopack::{UpdateCheck, UpdateInfo, UpdateManager as VelopackUpdateManagerRust};
use velopack::sources::AutoSource;
use velopack::{UpdateCheck, UpdateInfo, UpdateManager as VelopackUpdateManagerRust};
use crate::exceptions::VelopackError;
use crate::asset::PyUpdateInfo;
use crate::types::*;
#[pyclass(name = "UpdateManager")]
pub struct UpdateManagerWrapper {
@@ -17,88 +16,80 @@ pub struct UpdateManagerWrapper {
#[pymethods]
impl UpdateManagerWrapper {
// for new, just take in a string, which is the source
#[new]
pub fn new(source: String) -> PyResult<Self> {
let source = AutoSource::new(&source);
// set myinner to a new VelopackUpdateManager with the source
let inner = VelopackUpdateManagerRust::new(source, None, None)
.map_err(|e| PyErr::new::<VelopackError, _>(format!("Failed to create UpdateManager: {}", e)))?;
Ok(UpdateManagerWrapper {
inner,
}
)
}
#[new]
#[pyo3(signature = (source, options = None, locator = None))]
pub fn new(source: String, options: Option<PyUpdateOptions>, locator: Option<PyVelopackLocatorConfig>) -> PyResult<Self> {
let source = AutoSource::new(&source);
// set myinner to a new VelopackUpdateManager with the source
let inner = VelopackUpdateManagerRust::new(source, options.map(Into::into), locator.map(Into::into))
.map_err(|e| PyErr::new::<VelopackError, _>(format!("Failed to create UpdateManager: {}", e)))?;
Ok(UpdateManagerWrapper { inner })
}
// check_for_updates return update info indicating if updates are available
// check_for_updates return update info indicating if updates are available
/// This method checks for updates and returns update info if updates are available, None otherwise.
pub fn check_for_updates(&mut self) -> PyResult<Option<PyUpdateInfo>> {
let update_check = self.inner.check_for_updates()
.map_err(|e| PyErr::new::<VelopackError, _>(format!("Failed to check for updates: {}", e)))?;
pub fn check_for_updates(&mut self) -> PyResult<Option<PyUpdateInfo>> {
let update_check =
self.inner.check_for_updates().map_err(|e| PyErr::new::<VelopackError, _>(format!("Failed to check for updates: {}", e)))?;
match update_check {
UpdateCheck::UpdateAvailable(updates) => {
let py_updates = PyUpdateInfo::from(updates);
Ok(Some(py_updates))
},
UpdateCheck::NoUpdateAvailable => {
Ok(None)
},
UpdateCheck::RemoteIsEmpty => {
Ok(None)
match update_check {
UpdateCheck::UpdateAvailable(updates) => {
let py_updates = PyUpdateInfo::from(updates);
Ok(Some(py_updates))
}
UpdateCheck::NoUpdateAvailable => Ok(None),
UpdateCheck::RemoteIsEmpty => Ok(None),
}
}
}
#[pyo3(signature = (update_info, progress_callback = None))]
pub fn download_updates(&mut self, update_info: &PyUpdateInfo, progress_callback: Option<PyObject>) -> PyResult<()> {
// Convert PyUpdateInfo back to rust UpdateInfo
let rust_update_info: UpdateInfo = update_info.clone().into();
#[pyo3(signature = (update_info, progress_callback = None))]
pub fn download_updates(&mut self, update_info: &PyUpdateInfo, progress_callback: Option<PyObject>) -> PyResult<()> {
// Convert PyUpdateInfo back to rust UpdateInfo
let rust_update_info: UpdateInfo = update_info.clone().into();
if let Some(callback) = progress_callback {
// Create a channel for progress updates
let (sender, receiver) = mpsc::channel::<i16>();
if let Some(callback) = progress_callback {
// Create a channel for progress updates
let (sender, receiver) = mpsc::channel::<i16>();
// Spawn a thread to handle progress updates
let progress_thread = thread::spawn(move || {
Python::with_gil(|py| {
while let Ok(progress) = receiver.recv() {
if let Err(e) = callback.call1(py, (progress,)) {
// Log error but continue - don't break the download
eprintln!("Progress callback error: {}", e);
break;
// Spawn a thread to handle progress updates
let progress_thread = thread::spawn(move || {
Python::with_gil(|py| {
while let Ok(progress) = receiver.recv() {
if let Err(e) = callback.call1(py, (progress,)) {
// Log error but continue - don't break the download
eprintln!("Progress callback error: {}", e);
break;
}
}
}
});
});
});
// Call download with the sender
let result = self.inner.download_updates(&rust_update_info, Some(sender))
.map_err(|e| PyErr::new::<VelopackError, _>(format!("Failed to download updates: {}", e)));
// Call download with the sender
let result = self
.inner
.download_updates(&rust_update_info, Some(sender))
.map_err(|e| PyErr::new::<VelopackError, _>(format!("Failed to download updates: {}", e)));
// Wait for the progress thread to finish
let _ = progress_thread.join();
// Wait for the progress thread to finish
let _ = progress_thread.join();
result.map(|_| ())
} else {
// No progress callback provided
self.inner.download_updates(&rust_update_info, None)
.map_err(|e| PyErr::new::<VelopackError, _>(format!("Failed to download updates: {}", e)))
result.map(|_| ())
} else {
// No progress callback provided
self.inner
.download_updates(&rust_update_info, None)
.map_err(|e| PyErr::new::<VelopackError, _>(format!("Failed to download updates: {}", e)))
.map(|_| ())
}
}
pub fn apply_updates_and_restart(&mut self, update_info: &PyUpdateInfo) -> PyResult<()> {
// Convert PyUpdateInfo back to rust UpdateInfo
let rust_update_info: UpdateInfo = update_info.clone().into();
self.inner
.apply_updates_and_restart(&rust_update_info)
.map_err(|e| PyErr::new::<VelopackError, _>(format!("Failed to apply updates and restart: {}", e)))
.map(|_| ())
}
}
pub fn apply_updates_and_restart(&mut self, update_info: &PyUpdateInfo) -> PyResult<()> {
// Convert PyUpdateInfo back to rust UpdateInfo
let rust_update_info: UpdateInfo = update_info.clone().into();
self.inner.apply_updates_and_restart(&rust_update_info)
.map_err(|e| PyErr::new::<VelopackError, _>(format!("Failed to apply updates and restart: {}", e)))
.map(|_| ())
}
}