1
0

prebuild.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import os
  2. from datetime import datetime
  3. def generate_python_sources():
  4. sources = {}
  5. for file in os.listdir("python"):
  6. if not file.endswith(".py"):
  7. continue
  8. key = file.split(".")[0]
  9. with open("python/" + file) as f:
  10. value = f.read()
  11. value = value.encode('utf-8').hex()
  12. new_value = []
  13. for i in range(0, len(value), 2):
  14. new_value.append("\\x" + value[i:i+2])
  15. sources[key] = "".join(new_value)
  16. timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  17. header = '''#pragma once
  18. // generated on ''' + timestamp + '''
  19. #include <map>
  20. #include <string>
  21. namespace pkpy{
  22. inline static std::map<std::string, const char*> kPythonLibs = {
  23. '''
  24. for key, value in sources.items():
  25. CHAR_LIMIT = 5000
  26. value = ['"' + value[i:i+CHAR_LIMIT] + '" ' for i in range(0, len(value), CHAR_LIMIT)]
  27. value = ''.join(value)
  28. header += ' '*8 + '{"' + key + '", ' + value + '},'
  29. header += '\n'
  30. header += '''
  31. };
  32. } // namespace pkpy
  33. '''
  34. return header
  35. with open("include/pocketpy/_generated.h", "w", encoding='utf-8') as f:
  36. f.write(generate_python_sources())