aboutsummaryrefslogtreecommitdiff
blob: 2cc93a9bd6850f3d79420cd0dcf93fdb3009e8ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
A set of utility functions to query portage repos
"""

import os

from os import path

from portage.versions import suffix_value

from pomu.repo.repo import portage_repos, portage_repo_path
from pomu.util.pkg import cpv_split, ver_str

misc_dirs = ['profiles', 'licenses', 'eclass', 'metadata', 'distfiles', 'packages', 'scripts', '.git']

def best_ver(repo, category, name, ver=None):
    """Gets the best (newest) version of a package in the repo"""
    if ver:
        return ver if path.exists(path.join(portage_repo_path(repo),
            category, name, '{}-{}.ebuild'.format(name, ver))) else None
    ebuilds = [category + '/' + name + x[len(name):-7] for x in
            os.listdir(path.join(portage_repo_path(repo), category, name))
            if x.endswith('.ebuild')]
    cat, name, vernum, suff, rev = cpv_split(best(ebuilds))
    return ver_str(vernum, suff, rev)

def repo_pkgs(repo, category, name, ver=None, slot=None):
    """List of package occurences in the repo"""
    if not repo:
        res = []
        for r in portage_repos():
            res.extend(repo_pkgs(r, category, name, ver, slot))
        return res
    if category:
        if path.exists(path.join(portage_repo_path(repo), category, name)):
            res = best_ver(repo, category, name, ver)
            return [(repo, category, name, res)] if res else []
        return []
    rpath = portage_repo_path(repo)
    dirs = set(os.listdir(rpath)) - set(misc_dirs)
    res = []
    for d in dirs:
        if path.isdir(path.join(rpath, d, name)):
            res.append((repo, d, name, best_ver(repo, d, name, ver)))
    return res