add firmware reader

This commit is contained in:
Pakin 2025-09-08 10:45:36 +07:00
parent 4af7cabf73
commit 95c57a6333
6 changed files with 982 additions and 5 deletions

898
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -6,6 +6,11 @@ edition = "2024"
[dependencies]
chrono = "0.4.41"
log = "0.4.27"
mlua = "0.11.3"
rand = "0.9.2"
rayon = "1.10.0"
serde = { version = "1.0.219", features = ["derive", "serde_derive"] }
serde_json = "1.0.140"
serde_json = "1.0.140"
tar = "0.4.44"
walkdir = "2.5.0"
zip = "5.0.0"

1
src/firmware/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod xbuilder;

78
src/firmware/xbuilder.rs Normal file
View file

@ -0,0 +1,78 @@
// Simplify build
// -
use std::fs::File;
use chrono::{DateTime, Utc};
use rayon::iter::{ParallelBridge, ParallelIterator};
use tar::Archive;
use crate::recipe_functions::common;
pub trait Peekable {
fn peek(&self) -> Result<Vec<String>, Box<dyn std::error::Error>>;
}
/// Is expected to get information about last build
pub struct LastBuildFirmware {
pub path: String,
pub date: DateTime<Utc>,
pub is_full: bool,
/// Should be added later by calling implemented functions
pub entries: Vec<String>,
pub tags: Option<Vec<String>>,
}
impl Peekable for LastBuildFirmware {
fn peek(&self) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let mut peeked = Vec::new();
let file = File::open(self.path).unwrap();
let mut ar = Archive::new(file);
for file2 in ar.entries().unwrap() {
let mut f = file2.unwrap();
peeked.push(f.path().unwrap().file_name().unwrap().to_str().unwrap());
}
Ok(peeked)
}
}
pub fn read_last_build_firmwares() -> Result<Vec<LastBuildFirmware>, Box<dyn std::error::Error>> {
let mut result = Vec::new();
// get config
let ucfg = common::get_config();
let firmware_dir = ucfg
.get("FIRMWARE_DIR")
.expect("Unknown firmware directory, check .tbcfg");
// parallel walk to dir
walkdir::WalkDir::new(firmware_dir)
.max_depth(2)
.into_iter()
.par_bridge()
.filter(|w| {
let filename = w.ok().unwrap().file_name().to_str().unwrap();
filename.ends_with(".tar") || filename.ends_with(".zip")
})
.for_each(|entry| {
let dir = entry?;
let meta = dir.clone().metadata()?;
let filename = dir.clone().file_name().to_str().unwrap();
let mod_time: DateTime<Utc> = meta.modified()?.into();
let full_firmware = filename.clone().contains("full");
result.push(LastBuildFirmware {
path: filename.clone(),
date: mod_time,
is_full: full_firmware,
entries: Vec::new(),
tags: None,
});
});
Ok(result)
}

View file

@ -1,3 +1,4 @@
pub mod firmware;
pub mod models;
pub mod previews;
pub mod recipe_functions;

View file

@ -1,2 +1,4 @@
pub mod recipe;
pub mod recipev2;
// pub mod ui;
// pub mod x2engine;