1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-27 01:48:17 +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
COMMENT_PREFIX = '/// '
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
2017-06-14 15:39:32 +00:00
if line.startswith('class '):
current_class = line[6:].split('(')[0].strip(':')
current_indent = 0
2016-09-27 12:06:57 +00:00
yield (current_package, "\n")
2017-04-08 16:43:26 +00:00
yield (current_package, '# ' + mod_desc + "\n")
2017-06-14 15:39:32 +00:00
elif line.startswith('def '):
current_method = line[4:].split('(')[0]
yield (current_package, "\n")
if current_class is None:
yield (current_package, '# ' + mod_desc + "\n")
else:
current_indent = 4
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)
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
2017-06-14 17:27:02 +00:00
filepath = os.path.join(dirpath, filename + '.py')
if not os.path.exists(filepath):
with open(filepath, 'a') as f:
f.write('from typing import *\n')
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
2016-09-27 12:06:57 +00:00
if not (mod_file.endswith('.h') or mod_file.endswith('.c')):
return
2017-06-14 15:39:32 +00:00
if not os.path.basename(mod_file).startswith('mod'):
return
2016-09-27 12:06:57 +00:00
2017-06-14 15:39:32 +00:00
current_indent = 0
current_class = None
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
2017-06-13 14:50:03 +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)
for pkg in os.listdir(dir):
for mod in os.listdir(os.path.join(dir, pkg)):
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):
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
2016-09-27 12:06:57 +00:00
if __name__ == '__main__':
2017-06-14 15:39:32 +00:00
clear_directory('../mocks/generated')
build_directory('../embed/extmod', '../mocks/generated')