Skip to content

Commit f0adfda

Browse files
committed
allow users to set Kaleido path via envionment variable
- introduced a new feature to allow users to download Kaleido at compile time when the applications are targeted for the host machine - this can be overriden by the runtime environment variable Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent ea8d95c commit f0adfda

File tree

6 files changed

+120
-67
lines changed

6 files changed

+120
-67
lines changed

examples/kaleido/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
[package]
22
name = "kaleido"
33
version = "0.1.0"
4-
authors = ["Michael Freeborn <[email protected]>"]
4+
authors = [
5+
"Michael Freeborn <[email protected]>",
6+
"Andrei Gherghescu [email protected]",
7+
]
58
edition = "2021"
69

710
[dependencies]
8-
plotly = { path = "../../plotly", features = ["kaleido"] }
11+
plotly = { path = "../../plotly", features = ["kaleido", "kaleido_fetch"] }
12+
# plotly = { path = "../../plotly", features = ["kaleido"] }

plotly/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ exclude = ["target/*"]
1515

1616
[features]
1717
kaleido = ["plotly_kaleido"]
18+
kaleido_fetch = ["plotly_kaleido/download"]
19+
1820
plotly_ndarray = ["ndarray"]
1921
plotly_image = ["image"]
20-
# Embed JavaScript into library and templates for offline use
2122
plotly_embed_js = []
23+
2224
wasm = ["getrandom", "js-sys", "wasm-bindgen", "wasm-bindgen-futures"]
2325
with-axum = ["rinja/with-axum", "rinja_axum"]
2426

@@ -48,6 +50,8 @@ image = "0.25"
4850
itertools = ">=0.10, <0.14"
4951
itertools-num = "0.1"
5052
ndarray = "0.16"
51-
plotly_kaleido = { version = "0.11", path = "../plotly_kaleido" }
53+
plotly_kaleido = { version = "0.11", path = "../plotly_kaleido", features = [
54+
"download",
55+
] }
5256
rand_distr = "0.4"
5357
base64 = "0.22"

