summaryrefslogtreecommitdiff
path: root/git-interface/aurinfo.py
blob: 86d0705041b09f5859f92e7428ef7e94da1b0222 (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
#!/usr/bin/env python

from copy import copy, deepcopy
import pprint
import sys

class Attr(object):
    def __init__(self, name, is_multivalued=False, allow_arch_extensions=False):
        self.name = name
        self.is_multivalued = is_multivalued
        self.allow_arch_extensions = allow_arch_extensions

PKGBUILD_ATTRIBUTES = {
    'arch':         Attr('arch',            True),
    'backup':       Attr('backup',          True),
    'changelog':    Attr('changelog',       False),
    'checkdepends': Attr('checkdepends',    True),
    'conflicts':    Attr('conflicts',       True, True),
    'depends':      Attr('depends',         True, True),
    'epoch':        Attr('epoch',           False),
    'groups':       Attr('groups',          True),
    'install':      Attr('install',         False),
    'license':      Attr('license',         True),
    'makedepends':  Attr('makedepends',     True, True),
    'md5sums':      Attr('md5sums',         True, True),
    'noextract':    Attr('noextract',       True),
    'optdepends':   Attr('optdepends',      True, True),
    'options':      Attr('options',         True),
    'pkgname':      Attr('pkgname',         False),
    'pkgrel':       Attr('pkgrel',          False),
    'pkgver':       Attr('pkgver',          False),
    'provides':     Attr('provides',        True, True),
    'replaces':     Attr('replaces',        True, True),
    'sha1sums':     Attr('sha1sums',        True, True),
    'sha224sums':   Attr('sha224sums',      True, True),
    'sha256sums':   Attr('sha256sums',      True, True),
    'sha384sums':   Attr('sha384sums',      True, True),
    'sha512sums':   Attr('sha512sums',      True, True),
    'source':       Attr('source',          True, True),
    'url':          Attr('url',             False),
    'validpgpkeys': Attr('validpgpkeys',    True),
}

def find_attr(attrname):
    # exact match
    attr = PKGBUILD_ATTRIBUTES.get(attrname, None)
    if attr:
        return attr

    # prefix match
    # XXX: this could break in the future if PKGBUILD(5) ever
    # introduces a key which is a subset of another.
    for k in PKGBUILD_ATTRIBUTES.keys():
        if attrname.startswith(k + '_'):
            return PKGBUILD_ATTRIBUTES[k]

def IsMultiValued(attrname):
    attr = find_attr(attrname)
    return attr and attr.is_multivalued

class AurInfo(object):
    def __init__(self):
        self._pkgbase = {}
        self._packages = {}

    def GetPackageNames(self):
        return self._packages.keys()

    def GetMergedPackage(self, pkgname):
        package = deepcopy(self._pkgbase)
        package['pkgname'] = pkgname
        for k, v in self._packages.get(pkgname).items():
            package[k] = deepcopy(v)
        return package

    def AddPackage(self, pkgname):
        self._packages[pkgname] = {}
        return self._packages[pkgname]

    def SetPkgbase(self, pkgbasename):
        self._pkgbase = {'pkgname' : pkgbasename}
        return self._pkgbase


class StderrECatcher(object):
    def Catch(self, lineno, error):
        print('ERROR[%d]: %s' % (lineno, error), file=sys.stderr)


class CollectionECatcher(object):
    def __init__(self):
        self._errors = []

    def Catch(self, lineno, error):
        self._errors.append((lineno, error))

    def HasErrors(self):
        return len(self._errors) > 0

    def Errors(self):
        return copy(self._errors)


def ParseAurinfoFromIterable(iterable, ecatcher=None):
    aurinfo = AurInfo()

    if ecatcher is None:
        ecatcher = StderrECatcher()

    current_package = None
    lineno = 0

    for line in iterable:
        lineno += 1

        if line.startswith('#'):
            continue

        if not line.strip():
            # end of package
            current_package = None
            continue

        if not line.startswith('\t'):
            # start of new package
            try:
                key, value = map(str.strip, line.split('=', 1))
            except ValueError:
                ecatcher.Catch(lineno, 'unexpected header format in section=%s' %
                    current_package['pkgname'])
                continue

            if key == 'pkgbase':
                current_package = aurinfo.SetPkgbase(value)
            elif key == 'pkgname':
                current_package = aurinfo.AddPackage(value)
            else:
                ecatcher.Catch(lineno, 'unexpected new section not starting '
                               'with \'pkgname\' found')
                continue
        else:
            # package attribute
            if current_package is None:
                ecatcher.Catch(lineno, 'package attribute found outside of '
                               'a package section')
                continue

            try:
                key, value = map(str.strip, line.split('=', 1))
            except ValueError:
                ecatcher.Catch(lineno, 'unexpected attribute format in '
                               'section=%s' % current_package['pkgname'])

            if IsMultiValued(key):
                if not current_package.get(key):
                    current_package[key] = []
                if value:
                    current_package[key].append(value)
            else:
                if not current_package.get(key):
                    current_package[key] = value
                else:
                    ecatcher.Catch(lineno, 'overwriting attribute '
                                   '%s: %s -> %s' % (key, current_package[key],
                                                     value))

    return aurinfo


def ParseAurinfo(filename='.AURINFO', ecatcher=None):
    with open(filename) as f:
        return ParseAurinfoFromIterable(f, ecatcher)


def ValidateAurinfo(filename='.AURINFO'):
    ecatcher = CollectionECatcher()
    ParseAurinfo(filename, ecatcher)
    errors = ecatcher.Errors()
    for error in errors:
        print('error on line %d: %s' % error, file=sys.stderr)
    return not errors


if __name__ == '__main__':
    pp = pprint.PrettyPrinter(indent=4)

    if len(sys.argv) == 1:
        print('error: not enough arguments')
        sys.exit(1)
    elif len(sys.argv) == 2:
        action = sys.argv[1]
        filename = '.AURINFO'
    else:
        action, filename = sys.argv[1:3]

    if action == 'parse':
        aurinfo = ParseAurinfo()
        for pkgname in aurinfo.GetPackageNames():
            print(">>> merged package: %s" % pkgname)
            pp.pprint(aurinfo.GetMergedPackage(pkgname))
            print()
    elif action == 'validate':
        sys.exit(not ValidateAurinfo(filename))
    else:
        print('unknown action: %s' % action)
        sys.exit(1)

# vim: set et ts=4 sw=4: