| 1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
# Cuts out all the cruft for the udeb packages. |
| 4 |
|
| 5 |
import sys |
| 6 |
import os |
| 7 |
import string |
| 8 |
import re |
| 9 |
import traceback |
| 10 |
import getopt |
| 11 |
import xml.dom |
| 12 |
import xml.dom.minidom |
| 13 |
|
| 14 |
try: |
| 15 |
True |
| 16 |
except: |
| 17 |
True = 1 |
| 18 |
False = 0 |
| 19 |
|
| 20 |
config = { "filelist": [], |
| 21 |
"outfile": "-", |
| 22 |
"outtype": "device", |
| 23 |
"includebus": [], |
| 24 |
"excludebus": [], |
| 25 |
"classspec": [], |
| 26 |
"classversion": "", |
| 27 |
"modlistfile": "", |
| 28 |
"debug": False |
| 29 |
} |
| 30 |
|
| 31 |
toplevel_nodes = { "device": "device_list", |
| 32 |
"vendor": "vendor_list" |
| 33 |
} |
| 34 |
|
| 35 |
class UsageError(RuntimeError): |
| 36 |
pass |
| 37 |
|
| 38 |
def compare_versions(x, y): |
| 39 |
x_pieces = string.split(x, ".") |
| 40 |
y_pieces = string.split(y, ".") |
| 41 |
index = 0 |
| 42 |
while index < len(x_pieces) and index < len(y_pieces): |
| 43 |
if int(x_pieces[index]) != int(y_pieces[index]): |
| 44 |
return cmp(int(x_pieces[index]), int(y_pieces[index])) |
| 45 |
index = index + 1 |
| 46 |
|
| 47 |
return cmp(len(x_pieces), len(y_pieces)) |
| 48 |
|
| 49 |
def check_version(set_spec, version): |
| 50 |
if string.find(set_spec, "inf") != -1: |
| 51 |
pass |
| 52 |
|
| 53 |
include_left = (set_spec[0] == "[") |
| 54 |
include_right = (set_spec[-1] == "]") |
| 55 |
(left_version, right_version) = re.split(r',\s*', set_spec[1:-1]) |
| 56 |
|
| 57 |
if left_version == "inf": |
| 58 |
left_compare = -1 |
| 59 |
else: |
| 60 |
left_compare = compare_versions(left_version, version) |
| 61 |
if right_version == "inf": |
| 62 |
right_compare = 1 |
| 63 |
else: |
| 64 |
right_compare = compare_versions(version, right_version) |
| 65 |
|
| 66 |
if left_compare < 0 and right_compare < 0: |
| 67 |
return True |
| 68 |
if left_compare == 0 and include_left: |
| 69 |
return True |
| 70 |
if right_compare == 0 and include_right: |
| 71 |
return True |
| 72 |
|
| 73 |
return False |
| 74 |
|
| 75 |
def get_busclass(device_node, busclass_docs): |
| 76 |
if not device_node.attributes.has_key("busclass"): |
| 77 |
return None |
| 78 |
|
| 79 |
device_id = device_node.attributes["busclass"].value |
| 80 |
for doc in busclass_docs: |
| 81 |
for node in doc.documentElement.childNodes: |
| 82 |
if node.nodeType != xml.dom.Node.ELEMENT_NODE: |
| 83 |
continue |
| 84 |
|
| 85 |
id = node.attributes["id"].value |
| 86 |
if id == device_id: |
| 87 |
return node.attributes["name"].value |
| 88 |
|
| 89 |
return None |
| 90 |
|
| 91 |
def find_spec(spec, node): |
| 92 |
try: |
| 93 |
(spec_name, spec_rest) = string.split(spec, ":", 1) |
| 94 |
except: |
| 95 |
spec_name = spec |
| 96 |
spec_rest = None |
| 97 |
|
| 98 |
node_name = node.attributes["class"] |
| 99 |
if node_name is None: |
| 100 |
return False |
| 101 |
|
| 102 |
node_name = node_name.value |
| 103 |
if node_name == spec_name: |
| 104 |
if spec_rest is None: |
| 105 |
return True |
| 106 |
elif spec_rest[0] in string.digits: |
| 107 |
spec_version = spec_rest |
| 108 |
try: |
| 109 |
node_version_spec = node.attributes["version"] |
| 110 |
except KeyError: |
| 111 |
return True |
| 112 |
|
| 113 |
return check_version(node_version_spec.value, spec_version) |
| 114 |
else: |
| 115 |
for child_node in node.childNodes: |
| 116 |
if child_node.nodeType != xml.dom.Node.ELEMENT_NODE: |
| 117 |
continue |
| 118 |
if find_spec(spec_rest, child_node): |
| 119 |
return True |
| 120 |
return False |
| 121 |
else: |
| 122 |
return False |
| 123 |
|
| 124 |
def usage(f): |
| 125 |
f.write("need usage statement\n") |
| 126 |
|
| 127 |
def main(): |
| 128 |
global config |
| 129 |
global toplevel_nodes |
| 130 |
|
| 131 |
try: |
| 132 |
|
| 133 |
# Parse options |
| 134 |
|
| 135 |
(options, args) = getopt.getopt(sys.argv[1:], "di:o:", |
| 136 |
["input-file=", "output-file=", |
| 137 |
"output-type=", |
| 138 |
"include-bus=", |
| 139 |
"exclude-bus=", "class-spec=", |
| 140 |
"module-list=", |
| 141 |
"debug"]) |
| 142 |
for option in options: |
| 143 |
if option[0] in ("-i", "--input-file"): |
| 144 |
config["filelist"].append(option[1]) |
| 145 |
elif option[0] in ("-o", "--output-file"): |
| 146 |
config["outfile"] = option[1] |
| 147 |
elif option[0] == "--output-type": |
| 148 |
config["outtype"] = option[1] |
| 149 |
elif option[0] == "--include-bus": |
| 150 |
config["includebus"].extend(string.split(option[1], ",")) |
| 151 |
elif option[0] == "--exclude-bus": |
| 152 |
config["excludebus"].extend(string.split(option[1], ",")) |
| 153 |
elif option[0] == "--class-spec": |
| 154 |
config["classspec"].append(option[1]) |
| 155 |
elif option[0] == "--module-list": |
| 156 |
config["modlistfile"] = option[1] |
| 157 |
elif option[0] in ("-d", "--debug"): |
| 158 |
config["debug"] = True |
| 159 |
else: |
| 160 |
raise UsageError, "invalid option: %s" % (option[0],) |
| 161 |
|
| 162 |
# Sanity-check options |
| 163 |
|
| 164 |
if len(config["filelist"]) == 0: |
| 165 |
raise UsageError, "need input busclass, vendor, and device files" |
| 166 |
if len(config["classspec"]) == 0: |
| 167 |
raise UsageError, "need at least one class specification" |
| 168 |
|
| 169 |
# Load module list information if present |
| 170 |
|
| 171 |
modlist = [] |
| 172 |
if config["modlistfile"] and os.path.exists(config["modlistfile"]): |
| 173 |
modlistfile = open(config["modlistfile"]) |
| 174 |
for line in modlistfile: |
| 175 |
modlist.append(string.strip(line)) |
| 176 |
modlistfile.close() |
| 177 |
|
| 178 |
# Load files |
| 179 |
|
| 180 |
bus_info = {} |
| 181 |
for fn in config["filelist"]: |
| 182 |
try: |
| 183 |
document = xml.dom.minidom.parse(fn) |
| 184 |
bus_id = document.documentElement.attributes["bus"].value |
| 185 |
except: |
| 186 |
sys.stderr.write("warning: couldn't parse %s, skipping.\n" |
| 187 |
% (fn,)) |
| 188 |
continue |
| 189 |
|
| 190 |
if not bus_info.has_key(bus_id): |
| 191 |
bus_info[bus_id] = { "busclass": [], |
| 192 |
"vendor": [], |
| 193 |
"device": [] } |
| 194 |
data_type = document.documentElement.tagName[:-5] |
| 195 |
try: |
| 196 |
bus_info[bus_id][data_type].append(document) |
| 197 |
except: |
| 198 |
sys.stderr.write("warning: invalid file %s, continuing.\n" |
| 199 |
% (fn,)) |
| 200 |
|
| 201 |
# Sanity-check file information |
| 202 |
|
| 203 |
if len(bus_info.keys()) != 1: |
| 204 |
raise RuntimeError, \ |
| 205 |
"one and only one bus type can be used at a time" |
| 206 |
bus_data = bus_info[bus_info.keys()[0]] |
| 207 |
for bus_type in ("busclass", "device"): |
| 208 |
if len(bus_data[bus_type]) == 0: |
| 209 |
raise RuntimeError, \ |
| 210 |
"required data for bus %s missing, aborting" \ |
| 211 |
% (bus_type,) |
| 212 |
|
| 213 |
# Create output device document |
| 214 |
|
| 215 |
outtype = config["outtype"] |
| 216 |
out = bus_data[outtype][0].implementation.createDocument(None, toplevel_nodes[outtype], None) |
| 217 |
out.documentElement.setAttribute("bus", bus_info.keys()[0]) |
| 218 |
|
| 219 |
# Iterate over the input device document, copying nodes we |
| 220 |
# don't want to strip to the output document. |
| 221 |
|
| 222 |
attr_list = [] |
| 223 |
for device_doc in bus_data["device"]: |
| 224 |
for device_node in device_doc.documentElement.childNodes: |
| 225 |
if device_node.nodeType != xml.dom.Node.ELEMENT_NODE: |
| 226 |
continue |
| 227 |
|
| 228 |
is_approved_busclass = True |
| 229 |
do_device_node = False |
| 230 |
module_in_list = False |
| 231 |
new_device_node = None |
| 232 |
busclass = get_busclass(device_node, bus_data["busclass"]) |
| 233 |
if busclass is None or \ |
| 234 |
busclass in config["excludebus"]: |
| 235 |
is_approved_busclass = False |
| 236 |
if len(config["includebus"]) > 0 and \ |
| 237 |
busclass not in config["includebus"]: |
| 238 |
is_approved_busclass = False |
| 239 |
|
| 240 |
for data_node in device_node.childNodes: |
| 241 |
if data_node.nodeType != xml.dom.Node.ELEMENT_NODE: |
| 242 |
continue |
| 243 |
do_data_node = False |
| 244 |
for spec in config["classspec"]: |
| 245 |
if find_spec(spec, data_node): |
| 246 |
npath = ["module", "name"] |
| 247 |
nnode = data_node.childNodes[0] |
| 248 |
while len(npath) > 0 and nnode is not None: |
| 249 |
while nnode is not None and \ |
| 250 |
nnode.nodeType != \ |
| 251 |
xml.dom.Node.ELEMENT_NODE: |
| 252 |
nnode = nnode.nextSibling |
| 253 |
if nnode is None: |
| 254 |
continue |
| 255 |
if nnode.getAttribute("class") == npath[0]: |
| 256 |
nnode = nnode.childNodes[0] |
| 257 |
npath.pop(0) |
| 258 |
else: |
| 259 |
nnode = nnode.nextSibling |
| 260 |
|
| 261 |
if nnode is not None: |
| 262 |
module_name = string.strip(nnode.nodeValue) |
| 263 |
if module_name in modlist: |
| 264 |
module_in_list = True |
| 265 |
break |
| 266 |
elif module_name not in ("ignore", "unknown"): |
| 267 |
do_data_node = True |
| 268 |
break |
| 269 |
|
| 270 |
if module_in_list or \ |
| 271 |
(do_data_node and is_approved_busclass): |
| 272 |
do_device_node = True |
| 273 |
if outtype == "device": |
| 274 |
if new_device_node is None: |
| 275 |
new_device_node = device_node.cloneNode(False) |
| 276 |
new_data_node = data_node.cloneNode(False) |
| 277 |
for mod_node in data_node.childNodes: |
| 278 |
if mod_node.nodeType != xml.dom.Node.ELEMENT_NODE: |
| 279 |
continue |
| 280 |
if mod_node.getAttribute("class") != "module": |
| 281 |
continue |
| 282 |
new_mod_node = mod_node.cloneNode(True) |
| 283 |
new_data_node.appendChild(new_mod_node) |
| 284 |
break |
| 285 |
new_device_node.appendChild(new_data_node) |
| 286 |
|
| 287 |
if do_device_node: |
| 288 |
if outtype == "device": |
| 289 |
out.documentElement.appendChild(new_device_node) |
| 290 |
else: |
| 291 |
attr_list.append(device_node.getAttribute(outtype)) |
| 292 |
|
| 293 |
# If we're reducing something besides the device list, create |
| 294 |
# that reduced document here. |
| 295 |
|
| 296 |
if outtype != "device": |
| 297 |
for doc in bus_data[outtype]: |
| 298 |
for node in doc.documentElement.childNodes: |
| 299 |
if node.nodeType != xml.dom.Node.ELEMENT_NODE: |
| 300 |
continue |
| 301 |
if node.getAttribute("id") in attr_list: |
| 302 |
new_node = node.cloneNode(True) |
| 303 |
out.documentElement.appendChild(new_node) |
| 304 |
|
| 305 |
# Write the output document |
| 306 |
|
| 307 |
if config["outfile"] == "-": |
| 308 |
out_file = sys.stdout |
| 309 |
else: |
| 310 |
out_file = open(config["outfile"], "w") |
| 311 |
|
| 312 |
out.writexml(out_file, encoding="UTF-8") |
| 313 |
|
| 314 |
except UsageError, e: |
| 315 |
sys.stderr.write(sys.argv[0] + ": " + str(e) + "\n") |
| 316 |
if config["debug"]: |
| 317 |
traceback.print_exc(None, sys.stderr) |
| 318 |
usage(sys.stderr) |
| 319 |
sys.exit(1) |
| 320 |
except Exception, e: |
| 321 |
sys.stderr.write(sys.argv[0] + ": " + str(e) + "\n") |
| 322 |
if config["debug"]: |
| 323 |
traceback.print_exc(None, sys.stderr) |
| 324 |
sys.exit(1) |
| 325 |
|
| 326 |
if __name__ == "__main__": |
| 327 |
main() |