summaryrefslogtreecommitdiffstats
path: root/lib/python/debian_support.py
blob: 8657be82f2af0599b6e034fe9ec825f91f110775 (plain) (blame)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# debian_support.py -- Python module for Debian metadata
# Copyright (C) 2005 Florian Weimer <fw@deneb.enyo.de>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import print_function

"""This module implements facilities to deal with Debian-specific metadata."""

import gzip
import json
import os.path
import re
import sys
import tempfile
import types
import urllib2
from cStringIO import StringIO

try:
    from hashlib import sha1
except ImportError:
    import sha
    sha1 = sha.new

import apt_pkg
apt_pkg.init()

# Timeout for downloads.
TIMEOUT = 30

class ParseError(Exception):
    """An exception which is used to signal a parse failure.

    Attributes:

    filename - name of the file
    lineno - line number in the file
    msg - error message

    """
    
    def __init__(self, filename, lineno, msg):
        assert type(lineno) == types.IntType
        self.filename = filename
        self.lineno = lineno
        self.msg = msg

    def __str__(self):
        return self.msg

    def __repr__(self):
        return "ParseError(%s, %d, %s)" % (repr(self.filename),
                                           self.lineno,
                                           repr(self.msg))

    def printOut(self, file):
        """Writes a machine-parsable error message to file."""
        file.write("%s:%d: %s\n" % (self.filename, self.lineno, self.msg))
        file.flush()

# This regular expression is used to strip ~bpo1 and ~volatile1 from
# version numbers before they are compared.
_version_normalize_regexp = re.compile(r"~(?:bpo|volatile)[0-9.+]+$")

class Version:
    """Version class which uses the original APT comparison algorithm.

    ~bpo and ~volatile suffixes are ignored."""

    def __init__(self, version):
        """Creates a new Version object."""
        t = type(version)
        if t == types.UnicodeType:
            version = version.encode('UTF-8')
        else:
            assert t == types.StringType, repr(version)
        assert version != ""
        self.__asString = version
        self.__forCompare = _version_normalize_regexp.sub("", version)

    def __str__(self):
        return self.__asString

    def __repr__(self):
        return 'Version(%r)' % self.__asString

    def __cmp__(self, other):
        try:
            return apt_pkg.version_compare(self.__forCompare, other.__forCompare)
        except AttributeError:
            return apt_pkg.VersionCompare(self.__forCompare, other.__forCompare)

def version_compare(a, b):
    """Compares two versions according to the Debian algorithm.
    
    ~bpo and ~volatile suffixes are ignored."""
    a = _version_normalize_regexp.sub("", a)
    b = _version_normalize_regexp.sub("", b)
    try:
        vc = apt_pkg.version_compare
    except AttributeError:
        vc = apt_pkg.VersionCompare
    return vc(a, b)

class PackageFile:
    """A Debian package file.

    Objects of this class can be used to read Debian's Source and
    Packages files."""

    re_field = re.compile(r'^([A-Za-z][A-Za-z0-9-]+):(?:\s*(.*?))?\s*$')
    re_continuation = re.compile(r'^\s+(?:\.|(\S.*?)\s*)$')

    def __init__(self, name, fileObj=None):
        """Creates a new package file object.

        name - the name of the file the data comes from
        fileObj - an alternate data source; the default is to open the
                  file with the indicated name.
        """
        if fileObj is None:
            fileObj = open(name)
        self.name = name
        self.file = fileObj
        self.lineno = 0

    def __iter__(self):
        line = self.file.readline()
        self.lineno += 1
        pkg = []
        while line:
            if line == '\n':
                if len(pkg) == 0:
                    self.raiseSyntaxError('expected package record')
                yield pkg
                pkg = []
                line = self.file.readline()
                self.lineno += 1
                continue
            
            match = self.re_field.match(line)
            if not match:
                self.raiseSyntaxError("expected package field")
            (name, contents) = match.groups()
            contents = contents or ''

            while True:
                line = self.file.readline()
                self.lineno += 1
                match = self.re_continuation.match(line)
                if match:
                    (ncontents,) = match.groups()
                    if ncontents is None:
                        ncontents = ""
                    contents = "%s\n%s" % (contents, ncontents)
                else:
                    break
            pkg.append((name, contents))
        if pkg:
            yield pkg

    def raiseSyntaxError(self, msg, lineno=None):
        if lineno is None:
            lineno = self.lineno
        raise ParseError(self.name, lineno, msg)

