/usr/lib/python3/dist-packages/AptUrl/Parser.py is in apturl-common 0.5.2ubuntu11.
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 | # Copyright (c) 2007-2008 Canonical
#
# AUTHOR:
# Michael Vogt <mvo@ubuntu.com>
# With contributions by Siegfried-A. Gevatter <rainct@ubuntu.com>
#
# This file is part of AptUrl
#
# AptUrl 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.
#
# AptUrl 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 AptUrl; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os
import string
from string import Template
from .Helpers import get_dist
from .Helpers import _
class InvalidUrlException(Exception):
def __init__(self, url, msg=""):
self.url = url
self.message = msg
def __str__(self):
return self.message
MAX_URL_LEN=255
# substituion mapping
apturl_substitution_mapping = {
"distro" : get_dist(),
"kernel" : os.uname()[2]
}
# whitelist for the uri
whitelist = []
whitelist.extend(string.ascii_letters)
whitelist.extend(string.digits)
whitelist.extend(['_',':','?','/','+','.','~','=','<','>','-',',','$','&'])
class AptUrl(object):
" a class that contains the parsed data from an apt url "
def __init__(self):
self.package = None
self.schema = None
self.minver = None
self.refresh = None
# for added repos
self.keyfile = None
self.repo_url = None
self.dist = '/'
# for known sections
self.section = []
# for known channels
self.channel = None
def is_format_package_name(string):
" return True if string would be an acceptable name for a Debian package "
return (string.replace("+", "").replace("-", "").replace(".", "").replace(":", "").isalnum()
and string.islower() and string[0].isalnum() and len(string) > 1)
def do_apt_url_substitution(apt_url, mapping):
" substitute known templates against the field package and channel "
for field in ["package","channel"]:
if getattr(apt_url, field):
s=Template(getattr(apt_url, field))
setattr(apt_url, field, s.substitute(mapping))
def match_against_whitelist(raw_url):
" test if the url matches the internal whitelist "
for char in raw_url:
if not char in whitelist:
raise InvalidUrlException(
raw_url, _("Non whitelist char in the uri"))
return True
def set_value(apt_url, s):
" set a key,value pair from string s to AptUrl object "
(key, value) = s.split("=")
try:
if ' ' in value:
raise InvalidUrlException(apt_url, _("Whitespace in key=value"))
if type(getattr(apt_url, key)) == type([]):
getattr(apt_url, key).append(value)
else:
setattr(apt_url, key, value)
except Exception as e:
raise InvalidUrlException(apt_url, _("Exception '%s'") % e)
def parse(full_url, mapping=apturl_substitution_mapping):
" parse an apt url and return a list of AptUrl objects "
# apt:pkg1?k11=v11?k12=v12,pkg2?k21=v21?k22=v22,...
res = []
if len(full_url) > MAX_URL_LEN:
url = "%s ..." % full_url[0:(MAX_URL_LEN // 10)]
raise InvalidUrlException(url, _("Url string '%s' too long") % url)
# check against whitelist
match_against_whitelist(full_url)
for url in full_url.split(";"):
if not ":" in url:
raise InvalidUrlException(url, _("No ':' in the uri"))
# now parse it
(schema, packages) = url.split(":", 1)
packages = packages.split(",")
for package in packages:
apt_url = AptUrl()
apt_url.schema = schema
# check for schemas of the form: apt+http://
if schema.startswith("apt+"):
apt_url.repo_url = schema[len("apt+"):] + ":" + package.split("?",1)[0]
else:
if "?" in package:
apt_url.package = package.split("?")[0].lstrip("/")
else:
apt_url.package = package.lstrip("/")
# now parse the ?... bits
if "?" in package:
key_value_pairs = package.split("?")[1:]
for s in key_value_pairs:
if "&" in s:
and_key_value_pairs = s.split("&")
for s in and_key_value_pairs:
set_value(apt_url, s)
else:
set_value(apt_url, s)
# do substitution (if needed)
do_apt_url_substitution(apt_url, mapping)
# check if the package name is valid
if not is_format_package_name(apt_url.package):
raise InvalidUrlException(url, "Invalid package name '%s'" % apt_url.package)
res.append(apt_url)
return res
|