R/install-python.R

Defines functions generate_python_runtime_manifest python_expected_sha256 python_sha256sums_url install_python_standalone python_executable python_is_installed python_install_path python_download_url resolve_python_pbs python_resolve_pbs pbs_latest_release pbs_list_releases

Documented in generate_python_runtime_manifest install_python_standalone pbs_latest_release pbs_list_releases python_download_url python_executable python_expected_sha256 python_install_path python_is_installed python_resolve_pbs python_sha256sums_url resolve_python_pbs

#' List python-build-standalone releases from GitHub
#'
#' Fetches release metadata from the astral-sh/python-build-standalone GitHub
#' API. Returns a list of release objects, each with a `tag_name` field and an
#' `assets` list whose elements have a `name` field. Releases are ordered
#' newest first (GitHub API default). This function is intentionally a thin
#' network wrapper so it can be stubbed in tests.
#'
#' @return List of release objects from the GitHub releases API.
#' @keywords internal
pbs_list_releases <- function() {
  url <- "https://api.github.com/repos/astral-sh/python-build-standalone/releases"
  tmp <- tempfile(fileext = ".json")
  on.exit(unlink(tmp), add = TRUE)

  status <- tryCatch(
    utils::download.file(url, tmp, mode = "wb", quiet = TRUE),
    error = function(e) {
      cli::cli_abort(c(
        "Could not reach the python-build-standalone release API.",
        "x" = "{e$message}",
        "i" = "Check your internet connection.",
        "i" = paste0(
          "Or pin a version in your config matching the maintained default: ",
          "{.val {SHINYELECTRON_DEFAULTS$runtime_versions$python$version}}."
        )
      ))
    }
  )
  if (!identical(status, 0L)) {
    cli::cli_abort(c(
      "Could not reach the python-build-standalone release API.",
      "x" = "HTTP request returned status {status}",
      "i" = "Check your internet connection.",
      "i" = paste0(
        "Or pin a version in your config matching the maintained default: ",
        "{.val {SHINYELECTRON_DEFAULTS$runtime_versions$python$version}}."
      )
    ))
  }

  jsonlite::fromJSON(tmp, simplifyVector = FALSE)
}

#' Fetch the latest python-build-standalone release from GitHub
#'
#' Queries the single `releases/latest` endpoint. This is a small, reliable
#' call, unlike the full release list which is large enough to time out (HTTP
#' 504) on this repository. The latest release carries the newest patch of every
#' currently supported CPython minor, so it resolves `"latest"` and any current
#' version on its own. A thin network wrapper so tests can stub it.
#'
#' @return A single release object with a `tag_name` field and an `assets` list.
#' @keywords internal
pbs_latest_release <- function() {
  url <- "https://api.github.com/repos/astral-sh/python-build-standalone/releases/latest"
  tmp <- tempfile(fileext = ".json")
  on.exit(unlink(tmp), add = TRUE)

  status <- tryCatch(
    utils::download.file(url, tmp, mode = "wb", quiet = TRUE),
    error = function(e) {
      cli::cli_abort(c(
        "Could not reach the python-build-standalone release API.",
        "x" = "{e$message}"
      ))
    }
  )
  if (!identical(status, 0L)) {
    cli::cli_abort("python-build-standalone release API returned status {status}.")
  }

  jsonlite::fromJSON(tmp, simplifyVector = FALSE)
}

