📥 installer#

Installer module for stouputils.

Provides functions for platform-agnostic installation tasks by dispatching to platform-specific implementations (Windows, Linux/macOS).

It handles getting installation paths, adding programs to the PATH environment variable, and installing programs from local zip files or URLs.

Main functions:

Example usage from the upscaler application:

from stouputils.installer import check_executable

# Define download URLs for different platforms
WAIFU2X_RELEASES = {
    "Windows": "https://github.com/nihui/waifu2x-ncnn-vulkan/releases/download/20220728/waifu2x-ncnn-vulkan-20220728-windows.zip",
    "Linux": "https://github.com/nihui/waifu2x-ncnn-vulkan/releases/download/20220728/waifu2x-ncnn-vulkan-20220728-ubuntu.zip",
    "Darwin": "https://github.com/nihui/waifu2x-ncnn-vulkan/releases/download/20220728/waifu2x-ncnn-vulkan-20220728-macos.zip"
}

# Check if the executable exists, download if needed
check_executable(
    "waifu2x-ncnn-vulkan",
    "waifu2x-ncnn-vulkan",  # Help text to verify the executable
    WAIFU2X_RELEASES
)

# For executables in a subdirectory (e.g., FFmpeg in bin/)
FFMPEG_RELEASES = {
    "Windows": "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip",
    "Linux": "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz",
}
check_executable(
    "ffmpeg",
    "ffmpeg version",
    FFMPEG_RELEASES,
    append_to_path="bin"  # Executables are in the bin/ subdirectory
)

Manual installation example:

from stouputils.installer import install_program, get_install_path, add_to_path

# Get the appropriate installation path
install_path = get_install_path(
    "my-program",
    ask_global=0,  # Ask user: 0=ask, 1=force global, 2=force local
    add_path=True,
    append_to_path="bin"
)

# Install from a URL
success = install_program(
    "https://example.com/program.zip",
    install_path=install_path,
    program_name="my-program",
    add_path=True,
    append_to_path="bin"
)

# Or install from a local file
success = install_program(
    "/path/to/program.zip",
    install_path=install_path,
    program_name="my-program"
)

# Manually add to PATH if needed
if success:
    add_to_path(f"{install_path}/bin")
import subprocess
import zipfile
import stouputils as stp
from stouputils.installer import install_program

# A zip holding a tiny program, like the ones published on GitHub releases
with zipfile.ZipFile("tool.zip", "w") as archive:
    archive.writestr("tool/bin/tool", "echo tool 1.0 is ready")

# Extract it into programs/tool (add_path=True would also add it to the PATH)
install_program("tool.zip", install_path="programs/tool", program_name="tool", add_path=False)

# Run the installed program
result = subprocess.run(["sh", "programs/tool/bin/tool"], capture_output=True, text=True)
stp.info(result.stdout)
Terminal output of the example, the program extracted then run

⬇️ downloader#

Downloader module for the installer subpackage.

Provides functions for downloading and installing programs from URLs. It handles platform-specific downloads, checking if programs are already installed, and setting up the downloaded programs for use.

This module works with the main installer module to provide a complete installation solution for programs that need to be downloaded from the internet.

download_executable(
download_urls: dict[str, str],
program_name: str,
append_to_path: str = '',
) → bool[source]#

Ask the user if they want to download the program (ex: waifu2x-ncnn-vulkan). If yes, try to download the program from the GitHub releases page.

Parameters:
  • download_urls – The URLs to download the program from.

  • program_name – The name of the program to download.

  • append_to_path – String to append to installation path when adding to PATH.

Returns:

True if the program is now ready to use, False otherwise.

> # Download waifu2x-ncnn-vulkan for the current platform
> urls = {
>     "Windows": "https://github.com/.../waifu2x-windows.zip",
>     "Linux": "https://github.com/.../waifu2x-ubuntu.zip",
> }
> success = download_executable(urls, "waifu2x-ncnn-vulkan")
> # User is prompted: "Program executable not found, would you like to download it? (Y/n)"
> # If yes, downloads and installs the program

> # Download FFmpeg with executables in bin/ subdirectory
> ffmpeg_urls = {
>     "Windows": "https://github.com/.../ffmpeg-win64.zip",
>     "Linux": "https://github.com/.../ffmpeg-linux64.tar.xz",
> }
> download_executable(ffmpeg_urls, "ffmpeg", append_to_path="bin")
True
check_executable(
executable: str,
executable_help_text: str,
download_urls: dict[str, str],
append_to_path: str = '',
) → None[source]#

Check if the executable exists, optionally download it if it doesn’t.

This function runs the executable with -h flag and checks if the help text matches. If the executable is not found or the help text doesn’t match, it prompts the user to download it automatically from the provided URLs.

Parameters:
  • executable – The path to the executable.

  • executable_help_text – The help text to check for in the executable’s output.

  • download_urls – The URLs to download the executable from.

  • append_to_path – The path to append to the executable’s path. (ex: “bin” if executables are in the bin folder)

> from stouputils.installer import check_executable
>
> # Check if waifu2x-ncnn-vulkan is installed
> WAIFU2X_RELEASES = {
>     "Windows": "https://github.com/.../waifu2x-windows.zip",
>     "Linux": "https://github.com/.../waifu2x-ubuntu.zip",
> }
> check_executable(
>     "waifu2x-ncnn-vulkan",
>     "waifu2x-ncnn-vulkan",  # Text expected in help output
>     WAIFU2X_RELEASES
> )
> # If not found, user is prompted to download

