-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen_allocations_go.py
40 lines (35 loc) · 1.47 KB
/
gen_allocations_go.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
#hopefully this works on python2 and python3
from __future__ import print_function
import requests
import yaml #this is pyaml on pip
import sys
import os
import os.path
import textwrap
rq = requests.get("https://raw.githubusercontent.com/immesys/bw2_pid/master/allocations.yaml")
if rq.status_code != 200:
print ("Could not obtain allocations file from GitHub")
sys.exit(1)
doc = yaml.load(rq.text)
def parsedot(s):
i = s.split(".")
return (int(i[0])<<24) + (int(i[1])<<16) + (int(i[2]) << 8) + int(i[3])
subnets = sorted([(int(k.split("/")[1]), parsedot(k.split("/")[0]), k ) for k in doc.keys()])
def gen_golang(subnets, doc):
curpath=os.path.realpath(__file__)
package = curpath.split("/")[-2]
ofgo = open("poSymNames.go","w")
print("package %s\n\n" % package, file=ofgo)
print("//This file is autogenerated from https://github.com/immesys/bw2_pid/blob/master/allocations.yaml", file=ofgo)
for i in subnets:
d = doc[i[2]]
print("//%s (%s): %s" %(d["sym"], i[2], d["short"]), file=ofgo)
print("//"+"\n//".join(textwrap.wrap(d["desc"], 77)), file=ofgo)
print("const PONum%s = %d" % (d["sym"], i[1]), file=ofgo)
print("const PODFMask%s = `%s`" % (d["sym"], i[2]), file=ofgo)
print("const PODF%s = `%s`" % (d["sym"], i[2].split("/")[0]), file=ofgo)
print("const POMask%s = %d" % (d["sym"], i[0]), file=ofgo)
print("", file=ofgo)
ofgo.close()
gen_golang(subnets, doc)