1 /**
2  * \file GraphicsConverter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10 
11 #include <config.h>
12 
13 #include "GraphicsConverter.h"
14 
15 #include "Buffer.h"
16 #include "Converter.h"
17 #include "Format.h"
18 #include "LyXRC.h"
19 
20 #include "frontends/alert.h"
21 #include "support/lassert.h"
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26 #include "support/ForkedCalls.h"
27 #include "support/lstrings.h"
28 #include "support/os.h"
29 
30 #include "support/TempFile.h"
31 
32 #include <sstream>
33 #include <fstream>
34 
35 using namespace std;
36 using namespace lyx::support;
37 
38 namespace lyx {
39 
40 namespace graphics {
41 
42 class Converter::Impl {
43 public:
44 	///
45 	Impl(FileName const & doc_fname,
46 	     FileName const & from_file, string const & to_file_base,
47 	     string const & from_format, string const & to_format);
48 
49 	///
50 	void startConversion();
51 
52 	/** This method is connected to a signal passed to the forked call
53 	 *  class, passing control back here when the conversion is completed.
54 	 *  Cleans-up the temporary files, emits the finishedConversion
55 	 *  signal and removes the Converter from the list of all processes.
56 	 */
57 	void converted(pid_t pid, int retval);
58 
59 	/** At the end of the conversion process inform the outside world
60 	 *  by emitting a signal.
61 	 */
62 	typedef signals2::signal<void(bool)> sig;
63 	///
64 	sig finishedConversion;
65 
66 	///
67 	FileName const doc_fname_;
68 	///
69 	string script_command_;
70 	///
71 	FileName script_file_;
72 	///
73 	FileName to_file_;
74 	///
75 	bool valid_process_;
76 	///
77 	bool finished_;
78 	///
79 	Trackable tracker_;
80 };
81 
82 
isReachable(string const & from_format_name,string const & to_format_name)83 bool Converter::isReachable(string const & from_format_name,
84 			    string const & to_format_name)
85 {
86 	return theConverters().isReachable(from_format_name, to_format_name);
87 }
88 
89 
Converter(FileName const & doc_fname,FileName const & from_file,string const & to_file_base,string const & from_format,string const & to_format)90 Converter::Converter(FileName const & doc_fname,
91                      FileName const & from_file, string const & to_file_base,
92                      string const & from_format, string const & to_format)
93 	: pimpl_(new Impl(doc_fname, from_file, to_file_base, from_format, to_format))
94 {}
95 
96 
~Converter()97 Converter::~Converter()
98 {
99 	delete pimpl_;
100 }
101 
102 
startConversion() const103 void Converter::startConversion() const
104 {
105 	pimpl_->startConversion();
106 }
107 
108 
connect(slot_type const & slot) const109 signals2::connection Converter::connect(slot_type const & slot) const
110 {
111 	return pimpl_->finishedConversion.connect(slot);
112 }
113 
114 
convertedFile() const115 FileName const & Converter::convertedFile() const
116 {
117 	static FileName const empty;
118 	return pimpl_->finished_ ? pimpl_->to_file_ : empty;
119 }
120 
121 /** Build the conversion script.
122  *  The script is output to the stream \p script.
123  */
124 static void build_script(string const & doc_fname,
125 		  string const & from_file, string const & to_file_base,
126 		  string const & from_format, string const & to_format,
127 		  ostream & script);
128 
129 
Impl(FileName const & doc_fname,FileName const & from_file,string const & to_file_base,string const & from_format,string const & to_format)130 Converter::Impl::Impl(FileName const & doc_fname,
131 		      FileName const & from_file, string const & to_file_base,
132 		      string const & from_format, string const & to_format)
133 	: doc_fname_(doc_fname), valid_process_(false), finished_(false)
134 {
135 	LYXERR(Debug::GRAPHICS, "Converter c-tor:\n"
136 		<< "doc_fname:        " << doc_fname
137 		<< "\n\tfrom_file:    " << from_file
138 		<< "\n\tto_file_base: " << to_file_base
139 		<< "\n\tfrom_format:  " << from_format
140 		<< "\n\tto_format:    " << to_format);
141 
142 	// The converted image is to be stored in this file (we do not
143 	// use ChangeExtension because this is a basename which may
144 	// nevertheless contain a '.')
145 	to_file_ = FileName(to_file_base + '.' +  theFormats().extension(to_format));
146 
147 	// The conversion commands are stored in a stringstream
148 	ostringstream script;
149 	build_script(doc_fname_.absFileName(), from_file.toFilesystemEncoding(),
150 		     to_file_.toFilesystemEncoding(),
151 		     from_format, to_format, script);
152 	LYXERR(Debug::GRAPHICS, "\tConversion script:"
153 		   "\n--------------------------------------\n"
154 		<< script.str()
155 		<< "\n--------------------------------------\n");
156 
157 	// Output the script to file.
158 	TempFile tempfile(to_file_.onlyPath(), "lyxconvertXXXXXX.py");
159 	tempfile.setAutoRemove(false);
160 	script_file_ = tempfile.name();
161 
162 	ofstream fs(script_file_.toFilesystemEncoding().c_str());
163 	if (!fs.good()) {
164 		lyxerr << "Unable to write the conversion script to \""
165 		       << script_file_ << '\n'
166 		       << "Please check your directory permissions."
167 		       << endl;
168 		return;
169 	}
170 
171 	fs << script.str();
172 	fs.close();
173 
174 	// The command needed to run the conversion process
175 	// We create a dummy command for ease of understanding of the
176 	// list of forked processes.
177 	// Note: 'python ' is absolutely essential, or execvp will fail.
178 	script_command_ = os::python() + ' ' +
179 		quoteName(script_file_.toFilesystemEncoding()) + ' ' +
180 		quoteName(onlyFileName(from_file.toFilesystemEncoding())) + ' ' +
181 		quoteName(to_format);
182 	// All is ready to go
183 	valid_process_ = true;
184 }
185 
186 
startConversion()187 void Converter::Impl::startConversion()
188 {
189 	if (!valid_process_) {
190 		converted(0, 1);
191 		return;
192 	}
193 
194 	ForkedCall::sigPtr ptr = ForkedCallQueue::add(script_command_);
195 	ptr->connect(ForkedCall::slot([this](pid_t pid, int retval){
196 				converted(pid, retval);
197 			}).track_foreign(tracker_.p()));
198 }
199 
200 
converted(pid_t,int retval)201 void Converter::Impl::converted(pid_t /* pid */, int retval)
202 {
203 	if (finished_)
204 		// We're done already!
205 		return;
206 
207 	finished_ = true;
208 	// Clean-up behind ourselves
209 	script_file_.removeFile();
210 
211 	if (retval > 0) {
212 		to_file_.removeFile();
213 		to_file_.erase();
214 		finishedConversion(false);
215 	} else {
216 		finishedConversion(true);
217 	}
218 }
219 
220 
move_file(string const & from_file,string const & to_file)221 static string const move_file(string const & from_file, string const & to_file)
222 {
223 	if (from_file == to_file)
224 		return string();
225 
226 	ostringstream command;
227 	command << "fromfile = " << from_file << "\n"
228 		<< "tofile = "   << to_file << "\n\n"
229 		<< "try:\n"
230 		<< "  os.rename(fromfile, tofile)\n"
231 		<< "except:\n"
232 		<< "  try:\n"
233 		<< "    shutil.copy(fromfile, tofile)\n"
234 		<< "  except:\n"
235 		<< "    sys.exit(1)\n"
236 		<< "  unlinkNoThrow(fromfile)\n";
237 
238 	return command.str();
239 }
240 
241 
build_conversion_command(string const & command,ostream & script)242 static void build_conversion_command(string const & command, ostream & script)
243 {
244 	// Store in the python script
245 	script << "\nif os.system(r'" << commandPrep(command) << "') != 0:\n";
246 
247 	// Test that this was successful. If not, remove
248 	// ${outfile} and exit the python script
249 	script << "  unlinkNoThrow(outfile)\n"
250 	       << "  sys.exit(1)\n\n";
251 
252 	// Test that the outfile exists.
253 	// ImageMagick's convert will often create ${outfile}.0,
254 	// ${outfile}.1.
255 	// If this occurs, move ${outfile}.0 to ${outfile}
256 	// and delete ${outfile}.? (ignore errors)
257 	script << "if not os.path.isfile(outfile):\n"
258 		  "  if os.path.isfile(outfile + '.0'):\n"
259 		  "    os.rename(outfile + '.0', outfile)\n"
260 		  "    import glob\n"
261 		  "    for file in glob.glob(outfile + '.?'):\n"
262 		  "      unlinkNoThrow(file)\n"
263 		  "  else:\n"
264 		  "    sys.exit(1)\n\n";
265 
266 	// Delete the infile
267 	script << "if infile != outfile:\n"
268 		  "  unlinkNoThrow(infile)\n\n";
269 }
270 
271 
strip_digit(string const & format)272 static string const strip_digit(string const & format)
273 {
274 	// Strip trailing digits from format names e.g. "pdf6" -> "pdf"
275 	return format.substr(0, format.find_last_not_of("0123456789") + 1);
276 }
277 
278 
build_script(string const & doc_fname,string const & from_file,string const & to_file,string const & from_format,string const & to_format,ostream & script)279 static void build_script(string const & doc_fname,
280 		  string const & from_file,
281 		  string const & to_file,
282 		  string const & from_format,
283 		  string const & to_format,
284 		  ostream & script)
285 {
286 	LASSERT(from_format != to_format, return);
287 	LYXERR(Debug::GRAPHICS, "build_script ... ");
288 	typedef Graph::EdgePath EdgePath;
289 
290 	script << "#!/usr/bin/env python\n"
291 		  "# -*- coding: utf-8 -*-\n"
292 		  "import os, shutil, sys\n\n"
293 		  "def unlinkNoThrow(file):\n"
294 		  "  ''' remove a file, do not throw if an error occurs '''\n"
295 		  "  try:\n"
296 		  "    os.unlink(file)\n"
297 		  "  except:\n"
298 		  "    pass\n\n";
299 
300 	EdgePath const edgepath = from_format.empty() ?
301 		EdgePath() :
302 		theConverters().getPath(from_format, to_format);
303 
304 	// Create a temporary base file-name for all intermediate steps.
305 	string const from_ext = getExtension(from_file);
306 	TempFile tempfile(addExtension("gconvertXXXXXX", from_ext));
307 	tempfile.setAutoRemove(false);
308 	string outfile = tempfile.name().toFilesystemEncoding();
309 	string const to_base = from_ext.empty() ? outfile : removeExtension(outfile);
310 
311 	// Create a copy of the file in case the original name contains
312 	// problematic characters like ' or ". We can work around that problem
313 	// in python, but the converters might be shell scripts and have more
314 	// troubles with it.
315 	script << "infile = "
316 			<< quoteName(from_file, quote_python)
317 			<< "\n"
318 		  "outfile = "
319 			<< quoteName(outfile, quote_python) << "\n"
320 		  "shutil.copy(infile, outfile)\n";
321 
322 	// Some converters (e.g. lilypond) can only output files to the
323 	// current directory, so we need to change the current directory.
324 	// This has the added benefit that all other files that may be
325 	// generated by the converter are deleted when LyX closes and do not
326 	// clutter the real working directory.
327 	script << "os.chdir("
328 	       << quoteName(onlyPath(outfile)) << ")\n";
329 
330 	if (edgepath.empty()) {
331 		// Either from_format is unknown or we don't have a
332 		// converter path from from_format to to_format, so we use
333 		// the default converter.
334 		script << "infile = outfile\n"
335 		       << "outfile = "
336 		       << quoteName(to_file, quote_python) << "\n";
337 
338 		ostringstream os;
339 		os << os::python() << ' '
340 		   << commandPrep("$$s/scripts/convertDefault.py") << ' ';
341 		if (from_format.empty())
342 			os << "unknown ";
343 		else
344 			os << strip_digit(from_format) << ' ';
345 		// The extra " quotes around infile and outfile are needed
346 		// because the filename may contain spaces and it is used
347 		// as argument of os.system().
348 		os << "' + '\"' + infile + '\"' + ' "
349 		   << strip_digit(to_format) << " ' + '\"' + outfile + '\"' + '";
350 		string const command = os.str();
351 
352 		LYXERR(Debug::GRAPHICS,
353 			"\tNo converter defined! I use convertDefault.py\n\t"
354 			<< command);
355 
356 		build_conversion_command(command, script);
357 	}
358 
359 	// The conversion commands may contain these tokens that need to be
360 	// changed to infile, infile_base, outfile and output directory respectively.
361 	string const token_from  = "$$i";
362 	string const token_base  = "$$b";
363 	string const token_to    = "$$o";
364 	string const token_todir = "$$d";
365 
366 	EdgePath::const_iterator it  = edgepath.begin();
367 	EdgePath::const_iterator end = edgepath.end();
368 
369 	for (; it != end; ++it) {
370 		lyx::Converter const & conv = theConverters().get(*it);
371 
372 		// Build the conversion command
373 		string const infile      = outfile;
374 		string const infile_base = changeExtension(infile, string());
375 		outfile = conv.result_file().empty()
376 			? addExtension(to_base, conv.To()->extension())
377 			: addName(subst(conv.result_dir(),
378 					token_base, infile_base),
379 				  subst(conv.result_file(),
380 					token_base, onlyFileName(infile_base)));
381 
382 		// If two formats share the same extension we may get identical names
383 		if (outfile == infile && conv.result_file().empty()) {
384 			TempFile tempfile(addExtension("gconvertXXXXXX", conv.To()->extension()));
385 			tempfile.setAutoRemove(false);
386 			outfile = tempfile.name().toFilesystemEncoding();
387 		}
388 
389 		if (!theConverters().checkAuth(conv, doc_fname))
390 			return;
391 
392 		// Store these names in the python script
393 		script << "infile = "
394 				<< quoteName(infile, quote_python) << "\n"
395 			  "infile_base = "
396 				<< quoteName(infile_base, quote_python) << "\n"
397 			  "outfile = "
398 				<< quoteName(outfile, quote_python) << "\n"
399 			  "outdir  = os.path.dirname(outfile)\n" ;
400 
401 		// See comment about extra " quotes above (although that
402 		// applies only for the first loop run here).
403 		string command = conv.command();
404 		command = subst(command, token_from,  "' + '\"' + infile + '\"' + '");
405 		command = subst(command, token_base,  "' + '\"' + infile_base + '\"' + '");
406 		command = subst(command, token_to,    "' + '\"' + outfile + '\"' + '");
407 		command = subst(command, token_todir, "' + '\"' + outdir + '\"' + '");
408 
409 		build_conversion_command(command, script);
410 	}
411 
412 	// Move the final outfile to to_file
413 	script << move_file("outfile", quoteName(to_file, quote_python));
414 	LYXERR(Debug::GRAPHICS, "ready!");
415 }
416 
417 } // namespace graphics
418 } // namespace lyx
419