summaryrefslogtreecommitdiffstats
path: root/debian/lib/python/debian_linux/utils.py
blob: a43e986982b25bb8f33078bc2370d6a447ddde47 (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
import codecs
import os
import re
import textwrap


class Templates(object):
    def __init__(self, dirs=["debian/templates"]):
        self.dirs = dirs

        self._cache = {}

    def __getitem__(self, key):
        ret = self.get(key)
        if ret is not None:
            return ret
        raise KeyError(key)

    def _read(self, name):
        prefix, id = name.split('.', 1)

        for suffix in ['.in', '']:
            for dir in self.dirs:
                filename = "%s/%s%s" % (dir, name, suffix)
                if os.path.exists(filename):
                    with codecs.open(filename, 'r', 'utf-8') as f:
                        mode = os.stat(f.fileno()).st_mode
                        if prefix == 'control':
                            return (read_control(f), mode)
                        if prefix == 'tests-control':
                            return (read_tests_control(f), mode)
                        return (f.read(), mode)

    def _get(self, key):
        try:
            return self._cache[key]
        except KeyError:
            self._cache[key] = value = self._read(key)
            return value

    def get(self, key, default=None):
        value = self._get(key)
        if value is None:
            return default
        return value[0]

    def get_mode(self, key):
        value = self._get(key)
        if value is None:
            return None
        return value[1]


def read_control(f):
    from .debian import Package
    return _read_rfc822(f, Package)


def read_tests_control(f):
    from .debian import TestsControl
    return _read_rfc822(f, TestsControl)


def _read_rfc822(f, cls):
    entries = []
    eof = False

    while not eof:
        e = cls()
        last = None
        lines = []
        while True:
            line = f.readline()
            if not line:
                eof = True
                break
            # Strip comments rather than trying to preserve them
            if line[0] == '#':
                continue
            line = line.strip('\n')
            if not line:
                break
            if line[0] in ' \t':
                if not last:
                    raise ValueError(
                        'Continuation line seen before first header')
                lines.append(line.lstrip())
                continue
            if last:
                e[last] = '\n'.join(lines)
            i = line.find(':')
            if i < 0:
                raise ValueError(u"Not a header, not a continuation: ``%s''" %
                                 line)
            last = line[:i]
            lines = [line[i + 1:].lstrip()]
        if last:
            e[last] = '\n'.join(lines)
        if e:
            entries.append(e)

    return entries


class TextWrapper(textwrap.TextWrapper):
    wordsep_re = re.compile(
        r'(\s+|'                                  # any whitespace
        r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))')   # em-dash