prebuild.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import os
  2. def get_sources():
  3. sources = {}
  4. for file in sorted(os.listdir("python")):
  5. if not file.endswith(".py"):
  6. continue
  7. key = file.split(".")[0]
  8. const_char_array = []
  9. with open("python/" + file) as f:
  10. specials = { 10: '\\n', 34: '\\"' }
  11. for c in f.read().encode('utf-8'):
  12. if c in specials:
  13. const_char_array.append(specials[c])
  14. elif c >= 32 and c <= 126 and c != 92:
  15. const_char_array.append(chr(c))
  16. else:
  17. const_char_array.append(f'\\x{c:02x}')
  18. const_char_array = ''.join(const_char_array)
  19. sources[key] = '"' + const_char_array + '"'
  20. return sources
  21. sources = get_sources()
  22. # use LF line endings instead of CRLF
  23. with open("include/pocketpy/common/_generated.h", "wt", encoding='utf-8', newline='\n') as f:
  24. data = '''#pragma once
  25. // generated by prebuild.py
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. '''
  30. for key in sorted(sources.keys()):
  31. value = sources[key]
  32. data += f'extern const char kPythonLibs_{key}[];\n'
  33. data += '''
  34. #ifdef __cplusplus
  35. } // extern "C"
  36. #endif
  37. '''
  38. f.write(data)
  39. with open("src/common/_generated.c", "wt", encoding='utf-8', newline='\n') as f:
  40. data = '''// generated by prebuild.py
  41. #include "pocketpy/common/_generated.h"
  42. '''
  43. for key in sorted(sources.keys()):
  44. value = sources[key]
  45. data += f'const char kPythonLibs_{key}[] = {value};\n'
  46. f.write(data)