mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
...
This commit is contained in:
parent
ec7e0a6862
commit
ed690b4376
45
prebuild.cpp
45
prebuild.cpp
@ -2,8 +2,8 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <cstdio>
|
||||
#include <dirent.h>
|
||||
#include <ctime>
|
||||
|
||||
std::string to_hex_string(const std::string& input) {
|
||||
@ -20,16 +20,34 @@ std::string to_hex_string(const std::string& input) {
|
||||
|
||||
std::map<std::string, std::string> generate_python_sources() {
|
||||
std::map<std::string, std::string> sources;
|
||||
|
||||
for (const auto& file : std::filesystem::directory_iterator("python")) {
|
||||
if (file.path().extension() == ".py") {
|
||||
std::string key = file.path().stem().string();
|
||||
std::ifstream f(file.path());
|
||||
std::string content((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
DIR *dir;
|
||||
struct dirent *ent;
|
||||
if ((dir = opendir ("python")) != NULL)
|
||||
{
|
||||
while ((ent = readdir (dir)) != NULL)
|
||||
{
|
||||
std::string filename = ent->d_name;
|
||||
size_t pos = filename.rfind(".py");
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
std::string key = filename.substr(0, filename.length() - 3);
|
||||
std::string filepath = "python/" + filename;
|
||||
FILE* file = fopen(filepath.c_str(), "r");
|
||||
if(file == NULL) exit(2);
|
||||
std::string content;
|
||||
char buffer[1024];
|
||||
while (fgets(buffer, sizeof(buffer), file) != NULL)
|
||||
{
|
||||
content += buffer;
|
||||
}
|
||||
fclose(file);
|
||||
sources[key] = to_hex_string(content);
|
||||
}
|
||||
}
|
||||
|
||||
closedir (dir);
|
||||
}else{
|
||||
exit(1);
|
||||
}
|
||||
return sources;
|
||||
}
|
||||
|
||||
@ -46,8 +64,8 @@ std::string generate_header(const std::map<std::string, std::string>& sources) {
|
||||
header += "\n#include <map>\n#include <string>\n\nnamespace pkpy{\n";
|
||||
header += " inline static std::map<std::string, std::string> kPythonLibs = {\n";
|
||||
|
||||
for (const auto& [key, value] : sources) {
|
||||
header += " {\"" + key + "\", \"" + value + "\"},\n";
|
||||
for (auto it=sources.begin(); it!=sources.end(); ++it) {
|
||||
header += " {\"" + it->first + "\", \"" + it->second + "\"},\n";
|
||||
}
|
||||
|
||||
header += " };\n";
|
||||
@ -59,7 +77,8 @@ std::string generate_header(const std::map<std::string, std::string>& sources) {
|
||||
int main() {
|
||||
auto sources = generate_python_sources();
|
||||
std::string header = generate_header(sources);
|
||||
std::ofstream header_file("src/_generated.h");
|
||||
header_file << header;
|
||||
FILE* f = fopen("src/_generated.h", "w");
|
||||
fprintf(f, "%s", header.c_str());
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
g++ -o prebuild --std=c++17 prebuild.cpp
|
||||
./prebuild
|
||||
rm prebuild
|
@ -8,9 +8,11 @@ def generate_python_sources():
|
||||
key = file.split(".")[0]
|
||||
with open("python/" + file) as f:
|
||||
value = f.read()
|
||||
value = value.encode('utf-8').hex(':')
|
||||
value = '\\x' + value.replace(':', '\\x')
|
||||
sources[key] = value
|
||||
value = value.encode('utf-8').hex()
|
||||
new_value = []
|
||||
for i in range(0, len(value), 2):
|
||||
new_value.append("\\x" + value[i:i+2])
|
||||
sources[key] = "".join(new_value)
|
||||
|
||||
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user