void run_post_process_scripts(const std::string &path, const PrintConfig &config) { if (config.post_process.values.empty()) return; config.setenv_(); auto gcode_file = boost::filesystem::path(path); if (! boost::filesystem::exists(gcode_file)) throw std::runtime_error(std::string("Post-processor can't find exported gcode file")); for (const std::string &scripts : config.post_process.values) { std::vector<std::string> lines; boost::split(lines, scripts, boost::is_any_of("\r\n")); for (std::string script : lines) { // Ignore empty post processing script lines. boost::trim(script); if (script.empty()) continue; BOOST_LOG_TRIVIAL(info) << "Executing script " << script << " on file " << path; std::string std_err; const int result = run_script(script, gcode_file.string(), std_err); if (result != 0) { const std::string msg = std_err.empty() ? (boost::format("Post-processing script %1% on file %2% failed.\nError code: %3%") % script % path % result).str() : (boost::format("Post-processing script %1% on file %2% failed.\nError code: %3%\nOutput:\n%4%") % script % path % result % std_err).str(); BOOST_LOG_TRIVIAL(error) << msg; throw std::runtime_error(msg); } } } }
void run_post_process_scripts(const std::string &path, const PrintConfig &config) { #if 0 // disable postprocessor if (config.post_process.values.empty()) return; config.setenv_(); auto gcode_file = boost::filesystem::path(path); if (! boost::filesystem::exists(gcode_file)) throw std::runtime_error(std::string("Post-processor can't find exported gcode file")); for (const std::string &scripts : config.post_process.values) { std::vector<std::string> lines; boost::split(lines, scripts, boost::is_any_of("\r\n")); for (std::string script : lines) { // Ignore empty post processing script lines. boost::trim(script); if (script.empty()) continue; BOOST_LOG_TRIVIAL(info) << "Executing script " << script << " on file " << path; #ifdef WIN32 int result = run_script_win32(script, gcode_file.string()); #else //FIXME testing existence of a script is risky, as the script line may contain the script and some additional command line parameters. // We would have to process the script line into parameters before testing for the existence of the command, the command may be looked up // in the PATH etc. if (! boost::filesystem::exists(boost::filesystem::path(script))) throw std::runtime_error(std::string("The configured post-processing script does not exist: ") + script); struct stat info; if (stat(script.c_str(), &info)) throw std::runtime_error(std::string("Cannot read information for post-processing script: ") + script); boost::filesystem::perms script_perms = boost::filesystem::status(script).permissions(); //if UID matches, check UID perm. else if GID matches, check GID perm. Otherwise check other perm. if (!(script_perms & ((info.st_uid == geteuid()) ? boost::filesystem::perms::owner_exe : ((info.st_gid == getegid()) ? boost::filesystem::perms::group_exe : boost::filesystem::perms::others_exe)))) throw std::runtime_error(std::string("The configured post-processing script is not executable: check permissions. ") + script); int result = boost::process::system(script, gcode_file); if (result < 0) BOOST_LOG_TRIVIAL(error) << "Script " << script << " on file " << path << " failed. Negative error code returned."; #endif } } #endif }