class PseudoEnum:
    """A base class for types which resemble enumeration types."""
    def __init__(self, name, order):
        self._name = name
        self._order = order
    def __repr__(self):
        return '%s(%r)'% (self.__class__.__name__, self._name)
    def __str__(self):
        return self._name
    def __cmp__(self, other):
        return cmp(self._order, other._order)
    def __hash__(self):
        return hash(self._order)

class Release(PseudoEnum): pass

def listReleases():
    releases = {}
    rels = ("experimental", # For use in [brackets] in the list files.
            "potato", "woody", "sarge", "etch", "lenny", "squeeze", "wheezy", "jessie", "stretch", "buster", "sid")
    for r in range(len(rels)):
        releases[rels[r]] = Release(rels[r], r)
    Release.releases = releases
    return releases
def internRelease(name, releases=listReleases()):
    if name in releases:
        return releases[name]
    else:
        return None
del listReleases

def readLinesSHA1(lines):
    m = sha1()
    for l in lines:
        m.update(l)
    return m.hexdigest()

def patchesFromEdScript(source,
                        re_cmd=re.compile(r'^(\d+)(?:,(\d+))?([acd])$')):
    """Converts source to a stream of patches.

    Patches are triples of line indexes:

    - number of the first line to be replaced
    - one plus the number of the last line to be replaced
    - list of line replacements

    This is enough to model arbitrary additions, deletions and
    replacements.
    """

    i = iter(source)
    
    for line in i:
        match = re_cmd.match(line)
        if match is None:
            raise ValueError("invalid patch command: " + repr(line))

        (first, last, cmd) = match.groups()
        first = int(first)
        if last is not None:
            last = int(last)

        if cmd == 'd':
            first = first - 1
            if last is None:
                last = first + 1
            yield (first, last, [])
            continue

        if cmd == 'a':
            if last is not None:
                raise ValueError("invalid patch argument: " + repr(line))
            last = first
        else:                           # cmd == c
            first = first - 1
            if last is None:
                last = first + 1

        lines = []
        for l in i:
            if l == '':
                raise ValueError("end of stream in command: " + repr(line))
            if l == '.\n' or l == '.':
                break
            lines.append(l)
        yield (first, last, lines)

def patchLines(lines, patches):
    """Applies patches to lines.  Updates lines in place."""
    for (first, last, args) in patches:
        lines[first:last] = args

def replaceFile(lines, local):

    import os.path

    local_new = local + '.new'
    new_file = open(local_new, 'w+')

    try:
        for l in lines:
            new_file.write(l)
        new_file.close()
        os.rename(local_new, local)
    finally:
        if os.path.exists(local_new):
            os.unlink(local_new)

def downloadGunzipLines(remote):
    """Downloads a file from a remote location and gunzips it.

    Returns the lines in the file."""

    data = urllib2.urlopen(remote, timeout=TIMEOUT)
    try:
        gfile = gzip.GzipFile(fileobj=StringIO(data.read()))
        try:
            return gfile.readlines()
        finally:
            gfile.close()
    finally:
        data.close()
        
def downloadFile(remote, local):
    """Copies a gzipped remote file to the local system.

    remote - URL, without the .gz suffix
    local - name of the local file
    """
    
    lines = downloadGunzipLines(remote + '.gz')
    replaceFile(lines, local)
    return lines