#' Resolve a Python version to its python-build-standalone release
#'
#' Scans release asset names of the form
#' `cpython-<ver>+<release>-<arch>-<os>-install_only.tar.gz`. For an explicit
#' version, returns the newest release that contains an asset for that version.
#' For `"latest"`, returns the first CPython version found in the newest release.
#'
#' Resolution tries the lightweight `releases/latest` endpoint first via
#' [pbs_latest_release()], which carries the newest patch of every supported
#' minor and resolves the common case in one reliable call. It falls back to the
#' full release history ([pbs_list_releases()], which can time out) only when the
#' latest release does not contain the requested version. Network access is
#' isolated to those two helpers so tests can stub them.
#'
#' @param version Character string. A concrete Python version such as
#'   `"3.14.6"`, or `"latest"` for the newest available build.
#' @return Named list with elements `version` (character) and `release`
#'   (character YYYYMMDD tag).
#' @keywords internal
python_resolve_pbs <- function(version = "latest") {
  # Asset pattern: cpython-<ver>+<release_date>-<arch>-<os>-install_only.tar.gz
  asset_re <- "^cpython-(\\d+\\.\\d+\\.\\d+)\\+(\\d{8})-.*-install_only\\.tar\\.gz$"

  # Return the first install_only asset matching `version` across a list of
  # release objects, or NULL when none match. For "latest", every asset of a
  # release is considered and the highest Python version wins, since a release
  # lists its files in name order, not version order.
  scan_releases <- function(releases) {
    for (rel in releases) {
      found <- list()
      for (asset in rel$assets) {
        m <- regmatches(asset$name, regexec(asset_re, asset$name))[[1]]
        if (length(m) != 3L) next
        if (identical(version, "latest")) {
          found[[length(found) + 1L]] <- list(version = m[[2L]], release = m[[3L]])
        } else if (identical(m[[2L]], version)) {
          return(list(version = m[[2L]], release = m[[3L]]))
        }
      }
      if (length(found) > 0L) {
        versions <- numeric_version(vapply(found, `[[`, character(1), "version"))
        # order(), not which.max(): R 4.5 cannot coerce a version to a number.
        return(found[[order(versions, decreasing = TRUE)[[1L]]]])
      }
    }
    NULL
  }

  # Fast path: the latest release resolves "latest" and any current version in a
  # single lightweight call. Treat a failure here as a miss and fall back.
  hit <- tryCatch(scan_releases(list(pbs_latest_release())), error = function(e) NULL)
  if (!is.null(hit)) return(hit)

  # Fallback: scan the full release history for an older patch. This call can
  # time out; pbs_list_releases() aborts with actionable guidance if it does.
  hit <- scan_releases(pbs_list_releases())
  if (!is.null(hit)) return(hit)

  if (identical(version, "latest")) {
    cli::cli_abort(c(
      "No python-build-standalone release with a CPython install_only asset was found.",
      "i" = "Check your internet connection.",
      "i" = paste0(
        "Or pin the maintained default version in your config: ",
        "{.val {SHINYELECTRON_DEFAULTS$runtime_versions$python$version}}."
      )
    ))
  }
  cli::cli_abort(c(
    "Python {.val {version}} was not found in a recent python-build-standalone release.",
    "i" = paste0(
      "Check available releases at ",
      "{.url https://github.com/astral-sh/python-build-standalone/releases}."
    ),
    "i" = paste0(
      "Or pin the maintained default: ",
      "{.val {SHINYELECTRON_DEFAULTS$runtime_versions$python$version}}."
    )
  ))
}

#' Resolve a Python version to its python-build-standalone release (offline-first)
#'
#' Uses the offline default pin when the version matches it (no network), and
#' only queries the registry for a custom or "latest" version.
#'
#' @param version Character string. A concrete Python version such as
#'   `"3.14.6"`, or `"latest"` for the newest available build.
#' @return Named list with elements `version` (character) and `release`
#'   (character YYYYMMDD tag).
#' @keywords internal
resolve_python_pbs <- function(version) {
  pin <- SHINYELECTRON_DEFAULTS$runtime_versions$python
  if (identical(version, pin$version)) {
    return(list(version = pin$version, release = pin$release))
  }
  python_resolve_pbs(version)
}

