From f464d2ffd8574cc96db31ae2b4c67b3023d7a19b Mon Sep 17 00:00:00 2001 From: David Hendriks <davidhendriks93@gmail.com> Date: Sun, 10 Nov 2019 00:25:52 +0000 Subject: [PATCH] added functinality to write the shared libs to a tempdir and moved stuff into testing --- TODO.org | 3 ++- .../custom_logging_functions.py | 19 ++++++++++++++++++- custom_logging.c | 19 ------------------- .../run_system_with_custom_logging.py | 0 ...{testing.py => custom_logging_examples.py} | 17 +++++++++++------ tests_and_snippets/{test => test.py} | 0 6 files changed, 31 insertions(+), 27 deletions(-) delete mode 100644 custom_logging.c create mode 100644 testing_examples/run_system_with_custom_logging.py rename tests_and_snippets/{testing.py => custom_logging_examples.py} (69%) rename tests_and_snippets/{test => test.py} (100%) diff --git a/TODO.org b/TODO.org index 7c718ba74..bc11a5eab 100644 --- a/TODO.org +++ b/TODO.org @@ -150,7 +150,8 @@ That went very deep haha. alot of memory allocation stuff *** DONE Make new c function run_binary_with_custom_logging CLOSED: [2019-11-08 Fri 21:19] *** TODO Put in some new tests in the python test api -*** TODO Make sure the sharedlibs get written to the correct directory +*** DONE Make sure the sharedlibs get written to the correct directory + CLOSED: [2019-11-10 Sun 00:21] ** General: *** DONE Get a more reliable way of loading the default values (running a ./tbse echo or something?) CLOSED: [2019-10-29 Tue 17:44] diff --git a/binaryc_python_utils/custom_logging_functions.py b/binaryc_python_utils/custom_logging_functions.py index bfd3c2f98..5e95463b7 100644 --- a/binaryc_python_utils/custom_logging_functions.py +++ b/binaryc_python_utils/custom_logging_functions.py @@ -2,6 +2,7 @@ import os import textwrap import subprocess import socket +import tempfile # Functions for the automatic logging of stuff def autogen_C_logging_code(logging_dict): @@ -228,4 +229,20 @@ def compile_shared_lib(code, sourcefile_name, outfile_name): shell=True) if res: - print('Output of compilation command:\n{}'.format(res)) \ No newline at end of file + print('Output of compilation command:\n{}'.format(res)) + + +def temp_custom_logging_dir(): + """ + Function to return the path the custom logging library shared object and script will be written to. + + Makes use of os.makedirs exist_ok which requires python 3.2+ + """ + + tmp_dir = tempfile.gettempdir() + path = os.path.join(tmp_dir, 'binary_c_python') + + # + os.makedirs(path, exist_ok=True) + + return path \ No newline at end of file diff --git a/custom_logging.c b/custom_logging.c deleted file mode 100644 index 57b6574fa..000000000 --- a/custom_logging.c +++ /dev/null @@ -1,19 +0,0 @@ -#pragma push_macro("MAX") -#pragma push_macro("MIN") -#undef MAX -#undef MIN -#include "binary_c.h" - -// add visibility __attribute__ ((visibility ("default"))) to it -void binary_c_API_function custom_output_function(struct stardata_t * stardata); -void binary_c_API_function custom_output_function(struct stardata_t * stardata) -{ - // struct stardata_t * stardata = (struct stardata_t *)x; - Printf("MY_STELLAR_DATA %g %g\n",((double)stardata->model.time),((double)stardata->star[0].mass)); -Printf("my_sss2 %g %g\n",((double)stardata->model.time),((double)stardata->star[1].mass));; -} - -#undef MAX -#undef MIN -#pragma pop_macro("MIN") -#pragma pop_macro("MAX") \ No newline at end of file diff --git a/testing_examples/run_system_with_custom_logging.py b/testing_examples/run_system_with_custom_logging.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests_and_snippets/testing.py b/tests_and_snippets/custom_logging_examples.py similarity index 69% rename from tests_and_snippets/testing.py rename to tests_and_snippets/custom_logging_examples.py index 418e4486a..5bd5b9ea4 100644 --- a/tests_and_snippets/testing.py +++ b/tests_and_snippets/custom_logging_examples.py @@ -1,6 +1,8 @@ import ctypes +import tempfile +import os -from binaryc_python_utils.custom_logging_functions import autogen_C_logging_code, binary_c_log_code, compile_shared_lib +from binaryc_python_utils.custom_logging_functions import autogen_C_logging_code, binary_c_log_code, compile_shared_lib, temp_custom_logging_dir import binary_c # generate logging lines @@ -12,20 +14,25 @@ logging_line = autogen_C_logging_code( ) # Generate code around logging lines -created_code = binary_c_log_code(logging_line) +custom_logging_code = binary_c_log_code(logging_line) # -compile_shared_lib(created_code, sourcefile_name='custom_logging.c', outfile_name='libcustom_logging.so') +compile_shared_lib(created_code, + sourcefile_name=os.path.join(temp_custom_logging_dir(), 'custom_logging.c'), + outfile_name=os.path.join(temp_custom_logging_dir(), 'libcustom_logging.so') + ) # Loading library dll1 = ctypes.CDLL('libgslcblas.so', mode=ctypes.RTLD_GLOBAL) dll2 = ctypes.CDLL('libgsl.so', mode=ctypes.RTLD_GLOBAL) dll3 = ctypes.CDLL('libbinary_c.so', mode=ctypes.RTLD_GLOBAL) -libmean = ctypes.CDLL("libcustom_logging.so", mode=1) # loads the shared library +libmean = ctypes.CDLL(os.path.join(temp_custom_logging_dir(), 'libcustom_logging.so'), + mode=ctypes.RTLD_GLOBAL) # loads the shared library # Get memory adress of function. mimicking a pointer mem = ctypes.cast(libmean.custom_output_function, ctypes.c_void_p).value +# m1 = 15.0 # Msun m2 = 14.0 # Msun separation = 0 # 0 = ignored, use period @@ -38,7 +45,5 @@ argstring = "binary_c M_1 {0:g} M_2 {1:g} separation {2:g} orbital_period {3:g} output = binary_c.run_binary_custom_logging(argstring, mem) # output = binary_c.run_binary(argstring) - -# print ("\n\nBinary_c output:\n\n") print (output) diff --git a/tests_and_snippets/test b/tests_and_snippets/test.py similarity index 100% rename from tests_and_snippets/test rename to tests_and_snippets/test.py -- GitLab