pypi

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 24, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pypi fetches package metadata from a Python package index using the JSON API. It pools connections, retries transient failures with exponential backoff, and can fetch many releases concurrently. A Source interface lets the resolver run against the network client or an in-memory source in tests.

Index

Constants

View Source
const (
	// DefaultVersionTTL bounds how stale a package's version list may be. Short,
	// because a resolve that silently ignores a release published this morning is
	// answering yesterday's question.
	DefaultVersionTTL = 10 * time.Minute
	// DefaultReleaseTTL bounds how stale one release's metadata may be. Long,
	// because the only part of it that can change after publication is the yanked
	// flag.
	DefaultReleaseTTL = 7 * 24 * time.Hour
)

A resolve asks the index the same two questions over and over: which versions of this package exist, and what does this exact release require. The answers have very different lifetimes. A version list changes whenever anyone publishes, so it is worth re-reading often. The metadata of one released version is fixed at publication and only ever changes if it is later yanked, so re-reading it on every resolve is pure waste. The cache treats them accordingly.

View Source
const DefaultBaseURL = "https://pypi.org/pypi"

DefaultBaseURL is the JSON API root of the public Python Package Index.

Variables

View Source
var ErrNotCaptured = fmt.Errorf("not captured in the snapshot")

ErrNotCaptured means a package or version exists in a snapshot's version list but its release metadata was not recorded. It is distinct from ErrNotFound so a test can tell "the index does not have this" from "the capture is too narrow, widen it and record again".

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is returned when a package or release does not exist.

View Source
var ErrOffline = errors.New("not in the cache and offline")

ErrOffline reports that a lookup was needed but the cache did not have it and fetching was disallowed.

Functions

func CacheDir added in v0.1.1

func CacheDir(root, indexURL string) string

CacheDir returns the cache directory for one index. Entries are separated by index so a private index and the public one never answer for each other, with a readable host name for anyone looking through the cache and a digest to keep distinct URLs distinct.

func CacheRoot added in v0.1.1

func CacheRoot() (string, error)

CacheRoot returns the base directory gopip caches into, honoring the usual per-platform cache location.

Types

type CacheMode added in v0.1.1

type CacheMode int

CacheMode selects how the cache and the index are used together.

const (
	// CacheNormal serves entries that are still fresh and fetches the rest.
	CacheNormal CacheMode = iota
	// CacheRefresh ignores what is stored, fetches everything, and rewrites it.
	CacheRefresh
	// CacheOffline serves only what is stored and never reaches the network.
	CacheOffline
)

type CacheOption added in v0.1.1

type CacheOption func(*CachedSource)

CacheOption configures a CachedSource.

func WithCacheMode added in v0.1.1

func WithCacheMode(m CacheMode) CacheOption

WithCacheMode sets whether the cache is consulted, bypassed, or exclusive.

func WithReleaseTTL added in v0.1.1

func WithReleaseTTL(d time.Duration) CacheOption

WithReleaseTTL overrides how long cached release metadata stays fresh.

func WithVersionTTL added in v0.1.1

func WithVersionTTL(d time.Duration) CacheOption

WithVersionTTL overrides how long a cached version list stays fresh.

type CacheStats added in v0.1.1

type CacheStats struct {
	VersionHits   int64
	VersionMisses int64
	ReleaseHits   int64
	ReleaseMisses int64
}

CacheStats counts what a cached source did, so a run can report how much of it avoided the network.

func (CacheStats) Hits added in v0.1.1

func (s CacheStats) Hits() int64

Hits returns how many lookups were served from disk.

func (CacheStats) Requests added in v0.1.1

func (s CacheStats) Requests() int64

Requests returns how many index requests the misses implied.

type CacheUsage added in v0.1.1

type CacheUsage struct {
	Dir      string
	Packages int
	Entries  int
	Bytes    int64
}

CacheUsage describes what is stored under one index's cache directory.

func ListCaches added in v0.1.1

func ListCaches(root string) ([]CacheUsage, error)

ListCaches measures every index cache under a root, sorted by directory so the listing is stable.

func Usage added in v0.1.1

func Usage(dir string) (CacheUsage, error)

