bundle.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. from os import path
  3. import sys
  4. import re
  5. def process(file_path, indent = '', const_list = ''):
  6. f = open(file_path)
  7. dirname = path.dirname(path.realpath(file_path))
  8. lines = f.readlines()
  9. for line in lines:
  10. match = re.match(r"^([ \t]*)'<include> ([^']+)';", line)
  11. match_const = re.match(r"^([ \t]*)'<constants>';", line)
  12. if match:
  13. module_indent = match.group(1)
  14. module_file = match.group(2)
  15. process(path.join(dirname, module_file), indent + module_indent)
  16. elif match_const:
  17. const_indent = match_const.group(1)
  18. for statement in const_list:
  19. print(const_indent + statement)
  20. print('')
  21. else:
  22. print(indent + line, end='')
  23. f.close()
  24. def main():
  25. entry_file = sys.argv[1]
  26. const_file = sys.argv[2]
  27. const_table = {}
  28. const_values = {}
  29. with open(const_file) as f:
  30. for line in f.readlines():
  31. match = re.match (
  32. r'^[ \t]*(const)*[ \t]*([A-Z_]+) = "([A-Za-z0-9_]+)"',
  33. line
  34. )
  35. if match:
  36. name = match.group(2)
  37. value = match.group(3)
  38. if const_values.get(value, False):
  39. print('Duplicate Const Value: %s' % value, file=sys.stderr)
  40. sys.exit(1)
  41. const_table[name] = value
  42. const_values[value] = True
  43. const_list = []
  44. for name, value in const_table.items():
  45. const_list.append("const %s = '%s'" % (name, value))
  46. process(entry_file, const_list=const_list)
  47. main()