Skip to content
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
17 changes: 15 additions & 2 deletions python/cargo_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
CARGO_TOML = "Cargo.toml"
cargo_path = which("cargo")


def log_message(message: str, level: str = "info") -> None:
"""Print a formatted message with an emoji prefix."""
prefixes = {"info": "📍", "success": "📦", "error": "❌"}
print(f"{prefixes.get(level, '📍')} {message}")


@lru_cache(maxsize=1)
def find_cargo_toml(start_dir: str = ".") -> str | None:
"""Search for Cargo.toml starting from the specified directory.
Expand Down Expand Up @@ -42,20 +44,23 @@ def find_cargo_toml(start_dir: str = ".") -> str | None:
log_message("Cargo.toml not found in current or parent directories.", "error")
return None


def check_cargo_installed() -> bool:
"""Check if cargo is installed and available in PATH."""
if not cargo_path:
log_message("cargo is not installed or not found in PATH.", "error")
return False
return True


def validate_library_name(lib: str) -> bool:
"""Check if the library name is valid."""
if not lib or any(c in lib for c in '<>|&;"'):
log_message(f"Invalid library name: {lib}", "error")
return False
return True


def cargo_install(libs: list[str]) -> None:
"""Install Rust libraries using cargo add.

Expand All @@ -72,7 +77,10 @@ def cargo_install(libs: list[str]) -> None:
log_message(f"Directory {abs_cargo_dir} does not exist.", "error")
return
if not os.access(abs_cargo_dir, os.R_OK | os.X_OK):
log_message(f"Permission denied to access {abs_cargo_dir}. Try running with sudo.", "error")
log_message(
f"Permission denied to access {abs_cargo_dir}. Try running with sudo.",
"error",
)
return

original_dir = os.getcwd()
Expand All @@ -91,12 +99,16 @@ def cargo_install(libs: list[str]) -> None:
log_message("stdout:\n" + e.stdout)
log_message("stderr:\n" + e.stderr)
except PermissionError as e:
log_message(f"Permission denied: {e}. Run with sudo or check directory permissions.", "error")
log_message(
f"Permission denied: {e}. Run with sudo or check directory permissions.",
"error",
)
except FileNotFoundError as e:
log_message(f"File or directory error: {e}. Check Cargo.toml path.", "error")
finally:
os.chdir(original_dir)


def main():
if len(sys.argv) < 2:
log_message("Provide at least one Rust package name", "error")
Expand All @@ -112,5 +124,6 @@ def main():

cargo_install(libraries)


if __name__ == "__main__":
main()
7 changes: 6 additions & 1 deletion python/luarocks_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@

LUAROCKS_FLAG = "--local"


def log_message(message: str, level: str = "info") -> None:
prefixes = {"info": "📍", "success": "📦", "error": "❌"}
print(f"{prefixes.get(level, '📍')} {message}")


def check_luarocks_installed() -> bool:
"""Return True if luarocks binary is present in PATH."""
if not which("luarocks"):
log_message("luarocks is not installed or not found in PATH.", "error")
return False
return True


def validate_library_name(lib: str) -> bool:
"""Simple validation to avoid injection / weird names."""
if not lib or any(c in lib for c in '<>|&;"'):
log_message(f"Invalid library name: {lib}", "error")
return False
return True


def install_luarocks(libs: List[str]) -> None:
"""Install one or more luarocks packages."""
if not check_luarocks_installed():
Expand Down Expand Up @@ -62,6 +66,7 @@ def install_luarocks(libs: List[str]) -> None:
except PermissionError as e:
log_message(f"Permission error: {e}", "error")


def main() -> None:
if len(sys.argv) < 2:
log_message("Provide at least one LuaRocks package name", "error")
Expand All @@ -74,6 +79,6 @@ def main() -> None:

install_luarocks(libraries)


if __name__ == "__main__":
main()

11 changes: 9 additions & 2 deletions python/npm_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
from shutil import which
import sys


def log_message(message: str, level: str = "info") -> None:
prefixes = {"info": "📍", "success": "📦", "error": "❌"}
print(f"{prefixes.get(level, '📍')} {message}")


def check_npm_installed() -> bool:
if not which("npm"):
log_message("npm is not installed or not found in PATH.", "error")
return False
return True


def validate_library_name(lib: str) -> bool:
if not lib or any(c in lib for c in '<>|&;"'):
log_message(f"Invalid package name: {lib}", "error")
return False
return True


def install_npm(lib: str) -> None:
"""Install an npm package if not already present."""
if not check_npm_installed():
Expand All @@ -30,7 +34,9 @@ def install_npm(lib: str) -> None:

# First try to check if package is present. Use check=False so tests can mock returncode.
try:
result = run(["npm", "list", lib], stdout=PIPE, stderr=PIPE, text=True, check=False)
result = run(
["npm", "list", lib], stdout=PIPE, stderr=PIPE, text=True, check=False
)
# If list contains package name — treat as installed
if lib in (result.stdout or ""):
log_message(f"{lib} already installed", "success")
Expand Down Expand Up @@ -61,6 +67,7 @@ def install_npm(lib: str) -> None:
log_message(f"stdout:\n{e.stdout}")
log_message(f"stderr:\n{e.stderr}")


def main() -> None:
if len(sys.argv) < 2:
print("Provide at least one npm package name")
Expand All @@ -69,6 +76,6 @@ def main() -> None:
for lib in sys.argv[1:]:
install_npm(lib)


if __name__ == "__main__":
main()

106 changes: 0 additions & 106 deletions python/pip_install:q.py

This file was deleted.