Usage measures one index's cache directory. A directory that does not exist is reported as empty rather than as an error, because an unused cache is a normal state.

type CachedSource added in v0.1.1

type CachedSource struct {
	// contains filtered or unexported fields
}

CachedSource wraps a source with an on-disk metadata cache, so a second resolve does little or no network work. Entries are written atomically and an unreadable entry is treated as a miss, so a cache damaged by a crash or a full disk costs time and never correctness.

func NewCachedSource added in v0.1.1

func NewCachedSource(inner Source, dir string, opts ...CacheOption) *CachedSource

NewCachedSource wraps a source with a cache rooted at dir, which should already be specific to one index. Use CacheDir to derive it.

func (*CachedSource) Dir added in v0.1.1

func (c *CachedSource) Dir() string

Dir returns the directory this source caches into.

func (*CachedSource) Release added in v0.1.1

func (c *CachedSource) Release(ctx context.Context, name string, v *version.Version) (*ReleaseInfo, error)

Release returns one release's metadata, from the cache when fresh.

func (*CachedSource) Stats added in v0.1.1

func (c *CachedSource) Stats() CacheStats

Stats reports what the cache did during this run.

func (*CachedSource) Versions added in v0.1.1

func (c *CachedSource) Versions(ctx context.Context, name string) ([]*version.Version, error)

Versions returns a package's versions, from the cache when fresh.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client fetches metadata from a JSON package index.

func NewClient

func NewClient(opts ...Option) *Client

NewClient creates a client with connection pooling suited to fetching many small metadata documents.

func (*Client) FetchReleases

func (c *Client) FetchReleases(ctx context.Context, refs []Ref, concurrency int) []Result

FetchReleases fetches many releases concurrently, bounded by concurrency. The results are returned in the same order as refs.

func (*Client) Release

func (c *Client) Release(ctx context.Context, name string, v *version.Version) (*ReleaseInfo, error)

Release returns the metadata for a specific version.

func (*Client) Versions

func (c *Client) Versions(ctx context.Context, name string) ([]*version.Version, error)

Versions returns the parseable versions of a package, ascending. Unparseable version strings in the index are skipped rather than failing the whole call.

The response also describes the package's latest release, which is the version the resolver goes on to ask about for most packages, so that metadata is kept rather than fetched a second time.

type FileInfo added in v0.1.1

type FileInfo struct {
	Filename string
	SHA256   string
}

FileInfo identifies one published artifact of a release by its name and the digest the index publishes for it.

type MemSource

type MemSource struct {
	// contains filtered or unexported fields
}

MemSource is an in-memory Source. It backs tests and offline resolution from a local set of packages, such as a wheelhouse referenced with find-links.

func NewMemSource

func NewMemSource() *MemSource

NewMemSource creates an empty in-memory source.

func (*MemSource) Add

func (m *MemSource) Add(info *ReleaseInfo)

Add records a release. The package name is canonicalized for lookup.

func (*MemSource) AddFiles added in v0.1.1

func (m *MemSource) AddFiles(name, ver string, files ...FileInfo) error

AddFiles attaches published artifacts to a release already recorded, so tests can exercise anything that depends on a release's digests.

func (*MemSource) AddPackage

func (m *MemSource) AddPackage(name, ver string, deps ...string) error

AddPackage is a convenience for registering a release from strings.

func (*MemSource) Release

func (m *MemSource) Release(_ context.Context, name string, v *version.Version) (*ReleaseInfo, error)

Release returns the metadata for a specific version.

func (*MemSource) Versions

func (m *MemSource) Versions(_ context.Context, name string) ([]*version.Version, error)

Versions returns the versions of a package, ascending.

type Option

type Option func(*Client)

Option configures a Client.

func WithBaseURL

func WithBaseURL(u string) Option

WithBaseURL sets the JSON API root, for a mirror or a private index.

func WithHTTPClient

func WithHTTPClient(h *http.Client) Option

WithHTTPClient supplies a custom HTTP client.

func WithMaxRetries

func WithMaxRetries(n int) Option

WithMaxRetries sets how many times a transient failure is retried.

func WithMinBackoff

