Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
how to get ebuild package information from python?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Freed
n00b
n00b


Joined: 19 Jan 2020
Posts: 39
Location: China

PostPosted: Wed Nov 27, 2024 3:49 pm    Post subject: how to get ebuild package information from python? Reply with quote

https://github.com/pkgcore/pkgcore/discussions/439

I am writing a language server for gentoo ebuild and hope to complete package name. For example, user input app-editors/n will complete app-editors/neovim and app-editors/nano . Is there a python API like get_ebuild_information('app-editors/n') and return a list like
Code:
[{"name": "app-editors/neovim", "version": "0.10.0", "description": "XXX"}, ...]
?

Not only pkgcore, portage or eix or gentoolkit or portageutils is OK if they provide enough API.

TIA!
Back to top
View user's profile Send private message
JimRockford74
n00b
n00b


Joined: 26 Nov 2024
Posts: 5

PostPosted: Wed Nov 27, 2024 4:45 pm    Post subject: Reply with quote

Here is a Portage implementation

Code:
import portage

def get_package_completions(partial_name):
    db = portage.db[portage.root]["porttree"].dbapi
    matches = db.cp_all()
   
    # Filter matches based on partial name
    filtered = [cp for cp in matches if cp.startswith(partial_name)]
   
    results = []
    for cp in filtered:
        # Get the latest version
        versions = db.cp_list(cp)
        if versions:
            latest = versions[-1]
            metadata = db.aux_get(latest, ["DESCRIPTION"])
           
            results.append({
                "name": cp,
                "version": portage.versions.cpv_getversion(latest),
                "description": metadata[0]
            })
   
    return results

# Example usage:
completions = get_package_completions("app-editors/n")


And here is a pkgcore[/i] implimentation

Code:
from pkgcore.config import load_config
from pkgcore.repository.util import get_virtual_repos

def get_package_completions_pkgcore(partial_name):
    config = load_config()
    repo = get_virtual_repos(config.objects["repo-stack"].repos)
   
    results = []
    for pkg in repo:
        if pkg.key.startswith(partial_name):
            results.append({
                "name": pkg.key,
                "version": str(pkg.version),
                "description": pkg.description
            })
   
    return results

# Example usage:
completions = get_package_completions_pkgcore("app-editors/n")


I would choose to use portage since 92% of it is in python and its always available on Gentoo systems, as for pkgcore, it tends to be faster but might need to be installed separately
_________________
This is Jim Rockford. I'm either working a case or waiting for something to finish. If you're selling patience, I'm interested. $200 a day plus expenses. Leave your message at the tone. *beep*
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum