/usr/lib/python2.7/dist-packages/piupartslib/dependencyparser.py is in piuparts-common 0.77.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- coding: utf-8 -*-
# Copyright 2005 Lars Wirzenius (liw@iki.fi)
# Copyright © 2012 Andreas Beckmann (anbe@debian.org)
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""Parser for Debian package relationship strings
This module contains the class DependencyParser, which parses Debian
package relationship strings (e.g., the Depends header). The class
raises the DependencySyntaxError exception on syntactic errors.
The result uses SimpleDependency objects.
Lars Wirzenius <liw@iki.fi>
"""
import re
class DependencySyntaxError(Exception):
"""Syntax error in package dependency declaration"""
def __init__(self, msg, cursor):
self._msg = "Error: %s: %s (text at error: '%s', full text being parsed: '%s')" % \
(cursor.get_position(), msg, cursor.get_text(10),
cursor.get_full_text())
def __str__(self):
return self._msg
def __repr__(self):
return self._msg
class _Cursor:
"""Store an input string and a movable location in it"""
def __init__(self, input):
self._input = input
self._len = len(self._input)
self._pos = 0
def skip_whitespace(self):
while self._pos < self._len and self._input[self._pos].isspace():
self.next()
def at_end(self):
"""Are we at the end of the input?"""
self.skip_whitespace()
return self._pos >= self._len
def next(self):
"""Move to the next character"""
if self._pos < self._len:
self._pos += 1
def get_char(self):
"""Return current character, None if at end"""
if self._pos >= self._len:
return None
else:
return self._input[self._pos]
def get_full_text(self):
return self._input
def get_text(self, length):
"""Return up to length characters from the current position"""
if self._pos >= self._len:
return ""
else:
return self._input[self._pos:self._pos + length]
def match(self, regexp):
"""Match a regular expression against the current position
The cursor is advanced by the length of the match, if any.
"""
m = regexp.match(self._input[self._pos:])
if m:
self._pos += len(m.group())
return m
def match_literal(self, literal):
"""Match a literal string against the current position.
Return True and move position if there is a match, else return
False.
"""
if self.get_text(len(literal)) == literal:
self._pos += len(literal)
return True
else:
return False
def get_position(self):
"""Return current position, as string"""
return "pos %d" % self._pos
class SimpleDependency:
"""Express simple dependency towards another package"""
def __init__(self, name, operator, version, arch):
self.name = name
self.operator = operator
self.version = version
self.arch = arch
def __repr__(self):
return "<DEP: %s, %s, %s, %s>" % (self.name, self.operator,
self.version, self.arch)
class DependencyParser:
"""Parse Debian package relationship strings
Debian packages have a rich language for expressing their
relationships. See the Debian Policy Manual, chapter 7 ("Declaring
relationships between packages"). This Python module implements a
parser for strings expressing such relationships.
Syntax of dependency fields (Pre-Depends, Depends, Recommends,
Suggests, Conflicts, Provides, Replaces, Enhances, Build-Depends,
Build-Depends-Indep, Build-Conflicts, Build-Conflicts-Indep), in a
BNF-like form:
depends-field ::= EMPTY | dependency ("," dependency)*
dependency ::= possible-dependency ("|" possible-dependency)*
possible-dependency ::= package-name version-dependency?
arch-restriction?
version-dependency ::= "(" relative-operator version-number ")"
relative-operator ::= "<<" | "<=" | "=" | ">=" | ">>" | "<" | ">"
version-number ::= epoch? upstream-version debian-revision?
arch-restriction ::= "[" arch-name arch-name* "]" |
"[" "!" arch-name ("!" arch-name)* "]"
package-name ::= alphanumeric name-char name-char* ":any"?
epoch ::= integer ":"
upstream-version ::= alphanumeric version-char*
-- policy says "should start with digit", but not all packages do
debian-revision ::= "-" debian-version-char debian-version-char*
arch-name ::= alphanumeric alphanumeric*
EMPTY ::= ""
integer ::= digit digit*
alphanumeric ::=
"a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" |
"k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" |
"u" | "v" | "w" | "x" | "y" | "z" | digit
digit ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
name-char ::= alphanumeric | "+" | "-" | "." | "_"
version-char ::= alphanumeric | "." | "+" | "-" | ":" | "~"
debian-version-char ::= alphanumeric | "." | "+"
White space can occur between any tokens except inside package-name,
version-number, or arch-name. Some of the headers restrict the syntax
somewhat, e.g., Provides does not allow version-dependency, but this is
not included in the syntax for simplicity.
Note: Added "_" to name-char, because some packages (type-handling
in particular) use Provides: headers with bogus package names.
Note: Added upper case letters to name pattern, since it some of the
Mozilla localization packages use or used them.
"""
def __init__(self, input_string):
self._cursor = _Cursor(input_string)
self._list = self._parse_dependencies()
def get_dependencies(self):
"""Return parsed dependencies
The result is a list of lists of SimpleDependency objects.
Let's try that again.
The result is a list of dependencies, corresponding to
the comma-separated items in the dependency list. Each dependency
is also a list, or SimpleDependency objects, representing
alternative ways to fulfill the dependency; in other words,
items separated by the vertical bar (|).
For example, "foo, bar | foobar" would result in the following
list: [[foo], [bar, foobar]].
"""
return self._list
def _parse_dependencies(self):
vlist = []
dep = self._parse_dependency()
while dep:
vlist.append(dep)
self._cursor.skip_whitespace()
if self._cursor.at_end():
break
if not self._cursor.match_literal(","):
raise DependencySyntaxError("Expected comma", self._cursor)
dep = self._parse_dependency()
return vlist
def _parse_dependency(self):
vlist = []
dep = self._parse_possible_dependency()
while dep:
vlist.append(dep)
self._cursor.skip_whitespace()
if not self._cursor.match_literal("|"):
break
dep = self._parse_possible_dependency()
return vlist
def _parse_possible_dependency(self):
name = self._parse_package_name()
if not name:
return None
(op, version) = self._parse_version_dependency()
arch = self._parse_arch_restriction()
return SimpleDependency(name, op, version, arch)
_name_pat = re.compile(r"[a-zA-Z0-9][a-zA-Z0-9+._-]+")
# The MultiArch spec supports an ":any" modifier. Loosen the
# accepted arch's, to avoid crashing.
_any_suffix_pat = re.compile(r":[a-zA-Z0-9-]+")
def _parse_package_name(self):
self._cursor.skip_whitespace()
if self._cursor.at_end():
return None
m = self._cursor.match(self._name_pat)
if not m:
raise DependencySyntaxError("Expected a package name",
self._cursor)
if self._cursor.match(self._any_suffix_pat):
pass
return m.group()
_op_pat = re.compile(r"(<<|<=|=|>=|>>|<(?![<=])|>(?![>=]))")
_version_pat = re.compile(r"(?P<epoch>\d+:)?" +
r"(?P<upstream>[a-zA-Z0-9+][a-zA-Z0-9.+:~-]*)" +
r"(?P<debian>-[a-zA-Z0-9.+]+)?")
def _parse_version_dependency(self):
self._cursor.skip_whitespace()
if self._cursor.get_char() == "(":
self._cursor.next()
self._cursor.skip_whitespace()
opm = self._cursor.match(self._op_pat)
if not opm:
raise DependencySyntaxError("Expected a version relation " +
"operator", self._cursor)
operator = opm.group()
if operator == "<":
operator = "<="
elif operator == ">":
operator = ">="
self._cursor.skip_whitespace()
verm = self._cursor.match(self._version_pat)
if not verm:
raise DependencySyntaxError("Expected a version number",
self._cursor)
self._cursor.skip_whitespace()
if self._cursor.get_char() != ")":
raise DependencySyntaxError("Expected ')'", self._cursor)
self._cursor.next()
return opm.group(), verm.group()
else:
return None, None
_arch_pat = re.compile(r"!?[a-zA-Z0-9-]+")
def _parse_arch_restriction(self):
self._cursor.skip_whitespace()
if self._cursor.get_char() == "[":
self._cursor.next()
vlist = []
while True:
self._cursor.skip_whitespace()
if self._cursor.get_char() == "]":
self._cursor.next()
break
m = self._cursor.match(self._arch_pat)
if not m:
raise DependencySyntaxError("Expected architecture name",
self._cursor)
vlist.append(m.group())
return vlist
else:
return None
# vi:set et ts=4 sw=4 :
|