Skip to content

Command Launcher Update #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions rust/origen/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pub fn launch(
);

if let Some(t) = targets {
let _t: Vec<String> = 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<String> = t.iter().map(|__t| format!("r'{}'", __t)).collect();
cmd += &format!(", targets=[{}]", &_t.join(",")).to_string();
}

Expand All @@ -30,7 +31,8 @@ pub fn launch(
}

if files.is_some() {
let f: Vec<String> = 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<String> = files.unwrap().iter().map(|f| format!("r'{}'", f)).collect();
cmd += &format!(", files=[{}]", f.join(",")).to_string();
}

Expand Down
10 changes: 5 additions & 5 deletions rust/origen/cli/src/commands/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ 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;
use std::path::PathBuf;
use std::process::Command;

const POETRY_INSTALLER: &str =
"https://raw.githubusercontent.com/sdispater/poetry/1.0.0b7/get-poetry.py";
Expand Down Expand Up @@ -69,16 +69,16 @@ pub fn run() {
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()))
Command::new(&PYTHON_CONFIG.command)
.arg(get_poetry_file)
.arg("--yes")
.status()
.expect("Something went wrong install Poetry");

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("self:update")
.arg("--preview")
.status()
Expand All @@ -90,7 +90,7 @@ 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("install")
.arg("--no-root")
.status();
Expand Down
22 changes: 11 additions & 11 deletions rust/origen/cli/src/python.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Responsible for managing Python execution

use origen::core::os;
use std::process::Command;
use origen::STATUS;
use semver::Version;
use std::path::PathBuf;

const PYTHONS: &[&str] = &[
"python",
Expand All @@ -23,7 +24,7 @@ pub struct Config {
pub command: String,
pub version: Version,
pub error: String,
pub poetry_command: String,
pub poetry_command: PathBuf,
}

impl Default for Config {
Expand All @@ -33,10 +34,9 @@ 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,
Expand All @@ -56,25 +56,25 @@ impl Default for Config {
}
Config {
available: false,
command: "".to_string(),
command: String::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<Version> {
match os::cmd(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,
}
}

/// Returns the version of poetry (obtained from running "poetry --version")
pub fn poetry_version() -> Option<Version> {
match os::cmd(&PYTHON_CONFIG.poetry_command)
match Command::new(&PYTHON_CONFIG.poetry_command)
.arg("--version")
.output()
{
Expand All @@ -100,7 +100,7 @@ fn extract_version(text: &str) -> Option<Version> {

/// 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)
.arg("-c")
Expand Down
16 changes: 0 additions & 16 deletions rust/origen/src/core/os.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::process::Command;

pub fn on_windows() -> bool {
if cfg!(windows) {
return true;
Expand All @@ -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;
}
}