Skip to content

Commit

Permalink
check for file before grabbing json file from github
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Patro committed Feb 1, 2024
1 parent 410af7c commit 1235098
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
25 changes: 24 additions & 1 deletion src/utils/af_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,35 @@ pub fn add_chemistry_to_args_piscem(chem_str: &str, cmd: &mut std::process::Comm
}

pub fn get_permit_if_absent(af_home: &Path, chem: &Chemistry) -> Result<PermitListResult> {
// check if the file already exists
let odir = af_home.join("plist");
match chem {
Chemistry::TenxV2 => {
let chem_file = "10x_v2_permit.txt";
if odir.join(chem_file).exists() {
return Ok(PermitListResult::AlreadyPresent(odir.join(chem_file)));
}
}
Chemistry::TenxV3 => {
let chem_file = "10x_v3_permit.txt";
if odir.join(chem_file).exists() {
return Ok(PermitListResult::AlreadyPresent(odir.join(chem_file)));
}
}
_ => {
return Ok(PermitListResult::UnregisteredChemistry);
}
}

// the file doesn't exist, so get the json file that gives us
// the chemistry name to permit list URL mapping.
let permit_dict_url = "https://raw.githubusercontent.com/COMBINE-lab/simpleaf/dev/resources/permit_list_info.json";
let permit_dict: serde_json::Value = minreq::get(permit_dict_url)
.send()?
.json::<serde_json::Value>()?;
let opt_chem_file: Option<String>;
let opt_dl_url: Option<String>;
// parse the JSON appropriately based on the chemistry we have
match chem {
Chemistry::TenxV2 => {
if let Some(d) = permit_dict.get("10xv2") {
Expand Down Expand Up @@ -219,8 +242,8 @@ pub fn get_permit_if_absent(af_home: &Path, chem: &Chemistry) -> Result<PermitLi
}
}

// actually download the permit list if we need it and don't have it.
if let (Some(chem_file), Some(dl_url)) = (opt_chem_file, opt_dl_url) {
let odir = af_home.join("plist");
if odir.join(&chem_file).exists() {
Ok(PermitListResult::AlreadyPresent(odir.join(&chem_file)))
} else {
Expand Down
12 changes: 11 additions & 1 deletion src/utils/prog_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Once;
use tracing::{error, info};
use tracing::{debug, error, info};
use which::which;

// The below functions are taken from the [`execute`](https://crates.io/crates/execute)
Expand Down Expand Up @@ -50,10 +50,20 @@ pub fn shell<S: AsRef<OsStr>>(cmd: S) -> Command {

pub fn download_to_file<T: AsRef<str>>(url: T, filename: &str) -> Result<()> {
let url = url.as_ref();

debug!(
"Downloading file from {} and writing to file {}",
url, filename
);

let request = minreq::get(url).with_timeout(120).send()?;
match request.status_code {
200..=299 => {
// success
debug!(
"Obtained status code {} from final url {}",
request.status_code, request.url
);
}
x => {
bail!(
Expand Down

0 comments on commit 1235098

Please sign in to comment.