pocketpy/prebuild.py
2024-03-24 17:39:41 +08:00

34 lines
913 B
Python

import os
def generate_python_sources():
sources = {}
for file in sorted(os.listdir("python")):
if not file.endswith(".py"):
continue
key = file.split(".")[0]
with open("python/" + file) as f:
value = f.read()
const_array = ','.join([str(b) for b in value.encode('utf-8')])
sources[key] = '(const char[]){' + const_array + '}'
header = '''#pragma once
// generated by prebuild.py
#include <map>
#include <string>
namespace pkpy{
inline const std::map<std::string, const char*> kPythonLibs = {
'''
for key, value in sources.items():
header += f' {{ "{key}", {value} }},\n'
header += ''' };
} // namespace pkpy
'''
return header
# use LF line endings instead of CRLF
with open("include/pocketpy/_generated.h", "wt", encoding='utf-8', newline='\n') as f:
f.write(generate_python_sources())