conda

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 15 Imported by: 1 module, 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 conda holds the parts of the conda channel protocol that hosted, proxy and group repositories share: package file names, the request paths of a channel, and a streaming reader and merger for the very large repodata.json documents (the conda-forge linux-64 one is close to half a gigabyte) that never holds a whole document in memory.

Index

Constants

View Source
const (
	ExtTarBz2 = ".tar.bz2"
	ExtConda  = ".conda"
)

Package file extensions: .tar.bz2 files are listed under "packages" in a repodata.json, .conda files under "packages.conda".

View Source
const (
	MaxNameLen     = 255
	MaxVersionLen  = 100
	MaxBuildLen    = 255
	MaxPlatformLen = 50
)

Column widths of the hosted format's conda package rows, also the limits a package file name must respect.

View Source
const ChanneldataFile = "channeldata.json"

ChanneldataFile is the channel-root document listing every package name.

View Source
const MaxEntryBytes = 4 << 20

MaxEntryBytes bounds one repodata entry (and the info object): real entries are a few KiB, the largest depend lists tens of KiB. A hostile document must not be able to make the reader buffer a gigabyte-long string.

Variables

View Source
var (
	// ErrTooLarge is a document that decompresses to more than its limit.
	ErrTooLarge = errors.New("conda: repodata exceeds its size limit")
	// ErrMalformed is a document that is not a repodata.json.
	ErrMalformed = errors.New("conda: malformed repodata")
	// ErrBaseURL is a document whose info.base_url points package downloads
	// somewhere other than the channel: served through a proxy it would let
	// clients fetch around it.
	ErrBaseURL = errors.New("conda: repodata sets info.base_url")
)
View Source
var ErrNotListed = errors.New("conda: file is not listed in the repodata")

ErrNotListed is a package file the repodata does not list.

Functions

func BuildTimes added in v0.16.0

func BuildTimes(r io.Reader, limit int64, name string) (map[string]time.Time, error)

BuildTimes is, for every version of the package called name (compared without regard to case) in the plain repodata.json in r, the earliest "timestamp" of its entries; the zero time for a version none of whose entries carries one. limit bounds the bytes read.

func EarliestBuild added in v0.16.0

func EarliestBuild(r io.Reader, limit int64, name, version string) (time.Time, bool, error)

EarliestBuild is the earliest "timestamp" among the entries of the plain repodata.json in r whose name (compared without regard to case) and version are the given ones: when the package version was first built, the nearest thing to a publish time a channel states. ok is false when no matching entry carries a timestamp. limit bounds the bytes read.

func EntryIdentity added in v0.16.0

func EntryIdentity(raw json.RawMessage) (string, string)

EntryIdentity is the "name" and "version" a repodata entry states about its package, "" for a field it lacks or that is not a string.

func EntryTimestamp added in v0.16.0

func EntryTimestamp(raw json.RawMessage) time.Time

EntryTimestamp is the "timestamp" of a repodata entry: when the package was built, the only date repodata carries. The zero time when the entry has none, or one that is not a positive number.

func PlainSegment

func PlainSegment(s string) bool

PlainSegment reports whether s is valid UTF-8 without path separators or control characters: text a database column holds and a URL segment names.

func RepodataFile

func RepodataFile(current bool, comp Compression) string

RepodataFile is the file name of a repodata document.

func Scan

func Scan(r io.Reader, limit int64, v Visitor) error

Scan streams the repodata.json in r, calling v for the info object and every package entry, and holding one entry in memory at a time. r must be plain JSON (see Open). It returns ErrMalformed for anything that is not a JSON object whose "packages" and "packages.conda" members, when present, are objects of objects, and ErrTooLarge when r yields more than limit bytes.

func ValidSubdir

func ValidSubdir(subdir string) bool

ValidSubdir reports whether subdir, the directory of a channel packages and repodata live under (linux-64, noarch), is usable as a repository path segment and fits the platform column of the hosted format. Any other value is a database error or a path escape, not a subdirectory.

func ValidateIndex

func ValidateIndex(r io.Reader, comp Compression, limit int64) error

ValidateIndex checks that r, the body of a repodata file of the given encoding, is a usable one: plain and zstd documents are scanned in full (structure, and no info.base_url); a bzip2 stream, which no modern client asks for and which decodes slowly, is only checked for its magic number. limit bounds the decoded size.

func ValidateJSONObject

func ValidateJSONObject(r io.Reader, limit int64) error

ValidateJSONObject checks that r holds exactly one JSON object, without keeping any of it in memory; limit bounds the bytes read.

Types

type Builder

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

Builder merges the repodata.json documents of several members into one for a subdirectory, in a single streaming pass: entries go to temporary files, so memory is one entry plus the set of file names seen. Earlier members win a file name. Close it to remove the temporary files.

func NewBuilder

func NewBuilder(subdir, tmpDir string) (*Builder, error)

NewBuilder starts a merged document for subdir. Temporary files go to tmpDir ("" for the system default).

func (*Builder) Add

func (b *Builder) Add(r io.Reader, o MemberOptions) (MemberStats, error)

Add merges the repodata document in r (plain JSON or a zstd or bzip2 stream) into the result. On error the Builder is unusable: Close it.

func (*Builder) Close

func (b *Builder) Close()

Close removes the temporary files. A Document returned by Finish owns them afterwards and Close leaves them alone.

func (*Builder) Finish

func (b *Builder) Finish() (*Document, error)

Finish completes the document. The Builder must not be used afterwards except to Close it, which keeps the Document's files.

type Compression

type Compression int

Compression is how a repodata file is encoded.

