Check 'distributions' attribute contains valid names
[0publish.git] / validator.py
blobeda0a914b51f108134b83789479db9e6a0750a14
1 import os, io
2 from zeroinstall.injector import model, namespaces
3 from zeroinstall.injector.reader import InvalidInterface, update
4 from xml.dom import minidom, Node, XMLNS_NAMESPACE
5 import tempfile
6 from logging import warn, info
8 group_impl_attribs = ['version', 'version-modifier', 'released', 'main', 'stability', 'arch', 'license', 'doc-dir', 'self-test', 'langs', 'local-path']
10 known_elements = {
11 'interface' : ['uri', 'min-injector-version', 'main'], # (main is deprecated)
12 'name' : [],
13 'summary' : [],
14 'description' : [],
15 'needs-terminal' : [],
16 'homepage' : [],
17 'category' : ['type'],
18 'icon' : ['type', 'href'],
19 'feed' : ['src', 'arch'],
20 'feed-for' : ['interface'],
21 'replaced-by' : ['interface'],
23 'group' : group_impl_attribs,
24 'implementation' : ['id'] + group_impl_attribs,
25 'package-implementation' : ['package', 'main', 'distributions'],
26 'manifest-digest' : ['sha1new', 'sha256', 'sha256new'],
27 'command' : ['name', 'path', 'shell-command'],
28 'arg' : [],
30 'archive' : ['href', 'size', 'extract', 'type', 'start-offset'],
31 'recipe' : [],
32 'requires' : ['interface', 'use', 'importance'],
33 'runner' : ['interface', 'use', 'importance', 'command'],
34 'version' : ['not-before', 'before'],
35 'environment' : ['name', 'insert', 'value', 'default', 'mode'],
36 'executable-in-var' : ['name', 'command'],
37 'executable-in-path' : ['name', 'command'],
38 #'overlay' : ['src', 'mount-point'],
41 known_distros = frozenset([
42 "Windows", "Darwin", "Debian", "RPM", "Slack",
43 "Arch", "Gentoo", "Ports", "MacPorts", "Cygwin"
46 def checkElement(elem):
47 if elem.namespaceURI != namespaces.XMLNS_IFACE:
48 info("Note: Skipping unknown (but namespaced) element <%s>", elem.localName)
49 return # Namespaces elements are OK
51 if elem.localName not in known_elements:
52 warn("Unknown Zero Install element <%s>.\nNon Zero-Install elements should be namespaced.", elem.localName)
53 return
55 known_attrs = known_elements[elem.localName]
57 for (uri, name), value in elem.attributes.itemsNS():
58 if uri == XMLNS_NAMESPACE:
59 continue # Namespace declarations are fine
61 if uri:
62 info("Note: Skipping unknown (but namespaced) attribute '%s'", name)
63 continue
65 if name not in known_attrs:
66 warn("Unknown Zero Install attribute '%s' on <%s>.\nNon Zero-Install attributes should be namespaced.",
67 name, elem.localName)
69 if name == 'distributions':
70 if ',' in value:
71 warn("Use ' ' to separate distribution names, not ',' (%s)", value)
72 else:
73 for distro in value.split(' '):
74 if distro not in known_distros:
75 warn("Unknown distribution name '%s' (expected one of %s)", distro, '|'.join(known_distros))
77 for child in elem.childNodes:
78 if child.nodeType == Node.ELEMENT_NODE:
79 checkElement(child)
81 def check(data, warnings = True):
82 assert type(data) == bytes, type(data) # (must not be unicode)
83 fd, tmp_name = tempfile.mkstemp(prefix = '0publish-validate-')
84 os.close(fd)
85 tmp_iface = model.Interface(tmp_name)
86 try:
87 with io.open(tmp_name, 'wb') as stream:
88 stream.write(data)
89 try:
90 update(tmp_iface, tmp_name, local = True)
91 except InvalidInterface, ex:
92 raise
93 except Exception, ex:
94 warn("Internal error: %s", ex)
95 raise InvalidInterface(str(ex))
96 finally:
97 os.unlink(tmp_name)
99 if warnings:
100 doc = minidom.parseString(data)
101 checkElement(doc.documentElement)