def updateFile(remote, local, verbose=None):
    """Updates the local file by downloading a remote patch.

    Returns a list of lines in the local file.
    """

    try:
        local_file = open(local)
    except IOError:
        if verbose:
            print("updateFile: no local copy, downloading full file")
        return downloadFile(remote, local)

    lines = local_file.readlines()
    local_file.close()
    local_hash = readLinesSHA1(lines)
    patches_to_apply = []
    patch_hashes = {}
    
    index_name = remote + '.diff/Index'

    re_whitespace=re.compile('\s+')

    try:
        index_url = urllib2.urlopen(index_name, timeout=TIMEOUT)
        index_fields = list(PackageFile(index_name, index_url))
    except ParseError:
        if verbose:
            print("updateFile: could not interpret patch index file")
        return downloadFile(remote, local)
    except IOError:
        if verbose:
            print("updateFile: could not download patch index file")
        return downloadFile(remote, local)

    for fields in index_fields:
        for (field, value) in fields:
            if field == 'SHA1-Current':
                (remote_hash, remote_size) = re_whitespace.split(value)
                if local_hash == remote_hash:
                    if verbose:
                        print("updateFile: local file is up-to-date")
                    return lines
                continue

            if field =='SHA1-History':
                for entry in value.splitlines():
                    if entry == '':
                        continue
                    (hist_hash, hist_size, patch_name) \
                                = re_whitespace.split(entry)

                    # After the first patch, we have to apply all
                    # remaining patches.
                    if patches_to_apply or  hist_hash == local_hash:
                        patches_to_apply.append(patch_name)
                        
                continue
            
            if field == 'SHA1-Patches':
                for entry in value.splitlines():
                    if entry == '':
                        continue
                    (patch_hash, patch_size, patch_name) \
                                 = re_whitespace.split(entry)
                    patch_hashes[patch_name] = patch_hash
                continue
            
            if verbose:
                print("updateFile: field %s ignored" % repr(field))
        
    if not patches_to_apply:
        if verbose:
            print("updateFile: could not find historic entry", local_hash)
        return downloadFile(remote, local)

    for patch_name in patches_to_apply:
        if verbose:
            print("updateFile: downloading patch " + repr(patch_name))
        try:
            patch_contents = downloadGunzipLines(remote + '.diff/' + patch_name
                                                 + '.gz')
        except IOError:
            return downloadFile(remote, local)
        if readLinesSHA1(patch_contents ) != patch_hashes[patch_name]:
            if verbose:
                print("updateFile: patch was garbled: " + repr(patch_name))
            return downloadFile(remote, local)
        patchLines(lines, patchesFromEdScript(patch_contents))
        
    new_hash = readLinesSHA1(lines)
    if new_hash != remote_hash:
        if verbose:
            print("updateFile: patch failed, got %s instead of %s"
                % (new_hash, remote_hash))
        return downloadFile(remote, local)

    replaceFile(lines, local)
    return lines

def mergeAsSets(*args):
    """Create an order set (represented as a list) of the objects in
    the sequences passed as arguments."""
    s = {}
    for x in args:
        for y in x:
            s[y] = True
    l = s.keys()
    l.sort()
    return l

class BinaryPackage(object):
    __slots__ = ("name", "version", "arch", "source", "source_version")

    def __init__(self, data=None):
        if data is not None:
            self.loadtuple(data)

    def loadentry(self, lines):
        """Loads an entry from the Packages file.

        LINES is a sequence of string pairs (KEY, VALUE).
        """
        pkg_name = None
        pkg_version = None
        pkg_arch = None
        pkg_source = None
        pkg_source_version = None
        for (name, contents) in lines:
            name = name.lower()
            if name == "package":
                pkg_name = contents
            elif name == "version":
                pkg_version = contents
            elif name == "source":
                match = self.re_source.match(contents)
                if match is None:
                    raise SyntaxError(('package %s references '
                                       + 'invalid source package %s') %
                                      (pkg_name, repr(contents)))
                (pkg_source, pkg_source_version) = match.groups()
            elif name == "architecture":
                pkg_arch = contents
        if pkg_name is None:
            raise SyntaxError\
                  ("package record does not contain package name")
        if pkg_version is None:
            raise SyntaxError\
                  ("package record for %s does not contain version"
                   % pkg_name)
        if pkg_arch is None:
            raise SyntaxError\
                  ("package record for %s lacks Architecture: field"
                   % pkg_name)
        if pkg_source is None:
            pkg_source = pkg_name
        if pkg_source_version is None:
            pkg_source_version = pkg_version
        self.loadtuple((pkg_name, pkg_version, pkg_arch,
                        pkg_source, pkg_source_version))

    def loadtuple(self, data):
        if None in data:
            raise ValueError("None not permitted: " + repr(data))
        self.name, self.version, self.arch, self.source, self.source_version =\
            data
    
    def load822(self, data):
        "Loads this object from a Deb822-like object."

        pkg = data["Package"]
        version = data["Version"]
        if "Source" in data:
            source = data.get("Source", None)
            match = self.re_source.match(source)
            if match is None:
                raise ValueError("invalid Source field: " + repr(source))
            src, src_version = match.groups()
            if src_version is None:
                src_version = version
        else:
            src = pkg
            src_version = version
        self.loadtuple((pkg, version, data["Architecture"], src, src_version))

    def astuple(self):
        return (self.name, self.version, self.arch,
                self.source, self.source_version)

    def __repr__(self):
        return "BinaryPackage(" + repr(self.astuple()) + ")"

