{"modulePath":"github.com/carabiner-dev/unpack","version":"v0.4.1","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package python\n\nPackage python implements dependency extraction for Python codebases.\n\nThis file holds the naming rules. Python package names are compared after normalization (PEP 503): Flask, flask and flask are all one project, and an index, a lockfile and a wheel may each spell the name differently. Every name read from project metadata goes through NormalizeName before it is used as an identity anywhere.\n\n## Functions\n\n```go\nfunc InstalledNodeList(dists []*InstalledDistribution, includeFiles bool) (*sbom.NodeList, error)\n```\nInstalledNodeList builds the graph an installed environment holds. When includeFiles is set, every file the RECORDs own becomes a node related to its package.\n\n```go\nfunc NormalizeName(name string) string\n```\nNormalizeName returns the canonical form of a Python package name: every run of dashes, underscores and dots becomes one dash, and the result is lowercased. This is the form purls and lockfile lookups use.\n\n## Types\n\n```go\ntype Artifact struct {\n\tURL  string `toml:\"url\"`\n\tHash string `toml:\"hash\"`\n\tSize int64  `toml:\"size\"`\n}\n```\nArtifact is one distribution file: a wheel or an sdist.\n\n```go\nfunc (a *Artifact) HashValue() (algorithm, value string)\n```\nHashValue splits the artifact's hash into its algorithm and hex value. uv writes them as \"sha256:abc...\".\n\n```go\ntype Decomposer struct{}\n```\nDecomposer reads dependency data from Python codebases managed by uv. The whole graph comes from uv.lock: versions, edges, markers and artifact hashes are all resolved in the lock, so extraction needs no network and no Python interpreter.\n\n```go\nfunc New() *Decomposer\n```\n\n```go\nfunc (d *Decomposer) DefaultOptions() any\n```\nDefaultOptions returns the default options for the Python decomposer.\n\n```go\nfunc (d *Decomposer) Extract(opts *api.DecomposerOptions) (*sbom.NodeList, error)\n```\nExtract is ExtractWithContext with a background context, for a caller that has none to give.\n\n```go\nfunc (d *Decomposer) ExtractWithContext(ctx context.Context, opts *api.DecomposerOptions) (*sbom.NodeList, error)\n```\nExtractWithContext builds the dependency graph the target environment sees, from whichever lockfile the codebase has. A project migrating between tools may carry both; uv.lock wins, being the one uv keeps current.\n\n```go\nfunc (d *Decomposer) FindCodeBases(index *code.PathIndex) ([]string, error)\n```\nFindCodeBases locates Python codebases by their lockfiles.\n\n```go\nfunc (d *Decomposer) GenerationContext() api.GenerationContext\n```\nGenerationContext says in what context the decomposer's data is read: a lock file or a requirements file, as it stands in the source tree.\n\n```go\nfunc (d *Decomposer) Requirements(_ *api.DecomposerOptions) []api.Requirement\n```\nRequirements returns nothing: extraction is pure Go, offline.\n\n```go\ntype Dependency struct {\n\tName string `toml:\"name\"`\n\n\t// Version and Source disambiguate the target when the resolution\n\t// forked: a lock holding numpy at three versions states which one this\n\t// edge points to. Both empty mean the name alone identifies it.\n\tVersion string `toml:\"version\"`\n\tSource  Source `toml:\"source\"`\n\n\t// Extra lists the target's extras this dependency enables, as in\n\t// requests[socks].\n\tExtra []string `toml:\"extra\"`\n\n\t// Marker is the environment marker the edge is conditional on, empty\n\t// when it holds everywhere.\n\tMarker string `toml:\"marker\"`\n}\n```\nDependency is one edge of the resolved graph.\n\n```go\ntype DirectURL struct {\n\tURL     string `json:\"url\"`\n\tVCSInfo struct {\n\t\tVCS               string `json:\"vcs\"`\n\t\tCommitID          string `json:\"commit_id\"`\n\t\tRequestedRevision string `json:\"requested_revision\"`\n\t} `json:\"vcs_info\"`\n\tDirInfo struct {\n\t\tEditable bool `json:\"editable\"`\n\t} `json:\"dir_info\"`\n}\n```\nDirectURL is a parsed direct\\_url.json.\n\n```go\ntype Environment struct {\n\t// The PEP 508 environment variables, named as the markers name them.\n\tOSName                       string // os_name: \"posix\", \"nt\"\n\tSysPlatform                  string // sys_platform: \"linux\", \"darwin\", \"win32\"\n\tPlatformSystem               string // platform_system: \"Linux\", \"Darwin\", \"Windows\"\n\tPlatformMachine              string // platform_machine: \"x86_64\", \"aarch64\", \"arm64\", \"AMD64\"\n\tPythonVersion                string // python_version: \"3.12\"\n\tPythonFullVersion            string // python_full_version: \"3.12.0\"\n\tImplementationName           string // implementation_name: \"cpython\"\n\tPlatformPythonImplementation string // platform_python_implementation: \"CPython\"\n\n\t// Extras are the enabled extras, normalized. The \"extra\" marker\n\t// variable evaluates against this set.\n\tExtras []string\n}\n```\nEnvironment is the concrete target environment markers evaluate against: one operating system, one architecture, one Python version, one set of enabled extras. A lockfile resolves for every environment at once; an extraction is for one of these.\n\n```go\nfunc NewEnvironment(goos, goarch, pythonVersion string) (*Environment, error)\n```\nNewEnvironment builds the environment for an operating system and architecture in Go's vocabulary (GOOS and GOARCH names), and a Python version stated as \"3.12\" or \"3.12.4\". Empty os or arch mean the platform unpack itself runs on.\n\nThe translation is the reason this constructor exists: Python spells the same platform several ways, and the values must agree with what real interpreters report — linux/arm64 is aarch64, but a Mac's arm64 stays arm64 and Windows spells it ARM64.\n\n```go\nfunc (env *Environment) Evaluate(marker string) (bool, error)\n```\nEvaluate parses and evaluates a marker in this environment. An empty marker holds everywhere: a dependency with no marker is unconditional.\n\n```go\ntype InstalledDistribution struct {\n\t// Name is normalized; the dist-info directory spells it however the\n\t// wheel did (PySocks-1.7.1.dist-info).\n\tName    string\n\tVersion string\n\n\tSummary        string\n\tRequiresPython string\n\tHomePage       string\n\tProjectURLs    map[string]string\n\n\t// The three licence tiers, as METADATA states them; LicensesFromMetadata\n\t// of the node builder triages them.\n\tLicenseExpression string\n\tLicense           string\n\tClassifiers       []string\n\n\t// RequiresDist are the declared dependencies, raw PEP 508.\n\tRequiresDist []string\n\n\t// Installer names the tool that installed the package (the INSTALLER\n\t// file), and Requested says the install was asked for rather than\n\t// pulled in (the REQUESTED marker, PEP 376).\n\tInstaller string\n\tRequested bool\n\n\t// DirectURL is the provenance of a package installed from a URL or a\n\t// repository rather than an index (direct_url.json, PEP 610).\n\tDirectURL *DirectURL\n\n\t// Files is the RECORD: every file the installation owns.\n\tFiles []InstalledFile\n\n\t// Path is the dist-info directory, relative to the FS the environment\n\t// was read from.\n\tPath string\n}\n```\nInstalledDistribution is one installed package, read from its dist-info.\n\n```go\nfunc FindDistributions(fsys fs.FS) ([]*InstalledDistribution, error)\n```\nFindDistributions walks a filesystem and reads every installed distribution on it, wherever its site-packages lives: system interpreters, virtualenvs and --target directories all qualify. A distribution whose metadata cannot be read is skipped: one broken package should not hide an environment.\n\n```go\nfunc ReadDistribution(fsys fs.FS, dir string) (*InstalledDistribution, error)\n```\nReadDistribution reads one dist-info directory.\n\n```go\ntype InstalledFile struct {\n\t// Path is relative to the site-packages directory (entries may climb\n\t// out of it: ../../../bin/pip).\n\tPath string\n\n\t// Algorithm and Digest are the recorded hash, the digest in the\n\t// urlsafe base64 RECORD states it in. Empty for the files an\n\t// installer rewrites, RECORD itself among them.\n\tAlgorithm string\n\tDigest    string\n\n\tSize int64\n}\n```\nInstalledFile is one RECORD entry.\n\n```go\ntype Lockfile struct {\n\t// Version is the lock schema version, and Revision the revision within\n\t// it. Additions bump the revision; only breaking changes bump the\n\t// version, which is the one compatibility is judged by.\n\tVersion  int `toml:\"version\"`\n\tRevision int `toml:\"revision\"`\n\n\t// RequiresPython is the project's Python requirement, as a PEP 440\n\t// specifier set such as \"\u003e=3.10\".\n\tRequiresPython string `toml:\"requires-python\"`\n\n\t// ResolutionMarkers split the environments the resolution forked over;\n\t// SupportedMarkers restrict the environments the project supports at\n\t// all (tool.uv.environments). Both absent mean one universal\n\t// resolution.\n\tResolutionMarkers []string `toml:\"resolution-markers\"`\n\tSupportedMarkers  []string `toml:\"supported-markers\"`\n\n\tPackages []Package `toml:\"package\"`\n}\n```\nLockfile is a parsed uv.lock.\n\n```go\nfunc ParseLockfile(data []byte) (*Lockfile, error)\n```\nParseLockfile reads a uv.lock document.\n\n```go\nfunc ReadLockfile(path string) (*Lockfile, error)\n```\nReadLockfile reads a uv.lock from a file.\n\n```go\ntype Marker struct {\n\t// contains filtered or unexported fields\n}\n```\nMarker is a parsed environment marker, ready to evaluate.\n\n```go\nfunc ParseMarker(s string) (*Marker, error)\n```\nParseMarker parses an environment marker expression such as \"python\\_full\\_version \\\u003c '3.11' and sys\\_platform == 'linux'\".\n\n```go\nfunc (m *Marker) Eval(env *Environment) (bool, error)\n```\nEval says whether the marker holds in the given environment.\n\n```go\ntype Options struct {\n\t// Platform targets an operating system and architecture, in GOOS/GOARCH\n\t// vocabulary (\"linux/arm64\"), an os alone (\"windows\"), or empty for the\n\t// platform unpack runs on. A lockfile resolves for every platform at\n\t// once; the extraction reads it for this one.\n\tPlatform string\n\n\t// PythonVersion targets an interpreter version, \"3.12\" or fuller.\n\t// Empty picks the newest version the lockfile's own resolution forks\n\t// mention, so the default describes a current install with no Python\n\t// on the machine unpack runs on.\n\tPythonVersion string\n\n\t// Concurrency controls the parallel requests to PyPI when enriching\n\t// (default: 10).\n\tConcurrency int\n}\n```\nOptions configures the Python dependency extraction.\n\n```go\ntype Package struct {\n\tName    string `toml:\"name\"`\n\tVersion string `toml:\"version\"`\n\tSource  Source `toml:\"source\"`\n\n\t// Dependencies are the package's resolved runtime dependencies.\n\t// OptionalDependencies holds those of each extra, keyed by extra name,\n\t// and DevDependencies those of each dependency group (PEP 735), keyed\n\t// by group name.\n\tDependencies         []Dependency            `toml:\"dependencies\"`\n\tOptionalDependencies map[string][]Dependency `toml:\"optional-dependencies\"`\n\tDevDependencies      map[string][]Dependency `toml:\"dev-dependencies\"`\n\n\t// ResolutionMarkers claim this entry for part of the environment space\n\t// when the resolution forked: the numpy for one Python version range\n\t// carries the markers telling it apart from the numpy for another.\n\tResolutionMarkers []string `toml:\"resolution-markers\"`\n\n\t// Sdist and Wheels are the package's distribution artifacts. Hashes\n\t// live here: PyPI has no package-level hash, only per-artifact ones.\n\tSdist  *Artifact  `toml:\"sdist\"`\n\tWheels []Artifact `toml:\"wheels\"`\n}\n```\nPackage is one locked package. A package appears once per resolved version: the same name shows up several times when the resolution forked over environments, each entry claimed by its resolution markers.\n\n```go\ntype PoetryDependency struct {\n\tName   string\n\tMarker string\n\tExtras []string\n\n\t// Constraint is the version constraint the edge was resolved under.\n\t// A lock that holds several versions of the target, one for each\n\t// environment they differ in, is told apart by it.\n\tConstraint string\n}\n```\nPoetryDependency is one edge of the resolved graph, normalized.\n\n```go\ntype PoetryFile struct {\n\tFile string `toml:\"file\"`\n\tHash string `toml:\"hash\"`\n}\n```\nPoetryFile is one distribution artifact.\n\n```go\nfunc (f *PoetryFile) HashValue() (algorithm, value string)\n```\nHashValue splits the file's hash into its algorithm and hex value, written as \"sha256:abc...\".\n\n```go\ntype PoetryLockfile struct {\n\tPackages []PoetryPackage `toml:\"package\"`\n\tMetadata PoetryMetadata  `toml:\"metadata\"`\n}\n```\nPoetryLockfile is a parsed poetry.lock.\n\n```go\nfunc ParsePoetryLockfile(data []byte) (*PoetryLockfile, error)\n```\nParsePoetryLockfile reads a poetry.lock document.\n\n```go\nfunc ReadPoetryLockfile(path string) (*PoetryLockfile, error)\n```\nReadPoetryLockfile reads a poetry.lock from a file.\n\n```go\ntype PoetryMetadata struct {\n\tLockVersion    string `toml:\"lock-version\"`\n\tPythonVersions string `toml:\"python-versions\"`\n\tContentHash    string `toml:\"content-hash\"`\n}\n```\nPoetryMetadata is the lock's trailer.\n\n```go\ntype PoetryPackage struct {\n\tName           string `toml:\"name\"`\n\tVersion        string `toml:\"version\"`\n\tDescription    string `toml:\"description\"`\n\tOptional       bool   `toml:\"optional\"`\n\tPythonVersions string `toml:\"python-versions\"`\n\n\t// Groups lists the dependency groups the package serves (lock 2.1).\n\t// Empty in a 2.0 lock, where membership is derived from the manifest.\n\tGroups []string `toml:\"groups\"`\n\n\t// markers says where the package applies at all. A package serving\n\t// several groups may apply to each under a different condition, so the\n\t// lock writes either one marker or a group-to-marker table; the parser\n\t// normalizes both into the table, the plain form under every group.\n\tRawMarkers any               `toml:\"markers\"`\n\tMarkers    map[string]string `toml:\"-\"`\n\n\t// RawDependencies is the resolved dependency table: target name to a\n\t// constraint, which may be a bare version string, an object carrying a\n\t// marker and extras, or a list of such objects when the constraint\n\t// differs by environment. Dependencies is its normalized form.\n\tRawDependencies map[string]any     `toml:\"dependencies\"`\n\tDependencies    []PoetryDependency `toml:\"-\"`\n\n\t// Extras names the package's extras and their requirements. The values\n\t// are requirement strings kept only for completeness: the packages an\n\t// enabled extra pulls in are resolved through Dependencies.\n\tExtras map[string][]string `toml:\"extras\"`\n\n\t// Files are the distribution artifacts with their hashes, named by\n\t// filename rather than URL.\n\tFiles []PoetryFile `toml:\"files\"`\n\n\t// Source is set when the package does not come from an index.\n\tSource *PoetrySource `toml:\"source\"`\n}\n```\nPoetryPackage is one locked package.\n\n```go\ntype PoetrySource struct {\n\tType string `toml:\"type\"` // \"git\", \"url\", \"directory\", \"legacy\"\n\tURL  string `toml:\"url\"`\n\n\t// Reference is what was asked for (a branch, tag or rev) and\n\t// ResolvedReference the commit it resolved to.\n\tReference         string `toml:\"reference\"`\n\tResolvedReference string `toml:\"resolved_reference\"`\n}\n```\nPoetrySource says where a non-index package comes from.\n\n```go\ntype PyPIClient struct {\n\tAgent   *khttp.Agent\n\tBaseURL string\n\n\t// Logger is where the client says what went wrong with its requests.\n\t// The decomposer sets it to its caller's; left nil, nothing is logged.\n\tLogger *clog.Logger\n}\n```\nPyPIClient fetches package metadata from the PyPI JSON API.\n\n```go\nfunc NewPyPIClient(concurrency int) *PyPIClient\n```\nNewPyPIClient creates a client for the PyPI JSON API.\n\n```go\nfunc (c *PyPIClient) FetchAll(packages []packageKey) map[packageKey]*pypiInfo\n```\nFetchAll fetches metadata for the packages in parallel. Failures are skipped: enrichment adds what it can and never breaks an extraction.\n\n```go\ntype PyProject struct {\n\tProject struct {\n\t\tName                 string              `toml:\"name\"`\n\t\tVersion              string              `toml:\"version\"`\n\t\tRequiresPython       string              `toml:\"requires-python\"`\n\t\tDependencies         []string            `toml:\"dependencies\"`\n\t\tOptionalDependencies map[string][]string `toml:\"optional-dependencies\"`\n\t} `toml:\"project\"`\n\n\t// DependencyGroups is the standard groups table (PEP 735). Entries are\n\t// requirement strings, or include tables this reader skips.\n\tDependencyGroups map[string][]any `toml:\"dependency-groups\"`\n\n\tTool struct {\n\t\tPoetry struct {\n\t\t\tName         string              `toml:\"name\"`\n\t\t\tVersion      string              `toml:\"version\"`\n\t\t\tDependencies map[string]any      `toml:\"dependencies\"`\n\t\t\tExtras       map[string][]string `toml:\"extras\"`\n\t\t\tGroup        map[string]struct {\n\t\t\t\tDependencies map[string]any `toml:\"dependencies\"`\n\t\t\t} `toml:\"group\"`\n\t\t\t// DevDependencies is the oldest spelling of the dev group.\n\t\t\tDevDependencies map[string]any `toml:\"dev-dependencies\"`\n\t\t} `toml:\"poetry\"`\n\t} `toml:\"tool\"`\n}\n```\nPyProject is a parsed pyproject.toml, reduced to what the graph needs.\n\n```go\nfunc ReadPyProject(path string) (*PyProject, error)\n```\nReadPyProject reads a pyproject.toml file.\n\n```go\nfunc (p *PyProject) ExtraDependencies() (map[string][]*requirement, error)\n```\nExtraDependencies returns the direct dependencies of every extra.\n\n```go\nfunc (p *PyProject) GroupDependencies() (map[string][]*requirement, error)\n```\nGroupDependencies returns the direct dependencies of every dependency group, whichever of the three spellings declares them.\n\n```go\nfunc (p *PyProject) MainDependencies() ([]*requirement, error)\n```\nMainDependencies returns the project's direct runtime dependencies.\n\nIn the legacy table, python is a pseudo-dependency and an entry marked optional belongs to the extras rather than to the runtime set.\n\n```go\nfunc (p *PyProject) RootName() string\n```\nRootName returns the project's name, normalized, from whichever table states it.\n\n```go\nfunc (p *PyProject) RootVersion() string\n```\nRootVersion returns the project's version from whichever table states it.\n\n```go\ntype Source struct {\n\t// Registry is the index URL the package was resolved from.\n\tRegistry string `toml:\"registry\"`\n\n\t// Virtual and Editable mark the project's own packages: Virtual a\n\t// package that is not installed (the usual project root), Editable one\n\t// installed in place (workspace members). Both hold the directory,\n\t// relative to the lock.\n\tVirtual  string `toml:\"virtual\"`\n\tEditable string `toml:\"editable\"`\n\n\t// Git, Path, Directory and URL are direct dependencies on a repository,\n\t// a local archive, a local directory, and a remote archive.\n\tGit       string `toml:\"git\"`\n\tPath      string `toml:\"path\"`\n\tDirectory string `toml:\"directory\"`\n\tURL       string `toml:\"url\"`\n}\n```\nSource says where a package comes from. Exactly one field is set.\n\n```go\nfunc (s *Source) IsProject() bool\n```\nIsProject says whether the package is part of the project itself rather than something fetched: the roots of the dependency graph.\n\n```go\ntype Version struct {\n\tEpoch   int\n\tRelease []int\n\n\t// Pre is the pre-release phase, in order: \"a\" \u003c \"b\" \u003c \"rc\". The long\n\t// spellings (alpha, beta, c, pre, preview) normalize into those three.\n\tPre    int\n\tPreN   int\n\tHasPre bool\n\n\tPost    int\n\tHasPost bool\n\n\tDev    int\n\tHasDev bool\n\n\tLocal string\n}\n```\nVersion is a parsed PEP 440 version.\n\n```go\nfunc ParseVersion(s string) (*Version, error)\n```\nParseVersion parses a PEP 440 version string.\n\n```go\nfunc (v *Version) Compare(o *Version) int\n```\nCompare orders two versions per PEP 440: -1, 0 or 1 as v sorts before, with or after o. Trailing zeros in the release are insignificant (1.0 == 1.0.0), a dev release sorts before pre-releases, pre-releases before the final, the final before post-releases, and a local version after the same version without one.\n\n","path":"github.com/carabiner-dev/unpack/source/python","name":"python","synopsis":"Package python implements dependency extraction for Python codebases.","isRedistributable":true}
