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

tools/build_mocks: support classes

This commit is contained in:
Jan Pochyla 2017-06-14 17:39:32 +02:00
parent f746fd4e38
commit e29601e838

View File

@ -3,53 +3,71 @@ import os
COMMENT_PREFIX = '/// ' COMMENT_PREFIX = '/// '
current_indent = 0
current_class = None
current_method = None current_method = None
current_package = None current_package = None
def split_to_parts(line, mod_desc=None): def split_to_parts(line, mod_desc=None):
global current_indent
global current_class
global current_method 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)
*current_package, method_name = current_method.split('.') if line.startswith('class '):
current_class = line[6:].split('(')[0].strip(':')
current_indent = 0
yield (current_package, "\n") yield (current_package, "\n")
yield (current_package, '# ' + mod_desc + "\n") yield (current_package, '# ' + mod_desc + "\n")
line = line.replace(current_method, method_name)
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
yield (current_package, line) yield (current_package, line)
def store_to_file(dest, parts): def store_to_file(dest, parts):
while True: for package, line in parts:
try: dir_path = os.path.abspath(dest)
(package, line) = parts.__next__() filename = package
except StopIteration:
return
dir_path = os.path.abspath(os.path.join(dest, *package[:-1]))
filename = package[-1]
if not os.path.exists(dir_path): if not os.path.exists(dir_path):
os.makedirs(dir_path) os.makedirs(dir_path)
open(os.path.join(dir_path, '__init__.py'), 'w').close() open(os.path.join(dir_path, '__init__.py'), 'w').close()
open(os.path.join(dir_path, '.mock-generated'), 'w').close() open(os.path.join(dir_path, '.mock-generated'), 'w').close()
f = open(os.path.join(dir_path, filename + '.py'), 'a') with open(os.path.join(dir_path, filename + '.py'), 'a') as f:
f.write(line) f.write(line)
f.close()
def build_module(mod_file, dest): def build_module(mod_file, dest):
global current_indent
global current_class
global current_package
if not (mod_file.endswith('.h') or mod_file.endswith('.c')): if not (mod_file.endswith('.h') or mod_file.endswith('.c')):
return return
if not os.path.basename(mod_file).startswith('mod'):
return
current_indent = 0
current_class = None
current_package = os.path.basename(mod_file) \
.split('.')[0] \
.split('-')[0] \
.replace('mod', '')
mod_desc = mod_file.replace('../micropython/extmod', 'extmod') mod_desc = mod_file.replace('../micropython/extmod', 'extmod')
for l in open(mod_file): for l in open(mod_file):
if not l.startswith(COMMENT_PREFIX): if not l.startswith(COMMENT_PREFIX):
continue continue
@ -69,13 +87,13 @@ def clear_directory(top_dir):
print("Clearing up directory", top_dir) print("Clearing up directory", top_dir)
for root, dirs, files in os.walk(top_dir, topdown=False): for root, dirs, files in os.walk(top_dir, topdown=False):
if '.mock-generated' not in files: if '.mock-generated' not in files:
#print("Not a mock directory", root) # print("Not a mock directory", root)
continue continue
for name in files: for name in files:
#print('Deleting file', os.path.join(root, name)) # print('Deleting file', os.path.join(root, name))
os.remove(os.path.join(root, name)) os.remove(os.path.join(root, name))
for name in dirs: for name in dirs:
#print('Deleting directory', os.path.join(root, name)) # print('Deleting directory', os.path.join(root, name))
try: try:
os.rmdir(os.path.join(root, name)) os.rmdir(os.path.join(root, name))
except FileNotFoundError: except FileNotFoundError:
@ -84,5 +102,5 @@ def clear_directory(top_dir):
os.rmdir(root) os.rmdir(root)
if __name__ == '__main__': if __name__ == '__main__':
clear_directory('../mocks') clear_directory('../mocks/generated')
build_directory('../micropython/extmod', '../mocks') build_directory('../micropython/extmod', '../mocks/generated')