[Dune-devel] [Dune-Commit] [Commit] dune-grid - 74c2f6b: [vtktest][fs1539] Add missing header.

Dominic Kempf dominic.r.kempf at gmail.com
Mon Mar 16 11:19:54 CET 2015


Hey Jö, hey Dune list,

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:

find_package(PythonInterp)
add_test(${PYTHON_EXECUTABLE} myscript.py)

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.

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).

Best,
Dominic

On Fri, Mar 13, 2015 at 11:25 PM, Jö Fahlke <jorrit at jorrit.de> wrote:

> New commit, appeared at Fri Mar 13 23:25:12 2015 +0100
> as part of the following ref changes:
>
>     branch refs/heads/master    updated from 2098a41 -> 74c2f6b
>
> Browsable version:
> http://cgit.dune-project.org/repositories/dune-grid/commit/?id=74c2f6b6ec3dfc760d2566154e379ca0c590f67f
>
> ======================================================================
>
> commit 74c2f6b6ec3dfc760d2566154e379ca0c590f67f
> Author: Jö Fahlke <jorrit at jorrit.de>
> Date:   Fri Mar 13 23:23:55 2015 +0100
>
>     [vtktest][fs1539] Add missing header.
>
>     This header should have been added in 6f5f178.
>
>  dune/grid/io/file/test/checkvtkfile.hh | 119
> +++++++++++++++++++++++++++++++++
>  1 file changed, 119 insertions(+)
>  create mode 100644 dune/grid/io/file/test/checkvtkfile.hh
>
>
>
> diff --git a/dune/grid/io/file/test/checkvtkfile.hh
> b/dune/grid/io/file/test/checkvtkfile.hh
> new file mode 100644
> index 0000000..8e9ca76
> --- /dev/null
> +++ b/dune/grid/io/file/test/checkvtkfile.hh
> @@ -0,0 +1,119 @@
> +#ifndef DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH
> +#define DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH
> +
> +#include <cstddef>
> +#include <cstdlib>
> +#include <iostream>
> +#include <iomanip>
> +#include <ios>
> +#include <iostream>
> +#include <ostream>
> +#include <sstream>
> +#include <string>
> +
> +#include <sys/wait.h>
> +
> +#include <dune/common/exceptions.hh>
> +
> +// quote so the result can be used inside '...' in python
> +// quotes not included in the result
> +inline std::string pyq(const std::string &s)
> +{
> +  std::ostringstream result;
> +  for(std::size_t i = 0; i < s.size(); ++i)
> +  {
> +    char c = s[i];
> +    switch(c) {
> +    case '\'': result << "\\'";  break;
> +    case '\\': result << "\\\\"; break;
> +    case '\n': result << "\\n";  break;
> +    default:
> +      if(c < 32 || c >= 127)
> +        result << "\\x" << std::hex << std::setfill('0') << std::setw(2)
> +               << static_cast<int>(c);
> +      else
> +        result << c;
> +    }
> +  }
> +  return result.str();
> +}
> +
> +// quote so the result can be used inside '...' in the bourne shell
> +// quotes not included in the result
> +inline std::string shq(const std::string &s)
> +{
> +  std::ostringstream result;
> +  bool pend = false;
> +  for(std::size_t i = 0; i < s.size(); ++i)
> +  {
> +    char c = s[i];
> +    switch(c) {
> +    case '\0': DUNE_THROW(Dune::NotImplemented,
> +                          "Can't pass \\0 through the shell");
> +    case '\'': result << (pend ? "" : "'") << "\\'"; pend = true;  break;
> +    default:   result << (pend ? "'" : "") << c;     pend = false; break;
> +    }
> +  }
> +  if(pend) result << "'";
> +  return result.str();
> +}
> +
> +inline int runShell(const std::string &code)
> +{
> +  int result = std::system(code.c_str());
> +  // Avoid returning anything that is a multiple of 256, unless the return
> +  // value was 0.  This way the return value can be directly used as an
> +  // argument to exit(), which usually interprets its argument modulo 256.
> +  if(WIFEXITED(result))
> +    return WEXITSTATUS(result);
> +  if(WIFSIGNALED(result))
> +    return WTERMSIG(result) + 256;
> +  else
> +    return 513;
> +}
> +
> +inline int runPython(const std::string &code)
> +{
> +  return runShell("python -c '"+shq(code)+"'");
> +}
> +
> +inline bool is_suffix(const std::string &s, const std::string &suffix)
> +{
> +  return s.size() >= suffix.size() &&
> +    s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0;
> +}
> +
> +inline int checkVTKFile(const std::string &name)
> +{
> +  static const bool havePythonVTK = (runPython("from vtk import *") == 0);
> +  if(!havePythonVTK)
> +  {
> +    std::cerr << "warning: python or python vtk module not available"
> +              << std::endl;
> +    std::cerr << "skip: " << name << std::endl;
> +    return 77;
> +  }
> +
> +  std::string reader;
> +  if     (is_suffix(name, ".vtu"))  reader =
> "vtkXMLUnstructuredGridReader";
> +  else if(is_suffix(name, ".pvtu")) reader =
> "vtkXMLPUnstructuredGridReader";
> +  else if(is_suffix(name, ".vtp"))  reader = "vtkXMLPolyDataReader";
> +  else if(is_suffix(name, ".pvtp")) reader = "vtkXMLPPolyDataReader";
> +  else DUNE_THROW(Dune::NotImplemented,
> +                  "Unknown vtk file extension: " << name);
> +
> +  std::cout << "Loading " << name << " using python vtk" << std::endl;
> +  std::string pycode =
> +    "from vtk import *;"
> +    "import sys;"
> +    "reader = "+reader+"();"
> +    "reader.SetFileName('"+pyq(name)+"');"
> +    "reader.Update();"
> +    // check that the number of of cells is > 0
> +    "sys.exit(not (reader.GetOutput().GetNumberOfCells() > 0));";
> +  int result = runPython(pycode);
> +  std::cout << (result == 0 ? "pass: " : "fail: ") << name << std::endl;
> +  return result;
> +}
> +
> +#endif // DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH
>
> _______________________________________________
> Dune-Commit mailing list
> Dune-Commit at dune-project.org
> http://lists.dune-project.org/mailman/listinfo/dune-commit
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.dune-project.org/pipermail/dune-devel/attachments/20150316/e13e28d0/attachment.htm>


More information about the Dune-devel mailing list