Skip to content

vast-ai/vast-cli / vastai

vastai

Flagged

Vast.ai CLI to manage GPU instances, volumes, serverless endpoints, and billing.

FileKindSize
SKILL.mdskill md18.6 KB
__init__.pyscript1.8 KB
_base.pyscript1.6 KB
api/__init__.pyscript31 B
api/auth.pyscript15.3 KB
api/billing.pyscript10.4 KB
api/client.pyscript9.1 KB
api/clusters.pyscript4.5 KB
api/deployments.pyscript1.4 KB
api/endpoints.pyscript12.0 KB
api/instances.pyscript17.1 KB
api/keys.pyscript4.3 KB
api/machines.pyscript7.4 KB
api/metrics.pyscript2.6 KB
api/offers.pyscript21.2 KB
api/price_increase.pyscript4.0 KB
api/query.pyscript12.2 KB
api/storage.pyscript14.5 KB
api/teams.pyscript4.4 KB
async_/__init__.pyscript79 B
async_/client.pyscript9.6 KB
async_/results.pyscript1.3 KB
cli/__init__.pyscript25 B
cli/commands/__init__.pyscript757 B
cli/commands/auth.pyscript42.8 KB
cli/commands/benchmarks.pyscript42.0 KB
cli/commands/billing.pyscript30.4 KB
cli/commands/clusters.pyscript7.8 KB
cli/commands/deployments.pyscript7.8 KB
cli/commands/endpoints.pyscript17.3 KB
cli/commands/instances.pyscript55.4 KB
cli/commands/keys.pyscript7.9 KB
cli/commands/machines.pyscript78.4 KB
cli/commands/metrics.pyscript12.7 KB
cli/commands/misc.pyscript11.0 KB
cli/commands/offers.pyscript30.0 KB
cli/commands/price_increase.pyscript14.5 KB
cli/commands/repl.pyscript719 B
cli/commands/storage.pyscript38.9 KB
cli/commands/teams.pyscript6.4 KB
cli/commands/uninstall.pyscript2.4 KB
cli/commands/update.pyscript3.1 KB
cli/display.pyscript20.1 KB
cli/main.pyscript11.5 KB
cli/parser.pyscript18.1 KB
cli/repl/__init__.pyscript318 B
cli/repl/bridge.pyscript7.3 KB
cli/repl/catalog.pyscript6.3 KB
cli/repl/completion.pyscript8.2 KB
cli/repl/session.pyscript7.3 KB
cli/self_test/__init__.pyscript46 B
cli/self_test/machine_diagnostics.pyscript21.0 KB
cli/self_test/runtime_diagnostics.pyscript18.6 KB
cli/self_test/support_bundle.pyscript11.0 KB
cli/selfupdate.pyscript14.0 KB
cli/util.pyscript26.3 KB
cli/utils.pyscript577 B
data/__init__.pyscript303 B
data/deployment.pyscript3.5 KB
data/endpoint.pyscript2.1 KB
data/instance.pyscript3.6 KB
data/offer.pyscript4.4 KB
data/query.pyscript12.8 KB
data/workergroup.pyscript1.4 KB
pdf/__init__.pyscript0 B
pdf/vast.ai-logo-50pct.pngbinary7.4 KB
pdf/vast.ai-logo.pngbinary10.1 KB
pdf/vast_pdf.pyscript19.4 KB
pdf/watcher.shexecutablescript390 B
sdk.pyscript52.2 KB
serverless/__init__.pyscript193 B
serverless/client/__init__.pyscript346 B
serverless/client/client.pyscript36.7 KB
serverless/client/connection.pyscript11.5 KB
serverless/client/endpoint.pyscript6.9 KB
serverless/client/managed.pyscript7.3 KB
serverless/client/request_status.pyscript371 B
serverless/client/session.pyscript2.8 KB
serverless/client/worker.pyscript1.3 KB
serverless/remote/__init__.pyscript245 B
serverless/remote/base.pyscript6.3 KB
serverless/remote/deploy.pyscript12.6 KB
serverless/remote/serialization.pyscript3.7 KB
serverless/remote/serve.pyscript8.6 KB
serverless/remote/serve_deployment.pyscript6.3 KB
serverless/remote/utils.pyscript9.7 KB
serverless/server/__init__.pyscript0 B
serverless/server/lib/__init__.pyscript0 B
serverless/server/lib/backend.pyscript43.3 KB
serverless/server/lib/data_types.pyscript10.6 KB
serverless/server/lib/metrics.pyscript12.2 KB
serverless/server/lib/server.pyscript3.2 KB
serverless/server/worker.pyscript17.1 KB
sync/__init__.pyscript76 B
sync/client.pyscript4.9 KB
sync/results.pyscript1.2 KB
utils.pyscript12.2 KB

serverless/remote/utils.py

View on GitHub
from __future__ import annotations

import dataclasses
import hashlib
import io
import json
import os
import re
import tarfile
import tempfile
from typing import TYPE_CHECKING, Tuple, BinaryIO

