aboutsummaryrefslogtreecommitdiff
blob: 10ee15f1d938700c1fbdc38567bc8f79b4218618 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__all__ = ['DescriptionTree']

from config import Config
conf = Config()

from description import *
from exception import DescriptionTreeException

import os

class DescriptionTree(object):
    
    def __init__(self):
        
        self.pkg_list = {}
        
        self.__db_path = os.path.join(conf.db, 'octave-forge')
        
        if not os.path.isdir(self.__db_path):
            raise DescriptionTreeException('Invalid db: %s' % self.__db_path)
        
        for cat in [i.strip() for i in conf.categories.split(',')]:
            catdir = os.path.join(self.__db_path, cat)
            if os.path.isdir(catdir):
                self.pkg_list[cat] = []
                pkgs = os.listdir(catdir)
                for pkg in pkgs:
                    mypkg = re_pkg_atom.match(pkg)
                    if mypkg == None:
                        raise DescriptionTreeException('Invalid Atom: %s' % mypkg)
                    if mypkg.group(1) not in conf.blacklist:
                        self.pkg_list[cat].append({
                            'name': mypkg.group(1),
                            'version': mypkg.group(2),
                        })
    
    
    def __getitem__(self, key):
        
        mykey = re_pkg_atom.match(key)
        if mykey == None:
            return None
        
        name = mykey.group(1)
        version = mykey.group(2)
        
        for cat in self.pkg_list:
            for pkg in self.pkg_list[cat]:
                if pkg['name'] == name and pkg['version'] == version:
                    pkgfile = os.path.join(
                        self.__db_path,
                        cat,
                        '%s-%s' % (pkg['name'], pkg['version']),
                        'DESCRIPTION'
                    )
                    return Description(pkgfile)
        
        return None
    
    
    def package_versions(self, pkgname):
        
        tmp = []
        
        for cat in self.pkg_list:
            for pkg in self.pkg_list[cat]:
                if pkg['name'] == pkgname:
                    tmp.append(pkg['version'])
        
        return tmp
        
    
    def latest_version(self, pkgname):
        
        tmp = self.package_versions(pkgname)
        return self.version_compare(tmp)


    def version_compare(self, versions):
        
        max = ('0',)
        maxstr = None
        
        for version in versions:
            tmp = tuple(version.split('.'))
            if tmp > max:
                max = tmp
                maxstr = version
        
        return maxstr