Skip to content

Commit 83c74dd

Browse files
committed
Read C++ and Makefile files into Python file
This commit provides a utility to read the C++ and Makefile files into a Python file so that the code can be updated via a script when a new C++ version is available. This is a halfway house to automatically generating the C++, as it is not possible to create a shared object library directly from Python. Also updates .gitignore to ignore compiled Python files.
1 parent 89f5d96 commit 83c74dd

4 files changed

Lines changed: 607 additions & 550 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@
3030
*.exe
3131
*.out
3232
*.app
33+
34+
# Compiled Python
35+
*.pyc
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
3+
plasma_source_cpp = "plasma_source.cpp"
4+
plasma_source_hpp = "plasma_source.hpp"
5+
source_sampling_cpp = "source_sampling.cpp"
6+
make_file = "Makefile"
7+
8+
py_file = "source.py"
9+
10+
if os.path.exists(py_file):
11+
os.remove(py_file)
12+
13+
def write_disclaimer(py_file_path):
14+
disclaimer = "\"\"\"\n"
15+
disclaimer += "Auto-generated file. To edit, run `python build_python.py`.\n"
16+
disclaimer += "This will need to be run whenever a new C++ version is made available.\n"
17+
disclaimer += "\"\"\"\n\n"
18+
with open(py_file_path, "a+") as f_py:
19+
f_py.write(disclaimer)
20+
21+
def generate_variable(cpp_file_path, variable_name, py_file_path, end_str="\n\n"):
22+
with open(cpp_file_path, "r") as f_cpp:
23+
lines = f_cpp.readlines()
24+
py_lines = variable_name
25+
py_lines += " = (\n\"\"\""
26+
py_lines += "".join(lines)
27+
py_lines += "\"\"\"\n)"
28+
py_lines += end_str
29+
with open(py_file_path, "a+") as f_py:
30+
f_py.write(py_lines)
31+
32+
write_disclaimer(py_file)
33+
generate_variable(source_sampling_cpp, "source_sampling_cpp", py_file)
34+
generate_variable(plasma_source_cpp, "plasma_source_cpp", py_file)
35+
generate_variable(plasma_source_hpp, "plasma_source_hpp", py_file)
36+
generate_variable(make_file, "make_file", py_file, "\n")

0 commit comments

Comments
 (0)