Portage API

From Funtoo
Revision as of 00:44, September 28, 2015 by Drobbins (talk | contribs) (Created page with "This page is the beginning of the missing documentation on the Portage Python API. == Introduction == Portage has always had a Python API; however, this API does not appear...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page is the beginning of the missing documentation on the Portage Python API.

Introduction

Portage has always had a Python API; however, this API does not appear to be documented at all. This page represents an initial effort to document the API that is available for querying packages, etc.

Portage DBAPI

The API that exists in Portage to query this is called the DBAPI, and the "porttree" -- What is typically stored in Template:P, and querying the porttree is what we will examine here, using the interactive python interpreter.

w520 drobbins # python
Python 3.3.5 (default, Sep 21 2015, 23:01:43) 
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> portage.root
'/'
>>> import portage
>>> p = portage.db[portage.root]["porttree"].dbapi
>>>

p now contains a reference to the DBAPI associated with the Portage tree associated with the system installed at portage.root, which is typically /usr/portage. We can now perform various queries using this variable.

>>> p.cp_list("sys-apps/portage")
['sys-apps/portage-2.3.6-r9', 'sys-apps/portage-2.3.8']

Above, cp_list takes what is called a "catpkg", which is a reference to a particular ebuild in a repository in "category/packagename" format. We can also get lists of specific available packages, using other functions:

>>> p.match("sys-apps/portage")
['sys-apps/portage-2.3.6-r9', 'sys-apps/portage-2.3.8']
>>> p.match("=sys-apps/portage-2*")
['sys-apps/portage-2.3.6-r9', 'sys-apps/portage-2.3.8']

While at first the match() method looks no different than the cp_list() method, you can see that it accepts any valid dependency atom, which is any individual dependency reference to a particular package. The match() method is actually a shortcut for the more sophisticated xmatch() method -- it matches all visible (non-masked) packages that satisfy the dependency.

Using xmatch(), more sophisticated matching can be performed: