1 | #!/usr/bin/env python
|
---|
2 | # zoek de eerste vrije (== niet in een wleiden.conf gedefinieerde) interlink
|
---|
3 | # range
|
---|
4 | # lodewijk@wirelessleiden.nl
|
---|
5 | import re
|
---|
6 | import sys
|
---|
7 | import glob
|
---|
8 | import getrange
|
---|
9 | import tempfile
|
---|
10 | import subprocess
|
---|
11 |
|
---|
12 | OUTFILE = 'network.png'
|
---|
13 |
|
---|
14 | def make_graph():
|
---|
15 | substre = re.compile("^(\$[a-zA-Z][^ =]*) ?= ?'([^']*)';")
|
---|
16 |
|
---|
17 |
|
---|
18 | poel = {}
|
---|
19 |
|
---|
20 | #XXX: Make parsing global, merge with getrange.get_ranges if possible
|
---|
21 | for fname in glob.glob("*/wleiden.conf"):
|
---|
22 | substs = {}
|
---|
23 | lineNr = 0
|
---|
24 | for l in open(fname):
|
---|
25 | lineNr += 1
|
---|
26 | l = l[:-1]
|
---|
27 | match = substre.match(l)
|
---|
28 | if match != None:
|
---|
29 | substs[match.group(1)] = match.group(2)
|
---|
30 | if l.startswith("IP="):
|
---|
31 | right = l[3:]
|
---|
32 | if '/' not in right:
|
---|
33 | addr = right
|
---|
34 | mask = 32
|
---|
35 | else:
|
---|
36 | addr, mask = right.split('/')
|
---|
37 | if addr in substs:
|
---|
38 | addr = substs[addr]
|
---|
39 | try:
|
---|
40 | addr = getrange.parseaddr(addr)
|
---|
41 | mask = int(mask)
|
---|
42 | addr = addr & ~((1 << (32 - mask)) - 1)
|
---|
43 | cname = fname.split('/')[0]
|
---|
44 | if poel.has_key(addr):
|
---|
45 | poel[addr] += [cname]
|
---|
46 | else:
|
---|
47 | poel[addr] = [cname]
|
---|
48 | except ValueError, e:
|
---|
49 | print "[FOUT] in bestand '%s' regel '%i': %s" % (fname, lineNr, l)
|
---|
50 |
|
---|
51 | f = tempfile.NamedTemporaryFile(bufsize=0)
|
---|
52 | sys.stderr.write("# Building temponary graph file\n")
|
---|
53 | print >> f, "Graph WirelessLeidenNetwork {"
|
---|
54 | print >> f ,"""
|
---|
55 | graph [ fontsize = 36,
|
---|
56 | overlap = scalexy,
|
---|
57 | splines = true,
|
---|
58 | ]
|
---|
59 | """
|
---|
60 | for addr,leden in poel.iteritems():
|
---|
61 | leden = sorted(set(leden))
|
---|
62 | for index,lid in enumerate(leden[:-1]):
|
---|
63 | for buur in leden[index + 1:]:
|
---|
64 | print >> f,' %s -- %s [label="%s"]' % (lid, buur, getrange.showaddr(addr))
|
---|
65 | print >> f, "}"
|
---|
66 | sys.stderr.write("# Plotting temponary graph file using graphviz\n")
|
---|
67 | retval = subprocess.call(["neato","-Tpng",f.name, "-o", OUTFILE])
|
---|
68 | if retval != 0:
|
---|
69 | sys.stderr.write("# FAILED [%i]\n" % retval)
|
---|
70 | subprocess.call(["cat",f.name])
|
---|
71 | exit(1)
|
---|
72 | sys.stderr.write("# COMPLETED find your output in %s\n" % OUTFILE)
|
---|
73 |
|
---|
74 |
|
---|
75 | if __name__ == "__main__":
|
---|
76 | make_graph()
|
---|
77 |
|
---|