def findresource(*pathseq):
    """Finds the file refered to PATHSEQ, relative to the installation
    based directory."""
    for path in sys.path:
        path = os.path.realpath(path)
        path = os.path.dirname(path)
        path = os.path.dirname(path)
        path = os.path.join(path, *pathseq)
        if os.path.exists(path):
            return path
    raise IOError("not found: " + repr(os.path.join(*pathseq)))

_config = None
def getconfig():
    """Returns the configuration in data/config.json."""
    global _config
    if _config is not None:
        return _config
    _config = json.load(open(findresource("data", "config.json")))
    return _config

_releasecodename = None
def releasecodename(dist):
    """Converts a release name to the code name.
    For instance, "sid" and "unstable" are turned into "sid"."""
    global _releasecodename
    if _releasecodename is None:
        result = {}
        for (codename, obj) in getconfig()["distributions"].items():
            result[codename] = codename
            if "release" in obj:
                result[obj["release"]] = codename
        _releasecodename = result
    try:
        return _releasecodename[dist]
    except:
        raise ValueError("invalid release name: " + repr(dist))

def test():
    # Version
    assert Version('0') < Version('a')
    assert Version('1.0') < Version('1.1')
    assert Version('1.2') < Version('1.11')
    assert Version('1.0-0.1') < Version('1.1')
    assert Version('1.0-0.1') < Version('1.0-1')
    assert Version('1.0-0.1') == Version('1.0-0.1')
    assert Version('1.0-0.1') < Version('1.0-1')
    assert Version('1.0final-5sarge1') > Version('1.0final-5') \
           > Version('1.0a7-2')
    assert Version('0.9.2-5') < Version('0.9.2+cvs.1.0.dev.2004.07.28-1.5')
    assert Version('1:500') < Version('1:5000')
    assert Version('100:500') > Version('11:5000')
    assert Version('1.0.4-2') > Version('1.0pre7-2')

    # Release
    assert internRelease('sarge') < internRelease('etch')

    # PackageFile
    # for p in PackageFile('../../data/packages/sarge/Sources'):
    #     assert p[0][0] == 'Package'
    # for p in PackageFile('../../data/packages/sarge/Packages.i386'):
    #     assert p[0][0] == 'Package'

    # Helper routines
    assert readLinesSHA1([]) == 'da39a3ee5e6b4b0d3255bfef95601890afd80709'
    assert readLinesSHA1(['1\n', '23\n']) \
           == '14293c9bd646a15dc656eaf8fba95124020dfada'

    file_a = map(lambda x: "%d\n" % x, range(1, 18))
    file_b = ['0\n', '1\n', '<2>\n', '<3>\n', '4\n', '5\n', '7\n', '8\n',
              '11\n', '12\n', '<13>\n', '14\n', '15\n', 'A\n', 'B\n', 'C\n',
              '16\n', '17\n',]
    patch = ['15a\n', 'A\n', 'B\n', 'C\n', '.\n', '13c\n', '<13>\n', '.\n',
             '9,10d\n', '6d\n', '2,3c\n', '<2>\n', '<3>\n', '.\n', '0a\n',
             '0\n', '.\n']
    patchLines(file_a, patchesFromEdScript(patch))
    assert ''.join(file_b) == ''.join(file_a)

    assert len(mergeAsSets([])) == 0
    assert ''.join(mergeAsSets("abc", "cb")) == "abc"

    assert repr(internRelease("sid")) == "Release('sid')"

if __name__ == "__main__":
    test()

© 2014-2024 Faster IT GmbH | imprint | privacy policy