1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-29 19:08:12 +00:00
trezor-firmware/tools/build_mocks

113 lines
3.1 KiB
Plaintext
Raw Normal View History

2016-09-27 12:06:57 +00:00
#!/usr/bin/env python3
import os
2018-07-31 09:35:09 +00:00
COMMENT_PREFIX = "/// "
2016-09-27 12:06:57 +00:00
2017-06-14 15:39:32 +00:00
current_indent = 0
current_class = None
2016-09-27 12:06:57 +00:00
current_method = None
current_package = None
2017-06-13 14:50:03 +00:00
2017-04-08 16:43:26 +00:00
def split_to_parts(line, mod_desc=None):
2017-06-14 15:39:32 +00:00
global current_indent
global current_class
2016-09-27 12:06:57 +00:00
global current_method
2016-10-03 13:47:54 +00:00
2018-07-31 09:35:09 +00:00
if line.startswith("class "):
current_class = line[6:].split("(")[0].strip(":")
2017-06-14 15:39:32 +00:00
current_indent = 0
2016-09-27 12:06:57 +00:00
yield (current_package, "\n")
2018-07-31 09:35:09 +00:00
yield (current_package, "# " + mod_desc + "\n")
2017-06-14 15:39:32 +00:00
2018-07-31 09:35:09 +00:00
elif line.startswith("def "):
current_method = line[4:].split("(")[0]
2017-06-14 15:39:32 +00:00
yield (current_package, "\n")
if current_class is None:
2018-07-31 09:35:09 +00:00
yield (current_package, "# " + mod_desc + "\n")
2017-06-14 15:39:32 +00:00
else:
current_indent = 4
2018-07-31 09:35:09 +00:00
line = current_indent * " " + line
2016-10-03 13:47:54 +00:00
2016-09-27 12:06:57 +00:00
yield (current_package, line)
2017-06-13 14:50:03 +00:00
2016-09-27 12:06:57 +00:00
def store_to_file(dest, parts):
2017-06-14 15:39:32 +00:00
for package, line in parts:
2017-06-14 17:27:02 +00:00
dirpath = os.path.abspath(dest)
2017-06-14 15:39:32 +00:00
filename = package
2016-10-03 13:47:54 +00:00
2017-06-14 17:27:02 +00:00
if not os.path.exists(dirpath):
os.makedirs(dirpath)
2018-07-31 09:35:09 +00:00
open(os.path.join(dirpath, "__init__.py"), "w").close()
open(os.path.join(dirpath, ".mock-generated"), "w").close()
2016-10-03 13:47:54 +00:00
2018-07-31 09:35:09 +00:00
filepath = os.path.join(dirpath, filename + ".py")
2017-06-14 17:27:02 +00:00
if not os.path.exists(filepath):
2018-07-31 09:35:09 +00:00
with open(filepath, "a") as f:
f.write("from typing import *\n")
2017-06-14 17:27:02 +00:00
2018-07-31 09:35:09 +00:00
with open(filepath, "a") as f:
2017-06-14 15:39:32 +00:00
f.write(line)
2016-09-27 12:06:57 +00:00
2017-06-13 14:50:03 +00:00
2016-09-27 12:06:57 +00:00
def build_module(mod_file, dest):
2017-06-14 15:39:32 +00:00
global current_indent
global current_class
global current_package
2018-07-31 09:35:09 +00:00
if not (mod_file.endswith(".h") or mod_file.endswith(".c")):
2016-09-27 12:06:57 +00:00
return
2018-07-31 09:35:09 +00:00
if not os.path.basename(mod_file).startswith("mod"):
2017-06-14 15:39:32 +00:00
return
2016-09-27 12:06:57 +00:00
2017-06-14 15:39:32 +00:00
current_indent = 0
current_class = None
2018-07-31 09:35:09 +00:00
current_package = (
os.path.basename(mod_file).split(".")[0].split("-")[0].replace("mod", "")
)
mod_desc = mod_file.replace("../embed/extmod", "extmod")
2017-06-14 15:39:32 +00:00
2016-09-27 12:06:57 +00:00
for l in open(mod_file):
if not l.startswith(COMMENT_PREFIX):
continue
2016-10-03 13:47:54 +00:00
2018-07-31 09:35:09 +00:00
l = l[len(COMMENT_PREFIX) :] # .strip()
2017-04-08 16:43:26 +00:00
store_to_file(dest, split_to_parts(l, mod_desc))
2016-10-03 13:47:54 +00:00
2017-06-13 14:50:03 +00:00
2016-09-27 12:06:57 +00:00
def build_directory(dir, dest):
print("Building mocks for", dir, "to", dest)
2018-01-26 13:27:02 +00:00
for pkg in sorted(os.listdir(dir)):
for mod in sorted(os.listdir(os.path.join(dir, pkg))):
2016-09-27 12:06:57 +00:00
build_module(os.path.join(dir, pkg, mod), dest)
2017-06-13 14:50:03 +00:00
2016-09-27 12:06:57 +00:00
def clear_directory(top_dir):
print("Clearing up directory", top_dir)
for root, dirs, files in os.walk(top_dir, topdown=False):
2018-07-31 09:35:09 +00:00
if ".mock-generated" not in files:
2017-06-14 15:39:32 +00:00
# print("Not a mock directory", root)
2016-09-27 12:06:57 +00:00
continue
for name in files:
2017-06-14 15:39:32 +00:00
# print('Deleting file', os.path.join(root, name))
2016-09-27 12:06:57 +00:00
os.remove(os.path.join(root, name))
for name in dirs:
2017-06-14 15:39:32 +00:00
# print('Deleting directory', os.path.join(root, name))
try:
os.rmdir(os.path.join(root, name))
except FileNotFoundError:
pass
os.rmdir(root)
2016-10-03 13:47:54 +00:00
2017-09-05 21:15:47 +00:00
2018-07-31 09:35:09 +00:00
if __name__ == "__main__":
clear_directory("../mocks/generated")
build_directory("../embed/extmod", "../mocks/generated")