summaryrefslogtreecommitdiff
path: root/pygnulib/module.py
blob: b72847d78006a4d74b3557e433d4948e8bebaac0 (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
#!/usr/bin/python
# encoding: UTF-8



import codecs
import collections
import hashlib
import os
import re



class Module:
    """gnulib generic module"""
    _TABLE_ = {
        "description"     : (0x00, str, "Description"),
        "comment"         : (0x01, str, "Comment"),
        "status"          : (0x02, str, "Status"),
        "notice"          : (0x03, str, "Notice"),
        "applicability"   : (0x04, str, "Applicability"),
        "files"           : (0x05, list, "Files"),
        "dependencies"    : (0x06, list, "Depends-on"),
        "configure_early" : (0x07, str, "configure.ac-early"),
        "configure"       : (0x08, str, "configure.ac"),
        "makefile"        : (0x09, str, "Makefile.am"),
        "include"         : (0x0A, list, "Include"),
        "link"            : (0x0B, list, "Link"),
        "license"         : (0x0C, str, "License"),
        "maintainers"     : (0x0D, list, "Maintainer"),
    }
    _PATTERN_DEPENDENCIES_ = re.compile("^(\\S+)(?:\\s+(.+))*$")
    _PATTERN_INCLUDE_ = re.compile("^[\\<\"]([A-Za-z0-9/\\-_]+\\.h)[\\>\"](?:\\s+.*^)*$")


    def __init__(self, name, **kwargs):
        if not isinstance(name, str):
            raise TypeError("name must be of 'str' type")
        self._name_ = name
        self._table_ = {"maintainers": ["all"]}
        for key in Module._TABLE_:
            self._table_[key] = ""
        for key, value in kwargs.items():
            self._table_[key] = value


    @property
    def name(self):
        """name"""
        return self._name_

    @name.setter
    def name(self, value):
        if not isinstance(value, str):
            raise TypeError("'str' type is expected")
        self._name_ = value


    @property
    def description(self):
        """description"""
        return self._table_["description"]

    @description.setter
    def description(self, value):
        if not isinstance(value, str):
            raise TypeError("'str' type is expected")
        self._table_["description"] = value


    @property
    def comment(self):
        """comment"""
        return self._table_["comment"]

    @comment.setter
    def comment(self, value):
        if not isinstance(value, str):
            raise TypeError("'str' type is expected")
        self._table_["comment"] = value


    @property
    def status(self):
        """status"""
        return self._table_["status"]

    @status.setter
    def status(self, value):
        if not isinstance(value, str):
            raise TypeError("'str' type is expected")
        self._table_["status"] = value


    @property
    def notice(self):
        """notice"""
        return self._table_["notice"]

    @notice.setter
    def notice(self, value):
        if not isinstance(value, str):
            raise TypeError("'str' type is expected")
        self._table_["notice"] = value


    @property
    def applicability(self):
        """applicability (usually "main" or "tests")"""
        default = "main" if self.name.endswith("-tests") else "tests"
        current = self._table_["applicability"]
        return current if current.strip() else default

    @applicability.setter
    def applicability(self, value):
        if not isinstance(value, str):
            raise TypeError("'str' type is expected")
        self._table_["applicability"] = value


    @property
    def files(self):
        """file dependencies iterator (set of strings)"""
        for file in self._table_["files"]:
            yield file

    @files.setter
    def files(self, iterable):
        if isinstance(iterable, (bytes, str)) \
        or not isinstance(iterable, collections.Iterable):
            raise TypeError("iterable of strings is expected")
        result = set()
        for item in iterable:
            if not isinstance(item, str):
                raise TypeError("iterable of strings is expected")
            result.update([item])
        self._table_["files"] = result


    @property
    def dependencies(self):
        """dependencies iterator (name, condition)"""
        for entry in self._table_["dependencies"]:
            yield Module._PATTERN_DEPENDENCIES_.findall(entry)[0]

    @dependencies.setter
    def dependencies(self, iterable):
        error = TypeError("iterable of pairs (name, condition) is expected")
        if isinstance(iterable, (bytes, str)) \
        or not isinstance(iterable, collections.Iterable):
            raise error
        result = set()
        try:
            for pair in iterable:
                (name, condition) = pair
                if not isinstance(name, str) \
                or not isinstance(condition, str):
                    raise error
                result.update([(name, condition)])
        except ValueError:
            raise error
        self._table_["dependencies"] = result


    @property
    def configure_early(self):
        """early configure.ac snippet"""
        return self._table_["configure_early"]

    @configure_early.setter
    def configure_early(self, value):
        if not isinstance(value, str):
            raise TypeError("'str' type is expected")
        self._table_["configure_early"] = value


    @property
    def configure(self):
        """configure.ac snippet"""
        return self._table_["configure"]

    @configure.setter
    def configure(self, value):
        if not isinstance(value, str):
            raise TypeError("'str' type is expected")
        self._table_["configure"] = value


    @property
    def makefile(self):
        """makefile snippet"""
        return self._table_["makefile"]

    @makefile.setter
    def makefile(self, value):
        if not isinstance(value, str):
            raise TypeError("'str' type is expected")
        self._table_["makefile"] = value


    @property
    def include(self):
        """include files iterator (header, comment)"""
        for entry in self._table_["include"]:
            match = Module._PATTERN_INCLUDE_.findall(entry)
            yield match[0] if match else entry

    @include.setter
    def include(self, iterable):
        error = TypeError("iterable of pairs (header, comment) is expected")
        if isinstance(iterable, (bytes, str)) \
        or not isinstance(iterable, collections.Iterable):
            raise error
        result = set()
        try:
            for pair in iterable:
                (header, comment) = pair
                if not isinstance(header, str) \
                or not isinstance(comment, str):
                    raise error
                result.update([(header, comment)])
        except ValueError:
            raise error
        self._table_["include"] = result


    @property
    def link(self):
        """linkage iterator (string)"""
        for entry in self._table_["link"]:
            yield entry

    @link.setter
    def link(self, iterable):
        if isinstance(iterable, (bytes, str)) \
        or not isinstance(iterable, collections.Iterable):
            raise TypeError("iterable of strings is expected")
        result = set()
        for item in iterable:
            if not isinstance(item, str):
                raise TypeError("iterable of strings is expected")
            result.update([item])
        self._table_["link"] = result


    @property
    def license(self):
        """license"""
        return self._table_["license"]

    @license.setter
    def license(self, value):
        if not isinstance(value, str):
            raise TypeError("'str' type is expected")
        self._table_["license"] = value


    @property
    def maintainers(self):
        """maintainers iterator (maintainer)"""
        for entry in self._table_["maintainers"]:
            yield entry

    @maintainers.setter
    def maintainers(self, iterable):
        if isinstance(iterable, (bytes, str)) \
        or not isinstance(iterable, collections.Iterable):
            raise TypeError("iterable of strings is expected")
        result = set()
        for item in iterable:
            if not isinstance(item, str):
                raise TypeError("iterable of strings is expected")
            result.update([item])
        self._table_["maintainers"] = result


    def shell_variable(self, macro_prefix="gl"):
        """Get the name of the shell variable set to true once m4 macros have been executed."""
        module = self.name
        if len(module) != len(module.encode()):
            module = (module + "\n").encode("UTF-8")
            module = hashlib.md5(module).hexdigest()
        return "%s_gnulib_enabled_%s" % (macro_prefix, module)


    def shell_function(self, macro_prefix="gl"):
        """Get the name of the shell function containing the m4 macros."""
        module = self.name
        if len(module) != len(module.encode()):
            module = (module + "\n").encode("UTF-8")
            module = hashlib.md5(module).hexdigest()
        return "func_%s_gnulib_m4code_%s" % (macro_prefix, module)


    def conditional_name(self, macro_prefix="gl"):
        """Get the automake conditional name."""
        module = self.name
        if len(module) != len(module.encode()):
            module = (module + "\n").encode("UTF-8")
            module = hashlib.md5(module).hexdigest()
        return "%s_GNULIB_ENABLED_%s" % (macro_prefix, module)


    def __hash__(self):
        return hash(str(self))


    def __repr__(self):
        return self._name_


    def __str__(self):
        result = ""
        for key, (_, typeid, field) in sorted(Module._TABLE_.items(), key=lambda k: k[1][0]):
            field += ":\n"
            if typeid is list:
                value = "\n".join(self._table_[key])
            else:
                value = self._table_[key]
            if value:
                result += field
                result += value
                result += "\n\n" if value else "\n"
        return result.strip() + "\n"


    def __lt__(self, value):
        return self.name < value.name

    def __le__(self, value):
        return self.__lt__(value) or self.__eq__(value)

    def __eq__(self, value):
        return self.name == value.name

    def __ne__(self, value):
        return not self.__eq__(value)

    def __ge__(self, value):
        return value.__le__(self)

    def __gt__(self, value):
        return value.__lt__(self)



class FileModule(Module):
    """gnulib module text file"""
    _FIELDS_ = [field for (_, _, field) in Module._TABLE_.values()]
    _PATTERN_ = re.compile("(%s):" % "|".join(_FIELDS_))

    def __init__(self, path, mode="r", name=None, **kwargs):
        if name is None:
            name = os.path.basename(path)
        if mode not in ("r", "w", "rw"):
            raise ValueError("illegal mode: %r" % mode)
        if mode == "r":
            super().__init__(name)
            with codecs.open(path, "rb", "UTF-8") as stream:
                data = ""
                for line in stream:
                    line = line.strip()
                    if line.startswith("#") \
                    or (line.startswith("/*") and line.endswith("*/")):
                        continue
                    data += (line + "\n")
            match = FileModule._PATTERN_.split(data)[1:]
            for (group, value) in zip(match[::2], match[1::2]):
                key = None
                typeid = type(None)
                for key, (_, typeid, field) in Module._TABLE_.items():
                    if group == field:
                        break
                if typeid is list:
                    self._table_[key] = [_ for _ in "".join(value).split("\n") if _.strip()]
                else:
                    self._table_[key] = value.strip()
            self._stream_ = None
        elif mode == "w":
            super().__init__(name)
            self._stream_ = codecs.open(path, "w+", "UTF-8")
        elif mode == "rw":
            self.__init__(path, "r")
            self._stream_ = codecs.open(path, "w+", "UTF-8")
        else:
            raise ValueError("invalid mode: %r" % mode)
        for key, value in kwargs.items():
            self._table_[key] = value


    def close(self):
        """Close the underlying stream and write data into the file."""
        if self._stream_:
            self._stream_.truncate(0)
            self._stream_.write(str(self))
            self._stream_.close()


    def __enter__(self):
        return self


    def __exit__(self, exctype, excval, exctrace):
        self.close()