if TYPE_CHECKING:
    from vastai.serverless.remote.base import Config


def _sanitize_info(
    info: tarfile.TarInfo, arcname: str | None = None
) -> tarfile.TarInfo:
    """Set uid/gid to 0 on a TarInfo.

    If arcname is provided, override info.name. This is needed because
    gettarinfo strips leading '/' from absolute arcnames.
    """
    info.uid = 0
    info.gid = 0
    info.uname = "root"
    info.gname = "root"
    if arcname is not None:
        info.name = arcname
    return info


def add_file(tf: tarfile.TarFile, src_path: str, arcname: str) -> None:
    """Add a single file to the tarball under arcname, with uid/gid 0."""
    info = tf.gettarinfo(src_path, arcname=arcname)
    _sanitize_info(info, arcname)
    with open(src_path, "rb") as f:
        tf.addfile(info, f)


def add_folder(tf: tarfile.TarFile, src_path: str, arcname: str) -> None:
    """Recursively add a folder to the tarball, renaming the root to arcname, with uid/gid 0."""
    src_path = os.path.normpath(src_path)
    for root, dirs, files in os.walk(src_path):
        rel = os.path.relpath(root, src_path)
        arc_root = arcname if rel == "." else os.path.join(arcname, rel)

        # Add directory entry
        info = tf.gettarinfo(root, arcname=arc_root)
        _sanitize_info(info, arc_root)
        tf.addfile(info)

        for fname in files:
            file_path = os.path.join(root, fname)
            arc_path = os.path.join(arc_root, fname)
            info = tf.gettarinfo(file_path, arcname=arc_path)
            _sanitize_info(info, arc_path)
            with open(file_path, "rb") as f:
                tf.addfile(info, f)


def add_string(
    tf: tarfile.TarFile,
    content: str,
    arcname: str,
    executable: bool = False,
) -> None:
    """Add a string as a text file to the tarball with uid/gid 0.

    If executable=True, the file gets mode 0o755; otherwise 0o644.
    """
    data = content.encode("utf-8")
    info = tarfile.TarInfo(name=arcname)
    info.size = len(data)
    info.mode = 0o755 if executable else 0o644
    _sanitize_info(info)
    tf.addfile(info, io.BytesIO(data))


def add_path(tf: tarfile.TarFile, src_path: str, arcname: str) -> None:
    """Add a file or directory to the tarball, dispatching to add_file or add_folder."""
    if os.path.isdir(src_path):
        add_folder(tf, src_path, arcname)
    elif os.path.isfile(src_path):
        add_file(tf, src_path, arcname)
    else:
        raise FileNotFoundError(f"Source path does not exist: {src_path}")


def serialize_config(config: Config) -> str:
    """JSON-serialize a Config dataclass."""
    return json.dumps(dataclasses.asdict(config))


def is_python_package(path: str) -> bool:
    """Return True if path is a Python package directory (contains __init__.py)."""
    return os.path.isdir(path) and os.path.isfile(os.path.join(path, "__init__.py"))


def is_python_module(path: str) -> bool:
    """Return True if path is a single .py file."""
    return os.path.isfile(path) and path.endswith(".py")


def deployment_arcname(deployment_path: str) -> str:
    """Return the archive name for a deployment: './deployment' for packages, './deployment.py' for modules.

    Raises ValueError if the path is neither a Python package nor a module.
    """
    if is_python_package(deployment_path):
        return "./deployment"
    if is_python_module(deployment_path):
        return "./deployment.py"
    raise ValueError(
        f"Deployment path is neither a Python package (directory with __init__.py) "
        f"nor a Python module (.py file): {deployment_path}"
    )


IGNORE_COMMENT_BODY = b"!VAST_IGNORE_CHANGES"

# Matches the closing delimiter (with escape handling) for a multiline string.
_CLOSE_MULTILINE_RE = {
    b'"""': re.compile(rb'(?:[^\\]|\\.)*?"""'),
    b"'''": re.compile(rb"(?:[^\\]|\\.)*?'''"),
}

# Tokenizer for Python source outside multiline strings.
# Matches (in priority order): closed triple-quoted strings, unclosed
# triple-quotes (groups 1/2), single-quoted strings, or comment start (group 3).
# Unclosed triple-quotes MUST precede single-quote patterns, otherwise
# "..." would consume "" from the start of """.
# Unmatched text (code, whitespace, etc.) is simply skipped by finditer.
_PYTHON_TOKEN_RE = re.compile(
    rb'"""(?:[^\\]|\\.)*?"""|'
    rb"'''(?:[^\\]|\\.)*?'''|"
    rb'(""")|'
    rb"(''')|"
    rb'"(?:[^"\\\n]|\\.)*"|'
    rb"'(?:[^'\\\n]|\\.)*'|"
    rb"(#)"
)


