Hugin trunk 0.1
Loading...
Searching...
No Matches
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
38namespace HuginQueue
39{
40 // build final command and execute it
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 };
53 };
54
56 {
57 return true;
58 };
59
61 {
62 return wxEscapeFilename(m_prog) + " " + m_args;
63 };
64
66 {
67 return m_comment;
68 };
69
70 // optional command, returns always true, even if process failed
72 {
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("OMP_NUM_THREADS", s);
91 };
92 // set temp dir
93 wxString tempDir = wxConfig::Get()->Read("tempDir", wxEmptyString);
94 if (!tempDir.IsEmpty())
95 {
96#ifdef UNIX_LIKE
97 wxSetEnv("TMPDIR", tempDir);
98#else
99 wxSetEnv("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)
130 {
131#if defined __WXMAC__ && defined MAC_SELF_CONTAINED_BUNDLE
134 CFRelease(filename);
135 if (fn == wxEmptyString)
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
149 {
150#if defined __WXMAC__ && defined MAC_SELF_CONTAINED_BUNDLE
151 if (config->Read(name + "/Custom", 0l))
152 {
153 wxString fn = config->Read(name + "/Exe", wxEmptyString);
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 == "exiftool")
164 {
167 {
168 return exiftoolDirPath + "/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 "exiftool";
174 };
175 };
176
179 CFRelease(filename);
180 if (fn == wxEmptyString)
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 + "/Custom", 0l))
188 {
189 wxString fn = config->Read(name + "/Exe", wxEmptyString);
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
204 pathlist.Add(bindir);
205 pathlist.AddEnvList("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
239 {
241 const wxString sep = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER);
242 s.Replace(sep, ".");
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 };
268 pathlist.Add(bindir);
269 pathlist.AddEnvList("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
282{
283 wxString s = setting->Read(name, defaultValue);
284 s = s.Trim(true).Trim(false);
285 return s;
286};
287
289{
291 if (text.IsEmpty())
292 {
293 return wxEmptyString;
294 }
295 else
296 {
297 return wxGetTranslation(text);
298 };
299};
300
303{
304 wxString tempDir = config->Read("tempDir", wxEmptyString);
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
basic classes and function for queuing commands in wxWidgets
wxString GetComment() const
Definition Executor.cpp:65
virtual wxString GetCommand() const
Definition Executor.cpp:60
virtual bool Execute(bool dryRun)
Definition Executor.cpp:41
virtual bool CheckReturnCode() const
Definition Executor.cpp:55
virtual bool Execute(bool dryRun)
Definition Executor.cpp:71
virtual bool CheckReturnCode() const
Definition Executor.cpp:77
implementation of huginApp Class
str wxEscapeFilename(const str &arg)
special escaping routine for CommandQueues
Definition Executor.h:79
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
void CleanQueue(CommandQueue *queue)
clean the queue, delete all entries, but not the queue itself
Definition Executor.cpp:118
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
wxString wxStringFromCDouble(double val, int precision)
convert double to wxString, it is always using a '.
Definition Executor.cpp:238
std::vector< NormalCommand * > CommandQueue
Definition Executor.h:61
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
wxString GetInternalProgram(const wxString &bindir, const wxString &name)
return path and name of external program, which comes bundled with Hugin
Definition Executor.cpp:129
const wxString GetConfigTempDir(const wxConfigBase *config)
return the temp dir from the preferences, ensure that it ends with path separator
Definition Executor.cpp:302
const wxString GetSettingString(wxConfigBase *setting, const wxString &name, const wxString defaultValue)
read a string from setting and remove all whitespaces
Definition Executor.cpp:281
wxString doubleTowxString(double d, int digits)
std::vector< deghosting::BImagePtr > threshold(const std::vector< deghosting::FImagePtr > &inputImages, const double threshold, const uint16_t flags)
Threshold function used for creating alpha masks for images.
Definition threshold.h:41
platform/compiler specific stuff.