Executor.cpp

Go to the documentation of this file.
00001 
00008 /*  This is free software; you can redistribute it and/or
00009 *  modify it under the terms of the GNU General Public
00010 *  License as published by the Free Software Foundation; either
00011 *  version 2 of the License, or (at your option) any later version.
00012 *
00013 *  This software is distributed in the hope that it will be useful,
00014 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 *  Lesser General Public License for more details.
00017 *
00018 *  You should have received a copy of the GNU General Public
00019 *  License along with this software. If not, see
00020 *  <http://www.gnu.org/licenses/>.
00021 *
00022 */
00023 
00024 #include "Executor.h"
00025 #include "hugin_config.h"
00026 
00027 #include <iostream>
00028 #include <wx/utils.h>
00029 #include <wx/config.h>
00030 #include <wx/filename.h>
00031 #include <wx/log.h>
00032 #include <wx/translation.h>
00033 #if defined __WXMAC__ && defined MAC_SELF_CONTAINED_BUNDLE
00034 #include "base_wx/platform.h"
00035 #endif
00036 #include "base_wx/wxPlatform.h"
00037 
00038 namespace HuginQueue
00039 {
00040     // build final command and execute it
00041     bool NormalCommand::Execute(bool dryRun)
00042     {
00043         if (dryRun)
00044         {
00045             std::cout << GetCommand().mb_str(wxConvLocal) << std::endl;
00046             return true;
00047         }
00048         if (!m_comment.IsEmpty())
00049         {
00050             std::cout << std::endl << m_comment.mb_str(wxConvLocal) << std::endl;
00051         };
00052         return wxExecute(GetCommand(), wxEXEC_SYNC | wxEXEC_MAKE_GROUP_LEADER) == 0l;
00053     };
00054 
00055     bool NormalCommand::CheckReturnCode() const
00056     {
00057         return true;
00058     };
00059 
00060     wxString NormalCommand::GetCommand() const
00061     {
00062         return wxEscapeFilename(m_prog) + wxT(" ") + m_args;
00063     };
00064 
00065     wxString NormalCommand::GetComment() const
00066     {
00067         return m_comment;
00068     };
00069 
00070     // optional command, returns always true, even if process failed
00071     bool OptionalCommand::Execute(bool dryRun)
00072     {
00073         NormalCommand::Execute(dryRun);
00074         return true;
00075     };
00076 
00077     bool OptionalCommand::CheckReturnCode() const
00078     {
00079         return false;
00080     };
00081 
00082     // execute the command queue
00083     bool RunCommandsQueue(CommandQueue* queue, size_t threads, bool dryRun)
00084     {
00085         // set OMP_NUM_THREADS to limit number of threads in OpenMP programs
00086         if (threads > 0)
00087         {
00088             wxString s;
00089             s << threads;
00090             wxSetEnv(wxT("OMP_NUM_THREADS"), s);
00091         };
00092         // set temp dir
00093         wxString tempDir = wxConfig::Get()->Read(wxT("tempDir"), wxT(""));
00094         if (!tempDir.IsEmpty())
00095         {
00096 #ifdef UNIX_LIKE
00097             wxSetEnv(wxT("TMPDIR"), tempDir);
00098 #else
00099             wxSetEnv(wxT("TMP"), tempDir);
00100 #endif
00101         };
00102         bool isSuccessful = true;
00103         size_t i = 0;
00104         // prevent displaying message box if wxExecute failed
00105         wxLogStream log(&std::cerr);
00106         // final execute the commands
00107         while (isSuccessful && i < queue->size())
00108         {
00109             isSuccessful = (*queue)[i]->Execute(dryRun);
00110             ++i;
00111         };
00112         // clean up queue
00113         CleanQueue(queue);
00114         delete queue;
00115         return isSuccessful;
00116     };
00117 
00118     void CleanQueue(CommandQueue* queue)
00119     {
00120         while (!queue->empty())
00121         {
00122             delete queue->back();
00123             queue->pop_back();
00124         };
00125     };
00126 
00127 
00128     // return path in internal program (program that is shipped with Hugin)
00129     wxString GetInternalProgram(const wxString& bindir, const wxString& name)
00130     {
00131 #if defined __WXMAC__ && defined MAC_SELF_CONTAINED_BUNDLE
00132         CFStringRef filename = MacCreateCFStringWithWxString(name);
00133         wxString fn = MacGetPathToBundledExecutableFile(filename);
00134         CFRelease(filename);
00135         if (fn == wxT(""))
00136         {
00137             std::cerr << wxString::Format(_("External program %s not found in the bundle, reverting to system path"), name.c_str()) << std::endl;
00138             return name;
00139         }
00140         return fn;
00141 #else
00142         return bindir + name;
00143 #endif
00144     };
00145 
00146     // return name of external program (can be program bundeled with Hugin, or an external program 
00147     // as specified in preferences
00148     wxString GetExternalProgram(wxConfigBase * config, const wxString& bindir, const wxString& name)
00149     {
00150 #if defined __WXMAC__ && defined MAC_SELF_CONTAINED_BUNDLE
00151         if (config->Read(name + wxT("/Custom"), 0l))
00152         {
00153             wxString fn = config->Read(name + wxT("/Exe"), wxT(""));
00154             if (wxFileName::FileExists(fn))
00155             {
00156                 return fn;
00157             }
00158             else
00159             {
00160                 std::cerr << wxString::Format(_("WARNING: External program %s not found as specified in preferences, reverting to bundled version"), fn.c_str()) << std::endl;
00161             };
00162         };
00163         if (name == wxT("exiftool"))
00164         {
00165             wxString exiftoolDirPath = MacGetPathToBundledResourceFile(CFSTR("ExifTool"));
00166             if (exiftoolDirPath != wxT(""))
00167             {
00168                 return exiftoolDirPath + wxT("/exiftool");
00169             }
00170             else
00171             {
00172                 std::cerr << wxString::Format(_("WARNING: External program %s not found in the bundle, reverting to system path"), name.c_str()) << std::endl;
00173                 return wxT("exiftool");
00174             };
00175         };
00176 
00177         CFStringRef filename = MacCreateCFStringWithWxString(name);
00178         wxString fn = MacGetPathToBundledExecutableFile(filename);
00179         CFRelease(filename);
00180         if (fn == wxT(""))
00181         {
00182             std::cerr << wxString::Format(_("WARNING: External program %s not found in the bundle, reverting to system path"), name.c_str()) << std::endl;
00183             return name;
00184         };
00185         return fn;
00186 #else
00187         if (config->Read(name + wxT("/Custom"), 0l))
00188         {
00189             wxString fn = config->Read(name + wxT("/Exe"), wxT(""));
00190             wxFileName prog(fn);
00191             if (prog.IsAbsolute())
00192             {
00193                 if (prog.FileExists())
00194                 {
00195                     return fn;
00196                 };
00197             }
00198             else
00199             {
00200                 // search in PATH
00201                 wxPathList pathlist;
00202                 pathlist.Add(bindir);
00203                 pathlist.AddEnvList(wxT("PATH"));
00204                 fn = pathlist.FindAbsoluteValidPath(fn);
00205                 if (!fn.IsEmpty())
00206                 {
00207                     return fn;
00208                 };
00209             };
00210             std::cerr << wxString::Format(_("WARNING: External program %s not found as specified in preferences, reverting to bundled version"), name.c_str()) << std::endl;
00211         };
00212         // no user specified program or not found
00213 #ifdef __WXMSW__
00214         // on Windows assume prog is bundled in program dir
00215         return bindir + name;
00216 #else
00217         // on Unix simply return name, assume it is in the path
00218         return name;
00219 #endif
00220 #endif
00221     };
00222 
00223     wxString wxStringFromCDouble(double val, int precision)
00224     {
00225         wxString s = hugin_utils::doubleTowxString(val, precision);
00226         const wxString sep = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER);
00227         s.Replace(sep, wxT("."));
00228         return s;
00229     };
00230 
00231 #ifdef __WXMSW__
00232     // search for executable in program folder and in PATH
00233     // add exe extension if no one is given
00234     wxString MSWGetProgname(const wxString& bindir, const wxString& name)
00235     {
00236         wxFileName prog(name);
00237         if (prog.IsAbsolute())
00238         {
00239             if (prog.FileExists())
00240             {
00241                 return prog.GetFullPath();
00242             };
00243         }
00244         else
00245         {
00246             // search in program folder and in PATH
00247             const bool hasExt = prog.HasExt();
00248             if (!prog.HasExt())
00249             {
00250                 prog.SetExt("exe");
00251             };
00252             wxPathList pathlist;
00253             pathlist.Add(bindir);
00254             pathlist.AddEnvList(wxT("PATH"));
00255             const wxString fullName = pathlist.FindAbsoluteValidPath(prog.GetFullName());
00256             if (!fullName.IsEmpty())
00257             {
00258                 return fullName;
00259             };
00260         };
00261         return name;
00262     };
00263 #endif
00264 
00265 // read a string from setting and remove all whitespaces
00266 const wxString GetSettingString(wxConfigBase* setting, const wxString& name, const wxString defaultValue)
00267 {
00268     wxString s = setting->Read(name, defaultValue);
00269     s = s.Trim(true).Trim(false);
00270     return s;
00271 };
00272 
00273 }; // namespace

Generated on 20 Jan 2018 for Hugintrunk by  doxygen 1.4.7