prebuild.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import os
  2. def generate_python_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. with open("python/" + file) as f:
  9. value = f.read()
  10. value = value.encode('utf-8').hex()
  11. new_value = []
  12. for i in range(0, len(value), 2):
  13. new_value.append("\\x" + value[i:i+2])
  14. sources[key] = "".join(new_value)
  15. header = '''#pragma once
  16. // generated by prebuild.py
  17. #include <map>
  18. #include <string>
  19. namespace pkpy{
  20. inline static std::map<std::string, const char*> kPythonLibs = {
  21. '''
  22. for key, value in sources.items():
  23. CHAR_LIMIT = 5000
  24. value = ['"' + value[i:i+CHAR_LIMIT] + '" ' for i in range(0, len(value), CHAR_LIMIT)]
  25. value = ''.join(value)
  26. header += ' '*8 + '{"' + key + '", ' + value + '},'
  27. header += '\n'
  28. header += '''
  29. };
  30. } // namespace pkpy
  31. '''
  32. return header
  33. # use LF line endings instead of CRLF
  34. with open("include/pocketpy/_generated.h", "wt", encoding='utf-8', newline='\n') as f:
  35. f.write(generate_python_sources())