/usr/lib/python3/dist-packages/dep11/validate.py is in python3-dep11 0.4.0-1build1.
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 | #!/usr/bin/env python3
#
# Copyright (C) 2014-2015 Matthias Klumpp <mak@debian.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3.0 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program.
import yaml
import gzip
import xml.etree.ElementTree as ET
from voluptuous import Schema, Required, All, Any, Length, Range, Match, Url
__all__ = []
schema_header = Schema({
Required('File'): All(str, 'DEP-11', msg="Must be \"DEP-11\""),
Required('Origin'): All(str, Length(min=1)),
Required('Version'): All(str, Match(r'(\d+\.?)+$'), msg="Must be a valid version number"),
Required('MediaBaseUrl'): All(str, Url()),
'Time': All(str, str),
'Priority': All(str, int),
})
schema_provides_dbus = Schema({
Required('type'): All(str, Length(min=1)),
Required('service'): All(str, Length(min=1)),
})
schema_provides_firmware = Schema({
Required('type'): All(str, Length(min=1)),
Any('guid', 'fname'): All(str, Length(min=1))
})
schema_provides = Schema({
Any('mimetypes',
'binaries',
'libraries',
'python3',
'python2',
'modaliases',
'fonts'): All(list, [str], Length(min=1)),
'dbus': All(list, Length(min=1), [schema_provides_dbus]),
'firmware': All(list, Length(min=1), [schema_provides_firmware]),
})
schema_keywords = Schema({
Required('C'): All(list, [str], Length(min=1), msg="Must have an unlocalized 'C' key"),
dict: All(list, [str], Length(min=1)),
}, extra = True)
schema_translated = Schema({
Required('C'): All(str, Length(min=1), msg="Must have an unlocalized 'C' key"),
dict: All(str, Length(min=1)),
}, extra = True)
schema_image = Schema({
Required('width'): All(int, Range(min=10)),
Required('height'): All(int, Range(min=10)),
Required('url'): All(str, str, Length(min=1)),
})
schema_screenshots = Schema({
Required('default', default=False): All(bool),
Required('source-image'): All(dict, Length(min=1), schema_image),
'thumbnails': All(list, Length(min=1), [schema_image]),
'caption': All(dict, Length(min=1), schema_translated),
})
schema_icon = Schema({
'stock': All(str, Length(min=1)),
'cached': All(str, Match(r'.*[.].*$'), msg='Icon entry is missing filename or extension'),
'local': All(str, Match(r'^[\'"]?(?:/[^/]+)*[\'"]?$'), msg='Icon entry should be an absolute path'),
'remote': All(str, str, Length(min=1)),
})
schema_url = Schema({
Any('homepage',
'bugtracker',
'faq',
'help',
'donation'): All(str, Url()),
})
schema_releases = Schema({
Required('unix-timestamp'): All(int),
Required('version'): All(str, Length(min=1)),
'description': All(dict, Length(min=1), schema_translated),
})
schema_component = Schema({
Required('Type'): All(str, Any('generic', 'desktop-app', 'web-app', 'addon', 'codec', 'inputmethod', 'font')),
Required('ID'): All(str, Length(min=1)),
Required('Name'): All(dict, Length(min=1), schema_translated),
Required('Package'): All(str, Length(min=1)),
'Summary': All(dict, {str: str}, Length(min=1), schema_translated),
'Description': All(dict, {str: str}, Length(min=1), schema_translated),
'Categories': All(list, [str], Length(min=1)),
'CompulsoryForDesktops': All(list, [str], Length(min=1)),
'Url': All(dict, Length(min=1), schema_url),
'Icon': All(dict, Length(min=1), schema_icon),
'Keywords': All(dict, Length(min=1), schema_keywords),
'Provides': All(dict, Length(min=1), schema_provides),
'ProjectGroup': All(str, Length(min=1)),
'ProjectLicense': All(str, Length(min=1)),
'DeveloperName': All(dict, Length(min=1), schema_translated),
'Screenshots': All(list, Length(min=1), [schema_screenshots]),
'Extends': All(list, [str], Length(min=1)),
'Releases': All(list, Length(min=1), [schema_releases]),
# Internal, non-specified fields
'X-Source-Checksum': All(str, Length(min=10)),
})
class DEP11Validator:
issue_list = list()
def __init__(self):
pass
def add_issue(self, msg):
self.issue_list.append(msg)
def _is_quoted(self, s):
return (s.startswith("\"") and s.endswith("\"")) or (s.startswith("\'") and s.endswith("\'"))
def _test_localized_dict(self, doc, ldict, id_string):
ret = True
for lang, value in ldict.items():
if lang == 'x-test':
self.add_issue("[%s][%s]: %s" % (doc['ID'], id_string, "Found cruft locale: x-test"))
if lang == 'xx':
self.add_issue("[%s][%s]: %s" % (doc['ID'], id_string, "Found cruft locale: xx"))
if lang.endswith('.UTF-8'):
self.add_issue("[%s][%s]: %s" % (doc['ID'], id_string, "AppStream locale names should not specify encoding (ends with .UTF-8)"))
if self._is_quoted(value):
self.add_issue("[%s][%s]: %s" % (doc['ID'], id_string, "String is quoted: '%s' @ %s" % (value, lang)))
if " " in lang:
self.add_issue("[%s][%s]: %s" % (doc['ID'], id_string, "Locale name contains space: '%s'" % (lang)))
# this - as opposed to the other issues - is an error
ret = False
return ret
def _test_localized(self, doc, key):
ldict = doc.get(key, None)
if not ldict:
return True
return self._test_localized_dict(doc, ldict, key)
def _test_custom_objects(self, lines):
ret = True
for i in range(0, len(lines)):
if "!!python/" in lines[i]:
self.add_issue("Python object encoded in line %i." % (i))
ret = False
return ret
def _validate_description_tag(self, docid, child, allowed_tags):
ret = True
if not child.tag in allowed_tags:
self.add_issue("[%s]: %s" % (docid, "Invalid description markup found: '%s' @ data['Description']" % (child.tag)))
ret = False
if child.attrib.get('{http://www.w3.org/XML/1998/namespace}lang'):
self.add_issue("[%s]: Invalid, localized tag in long description: '%s' => %s @ data['Description']" % (docid, child.tag, child.text))
ret = False
elif len(child.attrib) > 0:
self.add_issue("[%s]: Markup tag has attributes: '%s' => %s @ data['Description']" % (docid, child.tag, child.attrib))
ret = False
return ret
def _validate_description(self, docid, desc, poshint="Description"):
ret = True
ET.register_namespace("xml", "http://www.w3.org/XML/1998/namespace")
try:
root = ET.fromstring("<root>%s</root>" % (desc))
except Exception as e:
self.add_issue("[%s]: %s" % (docid, "Broken description markup found: %s @ data['%s']" % (str(e), poshint)))
return False
for child in root:
if not self._validate_description_tag(docid, child, ['p', 'ul', 'ol']):
ret = False
if (child.tag == 'ul') or (child.tag == 'ol'):
for child2 in child:
if not self._validate_description_tag(docid, child2, ['li']):
ret = False
return ret
def validate_data(self, data):
ret = True
ids_found = dict()
lines = data.split("\n")
# see if there are any Python-specific objects encoded
ret = self._test_custom_objects(lines)
try:
docs = yaml.load_all(data)
header = next(docs)
except Exception as e:
self.add_issue("Could not parse file: %s" % (str(e)))
return False
try:
schema_header(header)
except Exception as e:
self.add_issue("Invalid DEP-11 header: %s" % (str(e)))
ret = False
for doc in docs:
docid = doc.get('ID')
pkgname = doc.get('Package')
if not pkgname:
pkgname = "?unknown?"
if not doc:
self.add_issue("FATAL: Empty document found.")
ret = False
continue
if not docid:
self.add_issue("FATAL: Component without ID found.")
ret = False
continue
if ids_found.get(docid):
self.add_issue("FATAL: Found two components with the same ID: %s (in packages %s and %s)." % (docid, ids_found[docid], pkgname))
ret = False
continue
else:
ids_found[docid] = pkgname
try:
schema_component(doc)
except Exception as e:
self.add_issue("[%s]: %s" % (docid, str(e)))
ret = False
continue
# more tests for the icon key
icon = doc.get('Icon')
if (doc['Type'] == "desktop-app") or (doc['Type'] == "web-app"):
if not doc.get('Icon'):
self.add_issue("[%s]: %s" % (docid, "Components containing an application must have an 'Icon' key."))
ret = False
if icon:
if (not icon.get('stock')) and (not icon.get('cached')) and (not icon.get('local')):
self.add_issue("[%s]: %s" % (docid, "A 'stock', 'cached' or 'local' icon must at least be provided. @ data['Icon']"))
ret = False
if not self._test_localized(doc, 'Name'):
ret = False
if not self._test_localized(doc, 'Summary'):
ret = False
if not self._test_localized(doc, 'Description'):
ret = False
if not self._test_localized(doc, 'DeveloperName'):
ret = False
for shot in doc.get('Screenshots', list()):
caption = shot.get('caption')
if caption:
if not self._test_localized_dict(doc, caption, "Screenshots.x.caption"):
ret = False
for rel in doc.get('Releases', list()):
desc = rel.get('description')
if not desc:
continue
if not self._test_localized_dict(doc, desc, "Releases.x.description"):
ret = False
for d in desc.values():
if not self._validate_description(docid, d, "Releases.x.description"):
ret = False
desc = doc.get('Description', dict())
for d in desc.values():
if not self._validate_description(docid, d):
ret = False
return ret
def validate_file(self, fname):
f = None
if fname.endswith(".gz"):
f = gzip.open(fname, 'r')
else:
f = open(fname, 'r')
data = str(f.read(), 'utf-8')
f.close()
return self.validate_data(data)
def print_issues(self):
for issue in self.issue_list:
print(issue)
def clear_issues():
self.issue_list = list()
__all__.append('DEP11Validator')
|