#' Construct download URL for portable Python
#'
#' Uses python-build-standalone releases for portable Python builds.
#'
#' @param version Character string. Python version (e.g., "3.14.6").
#' @param platform Character string. Target platform.
#' @param arch Character string. Target architecture.
#' @param release_date Character string. python-build-standalone release tag
#'   (YYYYMMDD). Required; must match a release tag on
#'   \url{https://github.com/astral-sh/python-build-standalone/releases}.
#' @return Character string. Download URL.
#' @keywords internal
python_download_url <- function(version, platform = NULL, arch = NULL,
                                release_date) {
  # release_date must match an astral-sh/python-build-standalone release tag
  # Check https://github.com/astral-sh/python-build-standalone/releases for latest
  platform <- platform %||% detect_current_platform()
  arch <- arch %||% detect_current_arch()

  pbs_arch <- switch(arch,
    "arm64" = "aarch64",
    "x64" = "x86_64",
    cli::cli_abort("Unsupported architecture for portable Python: {.val {arch}}")
  )
  pbs_os <- switch(platform,
    "win" = "pc-windows-msvc",
    "mac" = "apple-darwin",
    "linux" = "unknown-linux-gnu",
    cli::cli_abort("Unsupported platform for portable Python: {.val {platform}}")
  )

  # python-build-standalone ships the `install_only` asset as tar.gz on every
  # platform, Windows included. Modern Windows (10+) has tar.exe with gzip
  # support built in, so extraction works the same way everywhere.
  paste0(
    "https://github.com/astral-sh/python-build-standalone/releases/download/",
    release_date, "/",
    "cpython-", version, "+", release_date, "-",
    pbs_arch, "-", pbs_os, "-install_only.tar.gz"
  )
}

#' Get the installation path for a cached Python version
#'
#' @param version Character string. Python version.
#' @param platform Character string. Platform (default: current).
#' @param arch Character string. Architecture (default: current).
#' @return Character string. Path to the cached Python installation.
#' @keywords internal
python_install_path <- function(version, platform = NULL, arch = NULL) {
  platform <- platform %||% detect_current_platform()
  arch <- arch %||% detect_current_arch()
  fs::path(cache_dir(), "python", platform, arch, version)
}

#' Check if a portable Python version is installed
#'
#' @param version Character string. Python version to check.
#' @param platform Character string. Platform (default: current).
#' @param arch Character string. Architecture (default: current).
#' @return Logical.
#' @keywords internal
python_is_installed <- function(version, platform = NULL, arch = NULL) {
  path <- python_install_path(version, platform, arch)
  fs::dir_exists(path)
}

#' Get the path to the Python executable in a cached installation
#'
#' @param version Character string. Python version.
#' @param platform Character string. Platform (default: current).
#' @param arch Character string. Architecture (default: current).
#' @return Character string or NULL.
#' @keywords internal
python_executable <- function(version, platform = NULL, arch = NULL) {
  platform <- platform %||% detect_current_platform()
  arch <- arch %||% detect_current_arch()
  install_path <- python_install_path(version, platform, arch)

  if (platform == "win") {
    exe_path <- fs::path(install_path, "python", "python.exe")
  } else {
    exe_path <- fs::path(install_path, "python", "bin", "python3")
  }

  if (fs::file_exists(exe_path)) exe_path else NULL
}

