diff --git a/src/hostinfo.py b/src/hostinfo.py index 9045522fa0b8a64e787dc44840cd10decaedbda5..ba7b44caed77fa842f001363834084180f4905a0 100755 --- a/src/hostinfo.py +++ b/src/hostinfo.py @@ -88,14 +88,47 @@ attr_weight = { ('subnet', 'name_servers') : 6, } -def cmp(a,b): - if a < b: +#def cmp(a,b): +# if a < b: +# return -1 +# elif a == b: +# return 0 +# else: +# return 1 + +def host_order(a,b): + def at_top(n): + # Place hosts without ethernet or ip address at top + for i in n._interface_: + if i.ip[0]: + return False + if i.ethernet[0]: + return False + pass + return True + def at_bottom(n): + # Place hosts with ethernet but without ip address at bottom + result = False + for i in n._interface_: + if i.ip[0]: + return False + if i.ethernet[0]: + result = True + pass + return result + if (at_top(a) == at_top(b) and at_bottom(a) == at_bottom(b)): + return cmp(a.name[0], b.name[0]) + elif at_top(a): return -1 - elif a == b: - return 0 - else: + elif at_top(b): return 1 - + elif at_bottom(a): + return 1 + elif at_bottom(b): + return -1 + else: + raise Exception('Should never happen') + tag_weight = { ('hostinfo', 'soa') : (1, None), @@ -104,12 +137,10 @@ tag_weight = { ('hostinfo', 'netgroup') : (3, lambda a,b: cmp(a.name[0],b.name[0])), ('hostinfo', 'host') : (4, - lambda a,b: cmp(a.name[0],b.name[0])), - ('host', 'netgroup') : (1, - lambda a,b: cmp(a.name[0],b.name[0])), - ('host', 'automount') : (2, + host_order), + ('host', 'automount') : (1, lambda a,b: cmp(a.host[0],b.host[0])), - ('host', 'mio') : (3, + ('host', 'mio') : (2, None), ('interface', 'kickstart') : (1, None), @@ -267,7 +298,6 @@ if __name__ == '__main__': result += tree._xml(attr_sort=attr_sort, tag_sort=tag_sort) print result.encode("iso8859-1") - if options.yp: for (f, c) in hostinfo.yp.generate(tree, options.yp_auto_domain): file["%s/%s" % (options.yp, f)] = c diff --git a/src/hostinfo/ifconfig.py b/src/hostinfo/ifconfig.py index 4eef92b94af4c8ccefbcc35d447a0e2b6bc50d9c..6b94d0d08b45a9945b2c7a0543ebf90cd5186a5f 100755 --- a/src/hostinfo/ifconfig.py +++ b/src/hostinfo/ifconfig.py @@ -98,8 +98,11 @@ def generate(tree, host): def subnet(tree, ip): for s in tree._subnet_: + if not s.netmask[0] or not s.network[0]: + continue if aton(s.network[0]) == aton(ip) & aton(s.netmask[0]): return s + pass return None def device(ethernet): diff --git a/src/hostinfo/parser.py b/src/hostinfo/parser.py index be9764797b6dd60d316d0227231ce73e2f9d194d..4bd83308660cc511909dcf822c2b5a04390f3112 100755 --- a/src/hostinfo/parser.py +++ b/src/hostinfo/parser.py @@ -360,6 +360,12 @@ class Node: def _xml(self, indent=0, attr_sort=None, tag_sort=None, width=80): """Generate a prettyprinted xml of the tree rooted in this node""" + if self._tag == '': + # Comment node + result = "%s<!--" % (" " * indent) + result += self._attribute[''] + result += "-->\n" + return result result = "" line = "%s<%s" % (" " * indent, self._tag) blanks = " " * len(line) @@ -436,17 +442,27 @@ class Parser(xml.sax.ContentHandler): self.tree = None # Create a xml parser - parser = xml.sax.make_parser() + import xml.parsers.expat + self.parser = xml.parsers.expat.ParserCreate() + self.parser.StartElementHandler = self.startElement + self.parser.EndElementHandler = self.endElement + self.parser.CharacterDataHandler = self.characters + self.parser.CommentHandler = self.comment + """parser = xml.sax.make_parser() parser.setFeature(xml.sax.handler.feature_namespaces, 0) parser.setContentHandler(self) try: parser.setFeature(xml.sax.handler.feature_external_ges, 0) except: pass - + """ + # Parse the url self.current = [ self ] - parser.parse(url) + self.parser.Parse(open(url).read()) + + #import urllib2 + #parser.Parse(urllib2.urlopen(url).read()) # Set the mtime mtime = 0 @@ -469,7 +485,7 @@ class Parser(xml.sax.ContentHandler): # SAX parser callbacks # def startElement(self, tag, attrs): - child = Node(tag, self._locator.getLineNumber()) + child = Node(tag, self.parser.CurrentLineNumber) for (name, value) in attrs.items(): child._set(name, value) self.current.append(child) @@ -481,7 +497,12 @@ class Parser(xml.sax.ContentHandler): def characters(self, char): if not char.isspace(): - self.current[-1]._append(char) + self.current[-1]._append(char) + + def comment(self, data): + # Ugly hack, ... + parent = self.current[-1] + parent._add(Node('', self.parser.CurrentLineNumber, { '': data })) def parse(url): """Parse the given 'url' returning an easily traversable tree"""