plotly/src/plot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ impl PartialEq for Plot {
585585
mod tests {
586586
use std::path::PathBuf;
587587

588-
use base64::{engine::general_purpose, Engine as _};
588+
589589
use serde_json::{json, to_value};
590590

591591
use super::*;

plotly_kaleido/Cargo.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
name = "plotly_kaleido"
33
version = "0.11.0"
44
description = "Additional output format support for plotly using Kaleido"
5-
authors = ["Ioannis Giagkiozis <[email protected]>"]
5+
authors = [
6+
"Ioannis Giagkiozis <[email protected]>",
7+
"Andrei Gherghescu [email protected]",
8+
]
69
license = "MIT"
710
readme = "README.md"
811
workspace = ".."
@@ -14,13 +17,19 @@ keywords = ["plot", "chart", "plotly", "ndarray"]
1417

1518
exclude = ["target/*", "kaleido/*", "examples/*"]
1619

20+
[features]
21+
download = []
22+
1723
[dependencies]
1824
serde = { version = "1.0", features = ["derive"] }
1925
serde_json = "1.0"
2026
base64 = "0.22"
2127
dunce = "1.0"
2228
directories = ">=4, <6"
2329

30+
[dev-dependencies]
31+
plotly_kaleido = { version = "0.11", path = ".", features = ["download"] }
32+
2433
[build-dependencies]
2534
zip = "2.1"
2635
directories = ">=4, <6"

plotly_kaleido/build.rs

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@ const KALEIDO_URL: &str =
3030
const KALEIDO_URL: &str =
3131
"https://github.com/plotly/Kaleido/releases/download/v0.2.1/kaleido_mac_arm64.zip";
3232

33-
#[cfg(target_os = "linux")]
33+
#[cfg(any(target_os = "linux", target_os = "macos"))]
3434
const KALEIDO_BIN: &str = "kaleido";
3535

3636
#[cfg(target_os = "windows")]
3737
const KALEIDO_BIN: &str = "kaleido.exe";
3838

39-
#[cfg(target_os = "macos")]
40-
const KALEIDO_BIN: &str = "kaleido";
41-
4239
fn extract_zip(p: &Path, zip_file: &Path) -> Result<()> {
4340
let file = fs::File::open(zip_file).unwrap();
4441
let mut archive = zip::ZipArchive::new(file).unwrap();
@@ -95,35 +92,56 @@ fn extract_zip(p: &Path, zip_file: &Path) -> Result<()> {
9592
}
9693

9794
fn main() -> Result<()> {
98-
let project_dirs = ProjectDirs::from("org", "plotly", "kaleido")
99-
.expect("Could not create plotly_kaleido config directory.");
100-
let dst: PathBuf = project_dirs.config_dir().into();
95+
if cfg!(feature = "download") {
96+
let project_dirs = ProjectDirs::from("org", "plotly", "kaleido")
97+
.expect("Could not create Kaleido config directory path.");
98+
let dst: PathBuf = project_dirs.config_dir().into();
99+
100+
let kaleido_binary = dst.join("bin").join(KALEIDO_BIN);
101+
102+
println!("cargo:rerun-if-changed=src/lib.rs");
103+
println!(
104+
"cargo::rerun-if-changed={}",
105+
kaleido_binary.to_string_lossy()
106+
);
107+
108+
println!(
109+
"cargo:rustc-env=KALEIDO_COMPILE_TIME_DLD_PATH={}",
110+
dst.to_string_lossy()
111+
);
112+
113+
if kaleido_binary.exists() {
114+
return Ok(());
115+
}
101116

102-
let kaleido_binary = dst.join("bin").join(KALEIDO_BIN);
103-
if kaleido_binary.exists() {
104-
return Ok(());
117+
let msg = format!(
118+
"Downloaded Plotly Kaleido from {KALEIDO_URL} to '{}'",
119+
dst.to_string_lossy()
120+
);
121+
println!("cargo::warning={msg}");
122+
123+
let p = PathBuf::from(env::var("OUT_DIR").unwrap());
124+
let kaleido_zip_file = p.join("kaleido.zip");
125+
126+
let mut cmd = Command::new("cargo")
127+
.args(["install", "ruget"])
128+
.spawn()
129+
.unwrap();
130+
cmd.wait()?;
131+
132+
let mut cmd = Command::new("ruget")
133+
.args([
134+
KALEIDO_URL,
135+
"-o",
136+
kaleido_zip_file.as_path().to_str().unwrap(),
137+
])
138+
.spawn()
139+
.unwrap();
140+
cmd.wait()?;
141+
142+
extract_zip(&dst, &kaleido_zip_file)?;
143+
} else {
144+
println!("'download' feature disabled. Kaleido binary won't be downloaded and must be installed manually.")
105145
}
106-
107-
let p = PathBuf::from(env::var("OUT_DIR").unwrap());
108-
let kaleido_zip_file = p.join("kaleido.zip");
109-
110-
let mut cmd = Command::new("cargo")
111-
.args(["install", "ruget"])
112-
.spawn()
113-
.unwrap();
114-
cmd.wait()?;
115-
116-
let mut cmd = Command::new("ruget")
117-
.args([
118-
KALEIDO_URL,
119-
"-o",
120-
kaleido_zip_file.as_path().to_str().unwrap(),
121-
])
122-
.spawn()
123-
.unwrap();
124-
cmd.wait()?;
125-
126-
extract_zip(&dst, &kaleido_zip_file)?;
127-
println!("cargo:rerun-if-changed=src/lib.rs");
128146
Ok(())
129147
}

plotly_kaleido/src/lib.rs

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use std::path::{Path, PathBuf};
1717
use std::process::{Command, Stdio};
1818

1919
use base64::{engine::general_purpose, Engine as _};
20-
use directories::ProjectDirs;
2120
use serde::{Deserialize, Serialize};
2221
use serde_json::Value;
2322

@@ -77,49 +76,59 @@ pub struct Kaleido {
7776
}
7877

7978
impl Kaleido {
79+
const KALEIDO_PATH_ENV: &str = "KALEIDO_PATH";
80+
8081
pub fn new() -> Kaleido {
81-
let path = match Kaleido::binary_path() {
82-
Ok(path) => path,
83-
Err(msg) => panic!("{}", msg),
82+
use std::env;
83+
84+
let path = match env::var(Self::KALEIDO_PATH_ENV) {
85+
Ok(runtime_env) => runtime_env,
86+
Err(runtime_env_err) => match option_env!("KALEIDO_COMPILE_TIME_DLD_PATH") {
87+
Some(compile_time_path) => compile_time_path.to_string(),
88+
None => {
89+
println!("{}: {}", Self::KALEIDO_PATH_ENV, runtime_env_err);
90+
println!("Use `kaleido_fetch` feature to automatically download, install and use Kaleido when targeting applications that run on the host machine.");
91+
println!("Use `{}` environment variable when targeting applications intended to run on different machines. Manually install Kaleido on the target machine and point {} to the installation location.", Self::KALEIDO_PATH_ENV, Self::KALEIDO_PATH_ENV
92+
);
93+
std::process::exit(1);
94+
}
95+
},
8496
};
8597

86-
Kaleido { cmd_path: path }
87-
}
98+
let path = match Kaleido::binary_path(&path) {
99+
Ok(kaleido_path) => kaleido_path,
100+
Err(msg) => panic!("Failed tu use Kaleido binary at {} due to {}", path, msg),
101+
};
88102

89-
fn root_dir() -> Result<PathBuf, &'static str> {
90-
let project_dirs = ProjectDirs::from("org", "plotly", "kaleido")
91-
.expect("Could not create plotly_kaleido config directory.");
92-
Ok(project_dirs.config_dir().into())
103+
Kaleido { cmd_path: path }
93104
}
94105

95-
#[cfg(target_os = "linux")]
96-
fn binary_path() -> Result<PathBuf, &'static str> {
97-
let mut p = Kaleido::root_dir()?;
98-
p = p.join("kaleido").canonicalize().unwrap();
106+
fn binary_path(dld_path: &str) -> Result<PathBuf, &'static str> {
107+
let mut p = PathBuf::from(dld_path);
108+
p = Self::os_binary_path(p);
99109
if !p.exists() {
100110
return Err("could not find kaleido executable in path");
101111
}
102112
Ok(p)
103113
}
104114

105-
#[cfg(target_os = "macos")]
106-
fn binary_path() -> Result<PathBuf, &'static str> {
107-
let mut p = Kaleido::root_dir()?;
108-
p = p.join("kaleido").canonicalize().unwrap();
109-
if !p.exists() {
110-
return Err("could not find kaleido executable in path");
115+
#[cfg(any(target_os = "linux", target_os = "macos"))]
116+
fn os_binary_path(path: PathBuf) -> PathBuf {
117+
match path.join("kaleido").canonicalize() {
118+
Ok(v) => v,
119+
Err(e) => {
120+
println!(
121+
"Failed to find Kaleido binary at '{}': {e}",
122+
path.to_string_lossy()
123+
);
124+
panic!("{e}");
125+
}
111126
}
112-
Ok(p)
113127
}
114128

115129
#[cfg(target_os = "windows")]
116-
fn binary_path() -> Result<PathBuf, &'static str> {
117-
let mut p = Kaleido::root_dir()?;
118-
p = p.join("kaleido.cmd");
119-
if !p.exists() {
120-
return Err("could not find kaleido executable in path");
121-
}
122-
Ok(p)
130+
fn os_binary_path(path: PathBuf) -> PathBuf {
131+
p.join("kaleido.cmd")
123132
}
124133

125134
/// Generate a static image from a Plotly graph and save it to a file
@@ -193,7 +202,16 @@ impl Kaleido {
193202
.stdout(Stdio::piped())
194203
.stderr(Stdio::piped())
195204
.spawn()
196-
.expect("failed to spawn Kaleido binary");
205+
.unwrap_or_else(|_| {
206+
panic!(
207+
"{}",
208+
format!(
209+
"failed to spawn Kaleido binary at {}",
210+
self.cmd_path.to_string_lossy()
211+
)
212+
.to_string()
213+
)
214+
});
197215

198216
{
199217
let plot_data = PlotData::new(plotly_data, format, width, height, scale).to_json();

0 commit comments

Comments
 (0)