| 1 |
|
| 2 |
import os |
| 3 |
import glob |
| 4 |
import csv |
| 5 |
import json |
| 6 |
|
| 7 |
base = os.path.dirname(os.path.realpath(__file__)) |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
with open(os.path.join(base, 'manuf.go.template')) as fp: |
| 13 |
template = fp.read() |
| 14 |
|
| 15 |
data = {} |
| 16 |
|
| 17 |
for path in glob.glob("./manuf/*.csv"): |
| 18 |
with open(path, 'r') as fp: |
| 19 |
reader = csv.reader(fp.readlines()) |
| 20 |
|
| 21 |
next(reader) |
| 22 |
for row in reader: |
| 23 |
(registry, assignment, org_name, org_addr) = row |
| 24 |
data[assignment] = org_name |
| 25 |
|
| 26 |
|
| 27 |
code = "map[string]string {\n" |
| 28 |
|
| 29 |
for (key, vendor) in data.items(): |
| 30 |
code += " \"%s\": %s,\n" % (key, |
| 31 |
json.dumps(vendor)) |
| 32 |
|
| 33 |
code += "}\n" |
| 34 |
|
| 35 |
code = template.replace('#MAP#', code) |
| 36 |
|
| 37 |
with open(os.path.join(base, 'manuf.go'), 'w+t') as fp: |
| 38 |
fp.write(code) |
| 39 |
|
| 40 |
""" |
| 41 |
with open(os.path.join(base, 'manuf')) as fp: |
| 42 |
lines = [l.strip() for l in fp.readlines()] |
| 43 |
lines = [l for l in lines if l != "" and l[0] != '#'] |
| 44 |
|
| 45 |
|
| 46 |
def get_mac_and_mask(mac): |
| 47 |
# simple case |
| 48 |
if not "/" in mac: |
| 49 |
mac_hex = mac.replace(":", '') |
| 50 |
mask = 48 - 4 * len(mac_hex) |
| 51 |
mac_int = int(mac_hex, 16) << mask |
| 52 |
|
| 53 |
# 00:1B:C5:00:00:00/36 |
| 54 |
else: |
| 55 |
parts = mac.split("/") |
| 56 |
mac_hex = parts[0].replace(":", '') |
| 57 |
mask = 48 - int(parts[1]) |
| 58 |
mac_int = int(mac_hex, 16) << mask |
| 59 |
|
| 60 |
return (mac_int, mask) |
| 61 |
|
| 62 |
|
| 63 |
index = {} |
| 64 |
|
| 65 |
for line in lines: |
| 66 |
m = re.match(r'^([^\s]+)\s+([^\s]+)(.*)$', line, re.M) |
| 67 |
parts = m.groups() |
| 68 |
mac = parts[0] |
| 69 |
short = parts[1] |
| 70 |
manuf = parts[2].strip() |
| 71 |
if manuf == "": |
| 72 |
manuf = short |
| 73 |
|
| 74 |
m = re.match(r'^([^#]+)#.+$', manuf) |
| 75 |
if m is not None: |
| 76 |
manuf = m.groups()[0].strip() |
| 77 |
|
| 78 |
mac_int, mask = get_mac_and_mask(mac) |
| 79 |
|
| 80 |
key = "%d.%d" % (mask, mac_int >> mask) |
| 81 |
index[key] = manuf |
| 82 |
|
| 83 |
code = "map[string]string {\n" |
| 84 |
|
| 85 |
for key, vendor in six.iteritems(index): |
| 86 |
code += " \"%s\": \"%s\",\n" % (key, vendor.replace('"', '\\"')) |
| 87 |
|
| 88 |
code += "}\n" |
| 89 |
|
| 90 |
code = template.replace('#MAP#', code) |
| 91 |
|
| 92 |
with open(os.path.join(base, 'manuf.go'), 'w+t') as fp: |
| 93 |
fp.write(code) |
| 94 |
""" |