Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
execute.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file src/basic/execute.hh
11 /// @brief Various functions to run external executables
12 /// @author Sergey Lyskov
13 ///
14 /// Note: this code is placed in to 'basic' level instead of utility because we want to have access to Tracers so verbosity of all external calls could be controlled
15 
16 
17 #include <basic/execute.hh>
18 
19 #include <basic/Tracer.hh>
20 #include <utility/exit.hh>
21 
22 
23 #include <iostream>
24 
25 namespace basic {
26 
27 static THREAD_LOCAL basic::Tracer TR("basic.execute");
28 
29 #ifdef WIN32
30 #define popen _popen
31 #define pclose _pclose
32 #endif
33 
34 ExecutionResult execute(std::string const & message, std::string const & command_line, bool terminate_on_failure, bool silent)
35 {
37 
38  if ( !silent ) TR << message << ' ' << command_line << std::endl; // TR.flush();
39 
40  FILE *pipe = popen( (command_line+ " 2>&1" ).c_str(), "r"); // we need to redirect std error in to std output so it will got captured by pipe...
41 
42  if ( pipe ) {
43  char buffer[256];
44 
45  while ( !feof(pipe) ) {
46  if ( fgets(buffer, 256, pipe) != NULL ) r.output += buffer;
47  }
48 
49  r.result = pclose(pipe);
50  } else {
51  r.result = 1;
52  r.output = "Could not open pipe!";
53  }
54 
55  if ( terminate_on_failure && r.result ) utility_exit_with_message("basic.execute encounter error while runnning " + command_line+ '\n'+ r.output +"\nExiting...");
56  return r;
57 }
58 
59 
60 } // namespace basic
#define utility_exit_with_message(m)
Exit with file + line + message.
Definition: exit.hh:47
#define THREAD_LOCAL
Various functions to run external executables.
std::string output
Definition: execute.hh:28
ExecutionResult execute(std::string const &message, std::string const &command_line, bool terminate_on_failure, bool silent)
excute provided command_line though shell and return exit_code and output
Definition: execute.cc:34
static THREAD_LOCAL basic::Tracer TR("basic.execute")
Program exit functions and macros.
Tracer IO system.
Struct that hold result code + output of external process.
Definition: execute.hh:25
Class for handling user debug/warnings/errors. Use instance of this class instead of 'std::cout' for ...
Definition: Tracer.hh:134
int const silent
Named verbosity levels.