const (
	// CompNone is plain JSON.
	CompNone Compression = iota
	// CompZstd is a zstd stream (repodata.json.zst).
	CompZstd
	// CompBzip2 is a bzip2 stream (repodata.json.bz2).
	CompBzip2
)

func Open

func Open(r io.Reader, limit int64) (io.ReadCloser, Compression, error)

Open returns the decoded bytes of a repodata document in r: plain JSON, or a zstd or bzip2 stream, told apart by their magic numbers. The decoded output is limited to limit bytes (ErrTooLarge). Closing the result releases the decoder; it does not close r.

func (Compression) Suffix

func (c Compression) Suffix() string

Suffix is the file name suffix of the compression, "" for plain JSON.

type Document

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

Document is a merged repodata.json held in temporary files.

func (*Document) Close

func (d *Document) Close()

Close removes the temporary files.

func (*Document) Size

func (d *Document) Size() int64

Size is the length of the encoded document in bytes.

func (*Document) WriteTo

func (d *Document) WriteTo(w io.Writer) (int64, error)

WriteTo writes the document to w.

type Entry

type Entry struct {
	// SHA256 and MD5 are lowercase hex, "" when the entry has none (or a
	// malformed one).
	SHA256, MD5 string
	// Name and Version are the package name and version the entry itself
	// states ("" when it has none), which need not agree with its file name.
	Name, Version string
	// Size is the file size in bytes, -1 when the entry has none.
	Size int64
	// Published is the entry's "timestamp" (see EntryTimestamp), the zero time
	// when it has none.
	Published time.Time
}

Entry is what a repodata entry says about its package file: the digests a download must match.

func Lookup

func Lookup(r io.Reader, filename string, limit int64) (Entry, error)

Lookup finds filename in the plain repodata.json in r and returns its digests, or ErrNotListed. It reads r at most once and keeps about a megabyte: instead of parsing the whole document it searches for the entry's key and decodes only the value after it. r must hold a document that was validated (ValidateIndex); limit bounds the bytes read.

type File

type File struct {
	Name, Version, Build, Ext string
}

File is a conda package file name taken apart.

func ParseFilename

func ParseFilename(filename string) (File, bool)

ParseFilename splits <name>-<version>-<build>(.tar.bz2|.conda) from the right: a package name may contain hyphens (scikit-learn), but neither a version nor a build string can. ok is false for any other extension, a name with fewer than three parts, empty parts, invalid UTF-8, path separators or control characters.

type Kind

type Kind int

Kind is the type of a channel request path.

const (
	// KindOther is a path a proxy or group does not serve (repodata.jlap,
	// sharded repodata, index.html, anything unknown).
	KindOther Kind = iota
	// KindRepodata is <subdir>/repodata.json or <subdir>/current_repodata.json,
	// plain or compressed (.zst, .bz2).
	KindRepodata
	// KindPackage is <subdir>/<name>-<version>-<build>.conda or .tar.bz2.
	KindPackage
	// KindChanneldata is channeldata.json at the channel root.
	KindChanneldata
)

type MemberOptions

type MemberOptions struct {
	// Hidden reports whether entries of the named package are dropped. It is
	// asked about the name in the file name and about the entry's own "name"
	// field, so a document cannot smuggle a package past it by lying in one.
	Hidden func(name string) bool
	// Drop, when set, is asked about every entry that passed the checks above,
	// with its parsed file name (its raw JSON is only valid during the call),
	// and drops the entry when it returns true.
	Drop func(file File, entry json.RawMessage) bool
	// MaxBytes bounds the decoded size of the member's document.
	MaxBytes int64
}

MemberOptions tunes Builder.Add for one member's document.

type MemberStats

type MemberStats struct {
	// Added entries went into the merged document.
	Added int
	// Duplicates were dropped: an earlier member lists the same file.
	Duplicates int
	// Hidden entries were dropped by MemberOptions.Hidden.
	Hidden int
	// Dropped entries were dropped by MemberOptions.Drop.
	Dropped int
	// Invalid entries were dropped: a key that is no package file name, or one
	// listed in the wrong section.
	Invalid int
}

MemberStats counts what Builder.Add did with one member's entries.

type Path

type Path struct {
	Kind Kind
	// Subdir is the platform directory of KindRepodata and KindPackage paths.
	Subdir string
	// File is the file name below Subdir (the whole path for KindChanneldata).
	File string
	// Current reports a current_repodata.json (the latest versions only).
	Current bool
	// Comp is the encoding of a KindRepodata file.
	Comp Compression
}

Path is a parsed channel request path.

func ParsePath

func ParsePath(rel string) (Path, bool)

ParsePath classifies a request path relative to the channel root (a leading slash is allowed). ok is false when the path is not one a channel serves; a path that is shaped like a channel file but not one a proxy or group serves (repodata.jlap) is KindOther with ok true.

func (Path) Key

func (p Path) Key() string

Key is the repository-relative path of the file.

type Section

type Section int

Section is one of the two package maps of a repodata.json.

const (
	// SectionTarBz2 is "packages": the .tar.bz2 files.
	SectionTarBz2 Section = iota
	// SectionConda is "packages.conda": the .conda files.
	SectionConda
)

func (Section) Ext

func (s Section) Ext() string

Ext is the file extension of the packages listed in the section.

func (Section) Key

func (s Section) Key() string

Key is the repodata.json key of the section.

type Visitor

type Visitor struct {
	// Info receives the "info" object.
	Info func(info json.RawMessage) error
	// Entry receives one package entry of a section.
	Entry func(section Section, filename string, entry json.RawMessage) error
}

Visitor receives the parts of a repodata.json as Scan streams over it. Any field may be nil. The slices passed in are only valid during the call. A returned error stops the scan and is returned as is.

Jump to

Keyboard shortcuts

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