aboutsummaryrefslogtreecommitdiff
blob: 599dedefa9e203d139fea50635bab42af3fbdb39 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__all__ = [
    'Description',
    'DescriptionException',
    're_depends',
    're_atom',
    're_pkg_atom'
]

from config import Config
conf = Config()

import re, os

# octave-forge DESCRIPTION's dependencies atoms
re_depends = re.compile(r'([a-zA-Z]+) *(\( *([><=]?=?) *([0-9.]+) *\))?')

# gentoo-like atoms, to use with emerge/whatever package manager
re_atom = re.compile(r'^([><]?=?)([a-zA-Z\-/]+)(-(.*))?$')

# we'll use atoms like 'control-1.0.11' to g-octave packages
re_pkg_atom = re.compile(r'^(.+)-([0-9.]+)$')


class DescriptionException(Exception):
    pass


class Description(object):
    
    def __init__(self, file):
        
        if not os.path.exists(file):
            raise DescriptionException('File not found: %s' % file)
        
        fp = open(file)
        myfile = fp.readlines()
        fp.close()
        
        kw = ''
        
        self.__desc = {}
        
        for i in myfile:
            line = i.split(':')
            if len(line) < 2:
                if i[0].isspace() and kw != '':
                    self.__desc[kw] += ' ' + i.strip()
            else:
                kw = line[0].strip().lower()
                value = ':'.join(line[1:]).strip()
                if self.__desc.has_key(kw):
                    if kw == 'depends' or kw == 'systemrequirements' or kw == 'buildrequires':
                        self.__desc[kw] += ', ' + value
                    else:
                        self.__desc[kw] += ' ' + value
                else:
                    self.__desc[kw] = value
        
        self_depends = []
        
        for i in self.__desc:
            if i == 'depends':
                depends = self.__desc[i]
                self.__desc[i] = self.__depends(depends)
                self_depends = self.__self_depends(depends)
            if i == 'systemrequirements' or i == 'buildrequires':
                self.__desc[i] = self.__requirements(self.__desc[i])
            
        self.__desc['self_depends'] = self_depends
    
    
    def __depends(self, long_atom):
        
        tmp = []
        
        for atom in long_atom.split(','):
            
            r = re_depends.match(atom.strip())
            
            if r != None:
                
                myatom = ''
                
                if r.group(3) != None:
                    myatom += str(r.group(3)) == '==' and '=' or str(r.group(3))
                
                if r.group(1).lower() == 'octave':
                    myatom += 'sci-mathematics/octave'
                else:
                    myatom += 'g-octave/%s' % r.group(1)
                
                if r.group(4) != None:
                    myatom += '-%s' % r.group(4)
                
                tmp.append(myatom)
        
        return tmp
    
    
    def __self_depends(self, long_atom):
        
        tmp = []
        
        for atom in long_atom.split(','):
            
            r = re_depends.match(atom.strip())
            
            if r != None:
                if r.group(1).lower() != 'octave':
                    tmp.append((r.group(1), r.group(3), r.group(4)))
        
        return tmp
    
    
    def __requirements(self, long_atom):
        
        tmp = []
        
        for atom in long_atom.split(','):
            atom = atom.strip()
            
            if conf.dependencies.has_key(atom):
                dep = conf.dependencies[atom]
            
                if dep != '':
                    tmp.append(dep)
        
        return tmp

    
    def __getattr__(self, name):
        
        if self.__desc.has_key(name):
            return self.__desc[name]
        
        return None


if __name__ == '__main__':
    a = Description('/development/contrib/octave-forge-20090607/main/zenity-0.5.7/DESCRIPTION')
    print a.depends