-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.py
executable file
·195 lines (177 loc) · 6.09 KB
/
build.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/env python3
import subprocess
from sys import argv
import tempfile
import re
from pathlib import Path
import zipfile
from os import mkdir, listdir, makedirs
from os.path import exists, join, isfile
from docx_asn1 import extract_text_from_docx
import requests
from bs4 import BeautifulSoup
def download_one_spec(serie):
"""download one spec"""
params = {"sortby": "daterev"}
url = f"https://www.3gpp.org/ftp/Specs/archive/36_series/{serie}"
file = requests.get(url, params=params, timeout=5)
soup = BeautifulSoup(file.content, "html.parser")
table = soup.find("table")
tbody = table.find("tbody")
link = tbody.find("a")
path_to_zip = tempfile.gettempdir() + "/3gpp_build"
if not exists(path_to_zip):
mkdir(path_to_zip)
path_to_zip_file = path_to_zip + "/3gpp.zip"
with open(path_to_zip_file, "wb") as f:
f.write(requests.get(link["href"], timeout=5).content)
out = path_to_zip + f"/output_{serie}"
with zipfile.ZipFile(path_to_zip_file, "r") as zip_ref:
zip_ref.extractall(out)
files = [join(out, f) for f in listdir(out) if isfile(join(out, f))]
print("Downloaded", files[0])
return files[0]
def rrc_patch(text):
new_text = re.sub(r".*simultaneousAckNackAndCQI-Format4-Format5-r13.*", "", text)
new_text = new_text.replace(
"""\t[[condReconfigurationTriggerNR-r17 CondReconfigurationTriggerNR-r17 OPTIONAL-- Need ON
\t]]""",
"",
)
return new_text
spec_ids = {
"36.321": {
"spec": "2437",
"desc": "MAC",
},
"36.322": {
"spec": "2438",
"desc": "RLC",
},
"36.323": {
"spec": "2439",
"desc": "PDCP",
},
"36.331": {
"spec": "2440",
"desc": "RRC",
"patch": rrc_patch,
},
"36.413": {
"spec": "2441",
"desc": "S1AP",
"start": "-- ***************",
"end": "END",
"add": True,
},
"36.423": {
"spec": "2452",
"desc": "X2AP",
},
"36.443": {
"spec": "2458",
"desc": "M2AP",
"start": "-- ***************",
"end": "END",
"add": True,
},
"36.455": {
"spec": "2462",
"desc": "LPPa",
},
}
def write_asn1(one_key, path, asn1):
"""write asn1 to file"""
code_asn1 = "asn"
if not exists(code_asn1):
mkdir(code_asn1)
if asn1 is None:
print(f"Error {one_key}")
asn_path = code_asn1 + "/" + path
with open(asn_path, "w", encoding="utf-8") as f:
f.write(asn1)
if asn1 == "":
print(f"Empty ASN1 {one_key} ({spec_ids[one_key]['desc']})")
print("ASN1", path, "written")
return asn_path
def translate_to_code(one_key, codec, asn_path, code):
"""translate asn1 to rust code"""
path_code = f"src/{codec}/spec_{code}.rs"
print("Translating", asn_path, "to", path_code, end="...")
if not exists(path_code):
makedirs(Path(path_code).parent, exist_ok=True)
try:
cmd = f"hampi-rs-asn1c --codec {codec} --derive serialize --derive deserialize --module {path_code} -- {asn_path} > /tmp/gpp"
with open(f"/tmp/gpp_{code}", "w", encoding="utf-8") as outfile:
subprocess.run(cmd, stdout=outfile, stderr=outfile, shell=True, check=True)
if exists(path_code):
print("done")
return True
else:
raise Exception("Error file not created")
except Exception as e:
print(
f"Error asn1 translation ({codec}) {one_key} ({spec_ids[one_key]['desc']}) see more at /tmp/gpp_{code}",
e,
)
return False
def main():
"""main func"""
should_download = "--download" in argv and len(argv) == 2 or len(argv) == 1
should_compile = "--compile" in argv and len(argv) == 2 or len(argv) == 1
codecs = ["uper"]
# clean
if "--clean" in argv:
for one_codec in codecs:
out_path = f"src/{one_codec}/"
if exists(out_path):
for f in listdir(out_path):
Path(join(out_path, f)).unlink()
codecs_text = ""
for one_codec in codecs:
for one_key, one_value in spec_ids.items():
desc = one_value["desc"]
path_asn = f"{one_key}_spec_{desc}.asn"
if should_download:
docx = download_one_spec(one_key)
if "start" in one_value:
asn1 = extract_text_from_docx(
docx,
one_value["start"],
one_value["end"],
one_value["add"],
)
else:
asn1 = extract_text_from_docx(docx)
if "patch" in one_value:
print(f"Patching {one_key} ({desc})")
asn1 = one_value["patch"](asn1)
if asn1 is None or asn1 == "":
print(f"Error no asn1 {one_key} ({desc})")
continue
asn1 = asn1.replace("\U000000a0", " ")
write_asn1(one_key, path_asn, asn1)
if should_compile:
path_asn = "asn/" + path_asn
for one_codec in codecs:
if translate_to_code(one_key, one_codec, path_asn, desc.lower()):
codecs_text += f"""#[cfg(feature = "{desc.lower()}")]\n"""
codecs_text += f"pub mod spec_{desc.lower()} {{\n"
codecs_text += " #![allow(warnings)]\n"
codecs_text += (
f""" include!("./spec_{desc.lower()}.rs");\n"""
)
codecs_text += "}\n"
# write mod.rs
path_mod = f"src/{one_codec}/"
if not exists(path_mod):
mkdir(path_mod)
with open(f"{path_mod}mod.rs", "w", encoding="utf-8") as file:
file.write(codecs_text)
file = "lib.rs"
with open("src/" + file, "w", encoding="utf-8") as f:
for one_code in codecs:
f.write(f"pub mod {one_code};\n")
f.write("pub mod export;\n") # for re-export
if __name__ == "__main__":
main()