root/trunk/pyHesiodFS/hesiod.py

Revision 38, 2.5 kB (checked in by broder, 16 years ago)

Adding pyHesiodFS to trunk. I fixed the bug with the 1-tuple, but made no other modifications

Line 
1 import DNS
2
3 DNS.DiscoverNameServers()
4
5 dnsreq = DNS.Request(qtype="txt")
6
7 class HesiodParseError(Exception):
8     pass
9
10 class HesiodLookup:
11     """A generic Hesiod lookup"""
12     def __init__(self, name, type, realm="athena.mit.edu"):
13         if "@" in name:
14             name, realm = name.rsplit("@", 1)
15         self.dnsname = ("%s.%s.ns.%s" % (name, type, realm))
16         self.dnsresult = dnsreq.req(name=self.dnsname)
17         self.parseRecords()
18     def parseRecords(self):
19         self.entries = []
20         for answer in self.dnsresult.answers:
21             if answer['name'] == self.dnsname:
22                 if isinstance(answer['data'],list):
23                     self.entries.extend(answer['data'])
24                 else:
25                     self.entries.append(answer['data'])
26     def getRawEntries(self):
27         return self.entries
28
29 class FilsysLookup(HesiodLookup):
30     def __init__(self, name, realm="athena.mit.edu"):
31         HesiodLookup.__init__(self, name, "filsys", realm)
32     def parseRecords(self):
33         HesiodLookup.parseRecords(self)
34         self.filsysPointers = []
35         if len(self.entries) > 1:
36             multiRecords = True
37         else:
38             multiRecords = False
39         for entry in self.entries:
40             priority = 0
41             if multiRecords:
42                 entry, priority = entry.rsplit(" ", 1)
43                 priority = int(priority)
44             parts = entry.split(" ")
45             type = parts[0]
46             if type == 'AFS':
47                 self.filsysPointers.append({'type': type, 'location': parts[1], 'mode': parts[2], 'mountpoint': parts[3], 'priority': priority})
48             elif type == 'NFS':
49                 self.filsysPointers.append({'type': type, 'remote_location': parts[1], 'server': parts[2], 'mode': parts[3], 'mountpoint': parts[4], 'priority': priority})
50             elif type == 'ERR':
51                 parts = entry.split(" ", 1)
52                 self.filsysPointers.append({'type': type, 'message': parts[1], 'priority': priority})
53             elif type == 'UFS':
54                 self.filsysPointers.append({'type': type, 'device': parts[1], 'mode': parts[2], 'mountpoint': parts[3], 'priority': priority})
55             elif type == 'LOC':
56                 self.filsysPointers.append({'type': type, 'location': parts[1], 'mode': parts[2], 'mountpoint': parts[3], 'priority': priority})
57             else:
58                 raise HesiodParseError("Unknown filsys type: "+type)
59         self.filsysPointers.sort(key=lambda x: x['priority'])
60     def getFilsys(self):
61         return self.filsysPointers
Note: See TracBrowser for help on using the browser.