2 * \file GraphicsConverter.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "GraphicsConverter.h"
15 #include "Converter.h"
18 #include "support/lassert.h"
19 #include "support/convert.h"
20 #include "support/debug.h"
21 #include "support/FileName.h"
22 #include "support/filetools.h"
23 #include "support/ForkedCalls.h"
24 #include "support/lstrings.h"
26 #include "support/TempFile.h"
32 using namespace lyx::support;
38 class Converter::Impl {
41 Impl(Converter const & parent, FileName const & doc_fname,
42 FileName const & from_file, string const & to_file_base,
43 string const & from_format, string const & to_format);
46 void startConversion();
48 /** This method is connected to a signal passed to the forked call
49 * class, passing control back here when the conversion is completed.
50 * Cleans-up the temporary files, emits the finishedConversion
51 * signal and removes the Converter from the list of all processes.
53 void converted(pid_t pid, int retval);
55 /** At the end of the conversion process inform the outside world
56 * by emitting a signal.
58 typedef signal<void(bool)> sig;
60 sig finishedConversion;
63 Converter const & parent_;
65 FileName const doc_fname_;
67 string script_command_;
69 FileName script_file_;
79 bool Converter::isReachable(string const & from_format_name,
80 string const & to_format_name)
82 return theConverters().isReachable(from_format_name, to_format_name);
86 Converter::Converter(FileName const & doc_fname,
87 FileName const & from_file, string const & to_file_base,
88 string const & from_format, string const & to_format)
89 : pimpl_(make_shared<Impl>(*this, doc_fname, from_file, to_file_base, from_format, to_format))
93 void Converter::startConversion() const
95 pimpl_->startConversion();
99 connection Converter::connect(slot_type const & slot) const
101 return pimpl_->finishedConversion.connect(slot);
105 FileName const & Converter::convertedFile() const
107 static FileName const empty;
108 return pimpl_->finished_ ? pimpl_->to_file_ : empty;
111 /** Build the conversion script.
112 * The script is output to the stream \p script.
114 static void build_script(string const & doc_fname,
115 string const & from_file, string const & to_file_base,
116 string const & from_format, string const & to_format,
120 Converter::Impl::Impl(Converter const & parent, FileName const & doc_fname,
121 FileName const & from_file, string const & to_file_base,
122 string const & from_format, string const & to_format)
123 : parent_(parent), doc_fname_(doc_fname), valid_process_(false), finished_(false)
125 LYXERR(Debug::GRAPHICS, "Converter c-tor:\n"
126 << "doc_fname: " << doc_fname
127 << "\n\tfrom_file: " << from_file
128 << "\n\tto_file_base: " << to_file_base
129 << "\n\tfrom_format: " << from_format
130 << "\n\tto_format: " << to_format);
132 // The converted image is to be stored in this file (we do not
133 // use ChangeExtension because this is a basename which may
134 // nevertheless contain a '.')
135 to_file_ = FileName(to_file_base + '.' + theFormats().extension(to_format));
137 // The conversion commands are stored in a stringstream
138 ostringstream script;
139 build_script(doc_fname_.absFileName(), from_file.toFilesystemEncoding(),
140 to_file_.toFilesystemEncoding(),
141 from_format, to_format, script);
142 LYXERR(Debug::GRAPHICS, "\tConversion script:"
143 "\n--------------------------------------\n"
145 << "\n--------------------------------------\n");
147 // Output the script to file.
148 TempFile tempfile(to_file_.onlyPath(), "lyxconvertXXXXXX.py");
149 tempfile.setAutoRemove(false);
150 script_file_ = tempfile.name();
152 ofstream fs(script_file_.toFilesystemEncoding().c_str());
154 lyxerr << "Unable to write the conversion script to \""
155 << script_file_ << '\n'
156 << "Please check your directory permissions."
164 // The command needed to run the conversion process
165 // We create a dummy command for ease of understanding of the
166 // list of forked processes.
167 // Note: 'python ' is absolutely essential, or execvp will fail.
168 script_command_ = os::python() + ' ' +
169 quoteName(script_file_.toFilesystemEncoding()) + ' ' +
170 quoteName(onlyFileName(from_file.toFilesystemEncoding())) + ' ' +
171 quoteName(to_format);
172 // All is ready to go
173 valid_process_ = true;
177 void Converter::Impl::startConversion()
179 if (!valid_process_) {
184 ForkedCall::sigPtr ptr = ForkedCallQueue::add(script_command_);
185 weak_ptr<Converter::Impl> this_ = parent_.pimpl_;
186 ptr->connect([this_](pid_t pid, int retval){
187 if (auto p = this_.lock()) {
188 p->converted(pid, retval);
194 void Converter::Impl::converted(pid_t /* pid */, int retval)
197 // We're done already!
201 // Clean-up behind ourselves
202 script_file_.removeFile();
205 to_file_.removeFile();
207 finishedConversion(false);
209 finishedConversion(true);
214 static string const move_file(string const & from_file, string const & to_file)
216 if (from_file == to_file)
219 ostringstream command;
220 command << "fromfile = " << from_file << "\n"
221 << "tofile = " << to_file << "\n\n"
223 << " os.rename(fromfile, tofile)\n"
226 << " shutil.copy(fromfile, tofile)\n"
229 << " unlinkNoThrow(fromfile)\n";
231 return command.str();
235 static void build_conversion_command(string const & command, ostream & script)
237 // Store in the python script
238 script << "\nif os.system(r'" << commandPrep(command) << "') != 0:\n";
240 // Test that this was successful. If not, remove
241 // ${outfile} and exit the python script
242 script << " unlinkNoThrow(outfile)\n"
243 << " sys.exit(1)\n\n";
245 // Test that the outfile exists.
246 // ImageMagick's convert will often create ${outfile}.0,
248 // If this occurs, move ${outfile}.0 to ${outfile}
249 // and delete ${outfile}.? (ignore errors)
250 script << "if not os.path.isfile(outfile):\n"
251 " if os.path.isfile(outfile + '.0'):\n"
252 " os.rename(outfile + '.0', outfile)\n"
254 " for file in glob.glob(outfile + '.?'):\n"
255 " unlinkNoThrow(file)\n"
260 script << "if infile != outfile:\n"
261 " unlinkNoThrow(infile)\n\n";
265 static string const strip_digit(string const & format)
267 // Strip trailing digits from format names e.g. "pdf6" -> "pdf"
268 return format.substr(0, format.find_last_not_of("0123456789") + 1);
272 static void build_script(string const & doc_fname,
273 string const & from_file,
274 string const & to_file,
275 string const & from_format,
276 string const & to_format,
279 LASSERT(from_format != to_format, return);
280 LYXERR(Debug::GRAPHICS, "build_script ... ");
281 typedef Graph::EdgePath EdgePath;
283 script << "# -*- coding: utf-8 -*-\n"
284 "import os, shutil, sys\n\n"
285 "def unlinkNoThrow(file):\n"
286 " ''' remove a file, do not throw if an error occurs '''\n"
292 EdgePath const edgepath = from_format.empty() ?
294 theConverters().getPath(from_format, to_format);
296 // Create a temporary base file-name for all intermediate steps.
297 string const from_ext = getExtension(from_file);
298 TempFile tempfile(addExtension("gconvertXXXXXX", from_ext));
299 tempfile.setAutoRemove(false);
300 string outfile = tempfile.name().toFilesystemEncoding();
301 string const to_base = from_ext.empty() ? outfile : removeExtension(outfile);
303 // Create a copy of the file in case the original name contains
304 // problematic characters like ' or ". We can work around that problem
305 // in python, but the converters might be shell scripts and have more
307 script << "infile = "
308 << quoteName(from_file, quote_python)
311 << quoteName(outfile, quote_python) << "\n"
312 "shutil.copy(infile, outfile)\n";
314 // Some converters (e.g. lilypond) can only output files to the
315 // current directory, so we need to change the current directory.
316 // This has the added benefit that all other files that may be
317 // generated by the converter are deleted when LyX closes and do not
318 // clutter the real working directory.
319 script << "os.chdir("
320 << quoteName(onlyPath(outfile)) << ")\n";
322 if (edgepath.empty()) {
323 // Either from_format is unknown or we don't have a
324 // converter path from from_format to to_format, so we use
325 // the default converter.
326 script << "infile = outfile\n"
328 << quoteName(to_file, quote_python) << "\n";
331 os << os::python() << ' '
332 << commandPrep("$$s/scripts/convertDefault.py") << ' ';
333 if (from_format.empty())
336 os << strip_digit(from_format) << ' ';
337 // The extra " quotes around infile and outfile are needed
338 // because the filename may contain spaces and it is used
339 // as argument of os.system().
340 os << "' + '\"' + infile + '\"' + ' "
341 << strip_digit(to_format) << " ' + '\"' + outfile + '\"' + '";
342 string const command = os.str();
344 LYXERR(Debug::GRAPHICS,
345 "\tNo converter defined! I use convertDefault.py\n\t"
348 build_conversion_command(command, script);
351 // The conversion commands may contain these tokens that need to be
352 // changed to infile, infile_base, outfile and output directory respectively.
353 string const token_from = "$$i";
354 string const token_base = "$$b";
355 string const token_to = "$$o";
356 string const token_todir = "$$d";
358 EdgePath::const_iterator it = edgepath.begin();
359 EdgePath::const_iterator end = edgepath.end();
361 for (; it != end; ++it) {
362 lyx::Converter const & conv = theConverters().get(*it);
364 // Build the conversion command
365 string const infile = outfile;
366 string const infile_base = changeExtension(infile, string());
367 outfile = conv.result_file().empty()
368 ? addExtension(to_base, conv.To()->extension())
369 : addName(subst(conv.result_dir(),
370 token_base, infile_base),
371 subst(conv.result_file(),
372 token_base, onlyFileName(infile_base)));
374 // If two formats share the same extension we may get identical names
375 if (outfile == infile && conv.result_file().empty()) {
376 TempFile tmpfile(addExtension("gconvertXXXXXX", conv.To()->extension()));
377 tmpfile.setAutoRemove(false);
378 outfile = tmpfile.name().toFilesystemEncoding();
381 if (!theConverters().checkAuth(conv, doc_fname))
384 // Store these names in the python script
385 script << "infile = "
386 << quoteName(infile, quote_python) << "\n"
388 << quoteName(infile_base, quote_python) << "\n"
390 << quoteName(outfile, quote_python) << "\n"
391 "outdir = os.path.dirname(outfile)\n" ;
393 // See comment about extra " quotes above (although that
394 // applies only for the first loop run here).
395 string command = conv.command();
396 command = subst(command, token_from, "' + '\"' + infile + '\"' + '");
397 command = subst(command, token_base, "' + '\"' + infile_base + '\"' + '");
398 command = subst(command, token_to, "' + '\"' + outfile + '\"' + '");
399 command = subst(command, token_todir, "' + '\"' + outdir + '\"' + '");
401 build_conversion_command(command, script);
404 // Move the final outfile to to_file
405 script << move_file("outfile", quoteName(to_file, quote_python));
406 LYXERR(Debug::GRAPHICS, "ready!");
409 } // namespace graphics