Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Executor.cpp
Go to the documentation of this file.
1 
8 /* This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This software is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this software. If not, see
20 * <http://www.gnu.org/licenses/>.
21 *
22 */
23 
24 #include "Executor.h"
25 #include "hugin_config.h"
26 
27 #include <iostream>
28 #include <wx/utils.h>
29 #include <wx/config.h>
30 #include <wx/filename.h>
31 #include <wx/log.h>
32 #include <wx/translation.h>
33 #if defined __WXMAC__ && defined MAC_SELF_CONTAINED_BUNDLE
34 #include "base_wx/platform.h"
35 #endif
36 #include "base_wx/wxPlatform.h"
37 
38 namespace HuginQueue
39 {
40  // build final command and execute it
41  bool NormalCommand::Execute(bool dryRun)
42  {
43  if (dryRun)
44  {
45  std::cout << GetCommand().mb_str(wxConvLocal) << std::endl;
46  return true;
47  }
48  if (!m_comment.IsEmpty())
49  {
50  std::cout << std::endl << m_comment.mb_str(wxConvLocal) << std::endl;
51  };
52  return wxExecute(GetCommand(), wxEXEC_SYNC | wxEXEC_MAKE_GROUP_LEADER) == 0l;
53  };
54 
56  {
57  return true;
58  };
59 
60  wxString NormalCommand::GetCommand() const
61  {
62  return wxEscapeFilename(m_prog) + wxT(" ") + m_args;
63  };
64 
65  wxString NormalCommand::GetComment() const
66  {
67  return m_comment;
68  };
69 
70  // optional command, returns always true, even if process failed
71  bool OptionalCommand::Execute(bool dryRun)
72  {
73  NormalCommand::Execute(dryRun);
74  return true;
75  };
76 
78  {
79  return false;
80  };
81 
82  // execute the command queue
83  bool RunCommandsQueue(CommandQueue* queue, size_t threads, bool dryRun)
84  {
85  // set OMP_NUM_THREADS to limit number of threads in OpenMP programs
86  if (threads > 0)
87  {
88  wxString s;
89  s << threads;
90  wxSetEnv(wxT("OMP_NUM_THREADS"), s);
91  };
92  // set temp dir
93  wxString tempDir = wxConfig::Get()->Read(wxT("tempDir"), wxT(""));
94  if (!tempDir.IsEmpty())
95  {
96 #ifdef UNIX_LIKE
97  wxSetEnv(wxT("TMPDIR"), tempDir);
98 #else
99  wxSetEnv(wxT("TMP"), tempDir);
100 #endif
101  };
102  bool isSuccessful = true;
103  size_t i = 0;
104  // prevent displaying message box if wxExecute failed
105  wxLogStream log(&std::cerr);
106  // final execute the commands
107  while (isSuccessful && i < queue->size())
108  {
109  isSuccessful = (*queue)[i]->Execute(dryRun);
110  ++i;
111  };
112  // clean up queue
113  CleanQueue(queue);
114  delete queue;
115  return isSuccessful;
116  };
117 
119  {
120  while (!queue->empty())
121  {
122  delete queue->back();
123  queue->pop_back();
124  };
125  };
126 
127 
128  // return path in internal program (program that is shipped with Hugin)
129  wxString GetInternalProgram(const wxString& bindir, const wxString& name)
130  {
131 #if defined __WXMAC__ && defined MAC_SELF_CONTAINED_BUNDLE
132  CFStringRef filename = MacCreateCFStringWithWxString(name);
133  wxString fn = MacGetPathToBundledExecutableFile(filename);
134  CFRelease(filename);
135  if (fn == wxT(""))
136  {
137  std::cerr << wxString::Format(_("External program %s not found in the bundle, reverting to system path"), name.c_str()) << std::endl;
138  return name;
139  }
140  return fn;
141 #else
142  return bindir + name;
143 #endif
144  };
145 
146  // return name of external program (can be program bundeled with Hugin, or an external program
147  // as specified in preferences
148  wxString GetExternalProgram(wxConfigBase * config, const wxString& bindir, const wxString& name)
149  {
150 #if defined __WXMAC__ && defined MAC_SELF_CONTAINED_BUNDLE
151  if (config->Read(name + wxT("/Custom"), 0l))
152  {
153  wxString fn = config->Read(name + wxT("/Exe"), wxT(""));
154  if (wxFileName::FileExists(fn))
155  {
156  return fn;
157  }
158  else
159  {
160  std::cerr << wxString::Format(_("WARNING: External program %s not found as specified in preferences, reverting to bundled version"), fn.c_str()) << std::endl;
161  };
162  };
163  if (name == wxT("exiftool"))
164  {
165  wxString exiftoolDirPath = MacGetPathToBundledResourceFile(CFSTR("ExifTool"));
166  if (exiftoolDirPath != wxT(""))
167  {
168  return exiftoolDirPath + wxT("/exiftool");
169  }
170  else
171  {
172  std::cerr << wxString::Format(_("WARNING: External program %s not found in the bundle, reverting to system path"), name.c_str()) << std::endl;
173  return wxT("exiftool");
174  };
175  };
176 
177  CFStringRef filename = MacCreateCFStringWithWxString(name);
178  wxString fn = MacGetPathToBundledExecutableFile(filename);
179  CFRelease(filename);
180  if (fn == wxT(""))
181  {
182  std::cerr << wxString::Format(_("WARNING: External program %s not found in the bundle, reverting to system path"), name.c_str()) << std::endl;
183  return name;
184  };
185  return fn;
186 #else
187  if (config->Read(name + wxT("/Custom"), 0l))
188  {
189  wxString fn = config->Read(name + wxT("/Exe"), wxT(""));
190  if (!fn.IsEmpty())
191  {
192  wxFileName prog(fn);
193  if (prog.IsAbsolute())
194  {
195  if (prog.FileExists())
196  {
197  return fn;
198  };
199  }
200  else
201  {
202  // search in PATH
203  wxPathList pathlist;
204  pathlist.Add(bindir);
205  pathlist.AddEnvList(wxT("PATH"));
206  fn = pathlist.FindAbsoluteValidPath(fn);
207  if (!fn.IsEmpty())
208  {
209  return fn;
210  };
211  };
212  std::cerr << wxString::Format(_("WARNING: External program %s not found as specified in preferences, reverting to bundled version"), name.c_str()) << std::endl;
213  };
214  };
215  // no user specified program or not found
216 #ifdef __WXMSW__
217  // on Windows assume prog is bundled in program dir
218  return bindir + name;
219 #elif defined UNIX_SELF_CONTAINED_BUNDLE
220  // for AppImage check if program is in it
221  if (wxFileName::FileExists(bindir + name))
222  {
223  // program found in AppImage, so return path in AppImage
224  return bindir + name;
225  }
226  else
227  {
228  // not in AppImage, assume it is in PATH of user
229  return name;
230  };
231 #else
232  // on Unix simply return name, assume it is in the path
233  return name;
234 #endif
235 #endif
236  };
237 
238  wxString wxStringFromCDouble(double val, int precision)
239  {
240  wxString s = hugin_utils::doubleTowxString(val, precision);
241  const wxString sep = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER);
242  s.Replace(sep, wxT("."));
243  return s;
244  };
245 
246 #ifdef __WXMSW__
247  // search for executable in program folder and in PATH
248  // add exe extension if no one is given
249  wxString MSWGetProgname(const wxString& bindir, const wxString& name)
250  {
251  wxFileName prog(name);
252  if (prog.IsAbsolute())
253  {
254  if (prog.FileExists())
255  {
256  return prog.GetFullPath();
257  };
258  }
259  else
260  {
261  // search in program folder and in PATH
262  const bool hasExt = prog.HasExt();
263  if (!prog.HasExt())
264  {
265  prog.SetExt("exe");
266  };
267  wxPathList pathlist;
268  pathlist.Add(bindir);
269  pathlist.AddEnvList(wxT("PATH"));
270  const wxString fullName = pathlist.FindAbsoluteValidPath(prog.GetFullName());
271  if (!fullName.IsEmpty())
272  {
273  return fullName;
274  };
275  };
276  return name;
277  };
278 #endif
279 
280 // read a string from setting and remove all whitespaces
281 const wxString GetSettingString(wxConfigBase* setting, const wxString& name, const wxString defaultValue)
282 {
283  wxString s = setting->Read(name, defaultValue);
284  s = s.Trim(true).Trim(false);
285  return s;
286 };
287 
288 const wxString GetSettingStringTranslated(wxConfigBase* setting, const wxString& name, const wxString defaultValue)
289 {
290  const wxString text = GetSettingString(setting, name, defaultValue);
291  if (text.IsEmpty())
292  {
293  return wxEmptyString;
294  }
295  else
296  {
297  return wxGetTranslation(text);
298  };
299 };
300 
302 const wxString GetConfigTempDir(const wxConfigBase* config)
303 {
304  wxString tempDir = config->Read(wxT("tempDir"), wxT(""));
305  if (!tempDir.IsEmpty())
306  {
307  if (tempDir.Last() != wxFileName::GetPathSeparator())
308  {
309  tempDir.Append(wxFileName::GetPathSeparator());
310  }
311  };
312  return tempDir;
313 };
314 
315 }; // namespace
const wxString GetSettingString(wxConfigBase *setting, const wxString &name, const wxString defaultValue)
read a string from setting and remove all whitespaces
Definition: Executor.cpp:281
implementation of huginApp Class
bool FileExists(const std::string &filename)
checks if file exists
Definition: utils.cpp:362
virtual wxString GetCommand() const
Definition: Executor.cpp:60
const wxString GetConfigTempDir(const wxConfigBase *config)
return the temp dir from the preferences, ensure that it ends with path separator ...
Definition: Executor.cpp:302
wxString doubleTowxString(double d, int digits)
Definition: wxPlatform.cpp:31
wxString GetInternalProgram(const wxString &bindir, const wxString &name)
return path and name of external program, which comes bundled with Hugin
Definition: Executor.cpp:129
basic classes and function for queuing commands in wxWidgets
bool RunCommandsQueue(CommandQueue *queue, size_t threads, bool dryRun)
execute the given, set environment variable OMP_NUM_THREADS to threads (ignored for 0) after running ...
Definition: Executor.cpp:83
virtual bool Execute(bool dryRun)
Definition: Executor.cpp:41
void CleanQueue(CommandQueue *queue)
clean the queue, delete all entries, but not the queue itself
Definition: Executor.cpp:118
vigra::RGBValue< T, RIDX, GIDX, BIDX > log(vigra::RGBValue< T, RIDX, GIDX, BIDX > const &v)
component-wise logarithm
Definition: utils.h:209
wxString GetComment() const
Definition: Executor.cpp:65
const wxString GetSettingStringTranslated(wxConfigBase *setting, const wxString &name, const wxString defaultValue)
read a translated string from settings and remove all whitespaces
Definition: Executor.cpp:288
str wxEscapeFilename(const str &arg)
special escaping routine for CommandQueues
Definition: Executor.h:79
virtual bool Execute(bool dryRun)
Definition: Executor.cpp:71
platform/compiler specific stuff.
virtual bool CheckReturnCode() const
Definition: Executor.cpp:77
wxString GetExternalProgram(wxConfigBase *config, const wxString &bindir, const wxString &name)
return path and name of external program, which can be overwritten by the user
Definition: Executor.cpp:148
virtual bool CheckReturnCode() const
Definition: Executor.cpp:55
std::vector< NormalCommand * > CommandQueue
Definition: Executor.h:61
wxString wxStringFromCDouble(double val, int precision)
convert double to wxString, it is always using a &#39;.
Definition: Executor.cpp:238