From 5d19b9124a248c23811da03da9896a07e6de12df Mon Sep 17 00:00:00 2001 From: namuyan Date: Sat, 12 Oct 2019 11:23:27 +0900 Subject: [PATCH 1/3] check python interpreter by --version --- build.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/build.rs b/build.rs index 7ee4b987..48526d40 100644 --- a/build.rs +++ b/build.rs @@ -25,10 +25,13 @@ const MIN_VERSION: &'static str = "1.37.0-nightly"; lazy_static! { static ref PYTHON_INTERPRETER: &'static str = { - ["python3", "python"] + ["python", "python3"] .iter() - .map(|bin| (bin, Command::new(bin).spawn())) - .find(|(_, r)| r.is_ok()) + .map(|bin| (bin, Command::new(bin).arg("--version").output())) + .filter(|(_, r)| r.is_ok()) + .map(|(bin, r)| (bin, r.unwrap().stderr)) + // begin with `Python 3.X.X :: additional info` + .find(|(_, r)| r.starts_with(b"Python 3")) .map(|(bin, _)| bin) .expect("Python 3.x interpreter not found") }; From 4b1c48831ab41c93986c2c7367003c8000e1ce00 Mon Sep 17 00:00:00 2001 From: namuyan Date: Sat, 12 Oct 2019 13:13:28 +0900 Subject: [PATCH 2/3] catch stdout(others) and stderr(windows) output --- build.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 48526d40..33a14947 100644 --- a/build.rs +++ b/build.rs @@ -29,9 +29,9 @@ lazy_static! { .iter() .map(|bin| (bin, Command::new(bin).arg("--version").output())) .filter(|(_, r)| r.is_ok()) - .map(|(bin, r)| (bin, r.unwrap().stderr)) + .map(|(bin, r)| (bin, r.unwrap())) // begin with `Python 3.X.X :: additional info` - .find(|(_, r)| r.starts_with(b"Python 3")) + .find(|(_, r)| r.stdout.starts_with(b"Python 3") || r.stderr.starts_with(b"Python 3")) .map(|(bin, _)| bin) .expect("Python 3.x interpreter not found") }; From 91b32310384d07a83c3e7646ff48de361a2012a2 Mon Sep 17 00:00:00 2001 From: namuyan Date: Sat, 12 Oct 2019 17:00:19 +0900 Subject: [PATCH 3/3] complicated method chain --- build.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/build.rs b/build.rs index 33a14947..7f1060ac 100644 --- a/build.rs +++ b/build.rs @@ -27,12 +27,14 @@ lazy_static! { static ref PYTHON_INTERPRETER: &'static str = { ["python", "python3"] .iter() - .map(|bin| (bin, Command::new(bin).arg("--version").output())) - .filter(|(_, r)| r.is_ok()) - .map(|(bin, r)| (bin, r.unwrap())) - // begin with `Python 3.X.X :: additional info` - .find(|(_, r)| r.stdout.starts_with(b"Python 3") || r.stderr.starts_with(b"Python 3")) - .map(|(bin, _)| bin) + .find(|bin| { + if let Ok(out) = Command::new(bin).arg("--version").output() { + // begin with `Python 3.X.X :: additional info` + out.stdout.starts_with(b"Python 3") || out.stderr.starts_with(b"Python 3") + } else { + false + } + }) .expect("Python 3.x interpreter not found") }; }