Commit 292734a9 authored by Seblu's avatar Seblu
Browse files

Move pacman query into a class

pacman database queries are now handled by a singleton class into pacman module.
This will avoid a huge memory consumption to the current implementation.
parent ea6ef213
Loading
Loading
Loading
Loading
+32 −1
Original line number Diff line number Diff line
# coding: utf-8

# archversion - Archlinux Version Controller
# Copyright © 2012 Sébastien Luttringer
# Copyright © 2013 Sébastien Luttringer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@@ -22,6 +22,7 @@

import logging
import os
import pycman
import re
import subprocess

@@ -62,4 +63,34 @@ def pkgbuild_update_checksums(path):
    '''
    subprocess.check_call(["updpkgsums"], shell=False, close_fds=True)

class Pacman(object):
    '''
    Cheap abstration of archlinux package manager
    This object is a singleton to avoid pyalpm to use too much memory
    '''

    _instance = None

    def __new__(cls, config="/etc/pacman.conf"):
        # singleton design pattern
        if cls._instance is None:
            cls._instance = object.__new__(cls)
            cls._handle = pycman.config.PacmanConfig(config).initialize_alpm()
        return cls._instance

    def find_pkg(self, name, repos=None):
        '''
        find a package named name in repos
        '''
        if repos is None:
            dbs = self._handle.get_syncdbs()
        else:
            dbs = [ db for db in self._handle.get_syncdbs() if db.name in repos ]
        # looking into db for package name
        for db in dbs:
            pkg = db.get_pkg(name)
            if pkg is not None:
                return (db, pkg)
        return (None, None)

# vim:set ts=4 sw=4 et ai:
+8 −17
Original line number Diff line number Diff line
@@ -21,13 +21,12 @@


from archversion import USER_AGENT
from archversion.pacman import parse_pkgbuild
from archversion.pacman import parse_pkgbuild, Pacman
from archversion.error import InvalidConfigFile, VersionNotFound
from urllib.request import urlopen, Request
import json
import logging
import os
import pycman
import re
import subprocess
import sys
@@ -101,22 +100,14 @@ class VersionController(object):
        '''Return pacman version'''
        logging.debug("Get pacman version")
        # Load pacman
        pacman = pycman.config.PacmanConfig("/etc/pacman.conf").initialize_alpm()
        # Map db and name
        repos = dict((db.name, db) for db in pacman.get_syncdbs())
        pacman = Pacman()
        # filter if repo is provided
        if "repo" in value:
            allowed_repos = value.get("repo").split(",")
            for r in list(repos.keys()):
                if r not in allowed_repos:
                    repos.pop(r)
        allowed_repos = value.get("repo").split(",") if "repo" in value else None
        # looking into db for package name
        for repo, db in repos.items():
            logging.debug("Repo %s" % repo)
            pkg = db.get_pkg(name)
        db, pkg = pacman.find_pkg(name, allowed_repos)
        if pkg is not None:
            v = pkg.version.rsplit("-")[0]
                logging.debug("pacman version is : %s" % v)
            logging.debug("pacman version in %s: %s" % (db.name, v))
            return v
        raise VersionNotFound("No pacman package found")