Skip to content

Commit

Permalink
🐞 fix: Fix some functions
Browse files Browse the repository at this point in the history
- Fix pattern match when data possibly contains error
 - Fix generic script
  • Loading branch information
wychlw committed Sep 26, 2024
1 parent fcbda36 commit 3dd5a8d
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 45 deletions.
25 changes: 15 additions & 10 deletions apps/system/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,23 @@ def loggin(self):
"""
Loggin to the system.
"""
i = 0
self.tty.wait_serial("login:", 600)
slp()
self.tty.writeln(self.username)
slp()
slp(10) # Some system would gives you "密码" instead of "Password"... Just sleep for a while.
slp()
self.tty.writeln(self.password)
slp()
self.tty.wait_serial(self.username)
slp()
try:
self.tty.wait_serial("Current")
while i < 4: # May be failed to login, try again.
slp(10)
self.tty.writeln(self.username)
slp()
slp(10) # Some system would gives you "密码" instead of "Password"... Just sleep for a while.
slp()
self.tty.writeln(self.password)
slp()
i += 1
self.tty.wait_serial("login:", 5)
except Exception as e:
pass
try:
self.tty.wait_serial("Current", 5)
# Means the password is expired.
slp()
self.tty.writeln(self.password)
Expand Down
51 changes: 25 additions & 26 deletions apps/utils/maintain_img.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,54 +44,53 @@ def extract_image(file: str) -> str:
"""
curr = os.getcwd()
os.chdir(os.path.dirname(file))
res = None
res = "".join(file.split(".")[:-1])
# check file type
if not os.path.exists(file):
os.chdir(curr)
res = None
elif not os.path.isfile(file):
os.chdir(curr)
res = None
# For now support .tar, .tar.gz, .tar.bz2, .tar.xz, .zip
elif os.path.exists(res):
os.chdir(curr)
return res
# For now support .tar, .tar.gz, .tar.bz2, .tar.xz, .zip .zst
elif file.endswith(".zst"):
if not os.path.exists(file[:-4]):
os.system(f"zstd -d {file}")
res = file[:-4]
elif file.endswith(".tar"):
if os.path.exists(file[:-4]):
res = file[:-4]
os.system(f"tar -xf {file}")
if not os.path.exists(file[:-4]):
os.system(f"tar -xf {file}")
res = file[:-4]
elif file.endswith(".tar.gz"):
if os.path.exists(file[:-7]):
res = file[:-7]
os.system(f"tar -xzf {file}")
if not os.path.exists(file[:-7]):
os.system(f"tar -xzf {file}")
res = file[:-7]
elif file.endswith(".tar.bz2"):
if os.path.exists(file[:-8]):
res = file[:-8]
os.system(f"tar -xjf {file}")
if not os.path.exists(file[:-8]):
os.system(f"tar -xjf {file}")
res = file[:-8]
elif file.endswith(".tar.xz"):
if os.path.exists(file[:-8]):
res = file[:-8]
os.system(f"tar -xJf {file}")
if not os.path.exists(file[:-8]):
os.system(f"tar -xJf {file}")
res = file[:-8]
elif file.endswith(".zip"):
if os.path.exists(file[:-4]):
res = file[:-4]
os.system(f"unzip {file}")
if not os.path.exists(file[:-4]):
os.system(f"unzip {file}")
res = file[:-4]
elif file.endswith(".xz"):
if os.path.exists(file[:-3]):
res = file[:-3]
os.system(f"unxz -k {file}")
if not os.path.exists(file[:-3]):
os.system(f"unxz -k {file}")
res = file[:-3]
elif file.endswith(".gz"):
if os.path.exists(file[:-3]):
res = file[:-3]
os.system(f"gunzip -k {file}")
if not os.path.exists(file[:-3]):
os.system(f"gunzip -k {file}")
res = file[:-3]
elif file.endswith(".bz2"):
if os.path.exists(file[:-4]):
res = file[:-4]
os.system(f"bunzip2 -k {file}")
if not os.path.exists(file[:-4]):
os.system(f"bunzip2 -k {file}")
res = file[:-4]
os.chdir(curr)
return res
73 changes: 65 additions & 8 deletions src/exec/cli_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use std::{
};

use crate::{
cli::tty::{DynTty, Tty, WrapperTty}, consts::DURATION, err, impl_any, info, util::util::rand_string
cli::tty::{DynTty, Tty, WrapperTty},
consts::DURATION,
err, impl_any, info,
util::util::rand_string,
};

use super::cli_api::{CliTestApi, SudoCliTestApi};
Expand Down Expand Up @@ -73,7 +76,60 @@ impl CliTester {
Ok(())
}

fn do_wait_serial(&mut self, expected: &str, timeout: u32, filter_echo_back: Option<&str>) -> Result<String, Box<dyn Error>> {
fn kmp_next(&self, target: &Vec<u8>) -> Vec<usize> {
let mut next = vec![0usize; target.len()];
let mut i = 1;
let mut j = 0;
while i < target.len() - 1 {
if target[i] == target[j] {
next[i] = j + 1;
i += 1;
j += 1;
} else {
if j == 0 {
next[i] = 0;
i += 1;
} else {
j = next[j - 1] as usize;
}
}
}
next
}

fn kmp_search(&self, content: &Vec<u8>, target: &Vec<u8>) -> Option<usize> {
let next = self.kmp_next(target);
let mut i = 0;
let mut j = 0;
let mut res = None;
while i < content.len() && j < target.len() {
if content[i] == target[j] {
if res.is_none() {
res = Some(i);
}
i += 1;
j += 1;
if j >= target.len() {
break;
}
} else {
if j == 0 {
i += 1;
} else {
j = next[j - 1];
}
res = None;
}
}
res
}

fn do_wait_serial(
&mut self,
expected: &str,
timeout: u32,
filter_echo_back: Option<&str>,
) -> Result<String, Box<dyn Error>> {
let begin = Instant::now();
let mut buf = Vec::new();
info!("Waiting for string {{{}}}", expected);
Expand All @@ -84,10 +140,14 @@ impl CliTester {
if let Some(filter) = filter_echo_back {
self.filter_assert_echo(filter, &mut buf)?;
}
let content = String::from_utf8(buf.clone()).unwrap_or_default();
if content.contains(expected) {
// The reason we compare raw u8 is... What if the data is corrupted?
let target = expected.as_bytes();
if let Some(pos) = self.kmp_search(&buf, &target.to_vec()) {
info!("Matched string {{{}}}", expected);
break;
let res = buf.split_off(pos + target.len());
let res = String::from_utf8(res)?;
buf.drain(0..pos + target.len());
return Ok(res);
}
if begin.elapsed().as_secs() > timeout as u64 {
err!(
Expand All @@ -98,9 +158,6 @@ impl CliTester {
return Err(Box::<dyn Error>::from("Timeout"));
}
}

let res = String::from_utf8(buf)?;
Ok(res)
}
}

Expand Down
Empty file added src/ui/cli_hooker.rs
Empty file.
3 changes: 2 additions & 1 deletion src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
pub mod main;
pub mod code_editor;
pub mod pyenv;
pub mod pyenv;
pub mod terminal;
35 changes: 35 additions & 0 deletions src/ui/terminal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use eframe::egui::{Context, Id, Ui, Window};

use crate::impl_sub_window;

use super::main::SubWindow;

pub struct Terminal {
size: (u32, u32),
}

impl Default for Terminal {
fn default() -> Self {
Terminal { size: (24, 80) }
}
}

impl Terminal {
fn show(&mut self, ui: &mut Ui) {
ui.label("Terminal");
}
}

impl SubWindow for Terminal {
fn show(&mut self, ctx: &Context, title: &str, id: &Id, open: &mut bool) {
let window = Window::new(title)
.id(id.to_owned())
.open(open)
.resizable([false, false]);
window.show(ctx, |ui| {
self.show(ui);
});
}
}

impl_sub_window!(Terminal, "Terminal");

0 comments on commit 3dd5a8d

Please sign in to comment.