This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparched.py
474 lines (376 loc) · 14.7 KB
/
parched.py
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
# Copyright (c) 2009 Sebastian Nowicki
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
"""
.. moduleauthor:: Sebastian Nowicki <[email protected]>
This module defines two classes which provide information about Pacman
packages and PKGBUILDs, :class:`PacmanPackage` and :class:`PKGBUILD`. These
classes iniherit from the :class:`Package` class, which provides the basic
metadata about package.
"""
import tarfile
from datetime import datetime
import re
import shlex
__all__ = ['Package', 'PacmanPackage', 'PKGBUILD']
class Package(object):
"""An abstract package class
This class provides no functionality whatsoever. Use either
:class:`PacmanPackage`, :class:`PKGBUILD`, or another subclass instead.
The class provides attributes common to all packages. All attributes are
supposed to be read-only.
.. attribute:: name
The name of the package.
.. attribute:: version
The version of the package, as a string.
.. attribute:: release
Release version of the package, i.e., version of the package itself,
as an integer.
.. attribute:: description
Description of the package.
.. attribute:: url
Package's website.
.. attribute:: licenses
A list of licenses.
.. attribute:: groups
A list of groups the package belongs to.
.. attribute:: provides
A list of "virtual provisions" that the package provides.
.. attribute:: depends
A list of the names of packages the package depends on.
.. attribute:: optdepends
A list of optional dependencies which are not required during runtime.
.. attribute:: conflicts
A list of packages the package conflicts with.
.. attribute:: replaces
A list of packages this package replaces.
.. attribute:: architectures
A list of architectures the package can be installed on.
.. attribute:: backup
A list of files which should be backed up on upgrades
.. attribute:: options
Options used when building the package, represented as a list. This
list is equivalent to that of `options` in a PKGBUILD. See
:manpage:`PKGBUILD(5)` for more information.
For more information about these attributes see :manpage:`PKGBUILD(5)`.
"""
def __init__(self, pkgfile):
super(Package, self).__init__()
self.name = ""
self.version = ""
self.release = ""
self.description = ""
self.url = ""
self.licenses = []
self.groups = []
self.provides = []
self.depends = []
self.optdepends = []
self.conflicts = []
self.replaces = []
self.architectures = []
self.options = []
self.backup = []
class PacmanPackage(Package):
"""
The :class:`PacmanPackage` class provides information about a package, by
parsing a tarball in `pacman <http://www.archlinux.org/pacman>`_ package
format. This tarball must have a `.PKGINFO` member. This member provides
all metadata about the package.
To instantiate a :class:`PacmanPackage` object, pass the package's file
path in the constructor::
>>> import parched
>>> package = PacmanPackage("foo-1.0-1-any.tar.gz")
If *tarfileobj* is specified, it is used as an alternative to a
:class:`TarFile` like object opened for *name*. It is supposed to be
at position 0. *tarfileobj* may be any object that has an
:meth:`extractfile` method, which returns a file like object::
>>> import tarfile
>>> f = tarfile.open("foo-1.0-1-any.tar.gz", "r|gz")
>>> package = PacmanPackage(tarfileobj=f)
>>> f.close()
.. note::
*tarfileobj* is not closed.
The packages metadata can then be accessed directly::
>>> print package
"foo 1.0-1"
>>> print package.description
"Example package"
In addition to the attributes provided by :class:`Package`,
:class:`PacmanPackage` provides the following attributes:
.. attribute:: builddate
A :class:`datetime` object indicating time at which the package was
built.
.. attribute:: packager
The person who made the package, represented as a string in the format::
First_name Last_name <[email protected]>
.. attribute:: is_force
Indicates whether an upgrade is forced
.. attribute:: files
An array of files contained in the package
"""
def __init__(self, name=None, tarfileobj=None):
super(PacmanPackage, self).__init__(tarfileobj)
self.builddate = ""
self.packager = ""
self.is_forced = ""
self.size = 0
self.files = []
self._symbol_map = {
'pkgname': 'name',
'pkgver': 'version',
'pkgdesc': 'description',
'license': 'licenses',
'arch': 'architectures',
'force': 'is_forced',
'conflict': 'conflicts',
'group': 'groups',
'optdepend': 'optdepends',
'makepkgopt': 'options',
'depend': 'depends',
}
self._arrays = (
'arch',
'license',
'replaces',
'group',
'depend',
'optdepend',
'conflict',
'provides',
'backup',
'makepkgopt',
)
if not name and not tarfileobj:
raise ValueError("nothing to open")
should_close = False
if not tarfileobj:
tarfileobj = tarfile.open(str(name), "r|*")
should_close = True
pkginfo = tarfileobj.extractfile(".PKGINFO")
self.files = tarfileobj.getnames()
self._parse(pkginfo)
if should_close:
tarfileobj.close()
def __str__(self):
return '%s %s-%s' % (self.name, self.version, self.release)
def _parse(self, pkginfo):
"""Parse the .PKGINFO file"""
if hasattr(pkginfo, "seek"):
pkginfo.seek(0)
for line in pkginfo:
if line[0] == '#' or line.strip() == '':
continue
var, _, value = line.strip().rpartition(' = ')
real_name = var
if var in self._symbol_map:
real_name = self._symbol_map[var]
if var in self._arrays:
array = getattr(self, real_name)
array.append(value)
else:
setattr(self, real_name, value)
if self.size:
self.size = int(self.size)
if not self.is_forced == False:
self.is_forced = self.is_forced == "True"
if self.builddate:
self.builddate = datetime.utcfromtimestamp(int(self.builddate))
if self.version:
self.version, _, self.release = self.version.rpartition('-')
self.release = int(self.release)
if self.packager == 'Uknown Packager':
self.packager = None
class PKGBUILD(Package):
"""A :manpage:`PKGBUILD(5)` parser
The :class:`PKGBUILD` class provides information about a
package by parsing a :manpage:`PKGBUILD(5)` file.
To instantiate a :class:`PacmanPackage` object, pass the package's file
path in the constructor::
>>> import parched
>>> package = PKGBUILD("PKGBUILD")
If *fileobj* is specified, it is used as an alternative to a
:class:`file` like object opened for *name*. It is supposed to be
at position 0. For example::
>>> f = open("PKGBUILD", "r")
>>> package = PKGBUILD(fileobj=f)
>>> f.close()
.. note::
*fileobj* is not closed.
The packages metadata can then be accessed directly::
>>> print package
"foo 1.0-1"
>>> print package.description
"Example package"
In addition to the attributes provided by :class:`Package`,
:class:`PKGBUILD` provides the following attributes:
.. attribute:: install
The filename of the install scriptlet.
.. attribute:: checksums
A dictionary containing the checksums of files in the
:attr:`sources` list. The dictionary's keys are the algorithms
used, and can be any of 'md5', 'sha1', 'sha256', 'sha384', and
'sha512'. The value is a list of checksums. The elements
correspond to files in the :attr:`sources` list, in relation to
their position.
.. attribute:: sources
A list containing the URIs of filenames. Local file paths can be
relative and do not require a protocol prefix.
.. attribute:: makedepends
A list of compile-time dependencies.
.. attribute:: noextract
A list of files not to be extracted. These files correspond to
the basenames of the URIs in :attr:`sources`
"""
_symbol_regex = re.compile(r"\$(?P<name>{[\w\d_]+}|[\w\d]+)")
def __init__(self, name=None, fileobj=None):
super(PKGBUILD, self).__init__(fileobj)
self.install = ""
self.checksums = {
'md5': [],
'sha1': [],
'sha256': [],
'sha384': [],
'sha512': [],
}
self.noextract = []
self.sources = []
self.makedepends = []
# Symbol lookup table
self._var_map = {
'pkgname': 'name',
'pkgver': 'version',
'pkgdesc': 'description',
'pkgrel': 'release',
'source': 'sources',
'arch': 'architectures',
'license': 'licenses',
}
self._checksum_fields = (
'md5sums',
'sha1sums',
'sha256sums',
'sha384sums',
'sha512sums',
)
# Symbol table
self._symbols = {}
if not name and not fileobj:
raise ValueError("nothing to open")
should_close = False
if not fileobj:
fileobj = open(name, "r")
should_close = True
self._parse(fileobj)
if should_close:
fileobj.close()
def _handle_assign(self, token):
var, equals, value = token.strip().partition('=')
# Is it an array?
if value[0] == '(' and value[-1] == ')':
self._symbols[var] = self._clean_array(value)
else:
self._symbols[var] = self._clean(value)
def _parse(self, fileobj):
"""Parse PKGBUILD"""
if hasattr(fileobj, "seek"):
fileobj.seek(0)
parser = shlex.shlex(fileobj, posix=True)
parser.whitespace_split = True
in_function = False
while 1:
token = parser.get_token()
if token is None or token == '':
break
# Skip escaped newlines and functions
if token == '\n' or in_function:
continue
# Special case:
# Array elements are dispersed among tokens, we have to join
# them first
if token.find("=(") >= 0 and not token.rfind(")") >= 0:
in_array = True
elements = []
while in_array:
_token = parser.get_token()
if _token == '\n':
continue
if _token[-1] == ')':
_token = '"%s")' % _token.strip(')')
token = token.replace('=(', '=("', 1) + '"'
token = " ".join((token, " ".join(elements), _token))
in_array = False
else:
elements.append('"%s"' % _token.strip())
# Assignment
if re.match(r"^[\w\d_]+=", token):
self._handle_assign(token)
# Function definitions
elif token == '{':
in_function = True
elif token == '}' and in_function:
in_function = False
self._substitute()
self._assign_local()
if self.release:
self.release = float(self.release)
def _clean(self, value):
"""Pythonize a bash string.
shlex strips enclosing double quotes from right-hand side of
assignment, but not enclosing single quotes.
"""
if value.startswith("'") and value.endswith("'"):
return value.strip("'")
else:
return value
def _clean_array(self, value):
"""Pythonize a bash array"""
return shlex.split(value.strip('()'))
def _replace_symbol(self, matchobj):
"""Replace a regex-matched variable with its value"""
symbol = matchobj.group('name').strip("{}")
# If the symbol isn't found fallback to an empty string, like bash
try:
value = self._symbols[symbol]
except KeyError:
value = ''
# BUG: Might result in an infinite loop, oops!
return self._symbol_regex.sub(self._replace_symbol, value)
def _substitute(self):
"""Substitute all bash variables within values with their values"""
for symbol in self._symbols:
value = self._symbols[symbol]
# FIXME: This is icky
if isinstance(value, str):
result = self._symbol_regex.sub(self._replace_symbol, value)
else:
result = [self._symbol_regex.sub(self._replace_symbol, x)
for x in value]
self._symbols[symbol] = result
def _assign_local(self):
"""Assign values from _symbols to PKGBUILD variables"""
for var in self._symbols:
value = self._symbols[var]
if var in self._checksum_fields:
key = var.replace('sums', '')
self.checksums[key] = value
else:
if var in self._var_map:
var = self._var_map[var]
setattr(self, var, value)