-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7415710
commit 030d19e
Showing
13 changed files
with
286 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use std::{fs, process}; | ||
|
||
use crate::{ | ||
pyproject, | ||
util::{abort, success}, | ||
}; | ||
|
||
pub fn reset() { | ||
let pcfg = pyproject::current::get_config().unwrap_or_else(|| process::exit(1)); | ||
if (&pcfg.pypackages_path).exists() && fs::remove_dir_all(&pcfg.pypackages_path).is_err() { | ||
abort("Problem removing `__pypackages__` directory") | ||
} | ||
if (&pcfg.lock_path).exists() && fs::remove_file(&pcfg.lock_path).is_err() { | ||
abort("Problem removing `pyflow.lock`") | ||
} | ||
success("`__pypackages__` folder and `pyflow.lock` removed") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::path::Path; | ||
|
||
use regex::Regex; | ||
|
||
use crate::{commands, pyproject::Config, util::abort}; | ||
|
||
/// Execute a python CLI tool, either specified in `pyproject.toml`, or in a dependency. | ||
pub fn run(lib_path: &Path, bin_path: &Path, vers_path: &Path, cfg: &Config, args: Vec<String>) { | ||
// Allow both `pyflow run ipython` (args), and `pyflow ipython` (opt.script) | ||
if args.is_empty() { | ||
return; | ||
} | ||
|
||
let name = if let Some(a) = args.get(0) { | ||
a.clone() | ||
} else { | ||
abort("`run` must be followed by the script to run, eg `pyflow run black`"); | ||
}; | ||
|
||
// If the script we're calling is specified in `pyproject.toml`, ensure it exists. | ||
|
||
// todo: Delete these scripts as required to sync with pyproject.toml. | ||
let re = Regex::new(r"(.*?):(.*)").unwrap(); | ||
|
||
let mut specified_args: Vec<String> = args.into_iter().skip(1).collect(); | ||
|
||
// If a script name is specified by by this project and a dependency, favor | ||
// this project. | ||
if let Some(s) = cfg.scripts.get(&name) { | ||
let abort_msg = format!( | ||
"Problem running the function {}, specified in `pyproject.toml`", | ||
name, | ||
); | ||
|
||
if let Some(caps) = re.captures(s) { | ||
let module = caps.get(1).unwrap().as_str(); | ||
let function = caps.get(2).unwrap().as_str(); | ||
let mut args_to_pass = vec![ | ||
"-c".to_owned(), | ||
format!(r#"import {}; {}.{}()"#, module, module, function), | ||
]; | ||
|
||
args_to_pass.append(&mut specified_args); | ||
if commands::run_python(bin_path, &[lib_path.to_owned()], &args_to_pass).is_err() { | ||
abort(&abort_msg); | ||
} | ||
} else { | ||
abort(&format!("Problem parsing the following script: {:#?}. Must be in the format module:function_name", s)); | ||
} | ||
return; | ||
} | ||
// None => { | ||
let abort_msg = format!( | ||
"Problem running the CLI tool {}. Is it installed? \ | ||
Try running `pyflow install {}`", | ||
name, name | ||
); | ||
let script_path = vers_path.join("bin").join(name); | ||
if !script_path.exists() { | ||
abort(&abort_msg); | ||
} | ||
|
||
let mut args_to_pass = vec![script_path | ||
.to_str() | ||
.expect("Can't find script path") | ||
.to_owned()]; | ||
|
||
args_to_pass.append(&mut specified_args); | ||
if commands::run_python(bin_path, &[lib_path.to_owned()], &args_to_pass).is_err() { | ||
abort(&abort_msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use std::{path::PathBuf, process}; | ||
|
||
use termcolor::Color; | ||
|
||
use crate::{files, pyproject, util}; | ||
|
||
/// Updates `pyproject.toml` with a new python version | ||
pub fn switch(version: &str) { | ||
let mut pcfg = pyproject::current::get_config().unwrap_or_else(|| process::exit(1)); | ||
|
||
let specified = util::fallible_v_parse(version); | ||
pcfg.config.py_version = Some(specified.clone()); | ||
files::change_py_vers(&PathBuf::from(&pcfg.config_path), &specified); | ||
util::print_color( | ||
&format!("Switched to Python version {}", specified.to_string()), | ||
Color::Green, | ||
); | ||
// Don't exit program here; now that we've changed the cfg version, let's run the normal flow. | ||
} |
Oops, something went wrong.