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             if (!fn.IsEmpty())
00191             {
00192                 wxFileName prog(fn);
00193                 if (prog.IsAbsolute())
00194                 {
00195                     if (prog.FileExists())
00196                     {
00197                         return fn;
00198                     };
00199                 }
00200                 else
00201                 {
00202                     // search in PATH
00203                     wxPathList pathlist;
00204                     pathlist.Add(bindir);
00205                     pathlist.AddEnvList(wxT("PATH"));
00206                     fn = pathlist.FindAbsoluteValidPath(fn);
00207                     if (!fn.IsEmpty())
00208                     {
00209                         return fn;
00210                     };
00211                 };
00212                 std::cerr << wxString::Format(_("WARNING: External program %s not found as specified in preferences, reverting to bundled version"), name.c_str()) << std::endl;
00213             };
00214         };
00215         // no user specified program or not found
00216 #ifdef __WXMSW__
00217         // on Windows assume prog is bundled in program dir
00218         return bindir + name;
00219 #else
00220         // on Unix simply return name, assume it is in the path
00221         return name;
00222 #endif
00223 #endif
00224     };
00225 
00226     wxString wxStringFromCDouble(double val, int precision)
00227     {
00228         wxString s = hugin_utils::doubleTowxString(val, precision);
00229         const wxString sep = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER);
00230         s.Replace(sep, wxT("."));
00231         return s;
00232     };
00233 
00234 #ifdef __WXMSW__
00235     // search for executable in program folder and in PATH
00236     // add exe extension if no one is given
00237     wxString MSWGetProgname(const wxString& bindir, const wxString& name)
00238     {
00239         wxFileName prog(name);
00240         if (prog.IsAbsolute())
00241         {
00242             if (prog.FileExists())
00243             {
00244                 return prog.GetFullPath();
00245             };
00246         }
00247         else
00248         {
00249             // search in program folder and in PATH
00250             const bool hasExt = prog.HasExt();
00251             if (!prog.HasExt())
00252             {
00253                 prog.SetExt("exe");
00254             };
00255             wxPathList pathlist;
00256             pathlist.Add(bindir);
00257             pathlist.AddEnvList(wxT("PATH"));
00258             const wxString fullName = pathlist.FindAbsoluteValidPath(prog.GetFullName());
00259             if (!fullName.IsEmpty())
00260             {
00261                 return fullName;
00262             };
00263         };
00264         return name;
00265     };
00266 #endif
00267 
00268 // read a string from setting and remove all whitespaces
00269 const wxString GetSettingString(wxConfigBase* setting, const wxString& name, const wxString defaultValue)
00270 {
00271     wxString s = setting->Read(name, defaultValue);
00272     s = s.Trim(true).Trim(false);
00273     return s;
00274 };
00275 
00276 }; // namespace

Generated on 20 Apr 2018 for Hugintrunk by  doxygen 1.4.7