Add -p base python option to venv command (#92)

This is the same option that `virtualenv` offers, except that we only
support absolute paths atm and not e.g. `-p 3.10` (which we need to
eventually).
This commit is contained in:
konsti 2023-10-12 20:41:52 +02:00 committed by GitHub
parent a622345fbc
commit 6a7954cdd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 6 deletions

View file

@ -8,15 +8,23 @@ use crate::commands::ExitStatus;
use crate::printer::Printer;
/// Create a virtual environment.
pub(crate) async fn venv(path: &Path, mut printer: Printer) -> Result<ExitStatus> {
pub(crate) async fn venv(
path: &Path,
base_python: Option<&Path>,
mut printer: Printer,
) -> Result<ExitStatus> {
// Locate the Python interpreter.
// TODO(charlie): Look at how Maturin discovers and ranks all the available Python interpreters.
let executable = which::which("python3").or_else(|_| which::which("python"))?;
let interpreter_info = gourgeist::get_interpreter_info(&executable)?;
let base_python = if let Some(base_python) = base_python {
base_python.to_path_buf()
} else {
which::which("python3").or_else(|_| which::which("python"))?
};
let interpreter_info = gourgeist::get_interpreter_info(&base_python)?;
writeln!(
printer,
"Using Python interpreter: {}",
format!("{}", executable.display()).cyan()
format!("{}", base_python.display()).cyan()
)?;
// If the path already exists, remove it.
@ -30,7 +38,7 @@ pub(crate) async fn venv(path: &Path, mut printer: Printer) -> Result<ExitStatus
)?;
// Create the virtual environment.
gourgeist::create_venv(path, &executable, &interpreter_info, true)?;
gourgeist::create_venv(path, &base_python, &interpreter_info, true)?;
Ok(ExitStatus::Success)
}

View file

@ -71,6 +71,11 @@ struct UninstallArgs {
#[derive(Args)]
struct VenvArgs {
/// The python interpreter to use for the virtual environment
// Short `-p` to match `virtualenv`
// TODO(konstin): Support e.g. `-p 3.10`
#[clap(short, long)]
python: Option<PathBuf>,
/// The path to the virtual environment to create.
name: PathBuf,
}
@ -143,7 +148,7 @@ async fn main() -> ExitCode {
)
.await
}
Commands::Venv(args) => commands::venv(&args.name, printer).await,
Commands::Venv(args) => commands::venv(&args.name, args.python.as_deref(), printer).await,
};
match result {