#' Install a portable Python distribution
#'
#' Downloads and caches a portable Python build from python-build-standalone.
#'
#' @param version Character string. Python version to install (e.g.,
#'   `"3.14.6"`). If NULL, the maintained pin in
#'   `SHINYELECTRON_DEFAULTS$runtime_versions$python$version` is used.
#' @param platform Character string. Target platform.
#' @param arch Character string. Target architecture.
#' @param force Logical. Whether to reinstall if already cached.
#' @param verbose Logical. Whether to show progress.
#' @return Character string. Path to the installed Python directory.
#'
#' @seealso [install_r_portable()], [install_nodejs()] for other runtime installers;
#'   [python_executable()] to find the installed Python path.
#'
#' @examplesIf interactive()
#' # Install default Python version
#' install_python_standalone()
#'
#' # Install specific version
#' install_python_standalone(version = "3.12.0")
#'
#' @export
install_python_standalone <- function(version = NULL, platform = NULL, arch = NULL,
                           force = FALSE, verbose = TRUE) {
  platform <- platform %||% detect_current_platform()
  arch <- arch %||% detect_current_arch()

  if (is.null(version)) version <- SHINYELECTRON_DEFAULTS$runtime_versions$python$version

  if (!grepl("^\\d+\\.\\d+\\.\\d+$", version)) {
    cli::cli_abort(c(
      "Invalid Python version format: {.val {version}}",
      "i" = "Expected format: major.minor.patch (e.g., 3.12.0)"
    ))
  }

  pbs <- resolve_python_pbs(version)

  if (verbose) {
    cli::cli_alert_info("Platform: {platform}, Architecture: {arch}")
  }

  expected_sha256 <- python_expected_sha256(version, platform, arch, pbs$release)
  if (is.null(expected_sha256) && verbose) {
    cli::cli_warn(c(
      "Could not fetch the published SHA-256 checksum for Python {version}",
      "i" = "Continuing without checksum verification"
    ))
  }

  download_and_extract_portable_tool(
    label = "Python",
    version = version,
    install_path = python_install_path(version, platform, arch),
    download_url = python_download_url(version, platform, arch, release_date = pbs$release),
    executable_finder = function() python_executable(version, platform, arch),
    force = force,
    is_installed = python_is_installed(version, platform, arch),
    expected_sha256 = expected_sha256,
    verbose = verbose
  )
}

#' URL of the python-build-standalone SHA256SUMS file for a release
#' @param release_date Character. python-build-standalone release tag (YYYYMMDD).
#' @return Character URL.
#' @keywords internal
python_sha256sums_url <- function(release_date) {
  paste0(
    "https://github.com/astral-sh/python-build-standalone/releases/download/",
    release_date, "/SHA256SUMS"
  )
}

#' Published SHA-256 checksum for a portable Python archive
#'
#' python-build-standalone publishes one `SHA256SUMS` file per release listing
#' every asset. Returns `NULL` when unavailable or unmatched so the caller can
#' continue without verification.
#'
#' @inheritParams python_download_url
#' @return Character SHA-256 hash, or `NULL`.
#' @keywords internal
python_expected_sha256 <- function(version, platform = NULL, arch = NULL, release_date) {
  url <- python_download_url(version, platform, arch, release_date = release_date)
  fetch_published_sha256(python_sha256sums_url(release_date), asset_filename = basename(url))
}

#' Generate a Python runtime manifest for auto-download
#'
#' @param version Character string. Python version.
#' @param platform Character string. Target platform.
#' @param arch Character string. Target architecture.
#' @param release_date Character string. python-build-standalone release tag
#'   (YYYYMMDD). If NULL, resolved automatically via `resolve_python_pbs()`.
#' @return Character string. JSON content.
#' @keywords internal
generate_python_runtime_manifest <- function(version, platform = NULL, arch = NULL,
                                             release_date = NULL) {
  platform <- platform %||% detect_current_platform()
  arch <- arch %||% detect_current_arch()

  if (is.null(release_date)) release_date <- resolve_python_pbs(version)$release

  manifest <- list(
    schema_version = MANIFEST_SCHEMA_VERSION,
    language = "python",
    version = version,
    download_url = python_download_url(version, platform, arch, release_date = release_date),
    install_path = paste0("~/.shinyelectron/runtimes/Python-", version),
    platform = platform,
    arch = arch
  )

  # Embed the published SHA-256 so the Electron runtime-downloader verifies the
  # archive before extraction on first launch. Omitted when unavailable.
  sha256 <- python_expected_sha256(version, platform, arch, release_date)
  if (!is.null(sha256)) manifest$sha256 <- sha256

  jsonlite::toJSON(manifest, pretty = TRUE, auto_unbox = TRUE)
}

Try the shinyelectron package in your browser

Any scripts or data that you put into this service are public.

shinyelectron documentation built on Dec. 3, 2026, 9:07 a.m.