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
19 changes: 13 additions & 6 deletions python/cargo_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

CARGO_TOML = "Cargo.toml"


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:
def find_cargo_toml(start_dir: str = ".") -> str | None:
"""Search for Cargo.toml starting from the specified directory.

Args:
Expand All @@ -26,7 +28,7 @@ def find_cargo_toml(start_dir: str = '.') -> str | None:
cargo_path = os.path.join(start_dir, CARGO_TOML)
if os.path.exists(cargo_path):
return cargo_path

current_dir = os.path.abspath(start_dir)
while current_dir != os.path.dirname(current_dir):
parent_dir = os.path.dirname(current_dir)
Expand All @@ -36,20 +38,23 @@ def find_cargo_toml(start_dir: str = '.') -> str | None:
current_dir = parent_dir
return None


def check_cargo_installed() -> bool:
"""Check if cargo is installed and available in PATH."""
if not which("cargo"):
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 @@ -60,7 +65,7 @@ def cargo_install(libs: list[str]) -> None:
if not cargo_path:
log_message("Cargo.toml not found in current or parent directories.", "error")
return

abs_cargo_path = os.path.abspath(cargo_path)
original_dir = os.getcwd()
try:
Expand All @@ -80,20 +85,22 @@ def cargo_install(libs: list[str]) -> None:
finally:
os.chdir(original_dir)


def main():
if len(sys.argv) < 2:
log_message("Provide at least one Rust package name", "error")
sys.exit(1)

libraries = [lib for lib in sys.argv[1:] if validate_library_name(lib)]
if not libraries:
log_message("No valid libraries provided", "error")
sys.exit(1)

if not check_cargo_installed():
sys.exit(1)

cargo_install(libraries)


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

LUAROCKS_FLAG = "--local"


def log_message(message: str, level: str = "info") -> None:
"""Print a formatted message with an emoji prefix.

Expand All @@ -26,6 +27,7 @@ def check_luarocks_installed() -> bool:
return False
return True


# --- CHECKING LIBRARY NAME
def validate_library_name(lib: str) -> bool:
"""Check if the library name is valid.
Expand Down Expand Up @@ -55,7 +57,7 @@ def install_luarocks(libs: List[str]) -> None:
for lib in libs:
if not validate_library_name(lib):
continue

log_message(f"Installing LuaRocks package {lib} ...")
try:
result = run(
Expand Down Expand Up @@ -83,13 +85,14 @@ def main():
if len(sys.argv) < 2:
log_message("Provide at least one LuaRocks package name", "error")
sys.exit(1)

libraries = [lib for lib in sys.argv[1:] if validate_library_name(lib)]
if not libraries:
log_message("No valid libraries provided", "error")
sys.exit(1)

install_luarocks(libraries)


if __name__ == "__main__":
main()