func WithMinBackoff(d time.Duration) Option

WithMinBackoff sets the initial backoff delay, which doubles per retry.

type Ref

type Ref struct {
	Name    string
	Version *version.Version
}

Ref names a specific release to fetch.

type ReleaseInfo

type ReleaseInfo struct {
	Name           string
	Version        *version.Version
	RequiresPython string
	RequiresDist   []*requirement.Requirement
	Yanked         bool
	// Files are the artifacts published for this release, one per wheel or
	// source distribution. A release usually has several, because a wheel is
	// built per platform, and any of them may be the one that gets installed.
	Files []FileInfo
}

ReleaseInfo is the metadata gopip needs about one release of a package.

func (*ReleaseInfo) Hashes added in v0.1.1

func (r *ReleaseInfo) Hashes() []string

Hashes returns the release's artifact digests in pip's hash syntax, sorted and without duplicates. Every artifact is listed because which one gets installed depends on the machine doing the installing.

type Result

type Result struct {
	Ref  Ref
	Info *ReleaseInfo
	Err  error
}

Result pairs a Ref with its fetched metadata or an error.

type Snapshot added in v0.1.1

type Snapshot struct {
	// Index records which index the capture came from.
	Index string `json:"index"`
	// Packages is keyed by canonical package name.
	Packages map[string]*SnapshotPackage `json:"packages"`
	// contains filtered or unexported fields
}

Snapshot is a frozen capture of index metadata for a fixed set of packages. It exists so resolution can be tested against real, messy package data without a network and without the answer changing every time the index publishes a new release. A snapshot is a Source, so it drops in wherever the live client goes.

func LoadSnapshot added in v0.1.1

func LoadSnapshot(path string) (*Snapshot, error)

LoadSnapshot reads a snapshot and decodes its releases.

func NewSnapshot added in v0.1.1

func NewSnapshot(index string) *Snapshot

NewSnapshot creates an empty snapshot for an index.

func (*Snapshot) AddRelease added in v0.1.1

func (s *Snapshot) AddRelease(info *ReleaseInfo)

AddRelease records one release's metadata.

func (*Snapshot) Release added in v0.1.1

func (s *Snapshot) Release(_ context.Context, name string, v *version.Version) (*ReleaseInfo, error)

Release returns the captured metadata for a version.

func (*Snapshot) Save added in v0.1.1

func (s *Snapshot) Save(path string) error

Save writes the snapshot as indented JSON. Map keys are emitted sorted by the encoder and version lists are sorted on record, so the file is stable across captures of the same data.

func (*Snapshot) SetVersions added in v0.1.1

func (s *Snapshot) SetVersions(name string, versions []*version.Version)

SetVersions records a package's full version list, stored in version order rather than string order so the saved fixture reads the way the index does.

func (*Snapshot) Versions added in v0.1.1

func (s *Snapshot) Versions(_ context.Context, name string) ([]*version.Version, error)

Versions returns every version the capture recorded for a package, ascending.

type SnapshotPackage added in v0.1.1

type SnapshotPackage struct {
	// Name is the package name as the index spells it.
	Name string `json:"name"`
	// Versions is every version the index listed, ascending. The full list is
	// always captured, because the resolver's search space depends on it.
	Versions []string `json:"versions"`
	// Releases holds recorded release metadata, keyed by normalized version.
	Releases map[string]*SnapshotRelease `json:"releases"`
}

SnapshotPackage is one package's captured metadata: its full version list, and release details for the subset of versions that were recorded.

type SnapshotRelease added in v0.1.1

type SnapshotRelease struct {
	RequiresPython string   `json:"requires_python,omitempty"`
	RequiresDist   []string `json:"requires_dist,omitempty"`
	Yanked         bool     `json:"yanked,omitempty"`
}

SnapshotRelease is the captured metadata of a single release.

type Source

type Source interface {
	// Versions returns all available versions of a package, ascending.
	Versions(ctx context.Context, name string) ([]*version.Version, error)
	// Release returns the metadata for a specific version.
	Release(ctx context.Context, name string, v *version.Version) (*ReleaseInfo, error)
}

Source provides package versions and release metadata.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL