summaryrefslogtreecommitdiff
path: root/portato/eix/py_parser.py
blob: a6927a1719f7875551fc21b611a7967bfc3f4a82 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# -*- coding: utf-8 -*-
#
# File: portato/eix/parser.py
# This file is part of the Portato-Project, a graphical portage-frontend.
#
# Copyright (C) 2006-2010 René 'Necoro' Neumann
# This is free software.  You may redistribute copies of it under the terms of
# the GNU General Public License version 2.
# There is NO WARRANTY, to the extent permitted by law.
#
# Written by René 'Necoro' Neumann <necoro@necoro.net>

"""
The cache file supports different types of data.
In this module (nearly) all of these types have a corresponding function.

For the exact way all the functions work, have a look at the eix format description.
"""


__docformat__ = "restructuredtext"

import os
import struct
from functools import partial

from ..helper import debug
from .exceptions import EndOfFileException

#
# Helper
#

def _get_bytes (file, length, expect_list = False):
    """
    Return a number of bytes.
    
    :Parameters:

        file : file
            The file to read from.

        length : int
            The number of bytes to read.

        expect_list : bool
            In case ``length`` is 1, only a single byte is returned. If ``expect_list`` is true, then a list is also returned in this case.

    :rtype: int or int[]
    :raises EndOfFileException: if EOF is reached during execution
    """

    s = file.read(length)

    if len(s) != length:
        raise EndOfFileException(file.name)

    if length == 1 and not expect_list:
        return ord(s) # is faster than unpack and we have a scalar
    else:
        return struct.unpack("%sB" % length, s)

#
# Base Types
#

def number (file, skip = False):
    """
    Returns a number.

    :Parameters:

        file : file
            The file to read from.

        skip : bool
            Do not return the actual value, but just skip to the next datum.

    :rtype: int
    """

    n = _get_bytes(file, 1)

    if n < 0xFF:
        value = n
    else:
        count = 0

        while (n == 0xFF):
            count += 1
            n = _get_bytes(file, 1)

        if n == 0:
            n = 0xFF # 0xFF is encoded as 0xFF 0x00
            count -= 1
        
        value = n << (count*8)

        if count > 0:

            if skip:
                file.seek(count, os.SEEK_CUR)
                return
            
            else:
                rest = _get_bytes(file, count, expect_list = True)

                for i, r in enumerate(rest):
                    value += r << ((count - i - 1)*8)
        
    return value

def vector (file, get_type, skip = False, nelems = None):
    """
    Returns a vector of elements.

    :Parameters:

        file : file
            The file to read from.

        get_type : function(file, bool)
            The function determining type of the elements.

        skip : bool
            Do not return the actual value, but just skip to the next datum.

        nelems : int
            Normally the eix-Vector has the number of elements as the first argument.
            If for some reason this is not the case, you can pass it in here.

    :rtype: list
    """

    if nelems is None:
        nelems = number(file)
    
    if skip:
        for i in range(nelems):
            get_type(file, skip = True)
    else:
        return [get_type(file) for i in range(nelems)]

def typed_vector(type, nelems = None):
    """
    Shortcut to create a function for a special type of vector.

    :Parameters:

        type : function(file, bool)
            The function determining type of the elements.

        nelems : int
            Normally the eix-Vector has the number of elements as the first argument.
            If for some reason this is not the case, you can pass it in here.
            Do not return the actual value, but just skip to the next datum.

    :rtype: function(file, bool)
    :see: `vector`
    """

    if nelems is None:
        return partial(vector, get_type = type)
    else:
        return partial(vector, get_type = type, nelems = nelems)

def string (file, skip = False):
    """
    Returns a string.

    :Parameters:

        file : file
            The file to read from.

        skip : bool
            Do not return the actual value, but just skip to the next datum.

    :rtype: str
    """
    nelems = number(file)

    if skip:
        file.seek(nelems, os.SEEK_CUR)
        return
    else:
        s = file.read(nelems)

    if len(s) != nelems:
        raise EndOfFileException(file.name)

    return s

#
# Complex Types
#

