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
|
import collections
import os.path
import re
import six
import six.moves
from . import utils
class Changelog(list):
_rules = r"""
^
(?P<source>
\w[-+0-9a-z.]+
)
\
\(
(?P<version>
[^\(\)\ \t]+
)
\)
\s+
(?P<distribution>
[-+0-9a-zA-Z.]+
)
\;
"""
_re = re.compile(_rules, re.X)
class Entry(object):
__slot__ = 'distribution', 'source', 'version'
def __init__(self, distribution, source, version):
self.distribution, self.source, self.version = distribution, source, version
def __init__(self, dir='', version=None):
if version is None:
version = Version
if six.PY3:
f = open(os.path.join(dir, "debian/changelog"), encoding="UTF-8")
else:
f = open(os.path.join(dir, "debian/changelog"))
while True:
line = f.readline()
if not line:
break
match = self._re.match(line)
if not match:
continue
try:
v = version(match.group('version'))
except Exception:
if not len(self):
raise
v = Version(match.group('version'))
self.append(self.Entry(match.group('distribution'), match.group('source'), v))
class Version(object):
_version_rules = r"""
^
(?:
(?P<epoch>
\d+
)
:
)?
(?P<upstream>
.+?
)
(?:
-
(?P<revision>[^-]+)
)?
$
"""
_version_re = re.compile(_version_rules, re.X)
def __init__(self, version):
match = self._version_re.match(version)
if match is None:
raise RuntimeError(u"Invalid debian version")
self.epoch = None
if match.group("epoch") is not None:
self.epoch = int(match.group("epoch"))
self.upstream = match.group("upstream")
self.revision = match.group("revision")
def __str__(self):
return self.complete
__unicode__ = __str__
@property
def complete(self):
if self.epoch is not None:
return u"%d:%s" % (self.epoch, self.complete_noepoch)
return self.complete_noepoch
@property
def complete_noepoch(self):
if self.revision is not None:
return u"%s-%s" % (self.upstream, self.revision)
return self.upstream
@property
def debian(self):
from warnings import warn
warn(u"debian argument was replaced by revision", DeprecationWarning, stacklevel=2)
return self.revision
class VersionLinux(Version):
_version_linux_rules = r"""
^
(?P<version>
\d+\.\d+
)
(?P<update>
(?:\.\d+)?
(?:-[a-z]+\d+)?
)
(?:
~
(?P<modifier>
.+?
)
)?
(?:
\.dfsg\.
(?P<dfsg>
\d+
)
)?
-
\d+
(\.\d+)?
(?:
(?P<revision_experimental>
~exp\d+
)
|
(?P<revision_backports>
~bpo\d\d\+\d+
)
|
(?P<revision_other>
[^-]+
)
)?
$
"""
_version_linux_re = re.compile(_version_linux_rules, re.X)
def __init__(self, version):
super(VersionLinux, self).__init__(version)
match = self._version_linux_re.match(version)
if match is None:
raise RuntimeError(u"Invalid debian linux version")
d = match.groupdict()
self.linux_modifier = d['modifier']
self.linux_version = d['version']
if d['modifier'] is not None:
assert not d['update']
self.linux_upstream = u'-'.join((d['version'], d['modifier']))
else:
self.linux_upstream = d['version']
self.linux_upstream_full = self.linux_upstream + d['update']
self.linux_dfsg = d['dfsg']
self.linux_revision_experimental = match.group('revision_experimental') and True
self.linux_revision_backports = match.group('revision_backports') and True
self.linux_revision_other = match.group('revision_other') and True
class PackageArchitecture(collections.MutableSet):
__slots__ = '_data'
def __init__(self, value=None):
self._data = set()
if value:
self.extend(value)
def __contains__(self, value):
return self._data.__contains__(value)
def __iter__(self):
return self._data.__iter__()
def __len__(self):
return self._data.__len__()
def __str__(self):
return u' '.join(sorted(self))
__unicode__ = __str__
def add(self, value):
self._data.add(value)
def discard(self, value):
self._data.discard(value)
def extend(self, value):
if isinstance(value, six.string_types):
for i in re.split('\s', value.strip()):
self.add(i)
else:
raise RuntimeError
class PackageDescription(object):
__slots__ = "short", "long"
def __init__(self, value=None):
self.short = []
self.long = []
if value is not None:
short, long = value.split(u"\n", 1)
self.append(long)
self.append_short(short)
def __str__(self):
wrap = utils.TextWrapper(width=74, fix_sentence_endings=True).wrap
short = u', '.join(self.short)
long_pars = []
for i in self.long:
long_pars.append(wrap(i))
long = u'\n .\n '.join([u'\n '.join(i) for i in long_pars])
return short + u'\n ' + long
__unicode__ = __str__
def append(self, str):
str = str.strip()
if str:
self.long.extend(str.split(u"\n.\n"))
def append_short(self, str):
for i in [i.strip() for i in str.split(u",")]:
if i:
self.short.append(i)
def extend(self, desc):
if isinstance(desc, PackageDescription):
self.short.extend(desc.short)
self.long.extend(desc.long)
else:
raise TypeError
class PackageRelation(list):
def __init__(self, value=None, override_arches=None):
if value:
self.extend(value, override_arches)
def __str__(self):
return u', '.join(six.text_type(i) for i in self)
__unicode__ = __str__
def _search_value(self, value):
for i in self:
if i._search_value(value):
return i
return None
def append(self, value, override_arches=None):
if isinstance(value, six.string_types):
value = PackageRelationGroup(value, override_arches)
elif not isinstance(value, PackageRelationGroup):
raise ValueError(u"got %s" % type(value))
j = self._search_value(value)
if j:
j._update_arches(value)
else:
super(PackageRelation, self).append(value)
def extend(self, value, override_arches=None):
if isinstance(value, six.string_types):
value = (j.strip() for j in re.split(u',', value.strip()))
for i in value:
self.append(i, override_arches)
class PackageRelationGroup(list):
def __init__(self, value=None, override_arches=None):
if value:
self.extend(value, override_arches)
def __str__(self):
return u' | '.join(six.text_type(i) for i in self)
__unicode__ = __str__
def _search_value(self, value):
for i, j in six.moves.zip(self, value):
if i.name != j.name or i.version != j.version:
return None
return self
def _update_arches(self, value):
for i, j in six.moves.zip(self, value):
if i.arches:
for arch in j.arches:
if arch not in i.arches:
i.arches.append(arch)
def append(self, value, override_arches=None):
if isinstance(value, six.string_types):
value = PackageRelationEntry(value, override_arches)
elif not isinstance(value, PackageRelationEntry):
raise ValueError
super(PackageRelationGroup, self).append(value)
def extend(self, value, override_arches=None):
if isinstance(value, six.string_types):
value = (j.strip() for j in re.split('\|', value.strip()))
for i in value:
self.append(i, override_arches)
class PackageRelationEntry(object):
__slots__ = "name", "operator", "version", "arches"
_re = re.compile(r'^(\S+)(?: \((<<|<=|=|!=|>=|>>)\s*([^)]+)\))?(?: \[([^]]+)\])?$')
class _operator(object):
OP_LT = 1
OP_LE = 2
OP_EQ = 3
OP_NE = 4
OP_GE = 5
OP_GT = 6
operators = {
u'<<': OP_LT,
u'<=': OP_LE,
u'=': OP_EQ,
u'!=': OP_NE,
u'>=': OP_GE,
u'>>': OP_GT,
}
operators_neg = {
OP_LT: OP_GE,
OP_LE: OP_GT,
OP_EQ: OP_NE,
OP_NE: OP_EQ,
OP_GE: OP_LT,
OP_GT: OP_LE,
}
operators_text = dict((b, a) for a, b in operators.items())
__slots__ = '_op',
def __init__(self, value):
self._op = self.operators[value]
def __neg__(self):
return self.__class__(self.operators_text[self.operators_neg[self._op]])
def __str__(self):
return self.operators_text[self._op]
__unicode__ = __str__
def __init__(self, value=None, override_arches=None):
if not isinstance(value, six.string_types):
raise ValueError
self.parse(value)
if override_arches:
self.arches = list(override_arches)
def __str__(self):
ret = [self.name]
if self.operator is not None and self.version is not None:
ret.extend((u' (', six.text_type(self.operator), u' ', self.version, u')'))
if self.arches:
ret.extend((u' [', u' '.join(self.arches), u']'))
return u''.join(ret)
__unicode__ = __str__
def parse(self, value):
match = self._re.match(value)
if match is None:
raise RuntimeError(u"Can't parse dependency %s" % value)
match = match.groups()
self.name = match[0]
if match[1] is not None:
self.operator = self._operator(match[1])
else:
self.operator = None
self.version = match[2]
if match[3] is not None:
self.arches = re.split('\s+', match[3])
else:
self.arches = []
class Package(dict):
_fields = collections.OrderedDict((
('Package', six.text_type),
('Source', six.text_type),
('Architecture', PackageArchitecture),
('Section', six.text_type),
('Priority', six.text_type),
('Maintainer', six.text_type),
('Uploaders', six.text_type),
('Standards-Version', six.text_type),
('Build-Depends', PackageRelation),
('Build-Depends-Indep', PackageRelation),
('Provides', PackageRelation),
('Pre-Depends', PackageRelation),
('Depends', PackageRelation),
('Recommends', PackageRelation),
('Suggests', PackageRelation),
('Replaces', PackageRelation),
('Breaks', PackageRelation),
('Conflicts', PackageRelation),
('Description', PackageDescription),
))
def __setitem__(self, key, value):
try:
cls = self._fields[key]
if not isinstance(value, cls):
value = cls(value)
except KeyError:
pass
super(Package, self).__setitem__(key, value)
def iterkeys(self):
keys = set(self.keys())
for i in self._fields.keys():
if i in self:
keys.remove(i)
yield i
for i in keys:
yield i
def iteritems(self):
for i in self.iterkeys():
yield (i, self[i])
def itervalues(self):
for i in self.iterkeys():
yield self[i]
|