meta_package_manager.specifier moduleΒΆ

Utilities to manage and resolve constraints from a set of package specifiers.

meta_package_manager.specifier.VERSION_SEP: Final = '@'ΒΆ

Separator used by mpm to split package’s ID from its version:

This has been chosen as a separator because it is shared by popular package managers (like npm) and pURLs.

..code-block:

package_id@version
meta_package_manager.specifier.PURL_MAP: dict[str, set[str] | None] = {'alpine': None, 'alpm': {'aura', 'dkp-pacman', 'pacaur', 'pacman', 'pamac', 'paru', 'pikaur', 'shelly', 'trizen', 'yay'}, 'android': None, 'apache': None, 'apk': {'apk'}, 'bitbucket': None, 'bitnami': None, 'bower': None, 'brew': {'brew', 'cask'}, 'buildroot': None, 'cargo': {'cargo'}, 'carthage': None, 'chef': None, 'chocolatey': {'choco'}, 'clojars': None, 'cocoapods': None, 'composer': {'composer'}, 'conan': None, 'conda': {'conda', 'mamba', 'micromamba', 'pixi'}, 'coreos': None, 'cpan': {'cpan'}, 'cran': {'cran'}, 'crystal': None, 'ctan': None, 'deb': {'apt', 'apt-mint', 'aptitude', 'nala'}, 'docker': None, 'drupal': None, 'dtype': None, 'dub': None, 'ebuild': {'emerge', 'pmerge'}, 'eclipse': None, 'elm': None, 'gem': {'gem'}, 'generic': None, 'gitea': None, 'github': None, 'gitlab': None, 'golang': {'go'}, 'gradle': None, 'guix': {'guix'}, 'hackage': None, 'haxe': {'haxelib'}, 'helm': None, 'hex': None, 'huggingface': None, 'julia': None, 'luarocks': {'luarocks'}, 'maven': None, 'melpa': {'emacs'}, 'meteor': None, 'mlflow': None, 'nim': {'nimble'}, 'nix': {'nix'}, 'npm': {'bun', 'npm', 'pnpm', 'volta', 'yarn', 'yarn-berry'}, 'nuget': {'dotnet'}, 'oci': None, 'opam': None, 'openwrt': {'opkg'}, 'osgi': None, 'p2': None, 'pear': {'pear'}, 'pecl': None, 'perl6': {'zef'}, 'platformio': None, 'pub': None, 'puppet': None, 'pypi': {'pip', 'pipx', 'pipxu', 'uv', 'uvx'}, 'qpkg': None, 'rpm': {'dnf', 'dnf5', 'microdnf', 'rpm-ostree', 'urpmi', 'yum', 'zypper'}, 'rubygems': {'gem'}, 'sourceforge': None, 'sublime': None, 'swid': None, 'terraform': None, 'vagrant': None, 'vim': None, 'vscode-extension': {'vscode', 'vscodium'}, 'wordpress': None, 'yocto': None}ΒΆ

Map pURL types to the IDs of the managers handling them.

The keys hold most registered types of PURL_TYPES, and the candidate types of the older list of the purl-spec repository, like alpine or nix, which a pURL can still carry. A key mapping to None names a type no manager handles.

meta_package_manager.specifier.BREW_DEFAULT_TAPS: dict[str, str] = {'homebrew/cask': 'cask', 'homebrew/core': 'brew'}ΒΆ

The default taps of Homebrew, by the ID of the manager installing from each.

Their formulae and casks go by a bare name: pkg:brew/homebrew/cask/firefox names the firefox cask. The brew type of purl-spec covers both, and reads a pURL without a namespace as a formula of homebrew/core.

class meta_package_manager.specifier.Specifier(raw_spec, package_id, manager_id=None, version=None)[source]ΒΆ

Bases: object

Lightweight representation of a package specification.

Contains all parsed metadata to be used as constraints.

raw_spec: strΒΆ

Original, un-parsed specifier string provided by the user.

package_id: strΒΆ

ID is required and is the primary key used for specification.

manager_id: str | None = NoneΒΆ
version: str | None = NoneΒΆ

Version string, a 1:1 copy of the one provided by the user.

classmethod parse_purl(spec_str)[source]ΒΆ

Resolve a pURL into its corresponding manager candidates.

Yields Specifier objects or returns None.

The package ID joins the namespace back to the name, as purl_parts() split them: pkg:npm/%40angular/animation resolves to @angular/animation, not to the unrelated animation package. The types of PURL_QUALIFYING_NAMESPACES keep the name alone, so pkg:deb/debian/curl resolves to curl.

parse_purl() decodes the parts, and normalizes them by the rules of a registered type alone: pkg:npm/JSONStream keeps its case. A pURL breaking the rules of its type is read anyway, with a warning. A type that is also a manager ID gets no warning: the pURLs mpm writes take the manager ID as the type, and some managers name a package in a way the registered type forbids, like a Perl module in a pkg:cpan/ pURL.

Return type:

tuple[Specifier, ...] | None

classmethod from_string(spec_str)[source]ΒΆ

Parse a string into a package specifier.

Supports various formats: - plain package_id - simple package ID with version: package_id@version - package with multiple version separators: @eslint/json@0.9.0 - pURL: pkg:npm/left-pad@3.7

If a specifier resolves to multiple constraints (as it might be the case for pURL), we produce and returns all variations. That way the Solver below has all necessary details to resolve the constraints.

Returns a tuple of Specifier.

Return type:

tuple[Specifier, ...]

property parsed_version: TokenizedString[source]ΒΆ
exception meta_package_manager.specifier.EmptyReduction[source]ΒΆ

Bases: Exception

Raised by the solver if no constraint can’t be met.

class meta_package_manager.specifier.Solver(spec_strings=None, manager_priority=None)[source]ΒΆ

Bases: object

Combine a set of Specifier and solve the constraints they represent.

spec_pool: set[Specifier]ΒΆ
manager_priority: Sequence[str] = ()ΒΆ
populate_from_strings(spec_strings)[source]ΒΆ

Populate the solver with package specifiers parsed from provided strings.

top_priority_manager(keep_managers=None)[source]ΒΆ

Returns the top priority manager configured on the solver.

keep_managers allows for filtering by discarding managers not in that list.

Return type:

str | None

reduce_specs(specs)[source]ΒΆ

Reduce a collection of Specifier to its essential, minimal and unique form.

This method assumes that all provided specs are of the same package (like resolve_package_specs() does).

The reduction process consist of several steps. At each step, as soon as we managed to reduce the constraints to one Specifier, we returns it.

Filtering steps:

  1. We remove all constraints tied to all by the top priority manager if provided.

  2. If no manager priority is provided, we discard constraints not tied to a manager.

  3. We discard constraints not tied to a version.

  4. We only keep constraints tied to the highest version.

If we ends up with more than one set of constraints after all this filtering, an error is raised to invite the developer to troubleshoot the situation and refine this process.

Return type:

Specifier

resolve_package_specs()[source]ΒΆ

Regroup specs of the pool by package IDs, and solve their constraints.

Each package ID yields one reduced spec per distinct target manager. A package therefore produces several specs when the user explicitly names several managers for it (pkg:uv/rich pkg:brew/rich β†’ one spec each). In contrast, a single alias pURL that expands to several managers (pkg:rpm/ping β†’ dnf/yum/zypper) is a set of alternatives, reduced to the top-priority one.

Return type:

Iterator[tuple[str, Specifier]]

resolve_specs_group_by_managers()[source]ΒΆ

Resolves package specs, and returns them grouped by managers.

Return type:

dict[str | None, set[Specifier]]