> # Check FFmpeg (executables in bin/ subdirectory)
> FFMPEG_RELEASES = {
>     "Windows": "https://github.com/.../ffmpeg-win64.zip",
> }
> check_executable(
>     "ffmpeg",
>     "ffmpeg version",
>     FFMPEG_RELEASES,
>     append_to_path="bin"
> )

🚀 main#

Main module of the installer subpackage for stouputils.

Provides functions for installing programs from local zip files or URLs. It handles downloading, extracting, and setting up programs in a platform-agnostic way.

This module contains the core installation functions that are used by both the Windows and Linux/macOS specific modules.

extract_archive(
extraction_path: str,
temp_dir: str,
extract_func: Callable[[str], None],
get_file_list_func: Callable[[], list[str]],
) → None[source]#

Helper function to extract archive files with consistent handling.

Parameters:
  • extraction_path – Path where files should be extracted

  • temp_dir – Temporary directory for intermediate extraction

  • extract_func – Function to extract the archive

  • get_file_list_func – Function to get the list of files in the archive

get_install_path(
program_name: str,
platform_str: str = 'Linux',
ask_global: int = 0,
add_path: bool = True,
append_to_path: str = '',
) → str[source]#

Get the installation path for the program on the current platform.

Parameters:
  • program_name – The name of the program to install.

  • platform_str – The platform to get the installation path for.

  • ask_global – Whether to ask the user for a path, 0 = ask, 1 = install globally, 2 = install locally.

  • add_path – Whether to add the program to the PATH environment variable.

  • append_to_path – String to append to the installation path when adding to PATH. (ex: “bin” if executables are in the bin folder)

Returns:

The installation path for the program.

> # Get install path, ask user if they want global or local
> path = get_install_path("waifu2x-ncnn-vulkan", ask_global=0)
> # User will be prompted to choose between global and local installation

> # Force local installation
> path = get_install_path("my-tool", ask_global=2)
> # Returns a local path like "/home/user/my-tool" or "C:/Users/user/my-tool"

> # Force global installation
> path = get_install_path("my-tool", ask_global=1)
> # Returns a global path like "/usr/local/bin/my-tool" or "C:/Program Files/my-tool"
add_to_path(
install_path: str,
platform_str: str = 'Linux',
) → bool[source]#

Add the program to the PATH environment variable.

Parameters:
  • install_path – The path to the program to add to the PATH environment variable.

  • platform_str – The platform you are running on (ex: “Windows”, “Linux”, “Darwin”, …), we use this to determine the installation path if not provided.

Returns:

True if add to PATH was successful, False otherwise.

> # Add a directory to PATH on the current platform
> success = add_to_path("/usr/local/bin/my-program")
> print(success)
True

> # Add a Windows path (when running on Windows)
> add_to_path("C:/Program Files/MyApp/bin", platform_str="Windows")
True
install_program(
input_path: str,
install_path: str = '',
platform_str: str = 'Linux',
program_name: str = '',
add_path: bool = True,
append_to_path: str = '',
) → bool[source]#

Install a program to a specific path from a local zip file or URL.

Parameters:
  • input_path – Path to a zip file or a download URL.

  • install_path – The directory to extract the program into, we ask user for a path if not provided.

  • platform_str – The platform you are running on (ex: “Windows”, “Linux”, “Darwin”, …), we use this to determine the installation path if not provided.

  • add_path – Whether to add the program to the PATH environment variable.

  • program_name – Override the program name, we get it from the input path if not provided.

  • append_to_path – String to append to the installation path when adding to PATH. (ex: “bin” if executables are in the bin folder)

Returns:

True if installation was successful, False otherwise.

> # Install from a URL
> success = install_program(
>     "https://github.com/example/tool/releases/download/v1.0/tool.zip",
>     install_path="/usr/local/bin/tool",
>     program_name="tool",
>     add_path=True
> )
> print(success)
True

> # Install from a local file
> install_program(
>     "/downloads/program.zip",
>     install_path="C:/Program Files/MyProgram",
>     add_path=False
> )
True

> # Install with executables in a subdirectory
> install_program(
>     "https://example.com/ffmpeg.zip",
>     program_name="ffmpeg",
>     append_to_path="bin"  # Adds /path/to/ffmpeg/bin to PATH
> )
True

💻 windows#

Installer module for Windows specific functions.

Provides Windows specific implementations for checking administrator privileges, determining appropriate installation paths (global/local), and modifying the user’s PATH environment variable.

add_to_path_windows(install_path: str) → bool | None[source]#

Add install_path to the User PATH environment variable on Windows.

Parameters:

install_path – The path to add to the User PATH environment variable.

Returns:

True if the path was added to the User PATH environment variable, None otherwise.

check_admin_windows() → bool[source]#

Check if the script is running with administrator privileges on Windows.

get_install_path_windows(
program_name: str,
ask_global: int = 0,
add_path: bool = True,
append_to_path: str = '',
default_global: str = 'C:\\Program Files',
) → str[source]#

Get the installation path for the program

Parameters:
  • program_name – The name of the program to install.

  • ask_global – 0 = ask for anything, 1 = install globally, 2 = install locally

  • add_path – Whether to add the program to the PATH environment variable. (Only if installed globally)

  • append_to_path – String to append to the installation path when adding to PATH. (ex: “bin” if executables are in the bin folder)

  • default_global – The default global installation path. (Default is “C:Program Files” which is the most common location for executables on Windows)

Returns:

The installation path.