1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-29 01:28:10 +00:00
trezor-firmware/tools/build_mocks

84 lines
2.5 KiB
Plaintext
Raw Normal View History

2016-09-27 12:06:57 +00:00
#!/usr/bin/env python3
import os
COMMENT_PREFIX = '/// '
current_method = None
current_package = None
def split_to_parts(line, mod_file=None):
2016-09-27 12:06:57 +00:00
global current_method
global current_package
# Line is beginning of method
if line.startswith('def '):
# Parse name of method from line like 'def trezor.config.get():'
current_method = line[4:].split('(')[0]
#print("Current method", current_method)
2016-10-03 13:47:54 +00:00
2016-09-27 12:06:57 +00:00
*current_package, method_name = current_method.split('.')
yield (current_package, "\n")
yield (current_package, '# ' + mod_file + "\n")
2016-09-27 12:06:57 +00:00
line = line.replace(current_method, method_name)
2016-10-03 13:47:54 +00:00
2016-09-27 12:06:57 +00:00
yield (current_package, line)
def store_to_file(dest, parts):
while True:
try:
(package, line) = parts.__next__()
except StopIteration:
return
2016-10-03 13:47:54 +00:00
2016-09-27 12:06:57 +00:00
dir_path = os.path.abspath(os.path.join(dest, *package[:-1]))
filename = package[-1]
2016-10-03 13:47:54 +00:00
2016-09-27 12:06:57 +00:00
if not os.path.exists(dir_path):
os.makedirs(dir_path)
open(os.path.join(dir_path, '__init__.py'), 'w').close()
open(os.path.join(dir_path, '.mock-generated'), 'w').close()
2016-10-03 13:47:54 +00:00
2016-09-27 12:06:57 +00:00
f = open(os.path.join(dir_path, filename + '.py'), 'a')
f.write(line)
f.close()
def build_module(mod_file, dest):
if not (mod_file.endswith('.h') or mod_file.endswith('.c')):
return
#print(mod_file)
for l in open(mod_file):
if not l.startswith(COMMENT_PREFIX):
continue
2016-10-03 13:47:54 +00:00
2016-09-27 12:06:57 +00:00
l = l[len(COMMENT_PREFIX):]#.strip()
store_to_file(dest, split_to_parts(l, mod_file))
2016-10-03 13:47:54 +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)
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:
2016-09-27 12:06:57 +00:00
#print("Not a mock directory", root)
continue
for name in files:
#print('Deleting file', os.path.join(root, name))
os.remove(os.path.join(root, name))
for name in dirs:
#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__':
clear_directory('../mocks')
build_directory('../extmod', '../mocks')