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
- Variables
- func CacheDir(root, indexURL string) string
- func CacheRoot() (string, error)
- type CacheMode
- type CacheOption
- type CacheStats
- type CacheUsage
- type CachedSource
- type Client
- type FileInfo
- type MemSource
- func (m *MemSource) Add(info *ReleaseInfo)
- func (m *MemSource) AddFiles(name, ver string, files ...FileInfo) error
- func (m *MemSource) AddPackage(name, ver string, deps ...string) error
- func (m *MemSource) Release(_ context.Context, name string, v *version.Version) (*ReleaseInfo, error)
- func (m *MemSource) Versions(_ context.Context, name string) ([]*version.Version, error)
- type Option
- type Ref
- type ReleaseInfo
- type Result
- type Snapshot
- func (s *Snapshot) AddRelease(info *ReleaseInfo)
- func (s *Snapshot) Release(_ context.Context, name string, v *version.Version) (*ReleaseInfo, error)
- func (s *Snapshot) Save(path string) error
- func (s *Snapshot) SetVersions(name string, versions []*version.Version)
- func (s *Snapshot) Versions(_ context.Context, name string) ([]*version.Version, error)
- type SnapshotPackage
- type SnapshotRelease
- type Source
Constants ¶
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.
const DefaultBaseURL = "https://pypi.org/pypi"
DefaultBaseURL is the JSON API root of the public Python Package Index.
Variables ¶
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".
var ErrNotFound = errors.New("not found")
ErrNotFound is returned when a package or release does not exist.
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 ¶
Types ¶
type CacheMode ¶ added in v0.1.1
type CacheMode int
CacheMode selects how the cache and the index are used together.
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
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.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client fetches metadata from a JSON package index.
func NewClient ¶
NewClient creates a client with connection pooling suited to fetching many small metadata documents.
func (*Client) FetchReleases ¶
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 ¶
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
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 (*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
AddFiles attaches published artifacts to a release already recorded, so tests can exercise anything that depends on a release's digests.
func (*MemSource) AddPackage ¶
AddPackage is a convenience for registering a release from strings.
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithBaseURL ¶
WithBaseURL sets the JSON API root, for a mirror or a private index.
func WithHTTPClient ¶
WithHTTPClient supplies a custom HTTP client.
func WithMaxRetries ¶
WithMaxRetries sets how many times a transient failure is retried.
func WithMinBackoff ¶
WithMinBackoff sets the initial backoff delay, which doubles per retry.
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
LoadSnapshot reads a snapshot and decodes its releases.
func NewSnapshot ¶ added in v0.1.1
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
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
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.
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.