From c654e3f503576524b0a7c7844092e4069908a03e Mon Sep 17 00:00:00 2001 From: Christopher Rabotin Date: Wed, 10 Jul 2024 08:27:38 -0600 Subject: [PATCH 1/3] Download assets in build.rs for the embed_ephem feature --- .github/workflows/rust.yml | 6 ++++++ anise/Cargo.toml | 5 ++++- anise/build.rs | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 anise/build.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index e2ef9937..432fa918 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -66,6 +66,12 @@ jobs: env: LAGRANGE_BSP: ${{ secrets.LAGRANGE_BSP }} run: cargo test --release --workspace --exclude anise-gui --exclude anise-py + + - name: Test rust_embed build + run: | + rm -rf data # Not available on crates.io + cargo build --features embed_ephem + cargo build --features embed_ephem --release lints: name: Lints diff --git a/anise/Cargo.toml b/anise/Cargo.toml index db91b18c..440be7a0 100644 --- a/anise/Cargo.toml +++ b/anise/Cargo.toml @@ -37,7 +37,7 @@ rust-embed = { version = "8.4.0", features = [ "interpolate-folder-path", "include-exclude", ], optional = true } -regex = {version = "1.10.5" , optional = true} +regex = { version = "1.10.5", optional = true } [dev-dependencies] rust-spice = "0.7.6" @@ -49,6 +49,9 @@ polars = { version = "0.41.1", features = ["lazy", "parquet"] } rayon = "1.7" serde_yaml = "0.9.30" +[build-dependencies] +reqwest = { version = "0.11", features = ["blocking"], optional = true } + [features] default = ["metaload"] # Enabling this flag significantly increases compilation times due to Arrow and Polars. diff --git a/anise/build.rs b/anise/build.rs new file mode 100644 index 00000000..4139d58c --- /dev/null +++ b/anise/build.rs @@ -0,0 +1,42 @@ +#[cfg(feature = "embed_ephem")] +fn main() { + // Download the files to embed at build time. + use std::{fs::File, io::Write, time::Duration}; + let client = reqwest::blocking::Client::builder() + .connect_timeout(Duration::from_secs(30)) + .timeout(Duration::from_secs(30)) + .build() + .unwrap(); + + let embedded_files = [ + ( + "http://public-data.nyxspace.com/anise/v0.4/pck11.pca", + format!("{}/../data/pck11.pca", env!("CARGO_MANIFEST_DIR")), + ), + ( + "http://public-data.nyxspace.com/anise/de440s.bsp", + format!("{}/../data/de440s.bsp", env!("CARGO_MANIFEST_DIR")), + ), + ]; + + for (url, dest_path) in embedded_files { + let resp = client + .get(url) + .send() + .expect(&format!("could not download {url}")); + + let bytes = resp + .bytes() + .expect(&format!("could not read bytes from {url}")); + + let mut file = + File::create(&dest_path).expect(&format!("could not create the data path {dest_path}")); + file.write_all(&bytes) + .expect(&format!("could not write asset data to {dest_path}")); + } +} + +#[cfg(not(feature = "embed_ephem"))] +fn main() { + // Nothing to do if we aren't embedded files. +} From 20ab7e80a282527cf5d9891d5068b0ca794c66ff Mon Sep 17 00:00:00 2001 From: Christopher Rabotin Date: Wed, 10 Jul 2024 08:50:46 -0600 Subject: [PATCH 2/3] Create data dir if needed, and prep for 0.4.1 --- Cargo.toml | 2 +- anise/build.rs | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 106f4d19..1fd8111f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "2" members = ["anise", "anise-cli", "anise-gui", "anise-py"] [workspace.package] -version = "0.4.0" +version = "0.4.1" edition = "2021" authors = ["Christopher Rabotin "] description = "ANISE provides a toolkit and files for Attitude, Navigation, Instrument, Spacecraft, and Ephemeris data. It's a modern replacement of NAIF SPICE file." diff --git a/anise/build.rs b/anise/build.rs index 4139d58c..4a5c1de5 100644 --- a/anise/build.rs +++ b/anise/build.rs @@ -1,7 +1,12 @@ #[cfg(feature = "embed_ephem")] fn main() { // Download the files to embed at build time. - use std::{fs::File, io::Write, time::Duration}; + use std::{ + fs::{self, File}, + io::Write, + path::Path, + time::Duration, + }; let client = reqwest::blocking::Client::builder() .connect_timeout(Duration::from_secs(30)) .timeout(Duration::from_secs(30)) @@ -19,6 +24,13 @@ fn main() { ), ]; + let data_path = Path::new(&env!("CARGO_MANIFEST_DIR")).join("../data"); + + // Create the directory if it doesn't exist + if !data_path.exists() { + fs::create_dir_all(&data_path).expect(&format!("failed to create directory {data_path:?}")); + } + for (url, dest_path) in embedded_files { let resp = client .get(url) From ab7f7d916d4f01876ed900c1ba2a7073ec48d065 Mon Sep 17 00:00:00 2001 From: Christopher Rabotin Date: Wed, 10 Jul 2024 09:05:48 -0600 Subject: [PATCH 3/3] Build only the rust lib with embed_ephem --- .github/workflows/rust.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 432fa918..fa62b68e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -70,6 +70,7 @@ jobs: - name: Test rust_embed build run: | rm -rf data # Not available on crates.io + cd anise # Build only the Rust library cargo build --features embed_ephem cargo build --features embed_ephem --release