mirror of
https://gitlab.cern.ch/wotsubo/psboard-qaqc-postprocess.git
synced 2024-11-21 23:00:20 +09:00
update: organize commands to subcommands and rename bin
- existing functions are moved to subcommand `add-master-log`
This commit is contained in:
parent
bdb415e6e6
commit
a94a871e52
5 changed files with 81 additions and 56 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -637,7 +637,7 @@ dependencies = [
|
|||
]
|
||||
|
||||
[[package]]
|
||||
name = "psb-qaqc-parse"
|
||||
name = "psb-qaqc"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
[package]
|
||||
name = "psb-qaqc-parse"
|
||||
name = "psb-qaqc"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
|
|
|
@ -9,6 +9,11 @@ cargo build
|
|||
cargo run -- -h
|
||||
```
|
||||
|
||||
Build for JATHub using cross:
|
||||
```sh
|
||||
CROSS_CONTAINER_ENGINE=podman cross build --release --target armv7-unknown-linux-musleabihf
|
||||
```
|
||||
|
||||
### test
|
||||
```sh
|
||||
cargo test
|
||||
|
|
119
src/main.rs
119
src/main.rs
|
@ -10,26 +10,35 @@ use std::{
|
|||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use chrono::{DateTime, Local, Utc};
|
||||
use clap::Parser;
|
||||
use clap::{Parser, Subcommand};
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use regex::Regex;
|
||||
use semver::Version;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_with::{serde_as, DisplayFromStr};
|
||||
|
||||
/// Parse master jathub logfile for PS Board QAQC and write out to CSV.
|
||||
/// PS Board QAQC shift related commands.
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
/// Master log file.
|
||||
master_log: path::PathBuf,
|
||||
/// Output CSV file.
|
||||
// #[arg(default_value = get_default_log_path().into_os_string())]
|
||||
outfile: PathBuf,
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
#[command(flatten)]
|
||||
verbose: clap_verbosity_flag::Verbosity,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
pub enum Commands {
|
||||
/// Parse master jathub logfile for PS Board QAQC and write out to CSV.
|
||||
AddMasterLog {
|
||||
/// Master log file.
|
||||
master_log: path::PathBuf,
|
||||
/// Output CSV file.
|
||||
// #[arg(default_value = get_default_log_path().into_os_string())]
|
||||
outfile: PathBuf,
|
||||
},
|
||||
}
|
||||
|
||||
/// Layer
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, PartialOrd, Ord)]
|
||||
enum PositionLayer {
|
||||
|
@ -444,55 +453,58 @@ fn main() -> Result<()> {
|
|||
" 1".split_whitespace().next().unwrap().parse::<u8>()
|
||||
);
|
||||
|
||||
let result = {
|
||||
let file = File::open(args.master_log.clone())?;
|
||||
let reader = BufReader::new(file);
|
||||
MasterLogResult::parse_file(
|
||||
reader,
|
||||
args.master_log
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
)?
|
||||
};
|
||||
debug!("{:?}", result);
|
||||
match args.command {
|
||||
Commands::AddMasterLog {
|
||||
master_log,
|
||||
outfile,
|
||||
} => {
|
||||
let result = {
|
||||
let file = File::open(master_log.clone())?;
|
||||
let reader = BufReader::new(file);
|
||||
MasterLogResult::parse_file(
|
||||
reader,
|
||||
master_log
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
)?
|
||||
};
|
||||
debug!("{:?}", result);
|
||||
|
||||
// {
|
||||
// let file = File::options()
|
||||
// .read(true)
|
||||
// .open(args.outfile)?;
|
||||
//
|
||||
// let mut rdr = csv::Reader::from_reader(file);
|
||||
// rdr.records();
|
||||
// }
|
||||
// {
|
||||
// let file = File::options()
|
||||
// .read(true)
|
||||
// .open(args.outfile)?;
|
||||
//
|
||||
// let mut rdr = csv::Reader::from_reader(file);
|
||||
// rdr.records();
|
||||
// }
|
||||
|
||||
let expanded_results = PsbQaqcResult::from_masterlogresult(result);
|
||||
let expanded_results = PsbQaqcResult::from_masterlogresult(result);
|
||||
|
||||
let mut wtr = match args.outfile.exists() {
|
||||
true => {
|
||||
let file = File::options().read(true).append(true).open(args.outfile)?;
|
||||
csv::WriterBuilder::new()
|
||||
.has_headers(false)
|
||||
.from_writer(file)
|
||||
let mut wtr = match outfile.exists() {
|
||||
true => {
|
||||
let file = File::options().read(true).append(true).open(outfile)?;
|
||||
csv::WriterBuilder::new()
|
||||
.has_headers(false)
|
||||
.from_writer(file)
|
||||
}
|
||||
false => {
|
||||
println!("Creating new file: {}", outfile.display());
|
||||
let file = File::options().create_new(true).write(true).open(outfile)?;
|
||||
csv::WriterBuilder::new()
|
||||
.has_headers(true)
|
||||
.from_writer(file)
|
||||
}
|
||||
};
|
||||
for result in expanded_results {
|
||||
wtr.serialize(result)?;
|
||||
}
|
||||
wtr.flush()?;
|
||||
}
|
||||
false => {
|
||||
println!("Creating new file: {}", args.outfile.display());
|
||||
let file = File::options()
|
||||
.create_new(true)
|
||||
.write(true)
|
||||
.open(args.outfile)?;
|
||||
csv::WriterBuilder::new()
|
||||
.has_headers(true)
|
||||
.from_writer(file)
|
||||
}
|
||||
};
|
||||
for result in expanded_results {
|
||||
wtr.serialize(result)?;
|
||||
}
|
||||
wtr.flush()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -625,4 +637,9 @@ mod test {
|
|||
)
|
||||
);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn parse_file() {
|
||||
// let logfile = r"""""";
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -15,8 +15,9 @@ mod integrated_test {
|
|||
let test_out = PathBuf::new().join(&test_out_dir).join("out.csv");
|
||||
|
||||
// 1st file
|
||||
let mut cmd = Command::cargo_bin("psb-qaqc-parse")?;
|
||||
let mut cmd = Command::cargo_bin("psb-qaqc")?;
|
||||
cmd.current_dir("tests")
|
||||
.arg("add-master-log")
|
||||
.arg("./example_logs/valid/20240720_171418.log")
|
||||
.arg(test_out.as_path())
|
||||
.assert()
|
||||
|
@ -33,8 +34,9 @@ mod integrated_test {
|
|||
}
|
||||
|
||||
// 2nd file
|
||||
let mut cmd = Command::cargo_bin("psb-qaqc-parse")?;
|
||||
let mut cmd = Command::cargo_bin("psb-qaqc")?;
|
||||
cmd.current_dir("tests")
|
||||
.arg("add-master-log")
|
||||
.arg("./example_logs/valid/20240720_171418.log")
|
||||
.arg(test_out.as_path())
|
||||
.assert()
|
||||
|
@ -48,8 +50,9 @@ mod integrated_test {
|
|||
let test_out_dir = assert_fs::TempDir::new()?;
|
||||
let test_out = PathBuf::new().join(&test_out_dir).join("out.csv");
|
||||
|
||||
let mut cmd = Command::cargo_bin("psb-qaqc-parse")?;
|
||||
let mut cmd = Command::cargo_bin("psb-qaqc")?;
|
||||
cmd.current_dir("tests")
|
||||
.arg("add-master-log")
|
||||
.arg("./example_logs/valid/20240720_171419.log")
|
||||
.arg(test_out.as_path())
|
||||
.assert()
|
||||
|
|
Loading…
Reference in a new issue