From 7751c7c45c049ffd47cd2c0ca2f4a3204109ae3c Mon Sep 17 00:00:00 2001 From: Paul DeRouen Date: Wed, 22 Apr 2020 09:07:18 -0500 Subject: [PATCH 1/6] working branch --- rust/origen/cli/src/commands/setup.rs | 26 +++++++++++++--------- rust/origen/cli/src/python.rs | 31 +++++++++++++++------------ rust/origen/src/core/os.rs | 7 ++++-- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/rust/origen/cli/src/commands/setup.rs b/rust/origen/cli/src/commands/setup.rs index ce66dd4a..bee757ca 100644 --- a/rust/origen/cli/src/commands/setup.rs +++ b/rust/origen/cli/src/commands/setup.rs @@ -2,11 +2,13 @@ extern crate time; use crate::python::{poetry_version, MIN_PYTHON_VERSION, PYTHON_CONFIG}; use online::online; -use origen::core::os; +//use origen::core::os; use origen::core::term::*; use std::fs; use std::io; use std::path::PathBuf; +use std::ffi::OsString; +use std::process::Command; const POETRY_INSTALLER: &str = "https://raw.githubusercontent.com/sdispater/poetry/1.0.0b7/get-poetry.py"; @@ -64,13 +66,15 @@ pub fn run() { fs::read_to_string(&get_poetry_file).expect("Unable to read Poetry install file"); let new_data = data.replace( "/bin/env python", - &format!("/bin/env {}", PYTHON_CONFIG.command), + &format!("/bin/env {}", PYTHON_CONFIG.command.clone().into_string().unwrap()), ); fs::write(&get_poetry_file, new_data).expect("Unable to write Poetry install file"); // Install Poetry - os::cmd(&PYTHON_CONFIG.command) - .arg(format!("{}", get_poetry_file.display())) + //os::cmd(&PYTHON_CONFIG.command) + Command::new(&PYTHON_CONFIG.command) + //.arg(format!("{}", get_poetry_file.display())) + .arg(get_poetry_file) .arg("--yes") .status() .expect("Something went wrong install Poetry"); @@ -78,9 +82,10 @@ pub fn run() { if poetry_version().unwrap().major != 1 { // Have to use --preview here to get a 1.0.0 pre version, can only use versions for // official releases - os::cmd(&PYTHON_CONFIG.poetry_command) - .arg("self:update") - .arg("--preview") + //os::cmd(&PYTHON_CONFIG.poetry_command) + Command::new(&PYTHON_CONFIG.poetry_command) + .arg(OsString::from("self:update")) + .arg(OsString::from("--preview")) .status() .expect("Something wend wrong updating Poetry"); } @@ -90,9 +95,10 @@ pub fn run() { print!("Are the app's deps. installed? ... "); - let status = os::cmd(&PYTHON_CONFIG.poetry_command) - .arg("install") - .arg("--no-root") + //let status = os::cmd(&PYTHON_CONFIG.poetry_command) + let status = Command::new(&PYTHON_CONFIG.poetry_command) + .arg(OsString::from("install")) + .arg(OsString::from("--no-root")) .status(); if status.is_ok() { diff --git a/rust/origen/cli/src/python.rs b/rust/origen/cli/src/python.rs index df80f87c..48e9e32a 100644 --- a/rust/origen/cli/src/python.rs +++ b/rust/origen/cli/src/python.rs @@ -1,8 +1,11 @@ // Responsible for managing Python execution -use origen::core::os; +//use origen::core::os; +use std::process::Command; +use std::ffi::OsString; use origen::STATUS; use semver::Version; +use std::path::PathBuf; const PYTHONS: &[&str] = &[ "python", @@ -20,10 +23,10 @@ lazy_static! { pub struct Config { pub available: bool, - pub command: String, + pub command: OsString, pub version: Version, pub error: String, - pub poetry_command: String, + pub poetry_command: PathBuf, } impl Default for Config { @@ -33,14 +36,13 @@ impl Default for Config { match get_version(cmd) { Some(version) => { available = true; - let poetry_cmd = format!( - "{}/.poetry/bin/poetry", - format!("{}", STATUS.home.display()) - ); + let mut poetry_cmd = PathBuf::from(&STATUS.home); + for d in [".poetry", "bin", "poetry"].iter() { poetry_cmd.push(d) } + if cfg!(windows) { poetry_cmd.set_extension("bat"); } if version >= Version::parse(MIN_PYTHON_VERSION).unwrap() { return Config { available: true, - command: cmd.to_string(), + command: OsString::from(cmd.to_string()), version: version, error: "".to_string(), poetry_command: poetry_cmd, @@ -56,17 +58,17 @@ impl Default for Config { } Config { available: false, - command: "".to_string(), + command: OsString::new(), version: Version::parse("0.0.0").unwrap(), error: msg, - poetry_command: "".to_string(), + poetry_command: PathBuf::new(), } } } /// Get the Python version from the given command fn get_version(command: &str) -> Option { - match os::cmd(command).arg("--version").output() { + match Command::new(OsString::from(command)).arg("--version").output() { Ok(output) => return extract_version(std::str::from_utf8(&output.stdout).unwrap()), Err(_e) => return None, } @@ -74,7 +76,7 @@ fn get_version(command: &str) -> Option { /// Returns the version of poetry (obtained from running "poetry --version") pub fn poetry_version() -> Option { - match os::cmd(&PYTHON_CONFIG.poetry_command) + match Command::new(&PYTHON_CONFIG.poetry_command) .arg("--version") .output() { @@ -100,11 +102,12 @@ fn extract_version(text: &str) -> Option { /// Execute the given Python code pub fn run(code: &str) { - let _status = os::cmd(&PYTHON_CONFIG.poetry_command) + //let _status = os::cmd(&PYTHON_CONFIG.poetry_command) + let _status = Command::new(&PYTHON_CONFIG.poetry_command) .arg("run") .arg(&PYTHON_CONFIG.command) .arg("-c") - .arg(&code) + .arg(OsString::from(&code)) .status(); } diff --git a/rust/origen/src/core/os.rs b/rust/origen/src/core/os.rs index 45c553e5..b45d15cf 100644 --- a/rust/origen/src/core/os.rs +++ b/rust/origen/src/core/os.rs @@ -1,4 +1,5 @@ use std::process::Command; +use std::ffi::OsString; pub fn on_windows() -> bool { if cfg!(windows) { @@ -21,8 +22,10 @@ pub fn on_linux() -> bool { #[allow(unused_mut)] pub fn cmd(cmd: &str) -> std::process::Command { if on_windows() { - let mut c = Command::new("cmd"); - c.arg("/C").arg(&cmd); + //let mut c = Command::new("cmd"); + //c.arg("/C").arg(&cmd); + //return c; + let mut c = Command::new(OsString::from(cmd)); return c; } else { let mut c = Command::new(&cmd); From b0da1622caad8a4323b7c5c67d2f8cb7dc2eb186 Mon Sep 17 00:00:00 2001 From: Paul DeRouen Date: Wed, 22 Apr 2020 16:21:31 -0500 Subject: [PATCH 2/6] clean up --- rust/origen/cli/src/commands/setup.rs | 5 ----- rust/origen/cli/src/python.rs | 1 - rust/origen/src/core/os.rs | 25 +++++++++++-------------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/rust/origen/cli/src/commands/setup.rs b/rust/origen/cli/src/commands/setup.rs index bee757ca..0af5585e 100644 --- a/rust/origen/cli/src/commands/setup.rs +++ b/rust/origen/cli/src/commands/setup.rs @@ -2,7 +2,6 @@ extern crate time; use crate::python::{poetry_version, MIN_PYTHON_VERSION, PYTHON_CONFIG}; use online::online; -//use origen::core::os; use origen::core::term::*; use std::fs; use std::io; @@ -71,9 +70,7 @@ pub fn run() { fs::write(&get_poetry_file, new_data).expect("Unable to write Poetry install file"); // Install Poetry - //os::cmd(&PYTHON_CONFIG.command) Command::new(&PYTHON_CONFIG.command) - //.arg(format!("{}", get_poetry_file.display())) .arg(get_poetry_file) .arg("--yes") .status() @@ -82,7 +79,6 @@ pub fn run() { if poetry_version().unwrap().major != 1 { // Have to use --preview here to get a 1.0.0 pre version, can only use versions for // official releases - //os::cmd(&PYTHON_CONFIG.poetry_command) Command::new(&PYTHON_CONFIG.poetry_command) .arg(OsString::from("self:update")) .arg(OsString::from("--preview")) @@ -95,7 +91,6 @@ pub fn run() { print!("Are the app's deps. installed? ... "); - //let status = os::cmd(&PYTHON_CONFIG.poetry_command) let status = Command::new(&PYTHON_CONFIG.poetry_command) .arg(OsString::from("install")) .arg(OsString::from("--no-root")) diff --git a/rust/origen/cli/src/python.rs b/rust/origen/cli/src/python.rs index 48e9e32a..6e48cc87 100644 --- a/rust/origen/cli/src/python.rs +++ b/rust/origen/cli/src/python.rs @@ -102,7 +102,6 @@ fn extract_version(text: &str) -> Option { /// Execute the given Python code pub fn run(code: &str) { - //let _status = os::cmd(&PYTHON_CONFIG.poetry_command) let _status = Command::new(&PYTHON_CONFIG.poetry_command) .arg("run") .arg(&PYTHON_CONFIG.command) diff --git a/rust/origen/src/core/os.rs b/rust/origen/src/core/os.rs index b45d15cf..1c1aea64 100644 --- a/rust/origen/src/core/os.rs +++ b/rust/origen/src/core/os.rs @@ -1,5 +1,4 @@ use std::process::Command; -use std::ffi::OsString; pub fn on_windows() -> bool { if cfg!(windows) { @@ -19,16 +18,14 @@ pub fn on_linux() -> bool { // Due to OS differences, the basic std::process::Command doesn't cooperate very well in a Windows environment. // Instead, wrap the cmd function here, which will ensures some semblance between Windows and Linux commands. -#[allow(unused_mut)] -pub fn cmd(cmd: &str) -> std::process::Command { - if on_windows() { - //let mut c = Command::new("cmd"); - //c.arg("/C").arg(&cmd); - //return c; - let mut c = Command::new(OsString::from(cmd)); - return c; - } else { - let mut c = Command::new(&cmd); - return c; - } -} +//#[allow(unused_mut)] +//pub fn cmd(cmd: &str) -> std::process::Command { +// if on_windows() { +// let mut c = Command::new("cmd"); +// c.arg("/C").arg(&cmd); +// return c; +// } else { +// let mut c = Command::new(&cmd); +// return c; +// } +//} From 3246e41ef400cb3a06ca997a877879617c32e62b Mon Sep 17 00:00:00 2001 From: Paul DeRouen Date: Wed, 22 Apr 2020 16:24:51 -0500 Subject: [PATCH 3/6] clean up --- rust/origen/src/core/os.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/origen/src/core/os.rs b/rust/origen/src/core/os.rs index 1c1aea64..478c70be 100644 --- a/rust/origen/src/core/os.rs +++ b/rust/origen/src/core/os.rs @@ -1,4 +1,4 @@ -use std::process::Command; +//use std::process::Command; pub fn on_windows() -> bool { if cfg!(windows) { From 7fde5b81deba7ff42f303b4784a18e0dc8c85edc Mon Sep 17 00:00:00 2001 From: Paul DeRouen Date: Sun, 26 Apr 2020 18:51:35 -0500 Subject: [PATCH 4/6] clean up --- rust/origen/cli/src/python.rs | 1 - rust/origen/src/core/os.rs | 16 ---------------- 2 files changed, 17 deletions(-) diff --git a/rust/origen/cli/src/python.rs b/rust/origen/cli/src/python.rs index 6e48cc87..22a9425a 100644 --- a/rust/origen/cli/src/python.rs +++ b/rust/origen/cli/src/python.rs @@ -1,6 +1,5 @@ // Responsible for managing Python execution -//use origen::core::os; use std::process::Command; use std::ffi::OsString; use origen::STATUS; diff --git a/rust/origen/src/core/os.rs b/rust/origen/src/core/os.rs index 478c70be..a167c026 100644 --- a/rust/origen/src/core/os.rs +++ b/rust/origen/src/core/os.rs @@ -1,5 +1,3 @@ -//use std::process::Command; - pub fn on_windows() -> bool { if cfg!(windows) { return true; @@ -15,17 +13,3 @@ pub fn on_linux() -> bool { return false; }; } - -// Due to OS differences, the basic std::process::Command doesn't cooperate very well in a Windows environment. -// Instead, wrap the cmd function here, which will ensures some semblance between Windows and Linux commands. -//#[allow(unused_mut)] -//pub fn cmd(cmd: &str) -> std::process::Command { -// if on_windows() { -// let mut c = Command::new("cmd"); -// c.arg("/C").arg(&cmd); -// return c; -// } else { -// let mut c = Command::new(&cmd); -// return c; -// } -//} From 40c2802baec542d95c139d53df9240c15fedc1fa Mon Sep 17 00:00:00 2001 From: Paul DeRouen Date: Mon, 27 Apr 2020 23:26:03 -0500 Subject: [PATCH 5/6] potential fix for python launcher file string bug --- rust/origen/cli/src/commands/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rust/origen/cli/src/commands/mod.rs b/rust/origen/cli/src/commands/mod.rs index cd05c063..180c0c9a 100644 --- a/rust/origen/cli/src/commands/mod.rs +++ b/rust/origen/cli/src/commands/mod.rs @@ -30,7 +30,8 @@ pub fn launch( } if files.is_some() { - let f: Vec = files.unwrap().iter().map(|f| format!("'{}'", f)).collect(); + // added r prefix to the string to force python to interpret as a string literal + let f: Vec = files.unwrap().iter().map(|f| format!("r'{}'", f)).collect(); cmd += &format!(", files=[{}]", f.join(",")).to_string(); } From 29462e03cde73dea441c69f14ddea3eb162bb3a9 Mon Sep 17 00:00:00 2001 From: Paul DeRouen Date: Tue, 28 Apr 2020 19:57:36 -0500 Subject: [PATCH 6/6] back out OsString additions and added string literal disignation to targets --- rust/origen/cli/src/commands/mod.rs | 3 ++- rust/origen/cli/src/commands/setup.rs | 11 +++++------ rust/origen/cli/src/python.rs | 11 +++++------ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/rust/origen/cli/src/commands/mod.rs b/rust/origen/cli/src/commands/mod.rs index 180c0c9a..47df683b 100644 --- a/rust/origen/cli/src/commands/mod.rs +++ b/rust/origen/cli/src/commands/mod.rs @@ -20,7 +20,8 @@ pub fn launch( ); if let Some(t) = targets { - let _t: Vec = t.iter().map(|__t| format!("'{}'", __t)).collect(); + // added r prefix to the string to force python to interpret as a string literal + let _t: Vec = t.iter().map(|__t| format!("r'{}'", __t)).collect(); cmd += &format!(", targets=[{}]", &_t.join(",")).to_string(); } diff --git a/rust/origen/cli/src/commands/setup.rs b/rust/origen/cli/src/commands/setup.rs index 0af5585e..2ffe6f39 100644 --- a/rust/origen/cli/src/commands/setup.rs +++ b/rust/origen/cli/src/commands/setup.rs @@ -6,7 +6,6 @@ use origen::core::term::*; use std::fs; use std::io; use std::path::PathBuf; -use std::ffi::OsString; use std::process::Command; const POETRY_INSTALLER: &str = @@ -65,7 +64,7 @@ pub fn run() { fs::read_to_string(&get_poetry_file).expect("Unable to read Poetry install file"); let new_data = data.replace( "/bin/env python", - &format!("/bin/env {}", PYTHON_CONFIG.command.clone().into_string().unwrap()), + &format!("/bin/env {}", PYTHON_CONFIG.command), ); fs::write(&get_poetry_file, new_data).expect("Unable to write Poetry install file"); @@ -80,8 +79,8 @@ pub fn run() { // Have to use --preview here to get a 1.0.0 pre version, can only use versions for // official releases Command::new(&PYTHON_CONFIG.poetry_command) - .arg(OsString::from("self:update")) - .arg(OsString::from("--preview")) + .arg("self:update") + .arg("--preview") .status() .expect("Something wend wrong updating Poetry"); } @@ -92,8 +91,8 @@ pub fn run() { print!("Are the app's deps. installed? ... "); let status = Command::new(&PYTHON_CONFIG.poetry_command) - .arg(OsString::from("install")) - .arg(OsString::from("--no-root")) + .arg("install") + .arg("--no-root") .status(); if status.is_ok() { diff --git a/rust/origen/cli/src/python.rs b/rust/origen/cli/src/python.rs index 22a9425a..bfb1b552 100644 --- a/rust/origen/cli/src/python.rs +++ b/rust/origen/cli/src/python.rs @@ -1,7 +1,6 @@ // Responsible for managing Python execution use std::process::Command; -use std::ffi::OsString; use origen::STATUS; use semver::Version; use std::path::PathBuf; @@ -22,7 +21,7 @@ lazy_static! { pub struct Config { pub available: bool, - pub command: OsString, + pub command: String, pub version: Version, pub error: String, pub poetry_command: PathBuf, @@ -41,7 +40,7 @@ impl Default for Config { if version >= Version::parse(MIN_PYTHON_VERSION).unwrap() { return Config { available: true, - command: OsString::from(cmd.to_string()), + command: cmd.to_string(), version: version, error: "".to_string(), poetry_command: poetry_cmd, @@ -57,7 +56,7 @@ impl Default for Config { } Config { available: false, - command: OsString::new(), + command: String::new(), version: Version::parse("0.0.0").unwrap(), error: msg, poetry_command: PathBuf::new(), @@ -67,7 +66,7 @@ impl Default for Config { /// Get the Python version from the given command fn get_version(command: &str) -> Option { - match Command::new(OsString::from(command)).arg("--version").output() { + match Command::new(command).arg("--version").output() { Ok(output) => return extract_version(std::str::from_utf8(&output.stdout).unwrap()), Err(_e) => return None, } @@ -105,7 +104,7 @@ pub fn run(code: &str) { .arg("run") .arg(&PYTHON_CONFIG.command) .arg("-c") - .arg(OsString::from(&code)) + .arg(&code) .status(); }