class LazyElement (object):
    """
    This class models a value in the cache, which is only read on access.

    If not accessed directly, only the position inside the file is stored.
    """
    __slots__ = ("file", "get_type", "_value", "pos")
    
    def __init__ (self, get_type, file):
        """
        :Parameters:

            get_type : function(file, bool)
                The function determining type of the elements.

            file : file
                The file to read from.
        """

        self.file = file
        self.get_type = get_type
        self._value = None

        self.pos = file.tell()
        get_type(file, skip=True) # skip it for the moment

    @property
    def value (self):
        """
        The value of the element.
        """

        if self._value is None:
            old_pos = self.file.tell()
            self.file.seek(self.pos, os.SEEK_SET)
            self._value = self.get_type(self.file, skip = False)
            self.file.seek(old_pos, os.SEEK_SET)
        
        return self._value

    def __call__ (self):
        """
        Convenience function. Also returns the value.
        """
        return self.value

class overlay (object):
    """
    Represents an overlay object.

    :IVariables:

        path : `LazyElement` <string>
            The path to the overlay

        label : `LazyElement` <string>
            The label/name of the overlay
    """
    __slots__ = ("path", "label")

    def __init__ (self, file, skip = False):
        """
        :Parameters:

            file : file
                The file to read from.

            skip : bool
                Do not return the actual value, but just skip to the next datum.
        """
        
        self.path = LazyElement(string, file)
        self.label = LazyElement(string, file)

class header (object):
    """
    Represents the header of the cache.

    :IVariables:

        version : `LazyElement` <int>
            The version of the cache file.

        ncats : `LazyElement` <int>
            The number of categories.

        overlays : `LazyElement` <`overlay` []>
            The list of overlays.

        provide : `LazyElement` <string[]>
            A list of "PROVIDE" values.

        licenses : `LazyElement` <string[]>
            The list of licenses.

        keywords : `LazyElement` <string[]>
            The list of keywords.
        
        useflags : `LazyElement` <string[]>
            The list of useflags.
        
        slots : `LazyElement` <string[]>
            The list of slots different from "0".

        sets : `LazyElement` <string[]>
            The names of world sets are the names (without leading @) of the world sets stored in /var/lib/portage/world_sets.
            If SAVE_WORLD=false, the list is empty.
    """
    __slots__ = ("version", "ncats", "overlays", "provide",
            "licenses", "keywords", "useflags", "slots", "sets")

    def __init__ (self, file, skip = False):
        """
        :Parameters:

            file : file
                The file to read from.

            skip : bool
                Do not return the actual value, but just skip to the next datum.
        """
        def LE (t):
            return LazyElement(t, file)

        self.version = LE(number)
        self.ncats = LE(number)
        self.overlays = LE(typed_vector(overlay))
        self.provide = LE(typed_vector(string))
        self.licenses = LE(typed_vector(string))
        self.keywords = LE(typed_vector(string))
        self.useflags = LE(typed_vector(string))
        self.slots = LE(typed_vector(string))
        self.sets = LE(typed_vector(string))

class package (object):
    """
    The representation of one package.

    Currently, version information is not parsed and stored.
    So you can gain general infos only.

    :IVariables:
        
        name : `LazyElement` <string>
            The name of the package.

        description : `LazyElement` <string>
            Description of the package.

        homepage : `LazyElement` <string>
            The homepage of the package.

        provide : `LazyElement` <int[]>
            The indices of `header.provide` representing the PROVIDE value of the package.

        license : `LazyElement` <int>
            The index of `header.licenses` representing the license of the package.

        useflags : `LazyElement` <int[]>
            The indices of `header.useflags` representing the IUSE value of the package.
    """

    __slots__ = ("_offset", "name", "description", "provide",
            "homepage", "license", "useflags")

    def __init__ (self, file, skip = False):
        """
        :Parameters:

            file : file
                The file to read from.

            skip : bool
                Do not return the actual value, but just skip to the next datum.
        """
        def LE (t):
            return LazyElement(t, file)
        
        self._offset = number(file)
        
        after_offset = file.tell()
        
        self.name = LE(string)
        self.description = LE(string)
        self.provide = LE(typed_vector(number))
        self.homepage = LE(string)
        self.license = LE(number)
        self.useflags = LE(typed_vector(number))
        
        # self.versions = LE(typed_vector(version))
        # for the moment just skip the versions
        file.seek(self._offset - (file.tell() - after_offset), os.SEEK_CUR)

class category (object):
    """
    Represents a whole category.

    :IVariables:

        name : `LazyElement` <string>
            The category name.

        packages : `LazyElement` <`package` []>
            All the packages of the category.
    """
    __slots__ = ("name", "packages")

    def __init__ (self, file, skip = False):
        """
        :Parameters:

            file : file
                The file to read from.

            skip : bool
                Do not return the actual value, but just skip to the next datum.
        """
        self.name = LazyElement(string, file)
        self.packages = LazyElement(typed_vector(package), file)