|
| 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 | +def write_disclaimer(py_file_path): |
| 11 | + disclaimer = "\"\"\"\n" |
| 12 | + disclaimer += "Auto-generated file. To edit, run `python build_python.py`.\n" |
| 13 | + disclaimer += "This will need to be run whenever a new C++ version is made available.\n" |
| 14 | + disclaimer += "\"\"\"\n\n" |
| 15 | + with open(py_file_path, "a+") as f_py: |
| 16 | + f_py.write(disclaimer) |
| 17 | + |
| 18 | +def generate_variable(cpp_file_path, variable_name, py_file_path, end_str="\n\n"): |
| 19 | + with open(cpp_file_path, "r") as f_cpp: |
| 20 | + lines = f_cpp.readlines() |
| 21 | + py_lines = variable_name |
| 22 | + py_lines += " = (\n\"\"\"" |
| 23 | + py_lines += "".join(lines) |
| 24 | + py_lines += "\"\"\"\n)" |
| 25 | + py_lines += end_str |
| 26 | + with open(py_file_path, "a+") as f_py: |
| 27 | + f_py.write(py_lines) |
| 28 | + |
| 29 | +def build(source_dir="."): |
| 30 | + output_path = os.path.join(source_dir, py_file) |
| 31 | + |
| 32 | + if os.path.exists(output_path): |
| 33 | + os.remove(output_path) |
| 34 | + |
| 35 | + write_disclaimer(output_path) |
| 36 | + generate_variable( |
| 37 | + os.path.join(source_dir, source_sampling_cpp), |
| 38 | + "source_sampling_cpp", |
| 39 | + output_path |
| 40 | + ) |
| 41 | + generate_variable( |
| 42 | + os.path.join(source_dir, plasma_source_cpp), |
| 43 | + "plasma_source_cpp", |
| 44 | + output_path |
| 45 | + ) |
| 46 | + generate_variable( |
| 47 | + os.path.join(source_dir, plasma_source_hpp), |
| 48 | + "plasma_source_hpp", |
| 49 | + output_path |
| 50 | + ) |
| 51 | + generate_variable( |
| 52 | + os.path.join(source_dir, make_file), |
| 53 | + "make_file", |
| 54 | + output_path, |
| 55 | + "\n" |
| 56 | + ) |
0 commit comments