Skip to content

Commit

Permalink
Add yen exec
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharsadhwani committed Jun 26, 2024
1 parent 350a16d commit c737b2b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
15 changes: 0 additions & 15 deletions src/yen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,3 @@ def install_package(
shutil.move(executable_path, shim_path)

return shim_path, False # False as in package didn't exist and was just installed


def run_package(shim_path: str, args: list[str]) -> None:
subprocess.run([shim_path, *args])


def create_symlink(python_bin_path: str, python_version: str) -> None:
python_version = "python" + ".".join(python_version.split(".")[:2])
symlink_path = os.path.join(PYTHON_INSTALLS_PATH, python_version)

if os.path.exists(symlink_path):
os.remove(symlink_path)

os.symlink(python_bin_path, symlink_path)
check_path(PYTHON_INSTALLS_PATH)
16 changes: 7 additions & 9 deletions src/yen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import argparse
import os.path
import subprocess
import sys
from typing import Literal

Expand All @@ -12,18 +13,16 @@
PACKAGE_INSTALLS_PATH,
ExecutableDoesNotExist,
check_path,
create_symlink,
create_venv,
ensure_python,
ensurepath,
install_package,
run_package,
)
from yen.github import NotAvailable, list_pythons


class YenArgs:
command: Literal["list", "ensurepath", "create", "install", "run", "use"]
command: Literal["list", "ensurepath", "create", "install", "run", "exec"]
python: str
venv_path: str
package_name: str
Expand Down Expand Up @@ -68,8 +67,8 @@ def cli() -> int:
nargs="*",
)

use_parser = subparsers.add_parser("use")
use_parser.add_argument("-p", "--python", required=True)
exec_parser = subparsers.add_parser("exec")
exec_parser.add_argument("-p", "--python", default=DEFAULT_PYTHON_VERSION)

args = parser.parse_args(namespace=YenArgs)

Expand Down Expand Up @@ -179,9 +178,9 @@ def cli() -> int:
)
return 4

run_package(shim_path, args.run_args)
return subprocess.call([shim_path, *args.run_args])

elif args.command == "use":
elif args.command == "exec":
try:
python_version, python_bin_path = ensure_python(args.python)
except NotAvailable:
Expand All @@ -192,7 +191,6 @@ def cli() -> int:
)
return 1

create_symlink(python_bin_path, python_version)
print(f"\033[1m{python_version}\033[m created 🐍")
return subprocess.call([python_bin_path])

return 0
35 changes: 35 additions & 0 deletions yen-rs/src/commands/exec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::{
process::{exit, Command, Stdio},
str::FromStr,
};

use clap::Parser;
use miette::IntoDiagnostic;

use crate::{github::Version, utils::ensure_python, DEFAULT_PYTHON_VERSION};

/// Install and run a Python package.
#[derive(Parser, Debug)]
pub struct Args {
/// Python version to install package with
#[arg(short, long, default_value_t = Version::from_str(DEFAULT_PYTHON_VERSION).unwrap())]
python: Version,

/// Arguments to pass to the command invocation
#[arg(num_args = 0..)]
run_args: Vec<String>,
}

pub async fn execute(args: Args) -> miette::Result<()> {
let (_, python_bin_path) = ensure_python(args.python).await?;

let output = Command::new(python_bin_path)
.args(args.run_args)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
.into_diagnostic()?;

exit(output.status.code().unwrap_or(1));
}
1 change: 1 addition & 0 deletions yen-rs/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod create;
pub mod ensurepath;
pub mod exec;
pub mod install;
pub mod list;
pub mod run;
4 changes: 3 additions & 1 deletion yen-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use env_logger::{Builder, WriteStyle};
use lazy_static::lazy_static;
use log::LevelFilter;

use commands::{create, ensurepath, install, list, run};
use commands::{create, ensurepath, exec, install, list, run};
use regex::Regex;
use reqwest::Client;

Expand Down Expand Up @@ -70,6 +70,7 @@ enum Command {
#[clap(alias = "c")]
Create(create::Args),
Ensurepath(ensurepath::Args),
Exec(exec::Args),
Install(install::Args),
Run(run::Args),
}
Expand Down Expand Up @@ -102,6 +103,7 @@ async fn execute(args: Args) -> miette::Result<()> {
match args.command {
Command::Create(args) => create::execute(args).await,
Command::Ensurepath(args) => ensurepath::execute(args).await,
Command::Exec(args) => exec::execute(args).await,
Command::List(args) => list::execute(args).await,
Command::Install(args) => install::execute(args).await,
Command::Run(args) => run::execute(args).await,
Expand Down

0 comments on commit c737b2b

Please sign in to comment.