<div dir="ltr"><div><div>Hey Jö, hey Dune list,<br><br></div>Doing a lot of C++/Python/CMake mixups in my work, I do not think it is the best solution to run python from C++. You could instead do a proper build system check for the python interpreter and provide a real python script under version control. The execution is then controlled by cmake, like this:<br><br></div>find_package(PythonInterp)<br><div class="gmail_extra">add_test(${PYTHON_EXECUTABLE} myscript.py)<br><br></div><div class="gmail_extra">With this approach you have only gained that you are now running C++ executables from python instead of the other way round. Still, you could completely separate the languages by having the test execute a subinstance of cmake in script mode which runs both the C++ and the pyhton parts and returns the test result. If you are interested, I can provide the implementation.<br><br></div><div class="gmail_extra">I do not think this is as that important for this task, but I wanted to initiate such discussion (because it will definitely come up again at some point).<br><br></div><div class="gmail_extra">Best,<br></div><div class="gmail_extra">Dominic<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 13, 2015 at 11:25 PM, Jö Fahlke <span dir="ltr"><<a href="mailto:jorrit@jorrit.de" target="_blank">jorrit@jorrit.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">New commit, appeared at Fri Mar 13 23:25:12 2015 +0100<br>
as part of the following ref changes:<br>
<br>
branch refs/heads/master updated from 2098a41 -> 74c2f6b<br>
<br>
Browsable version: <a href="http://cgit.dune-project.org/repositories/dune-grid/commit/?id=74c2f6b6ec3dfc760d2566154e379ca0c590f67f" target="_blank">http://cgit.dune-project.org/repositories/dune-grid/commit/?id=74c2f6b6ec3dfc760d2566154e379ca0c590f67f</a><br>
<br>
======================================================================<br>
<br>
commit 74c2f6b6ec3dfc760d2566154e379ca0c590f67f<br>
Author: Jö Fahlke <<a href="mailto:jorrit@jorrit.de">jorrit@jorrit.de</a>><br>
Date: Fri Mar 13 23:23:55 2015 +0100<br>
<br>
[vtktest][fs1539] Add missing header.<br>
<br>
This header should have been added in 6f5f178.<br>
<br>
dune/grid/io/file/test/checkvtkfile.hh | 119 +++++++++++++++++++++++++++++++++<br>
1 file changed, 119 insertions(+)<br>
create mode 100644 dune/grid/io/file/test/checkvtkfile.hh<br>
<br>
<br>
<br>
diff --git a/dune/grid/io/file/test/checkvtkfile.hh b/dune/grid/io/file/test/checkvtkfile.hh<br>
new file mode 100644<br>
index 0000000..8e9ca76<br>
--- /dev/null<br>
+++ b/dune/grid/io/file/test/checkvtkfile.hh<br>
@@ -0,0 +1,119 @@<br>
+#ifndef DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH<br>
+#define DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH<br>
+<br>
+#include <cstddef><br>
+#include <cstdlib><br>
+#include <iostream><br>
+#include <iomanip><br>
+#include <ios><br>
+#include <iostream><br>
+#include <ostream><br>
+#include <sstream><br>
+#include <string><br>
+<br>
+#include <sys/wait.h><br>
+<br>
+#include <dune/common/exceptions.hh><br>
+<br>
+// quote so the result can be used inside '...' in python<br>
+// quotes not included in the result<br>
+inline std::string pyq(const std::string &s)<br>
+{<br>
+ std::ostringstream result;<br>
+ for(std::size_t i = 0; i < s.size(); ++i)<br>
+ {<br>
+ char c = s[i];<br>
+ switch(c) {<br>
+ case '\'': result << "\\'"; break;<br>
+ case '\\': result << "\\\\"; break;<br>
+ case '\n': result << "\\n"; break;<br>
+ default:<br>
+ if(c < 32 || c >= 127)<br>
+ result << "\\x" << std::hex << std::setfill('0') << std::setw(2)<br>
+ << static_cast<int>(c);<br>
+ else<br>
+ result << c;<br>
+ }<br>
+ }<br>
+ return result.str();<br>
+}<br>
+<br>
+// quote so the result can be used inside '...' in the bourne shell<br>
+// quotes not included in the result<br>
+inline std::string shq(const std::string &s)<br>
+{<br>
+ std::ostringstream result;<br>
+ bool pend = false;<br>
+ for(std::size_t i = 0; i < s.size(); ++i)<br>
+ {<br>
+ char c = s[i];<br>
+ switch(c) {<br>
+ case '\0': DUNE_THROW(Dune::NotImplemented,<br>
+ "Can't pass \\0 through the shell");<br>
+ case '\'': result << (pend ? "" : "'") << "\\'"; pend = true; break;<br>
+ default: result << (pend ? "'" : "") << c; pend = false; break;<br>
+ }<br>
+ }<br>
+ if(pend) result << "'";<br>
+ return result.str();<br>
+}<br>
+<br>
+inline int runShell(const std::string &code)<br>
+{<br>
+ int result = std::system(code.c_str());<br>
+ // Avoid returning anything that is a multiple of 256, unless the return<br>
+ // value was 0. This way the return value can be directly used as an<br>
+ // argument to exit(), which usually interprets its argument modulo 256.<br>
+ if(WIFEXITED(result))<br>
+ return WEXITSTATUS(result);<br>
+ if(WIFSIGNALED(result))<br>
+ return WTERMSIG(result) + 256;<br>
+ else<br>
+ return 513;<br>
+}<br>
+<br>
+inline int runPython(const std::string &code)<br>
+{<br>
+ return runShell("python -c '"+shq(code)+"'");<br>
+}<br>
+<br>
+inline bool is_suffix(const std::string &s, const std::string &suffix)<br>
+{<br>
+ return s.size() >= suffix.size() &&<br>
+ s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0;<br>
+}<br>
+<br>
+inline int checkVTKFile(const std::string &name)<br>
+{<br>
+ static const bool havePythonVTK = (runPython("from vtk import *") == 0);<br>
+ if(!havePythonVTK)<br>
+ {<br>
+ std::cerr << "warning: python or python vtk module not available"<br>
+ << std::endl;<br>
+ std::cerr << "skip: " << name << std::endl;<br>
+ return 77;<br>
+ }<br>
+<br>
+ std::string reader;<br>
+ if (is_suffix(name, ".vtu")) reader = "vtkXMLUnstructuredGridReader";<br>
+ else if(is_suffix(name, ".pvtu")) reader = "vtkXMLPUnstructuredGridReader";<br>
+ else if(is_suffix(name, ".vtp")) reader = "vtkXMLPolyDataReader";<br>
+ else if(is_suffix(name, ".pvtp")) reader = "vtkXMLPPolyDataReader";<br>
+ else DUNE_THROW(Dune::NotImplemented,<br>
+ "Unknown vtk file extension: " << name);<br>
+<br>
+ std::cout << "Loading " << name << " using python vtk" << std::endl;<br>
+ std::string pycode =<br>
+ "from vtk import *;"<br>
+ "import sys;"<br>
+ "reader = "+reader+"();"<br>
+ "reader.SetFileName('"+pyq(name)+"');"<br>
+ "reader.Update();"<br>
+ // check that the number of of cells is > 0<br>
+ "sys.exit(not (reader.GetOutput().GetNumberOfCells() > 0));";<br>
+ int result = runPython(pycode);<br>
+ std::cout << (result == 0 ? "pass: " : "fail: ") << name << std::endl;<br>
+ return result;<br>
+}<br>
+<br>
+#endif // DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH<br>
<br>
_______________________________________________<br>
Dune-Commit mailing list<br>
<a href="mailto:Dune-Commit@dune-project.org">Dune-Commit@dune-project.org</a><br>
<a href="http://lists.dune-project.org/mailman/listinfo/dune-commit" target="_blank">http://lists.dune-project.org/mailman/listinfo/dune-commit</a><br>
</blockquote></div><br></div></div>