def _scan_python_line(
    line: bytes,
    in_multiline: bytes | None,
) -> tuple[bool, bytes | None]:
    """Scan a Python source line left-to-right, tracking string context.

    Returns (has_ignore_comment, new_multiline_state).
    """
    i = 0
    if in_multiline is not None:
        m = _CLOSE_MULTILINE_RE[in_multiline].match(line)
        if m is None:
            return False, in_multiline
        i = m.end()

    for m in _PYTHON_TOKEN_RE.finditer(line, i):
        if m.group(1):
            return False, b'"""'
        if m.group(2):
            return False, b"'''"
        if m.group(3):
            comment_body = line[m.start() + 1 :].strip()
            return comment_body == IGNORE_COMMENT_BODY, None

    return False, None


def filter_ignored_lines(data: bytes) -> bytes:
    """Remove lines whose comment is exactly ``#!VAST_IGNORE_CHANGES``.

    Tracks triple-quoted multiline strings so that occurrences inside string
    literals are not treated as comments.
    """
    lines = data.split(b"\n")
    result: list[bytes] = []
    in_multiline: bytes | None = None
    for line in lines:
        has_ignore, in_multiline = _scan_python_line(line, in_multiline)
        if not has_ignore:
            result.append(line)
    return b"\n".join(result)


def read_file_for_hash(path: str, filter_comments: bool = False) -> bytes:
    """Read a file's bytes for hashing.

    When filter_comments is True and the file is a .py file, lines whose
    comment is exactly ``# !VAST_IGNORE_CHANGES`` are excluded.
    """
    with open(path, "rb") as f:
        data = f.read()
    if filter_comments and path.endswith(".py"):
        data = filter_ignored_lines(data)
    return data


def hash_update_file(
    hasher: hashlib._Hash,
    src_path: str,
    arcname: str,
    filter_comments: bool = False,
) -> None:
    """Feed a single file into the hasher: its archive name followed by its content."""
    hasher.update(arcname.encode("utf-8"))
    hasher.update(read_file_for_hash(src_path, filter_comments=filter_comments))


def hash_update_directory(
    hasher: hashlib._Hash,
    src_path: str,
    arcname: str,
    filter_comments: bool = False,
) -> None:
    """Walk a directory in sorted order, feeding each file into the hasher."""
    src_path = os.path.normpath(src_path)
    for root, dirs, files in os.walk(src_path):
        dirs.sort()
        rel = os.path.relpath(root, src_path)
        arc_root = arcname if rel == "." else os.path.join(arcname, rel)
        for fname in sorted(files):
            hash_update_file(
                hasher,
                os.path.join(root, fname),
                os.path.join(arc_root, fname),
                filter_comments=filter_comments,
            )


def hash_update_path(
    hasher: hashlib._Hash,
    src_path: str,
    arcname: str,
    filter_comments: bool = False,
) -> None:
    """Feed a file or directory into the hasher, dispatching appropriately."""
    if os.path.isdir(src_path):
        hash_update_directory(
            hasher, src_path, arcname, filter_comments=filter_comments
        )
    elif os.path.isfile(src_path):
        hash_update_file(hasher, src_path, arcname, filter_comments=filter_comments)
    else:
        raise FileNotFoundError(f"Source path does not exist: {src_path}")


def compute_deployment_hash(
    config: Config,
    deployment_path: str,
    extra_files: list[tuple[str, str]] | None = None,
) -> str:
    """Compute a SHA-256 hex digest over deployment data without creating a tarball.

    Hashes (in order):
      1. The JSON-serialized Config
      2. The deployment files (with #!VAST_IGNORE_CHANGES comment lines excluded from .py files)
      3. Any extra files (no comment filtering)

    Args:
        config: The deployment Config object.
        deployment_path: Path to the deployment source (a .py file or a package directory).
        extra_files: List of (source_path, dest_path) pairs.
    """
    hasher = hashlib.sha256()
    hasher.update(serialize_config(config).encode("utf-8"))
    hash_update_path(
        hasher,
        deployment_path,
        deployment_arcname(deployment_path),
        filter_comments=True,
    )
    for src, dest in extra_files or []:
        hash_update_path(hasher, src, dest)
    return hasher.hexdigest()


def create_deployment_tarball(
    tar_path: str,
    config: Config,
    deployment_path: str,
    extra_files: list[tuple[str, str]] | None = None
):
    """Create a deployment tarball in /tmp and return its path.

    The tarball contains:
      - ./config.json        — JSON-serialized Config
      - ./deployment[.py]    — the deployment package or module
      - extra files at their specified destination paths

    Args:
        config: The deployment Config object.
        deployment_path: Path to the deployment source (a .py file or a package directory).
        extra_files: List of (source_path, dest_path) pairs. dest_path may be absolute.
    """
    tf = tarfile.TarFile.open(tar_path, mode="w:gz")
    try:
        add_string(tf, serialize_config(config), "./config.json")
        add_path(tf, deployment_path, deployment_arcname(deployment_path))
        for src, dest in extra_files or []:
            add_path(tf, src, dest)
    finally:
        tf.close()
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall