Skip to content

psadt

napt.psadt

PSAppDeployToolkit integration for NAPT.

Handles PSAppDeployToolkit (PSADT) release management, caching, and integration with NAPT's build system.

Modules:

Name Description
release

PSADT release download, caching, and version resolution.

napt.psadt.release

PSADT release management for NAPT.

This module fetches, verifies, and caches PSAppDeployToolkit releases from the official GitHub repository through napt.github.

Key Features:

  • Resolve "latest" to the current release, or find a pinned version's release however the repository tags it
  • Download the Template_v4 archive and check it against what the release publishes for it
  • Extract into the cache only once the archive holds what NAPT builds from
Note
  • Caches releases by version: cache/psadt/{version}/
  • Checks the download against the digest (or size) the release publishes for the asset
  • Extracts in a working folder beside the entry, and moves the result into place only once verified, so an interrupted run leaves no entry behind; a working folder a killed run left behind is removed once it is a day old
  • A cache entry counts only when it holds every Template_v4 entry NAPT builds from, the same check the download passes
  • GITHUB_TOKEN in the environment is sent on the API calls, which otherwise count against the unauthenticated limit of 60 per hour

missing_template_entries

missing_template_entries(root: Path) -> list[str]

Lists the Template_v4 entries NAPT needs that a folder lacks.

Parameters:

Name Type Description Default
root Path

An extracted archive, a cache entry, or a build directory.

required

Returns:

Type Description
list[str]

The missing entries, in the order NAPT lists them; empty when the

list[str]

layout is the one NAPT builds from.

Source code in napt/psadt/release.py
def missing_template_entries(root: Path) -> list[str]:
    """Lists the Template_v4 entries NAPT needs that a folder lacks.

    Args:
        root: An extracted archive, a cache entry, or a build directory.

    Returns:
        The missing entries, in the order NAPT lists them; empty when the
        layout is the one NAPT builds from.
    """
    missing = []
    for entry in REQUIRED_TEMPLATE_ENTRIES:
        path = root / entry.rstrip("/")
        present = path.is_dir() if entry.endswith("/") else path.is_file()
        if not present:
            missing.append(entry)
    return missing

is_psadt_cached

is_psadt_cached(version: str, cache_dir: Path) -> bool

Reports whether a PSADT version is cached and complete.

Parameters:

Name Type Description Default
version str

PSADT version to check (e.g., "4.1.7").

required
cache_dir Path

Base cache directory (e.g., Path("cache/psadt")).

required

Returns:

Type Description
bool

True if the cache entry holds every Template_v4 entry NAPT builds

bool

from, False otherwise.

Example

Check if PSADT version is cached:

from pathlib import Path

if is_psadt_cached("4.1.7", Path("cache/psadt")):
    print("Already downloaded!")

Source code in napt/psadt/release.py
def is_psadt_cached(version: str, cache_dir: Path) -> bool:
    """Reports whether a PSADT version is cached and complete.

    Args:
        version: PSADT version to check (e.g., "4.1.7").
        cache_dir: Base cache directory (e.g., Path("cache/psadt")).

    Returns:
        True if the cache entry holds every Template_v4 entry NAPT builds
        from, False otherwise.

    Example:
        Check if PSADT version is cached:
            ```python
            from pathlib import Path

            if is_psadt_cached("4.1.7", Path("cache/psadt")):
                print("Already downloaded!")
            ```

    """
    return not missing_template_entries(cache_dir / version)

get_psadt_release

get_psadt_release(release_spec: str, cache_dir: Path) -> Path

Returns the cache entry for a PSADT release, downloading it if needed.

Resolves "latest" to the current release, or finds a pinned version's release under either tag spelling, then downloads the Template_v4 archive, verifies it, and extracts it into the cache.

Parameters:

Name Type Description Default
release_spec str

Version specifier - either "latest" or specific version (e.g., "4.1.7" or "v4.1.7").

required
cache_dir Path

Base cache directory for PSADT releases.

required

Returns:

Type Description
Path

Path to the cached PSADT directory (cache_dir/{version}).

Raises:

Type Description
NetworkError

If the lookup or download fails, the release has no Template_v4 archive, or the download does not match what the release publishes for it.

PackagingError

If extraction fails, the archive lacks the Template_v4 entries NAPT builds from, or the cache cannot be written.

ConfigError

If release_spec is invalid.

Example

Get latest version:

from pathlib import Path

psadt = get_psadt_release("latest", Path("cache/psadt"))
print(psadt)  # Output: cache/psadt/4.1.7

Get specific version:

psadt = get_psadt_release("4.1.7", Path("cache/psadt"))

Source code in napt/psadt/release.py
def get_psadt_release(release_spec: str, cache_dir: Path) -> Path:
    """Returns the cache entry for a PSADT release, downloading it if needed.

    Resolves "latest" to the current release, or finds a pinned version's
    release under either tag spelling, then downloads the Template_v4
    archive, verifies it, and extracts it into the cache.

    Args:
        release_spec: Version specifier - either "latest" or specific version
            (e.g., "4.1.7" or "v4.1.7").
        cache_dir: Base cache directory for PSADT releases.

    Returns:
        Path to the cached PSADT directory (cache_dir/{version}).

    Raises:
        NetworkError: If the lookup or download fails, the release has no
            Template_v4 archive, or the download does not match what the
            release publishes for it.
        PackagingError: If extraction fails, the archive lacks the
            Template_v4 entries NAPT builds from, or the cache cannot be
            written.
        ConfigError: If release_spec is invalid.

    Example:
        Get latest version:
            ```python
            from pathlib import Path

            psadt = get_psadt_release("latest", Path("cache/psadt"))
            print(psadt)  # Output: cache/psadt/4.1.7
            ```

        Get specific version:
            ```python
            psadt = get_psadt_release("4.1.7", Path("cache/psadt"))
            ```

    """
    logger = get_global_logger()
    token = env_token()
    with make_session() as session:
        release: dict[str, Any] | None = None
        if release_spec == "latest":
            logger.verbose("PSADT", "Resolving 'latest' to current version...")
            release = latest_release(session, PSADT_REPO, token=token)
            tag = release_tag(release)
            version = version_from_tag(tag)
            logger.verbose("PSADT", f"Latest PSADT version: {version}")
        else:
            version = normalize_release_spec(release_spec, "psadt.release", "4.1.7")

        logger.verbose("PSADT", f"PSADT version: {version}")
        cache_dir.mkdir(parents=True, exist_ok=True)
        _remove_stale_work_dirs(cache_dir)

        if is_psadt_cached(version, cache_dir):
            version_dir = cache_dir / version
            logger.info("PSADT", f"Using cached PSADT: {version_dir}")
            return version_dir

        logger.info("PSADT", f"Downloading PSADT {version}...")
        if release is None:
            _, release = release_by_version(session, PSADT_REPO, version, token=token)

        asset = _template_archive(release_tag(release), release)
        name = str(asset.get("name"))
        logger.verbose("PSADT", f"Downloading: {name}")
        content = download(session, asset_url(asset), what=name)

    # Check it is the archive the release published before trusting
    # anything inside it.
    _verify_download(asset, content)
    version_dir = _install_archive(content, name, version, cache_dir)
    logger.verbose("PSADT", f"PSADT {version} cached successfully")
    return version_dir